Eecs280f19 - Midterm - Reference
Eecs280f19 - Midterm - Reference
Fall 2019
Multiple Choice and Reference Packet
This is a closed-book exam. You may use one note sheet, 8.5"x11", double--sided, with your
name on it. This booklet contains multiple-choice questions, reference code for the written
portion, and space for scratch work.
Read the entire exam through before you begin working. Work on those problems you find
easiest first. Read each question carefully, and note all that is required of you. Assume all code
is in standard C++11, and use only standard C++11 in your solutions.
Instructions:
● Throughout the exam, assume all necessary #include headers and the using
namespace std; directive are present unless otherwise directed.
● You do not need to verify REQUIRES clauses with assert unless instructed to do so.
● The multiple choice booklet WILL be turned in. However, NOTHING written inside of this
booklet will be graded. You must record your answers in the written-portion booklet.
● Turn in both this booklet and the written-portion exam book. One will not be accepted
without the other. Keep your note sheet.
1. Public class members marked as static can be accessed without referencing any
particular instance of the class.
3. Let ptr_y be a pointer to a double. Then ptr_y += 1; will increment the value of
ptr_y by the size of a double instead of 1.
5. A derived class inherits all public member functions of its base class, including the base
class constructor.
6. A derived class has direct access to private member variables of its base class.
7. Although not all abstract data types (ADTs) are implemented as structs, all structs are
examples of ADTs.
9. Procedural abstraction involves logically separating what a procedure (or function) does
from how it is implemented.
For questions 1-4, consider the following program which has been compiled into an
executable named myprog.exe:
int main(){
char x[] = "hello";
char y[] = {'h','e','l','l','o'};
std::string z = "hello";
// determine behavior at this point
}
The program compiles successfully. Determine what would be printed by each of the
following statements, assuming they were placed at the end of main. If the statement
would cause undefined behavior, select [Undefined]. If nothing is printed, select
[Nothing]
5. world(x,y,z);
a. z
b. h
c. hello
d. [Nothing]
e. [Undefined]
6. world(y,y,z);
a. z
b. h
c. hello
d. [Nothing]
e. [Undefined]
8. cout << y;
a. h
b. hello
c. hellohello
d. [Nothing]
e. [Undefined]
You must write the “main” function for this program (don’t forget to pass in argc and argv). You
may write and call any helper functions (do not worry about the relative order that you write the
functions, i.e. you may assume all functions are declared before their use). You may use
functions from the standard library.
Error checking: Your implementation MUST check whether there are enough arguments – one
integer and an optional filename. If there are not enough, return 1 from main. You DO NOT
need to check for any other errors, such as whether the integer is formatted properly or whether
the file was successfully opened. Remember to handle the case where the input file is not
specified.
int main() {
int a = 42;
int *ptr = &a;
int* &p = ptr;
foo(p);
return 0;
}
4. int **x;
int *y = 3;
*x = y;
++y;
cout << x << endl;
● You may NOT call any functions, including those from the standard library.
2c) (7 points) Consider a function which takes as input an array of integer arrays, and returns the
index of the inner array with the smallest range. In the below example, the function should return 0,
since the array {4,4} has the smallest range.
● Your implementation MUST use the range() function from the previous
problem (you may assume it has been implemented correctly).
● You may NOT call any other functions, including those from the standard
library.
// REQUIRES: length_outer is the length of the outer array,
// length_outer >= 1
// length_inner >= 1, the length of each inner array
// EFFECTS: Returns the index of the array with the smallest range.
// If there is a tie, returns the smaller of the indices
// EXAMPLE:
// Input: {{4, 4}, {7, 2}, {4, 3}}, 3, 2 → 0
int search(int **arr, int length_outer, int length_inner);
Use the following code for Problem 3. We recommend you briefly skim it before answering
questions.
struct Song {
int num_plays;
string title;
string artist;
};
// REQUIRES:
struct Playlist {
int num_songs;
string name;
Song songs[MAX_SONGS];
};
3a) (2 points) Implement the Playlist_init(), which is part of the Playlist ADT, below
according to its RME. You MUST respect the interface for the Song ADT.
3c) (8 points) Implement the Playlist_merge(),which is part of the Playlist ADT, below
according to its RME. You MUST respect the interface for the Song ADT, and you MUST call
Playlist_find_song()in your implementation .
// REQUIRES: p_in1 and p_in2 point to valid Playlists. The sum of the
// sizes of both playlists is less than MAX_SONGS
// MODIFIES: *p_in1
// EFFECTS: For any song in *p_in2 that also exists in *p_in1 (i.e. both
// the artist and song name match), the relevant Song object
// in *p_in1 is modified so that the number of plays is the sum
// of the play count of the two Song objects.
// If the song does not exist in *p_in1, it is added to
// the end of *p_in1
string getBrand() {
return brand;
}
int getBatteryPercentage() {
return batteryPercentage;
}
private:
int batteryPercentage;
string brand;
};
private:
string phoneNumber;
bool lowPowerModeOn;
};
void closeHinge() {
hingeOpen = false;
cout << "Laptop is now closed" << endl;
}
void openHinge() {
hingeOpen = true;
cout << "Laptop is now open" << endl;
}
private:
double aspectRatio;
bool hingeOpen;
};
4a) (3 points) Most modern smartphones have the ability to turn on a low power mode in which
the device uses less power by either dimming the display, providing a slower clock update, or
limiting other features while still maintaining basic functionality of the phone. Implement the
virtual function depletePower(int amount) for the "Phone" class such that if low power
mode is on, the phone depletes exactly half as much as the input "amount" and if low power
mode is off, deplete exactly "amount" battery percentage. Implement this as it would appear
outside of the class definition.
To receive full credit, your implementation should not “hardcode” the string literal for a phone,
but instead read the static data member DeviceID from the “Phone” class.
4c) (3 points) Implement the constructor for the Laptop, as it would appear outside of the class
definition. The laptop should be constructed initially with the hinge closed. You must use a
member initializer list.
4d) (8 points) Write the output of each of the code snippets in the answer booklet. If the code
does not compile, write "compile error". If it compiles but results in undefined behavior at
runtime, write "undefined". If it compiles and runs but prints nothing, write "nothing".
4. Device blackberry("Blackberry");
Phone *p_ptr = &blackberry;
p_ptr->depletePower(10);
4e) (2 points) The following code does not compile. In the answer booklet, briefly (in 20 words
or fewer), explain why.