Midterm Practice CS247 from Ross
Short Answer Questions
- Describe the conditions under which a method call uses the static type vs when it uses the dynamic type.
- Stack objects and non-virtual are static at compile time, pointers and references and virtual are dynamic.
- Explain why we prefer to put implementations in a .cc file vs in a .h file.
- if we change something in .h file all dependencies must recompile. cc needs to be updated and relinked. less compile time. Also good software practice, encapsulation, data abstraction.
- Describe at a high level how a templated class is compiled.
- at compile time, duplicates and fills in for each template type
- Define in your own words what an invariant is.
- condition that must always hold for valid ADT
- Describe the steps of the object construction sequence. How do they relate to those of the object destruction sequence?
- allocated space, calls superclass constructor, initialized MIL, constructor body
- destructor this class, member objects in reverse order, destructor superclass, reclaim space
- What is the difference between overloading and overriding?
- overloading: same name but different number of parameters and types
- overriding: derived class changing method implementation with same signatures
- Give two differences between overloading operators as a method vs as a standalone function.
- s
- Give one example where an operator overload has to be defined as a standalone function, and one where it has to be defined as a method.
- standalone: operator<<, >>
- method: operator(), [], →, =
- Explain the different situations where we would prefer pass-by-value, pass-by-reference, and pass-by- constant-reference.
- pass by value: temporary changes within function
- pass by reference: mutate a variable
- pass by const reference: don’t want to copy, don’t need to mutate at all
-
Give the definition of an lvalue, give the definition of an rvalue.
- Describe the mechanism behind the copy-swap idiom, and why one one may wish to use it.
- For a container class C, list what methods must be defined to implement an iterator for C.
- begin/cbegin, end/cend, operator++, star, !=
- List two advantages to using a Makefile for build automation
- keep track of changed files through modify time, so less compile time, tracks dependencies, simplicity for programmer
- Explain differences between the waterfall and agile methodology of software development.
- waterfall not in a loop
- Describe the conditions that indicate an owns-a relationship, and a has-a relationship.
- owns a, a dies b dies too, a copy b is copied (deep), b depends on a
- has a, a dies b lives on, b does not depend on a, copy a, b is shallow copy
Coding Questions
2 Coding Questions
- Implement the Big 5 for a doubly linked list. Do so using a Node with fields Node* next and Node* prev.
- Transform this implementation to be well encapsulated via a DoublyLinkedList class that only exposes public methods.
- Having implemented the big 5 for the doubly linked list, implement a bi-directional iterator. A bidirectional iterator allows both ++it to take you to the next element, as well as –-it to take you to the previous element.
- Implement a reverse iterator for your linked list. Reverse iterators can be created using rbegin() and rend(). They start at the end of the data structure, and ++it takes them to the previous element.
- Consider a class RepeatedString, which contains a string s and a positive integer k. For a RepeatedString rs, implement overloads so that the following code performs the desired behaviour:
- A single constructor that allows both default construction (with s set to the empty string and k set to 0), as well as construction given the string s and int k
cout << rs
which prints the stringrs.s
k
times.cin >> rs
which should read in a strings
- For integer j, rs * j and j * rs which creates a RepeatedString with the same string, and associated integer k ∗ j.
- Consider creating a chess game. Consider how you may represent different pieces using an inheritance hierarchy. Each piece should have a column and row field that range from 0 to 7, and also a 2D board that allows them to determine whether a piece is occupied at a particular location. Implement a virtual method getMoves that returns a vector of possible coordinates a given piece could move to. Do so for each possible piece on the chessboard, keeping in mind that shared logic should be abstracted into the base class where possible.