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
INVALIDthis == &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:
- 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:
-
True/False: Inside
f
, the expressionc
is an lvalue.- True
-
e) Consider the code from the previous question. In
f
, the expressionc.x
is an rvalue or not?- False, since
x
is a lvalue, thenc.x
is an lvalue inside off
. - Even though
c
is an rvalue reference, when you access a member of an object referred to by an rvalue reference (such asc
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 functionf
without needing to treat them as rvalues.
- False, since
-
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()
andend()
- True
- Why?
-
h) A range based for loop on a constant object implicitly calls
cbegin()
andcend()
.- False, it does not necessarily calls
cbegin()
andcend()
, since we may not have implemented it.
- False, it does not necessarily calls
-
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:
-
The implementation of method
A::foo(A ¶m)
is not allowed to accessparam.x
sincex
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 classA
. So member functionfoo
have access tox
.
- False, in C++, access control applies to the class, not the instance of the class. This means that any member function of class
-
k) If a class
A
contains a pointer to a classB
, then there is a “has-a” relationship betweenA
andB
.- False, if class
A
contains a pointer to classB
, it does not inherently imply a “has-a” relationship betweenA
andB
. 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 classB
as a member variable within classA
, not just a pointer to it.
- False, if class
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.
-
It won’t compile.
-
The reason is a type mismatch when calling the function
f()
withchar*
argument. The functionf
is defined to take astring
argument, which is typically represented using thestd::string
class in C++. In themain
function, achar*
(C-style string) is assigned the value"Hello World!"
. While C-style strings can be implicitly converted tostd::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.
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.
- Initializing
const
members - Initializing Reference members
- 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: