EECS 280 Final Exam Fall '18 - Multiple
Choice Questions and Graded Answers.
When should a virtual destructor be used? - When derived classes have member variables
that differ at all from the member variables of the base class.
Can you create an instance of an abstract class? - *No*; abstract classes are fundamentally
"incomplete" (they have no member variables) and you cannot declare instances of them.
Can you declare a pointer to an abstract class? - *Yes*, as long as the pointer is assigned
an instance of a derived class of the abstract class.
//Example: Animal *a = Gorilla &g;
If class B is declared within class A, which one is able to "give" friendship? - *Class B*;
only the class exposing its member variables may choose to give another class friendship.
"Friendship can be given but not taken."
Should template mechanism/typename code be written in .h files or .cpp files? - Should be
written in *.h* files; allows the compiler to see which functions might need to be overridden
with new typenames.
Are dynamic variables created on the heap or the stack? - Dynamic variables are always
created on the *heap*.
Which is ALWAYS legal in C++: upcasting or downcasting? - *Upcasting* is always
legal; downcasting is only legal in specific cases when using the dynamic_cast standard function.
//What is the proper way to delete the following //dynamic array allocation?
int *array = new int[size]; - *delete[ ] array;*
Choice Questions and Graded Answers.
When should a virtual destructor be used? - When derived classes have member variables
that differ at all from the member variables of the base class.
Can you create an instance of an abstract class? - *No*; abstract classes are fundamentally
"incomplete" (they have no member variables) and you cannot declare instances of them.
Can you declare a pointer to an abstract class? - *Yes*, as long as the pointer is assigned
an instance of a derived class of the abstract class.
//Example: Animal *a = Gorilla &g;
If class B is declared within class A, which one is able to "give" friendship? - *Class B*;
only the class exposing its member variables may choose to give another class friendship.
"Friendship can be given but not taken."
Should template mechanism/typename code be written in .h files or .cpp files? - Should be
written in *.h* files; allows the compiler to see which functions might need to be overridden
with new typenames.
Are dynamic variables created on the heap or the stack? - Dynamic variables are always
created on the *heap*.
Which is ALWAYS legal in C++: upcasting or downcasting? - *Upcasting* is always
legal; downcasting is only legal in specific cases when using the dynamic_cast standard function.
//What is the proper way to delete the following //dynamic array allocation?
int *array = new int[size]; - *delete[ ] array;*