Fall 2019: Week 3 Exercise Questions

Fall 2019: Week 3 Exercise Questions

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 Function overloading in C++ is done statically (static dispatch), at compile time. 2. You can declare an opaque data type on the stack. T F No. You must know the size of a data type to be able to declare it on the stack, but an opaque data type is not defined, making it impossible to calculate its size. 3. Pure virtual functions are not necessarily pure functions. T F Pure virtual functions can still have side effects such as modifying global variables, which makes it not always a pure function. 4. A virtual function overloading an operator is an example of dynamic dispatch. T F Yes, for example, virtual int operator+(int val) const; 5. Dynamically-typed interpreted language cannot implement early binding. T F Only compiled languages can legitimately implement early binding (name to address translation) 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(self, food): # a mistake was fixed (self added) print(str(food) + “ is delicious”) class Dog(Animal): def eat(self, food): super().eat(food) # or Animal.eat(self, food) print("Wags its tail") 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 ] def cond(v): return not (v//10+v%10)%7 b = list(map(str, filter(cond, range(10, 100)))) 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() import copy student.id = 0 person = copy.deepycopy(base) student.gpa = 0 person.name = “” student.year = 1900 person.age = 26 me = copy.deepcopy(student) person.gender = “unknown” me.name = “jack” def bday(self): me.gender = “male” self.age += 1 me.id = 123456789 Object.birthday = bday me.gpa = 3 student = me.year = 2015 copy.deepycopy(person) 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] A::vtable struct A { virtual void foo() { cout << “A.foo”; } A::foo virtual void bar() { cout << “A.bar”; } void baz() { cout << “A.baz”; } A::bar }; B::vtable struct B : public A { A::foo virtual void bar() { cout << “B.bar”; } B::bar }; C::vtable struct C : public B { C::foo virtual void foo() { cout << “C.foo”; } B::bar void baz() { cout << “C.baz”; } }; D::vtable struct D : public A { D::foo virtual void foo() { cout << “D.foo”; } A::bar 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(); A.baz D.foo B.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. Not Necessarily. (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. Only composition requires 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. Mixin requires multiple inheritance. (b) Java does not need virtual tables. Still required to implement dynamic dispatch. (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. Only late binding languages require MRO. 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”; } }; struct X B::b2 : int B::b1 : int 16 bytes B::__vptr X::x1 : int 4 bytes Q::q1 : int 4 bytes P::p1 : long 16 bytes P::__vptr padding: 2 bytes 40 bytes N::n1 : char[30] N::__vptr X::N::vtable virtual base offset: 64 offset to “bottom”: 0 typeinfo = typeinfo(X) X::foo X::~X() X::B::vtable X::P::vtable virtual base offset: 0 virtual base offset: 24 offset to “bottom”: 64 offset to “bottom”: 40 typeinfo = typeinfo(X) typeinfo = typeinfo(X) X::foo X::foo X::~X() X::~X() 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] L[A] = (A, o) L[B] = (B, o) L[C] = (C, o) L[D] = (D, o) L[E] = (E, o) L[P] = (P, A, B, C, o) L[Q] = (Q, D, B, E, o) L[R] = (R, D, A, o) L[X] = (X, merge((P, A, B, C, o), (R, D, A, o), (Q, D, B, E, o), (P, R, Q))) = (X, P, merge((A, B, C, o), (R, D, A, o), (Q, D, B, E, o), (R, Q))) = (X, P, R, merge((A, B, C, o), (D, A, o), (Q, D, B, E, o), (Q))) = (X, P, R, Q, merge((A, B, C, o), (D, A, o), (D, B, E, o))) # A bad head, in tail of 2nd list = (X, P, R, Q, D, merge((A, B, C, o), (A, o), (B, E, o))) = (X, P, R, Q, D, A, merge((B, C, o), (o), (B, E, o))) = (X, P, R, Q, D, A, B, merge((C, o), (o), (E, o))) = (X, P, R, Q, D, A, B, C, E, o) 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 Neither is a subset of each other, but they do have overlaps. 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 Have to write foo<int>() because C++ does not do type inference based on return type. 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 * ptr) { return container_of(ptr, struct derived, b); } 2. Implement binary search algorithm using a function template, assume the array is sorted and return -1 upon not found. [5 marks] template<typename T> /* find index of val in array of size n */ int binary_search(const T & val, T * array, int n) { int top = n-1; int bot = 0; while (bot <= top) { int mid = (top + bot)/2; if (array[mid] == val) return mid; else if (array[mid] < val) bot = mid+1; else top = mid-1; } return -1; } 3.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    31 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