CS247

CS247 Midterm

MCQ

  • You can call a const method on a non-const object YES

  • You can call a const method on a const object YES

  • You can call a non-const method on a const object NO

  • You can call a non-const method on a non-const object YES

  • An abstract class can have a concrete subclass YES

  • An abstract class can have an abstract subclass YES

  • A concrete class can have an abstract subclass NO

Select all that are valid check for self assignment within operator=, assuming the argument is called other:

  • this == other INVALID
  • this == &other VALID
  • *this == other INVALID

Consider the function call foo(5). Select all declarations of foo for which this call would be valid.

  • a) void foo(int n);
  • b) void foo(int &n);
  • c) void foo(int &&n);
  • d) void foo(const int &n);
  • e) void foo(A n); where the class Ahas an explicit constructor that takes an int parameter

a, c, d, e is wrong?

a,c,d?

Assuming you have a class Vec for which the big 5 operations are defined, and for which no other methods are defined, what is true for what will occur on line 7:

1 #include "vec.h"
2 Vec makeAVec(int x, int y){
3    Vec v = {x,y};
4    return v;
5 }
6 int main(){
7    Vec v = makeAVec(3,5); // ?
8    ...
9 }
  • a) The copy constructor is called
  • b) The move constructor is called
  • c) Either the copy constructor is called or no constructor is called
  • d) Either the move constructor is called or no constructor is called
  • e) None of the above

The copy constructor is called????

TRUE/FALSE

  • a) One ADT can be represented using multiple different classes.

    • True
  • b) Every class has a default constructor.

    • False
  • c) Every class should have a default constructor.

    • False, if we decide to provide a constructor to a class, then it is possible that a default constructor won’t be needed.
  • d) Consider the following code:

struct C{
	string x;
};
 
void f(C&& c){
	... 
}
  • True/False: Inside f, the expression c is an lvalue.

    • True
  • e) Consider the code from the previous question. In f, the expression c.x is an rvalue or not?

    • False, since x is a lvalue, then c.x is an lvalue inside of f.
    • Even though c is an rvalue reference, when you access a member of an object referred to by an rvalue reference (such as c in this case), the expression still has an lvalue category. This allows you to interact with the members of the object as you would with any other object’s members. This behaviour ensures that you can use the object’s members within the function f without needing to treat them as rvalues.
  • f) Every correct copy assignment operator must start with a self assignment check.

    • True, but i fucking never do it.
  • g) A range based for loop implicitly calls begin() and end()

    • True
    • Why?
  • h) A range based for loop on a constant object implicitly calls cbegin() and cend().

    • False, it does not necessarily calls cbegin() and cend(), since we may not have implemented it.
  • i) A base class that has a virtual method is an abstract class.

    • False, it should have a pure virtual method to be an abstract class.
  • j) Consider the following code:

class A {
	int x;
	public:
		void foo(A &param) {
			cout << param.x << endl;
		}
};
  • The implementation of method A::foo(A &param) is not allowed to access param.x since x is declared private.

    • False, in C++, access control applies to the class, not the instance of the class. This means that any member function of class A can access private members of any instance of class A. So member function foo have access to x.
  • k) If a class A contains a pointer to a class B , then there is a “has-a” relationship between A and B.

    • False, if class A contains a pointer to class B, it does not inherently imply a “has-a” relationship between A and B. The “has-a” relationship typically implies direct ownership or containment of an object of another class as a member variable, not just a pointer to it. For a true “has-a” relationship, you would usually have an object of class B as a member variable within class A, not just a pointer to it.

SHORT ANSWER

  • a) Define tampering of an ADT, and provide an example

    • Modifying a valid instance of an ADT to become invalid in an unexpected way
  • b) Define forgery of an ADT, and provide an example

    • Creating invalid instances of an ADT.
  • c) Below is a code snippet. If it compiles, explain what language feature is used to call f. If it doesn’t compile, explain why.

void f(string s){
	...
}
int main(){
	char* s = "Hello World!";
	f(s);
}
  • It won’t compile.

  • The reason is a type mismatch when calling the function f() with char* argument. The function f is defined to take a string argument, which is typically represented using the std::string class in C++. In the main function, a char* (C-style string) is assigned the value "Hello World!". While C-style strings can be implicitly converted to std::string, this conversion doesn’t happen automatically when passing the argument to a function.

  • d) Below is a code snippet. If it compiles, explain what language feature is used to call g. If it doesn’t compile, explain why.

void g(char* s){
	...
}
int main(){
	string s = "Hello World";
	g(s);
}

It won’t compile because string object isn’t implicitly converted to char*.

e) Give a reason why we prefer misuse of an ADT to result in a compile-time error instead of a run-time error. Catch mistake faster and won’t make program crash or have some dangerous and unknown behaviour.

f) Define in your own words what a default constructor is. Usually provided by the compiler if we don’t write one, that initializes space for a class to random garbage values. Sets the field.

g) Give three examples where using the MIL is necessary to ensure your code compiles.

  1. Initializing const members
  2. Initializing Reference members
  3. Base class initialization

h) Define physical constness. Define logical constness. Which does the compiler give us automatically via const objects and const methods.

  • Physical constness is when objects/methods will not be changed, that is their field: Compiler provided automatically.
  • Logical constness is when objects and methods are const because they have to be because their functions

i) Which compiler flag enables debugging support with gdb and valgrind? -g

j) Describe the three steps taken at run-time in order to achieve dynamic dispatch for virtual functions

  • Virtual function table (VTable) Lookup:
  • Function Call through VTable Pointer
  • Run the Chosen Function: