ECE326 – Fall 2019: Week 2 Exercise Questions

ECE326 – Fall 2019: Week 2 Exercise Questions

ECE326 – Fall 2019: Week 2 Exercise Questions 1. True or False [1 mark each] Circle T is true, otherwise circle F for false. 1. Assignment in Python is always by reference. T F 2. Dynamically typed languages do not perform type checking. T F 3. global keyword is required to read a global variable from inside a function. T F 2. Multiple Answers [2 marks each] Pick all answers that are correct. You will lose 1 mark per wrong choice, down to 0 marks. 1. Which of the following operations are allowed inside a pure function? (a) Read from a constant global variable (b) Read from a static function variable (c) Modify a local variable (d) Call print function to write to console (e) Call another pure function 2. In Python 3, print returns the string it printed to screen. Which is the following is true? (a) print is a statement (b) print is an expression (c) you can assign print to a variable, i.e. a = print (d) you can pass print to a function, i.e. foo(print) (e) you can assign a value to print, i.e. print = 5 3. Short Answers 1. What does this expression evaluate to? [2 marks] >> { b : a for a, b in enumerate(“HELLO”) } 2. What slice of the word “washington” will give the result of “ogisw”? (Give answer in the form [i:j:k]) 4. Programming Question [10 marks] Write a function reverse_dict() that will reverse keys and values such that the original values become the new keys to lists of one or more values that were the original keys. For example: {“bob”: 2, “greg”: 3, “joe”: 2, “tom”: 1, “dave”: 2, “stu”: 3, “mike”: 5} becomes: { 1: [“tom”], 2: [“bob”, “joe”, “dave”], 3: [“stu”], 5: [“mike”] } ECE326 – Fall 2019: Week 3 Exercise Questions 1. True or False [1 mark each] Circle T is true, otherwise circle F for false. 1. Overloading virtual functions in C++ is an example of multiple dispatch. T F 2. You can declare an opaque data type on the stack. T F 3. Pure virtual function are not necessarily pure functions. T F 4. A virtual function overloading an operator is an example of dynamic dispatch. T F 5. Dynamically-typed interpreted language cannot implement early binding. T F 2. Short Answers 1. Override the eat method in Animal so that the eat method in Dog will, in addition to what Animal.eat already does, print “Wags its tail” at the very end. Show the entire class definition for Dog. [3 marks] class Animal: … # may change this function in the future def eat(food): print(str(food) + “ is delicious”) 2. Write an expression with equivalent result as the following list comprehension, using only the map and filter function for control flow. [2 marks] [ str(v) for v in range(10, 100) if not (v//10+v%10)%7 ] 3. Prototype-based Programming [10 marks] In Prototype-based programming, all objects have the type Object. The base object initially has no attribute. We wish to program with this paradigm in Python. Create a Person object out of the base object, followed by inheriting from the Person object to create a Student object. Finally, make an instance of Student. A Person object should have the data attributes: name, age, and gender, with a method called birthday() that increments age. A Student object should have the data attributes: id, gpa, and year. Create an instance of Student named “me” with your credential (can be fake). Choose suitable defaults for the prototypes. class Object: pass base = Object() 4. Virtual Tables [10 marks] a. For the following inheritance hierarchy, draw a virtual table for each class and draw an arrow from each entry in the virtual table to their definition in the C++ classes. [7 marks] struct A { virtual void foo() { cout << “A.foo”; } virtual void bar() { cout << “A.bar”; } void baz() { cout << “A.baz”; } }; struct B : public A { virtual void bar() { cout << “B.bar”; } }; struct C : public B { virtual void foo() { cout << “C.foo”; } void baz() { cout << “C.baz”; } }; struct D : public A { virtual void foo() { cout << “D.foo”; } void baz() { cout << “D.baz”; } }; b. What is the output of the following program? [3 marks] D d = D(); C c = C(); A * ad = &d; A * ac = &c; ac->baz(); ad->foo(); ac->bar(); ECE326 – Fall 2019: Week 4 Exercise Questions 1. True or False [1 mark each] Circle T is true, otherwise circle F for false. 1. With C3 Linearization, Python completely solves the diamond problem. T F 2. 0x8888FEDC is a 4-byte aligned address. T F 3. Suppose class A is inherited by class B, and C, monotonicity guarantees that A will behave the same for both B and C. T F 4. Adding a new pure virtual function to a base class with many existing derived classes is an example of a fragile base class problem. T F 5. The main difference between delegation and type embedding is that with type embedding, you can no longer reference the embedded member by name. T F 2. Multiple Answers [2 marks each] Pick all answer(s) that are correct. You will lose 1 mark per wrong choice, down to 0 marks. 1. Which of the following are true about mixins? (a) It requires subclass to complete its implementation. (b) It can contain both member variables and functions. (c) It is used as a super type to the derived class. (d) Using it requires method forwarding. (e) The order in which mixins are composed may change behaviour of the subclass. 2. Java only supports single inheritance with runtime polymorphism. Which of the following is true? (a) Java does not support mixins. (b) Java does not need virtual tables. (c) Casting pointers (internally, Java does not expose pointers to programmers) in Java will never require point offsetting. (d) Java does not need to deal with inheritance-related ambiguity. (e) Java does not have method resolution order. 3. Virtual Base Class in C++ [10 marks] Draw the data layout of class X (include padding assuming 8-byte alignment, and write down the size of each sub-structure) and all the virtual tables generated for class X and its ancestors. struct B { int b1; int b2; virtual void foo() { cout << “A.foo”; } virtual ~A() {} }; struct P : virtual public B { long p1; virtual void foo() override { cout << “P.foo”; } }; struct Q : public P { int q1; }; struct N : virtual public B { char n1[30]; }; struct X : public N, public Q { int x1; virtual void foo() override { cout << “X.foo”; } }; 4. Method Resolution Order [10 marks] a. For the following inheritance hierarchy in Python, draw a diagram of the hierarchy. [2 marks] class A: pass class B: pass class C: pass class D: pass class E: pass class P(A, B, C): pass class Q(D, B, E): pass class R(D, A): pass class X(P, R, Q): pass b. What is the C3 Linearization of X? [8 marks] ECE326 – Fall 2019: Week 5 Exercise Questions 1. True or False [1 mark each] Circle T is true, otherwise circle F for false. 1. Generic programming is a subset of metaprogramming. T F 2. If no deep copy is required (e.g. class has no pointer), move semantics performs no better than copy semantics. T F 3. If template specialization is not used (i.e. not instantiated), its code is not generated for the final executable. T F 4. For template T foo(), you can write int a = foo() to instantiate the function template foo with an int parameter . T F 5. The new operator in C++ couples heap allocation and constructor invocation. T F 2. Short Answers 1. Use container_of to return a pointer to the parent object of member field base. [2 marks] struct base { int x, y, z; }; struct derived { int a; struct base b; char c[10]; }; struct derived * get_derived(struct base * b) { } 2. Implement binary search algorithm using a function template, assume swap template function has already been implemented and the array is sorted. [5 marks] template<typename T> /* find index of val in array of size n */ int binary_search(const T & val, T * array, int n) { } 3. Implement a template class named Triple that is a tuple of 3 elements of the same type. Overload enough operators so that binary search template you implemented above can be instantiated for Triple. Use lexicographical order. [5 marks] template<typename T> class Triple { }; 3. Generic Programming [10 marks] Create a generic Queue class without using templates. Implement the Queue using a singly linked list, with the member functions, push_back, that pushes new elements to end of the queue, front, which returns the first element of the queue, and pop_front, which removes the first element of the queue. 4. Template Programming [10 marks] Using the generic Queue made in Question 3, write a FIFO class template, which allows type- safe use of the generic Queue class for any parameterized type. Use move semantics for push_back instead of copy semantics. ECE326 – Fall 2019: Week 6 Exercise Questions 1. True or False [1 mark each] Circle T is true, otherwise circle F for false. 1. In Python, types are reified at runtime. T F 2. The decorator function is called every time the decorated function is called. T F 3. C++11 supports static reflection. T F 4. In multiple inheritance, TypeError is raised when there is a shared base metaclass. T F 5. type is to classes as object is to instances. T F 2. Multiple Answers [2 marks each] Pick all answer(s) that are correct.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    27 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us