0% found this document useful (0 votes)
29 views

Eecs280f19 - Midterm - Solutions

Uploaded by

jeremymoon567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Eecs280f19 - Midterm - Solutions

Uploaded by

jeremymoon567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

EECS 280 Midterm Exam

Fall 2019
Written Portion SOLUTIONS
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 space to write you answers for the multiple-choice and written
components of the exam.

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 last several pages include space for scratch work (including the last piece of
paper, which you may tear out).

Write your uniqname on the line provided at the top of each


page.

You are to abide by the University of Michigan/Engineering honor code. To receive a


grade, sign below to signify that you have kept the honor code pledge:

I have neither given nor received aid on this exam, nor have I concealed any violations of
the Honor Code.

Signature: _________________________________________

Name: _________________________________________

Uniqname: _________________________________________

UMID: _________________________________________

EECS 280 Fall 2019 Midterm Answers Packet


1/11
Problem 0: Short Answers (30 Points)
When you make a choice in this section, please fill in the bubble completely to make sure
Gradescope is able to read it.

0a) True/False (10 points)


True False

1. ⓣ ⓕ

2. ⓣ ⓕ Should be “int”

3. ⓣ ⓕ

4. ⓣ ⓕ References are just aliases for a preexisitng object

5. ⓣ ⓕ ERROR: THIS IS FALSE. Constructors are not inherited by default.

6. ⓣ ⓕ

7. ⓣ ⓕ Either accepted based on terminology discrepency

8. ⓣ ⓕ If p is a char pointer, it will be printed as a c-string

9. ⓣ ⓕ

10. ⓣ ⓕ

EECS 280 Fall 2019 Midterm Answers Packet


2/11
0b) Multiple Choice (20 points)

ⓐ ⓑ ⓒ ⓓ ⓔ 1.

ⓐ ⓑ ⓒ ⓓ ⓔ 2.

ⓐ ⓑ ⓒ ⓓ ⓔ 3.

ⓐ ⓑ ⓒ ⓓ ⓔ 4.

ⓐ ⓑ ⓒ ⓓ ⓔ 5.

ⓐ ⓑ ⓒ ⓓ ⓔ 6.

ⓐ ⓑ ⓒ ⓓ ⓔ 7.

ⓐ ⓑ ⓒ ⓓ ⓔ 8.

ⓐ ⓑ ⓒ ⓓ ⓔ 9.

ⓐ ⓑ ⓒ ⓓ ⓔ 10.

EECS 280 Fall 2019 Midterm Answers Packet


3/11
Problem 1: Strings and I/O (17 Points)
1a) (9 points)

void split_string(char *dst, const char *src, int pos);

int str_len(const char* str) {


int size = 0;
while(*str++) size++;
return size;
}

void split_string(char *dst, const char* src, int pos) {


int str_size = str_len(src);
int dst_idx = 0; // keep track of what we’ve written in dst

// copy back part of src


for(const char *it = src+pos; it < src+str_len(src); it++) {
*(dst+dst_idx++) = *it;
}

// copy front part of src


for(const char *it = src; it < src+pos; it++) {
*(dst+dst_idx++) = *it;
}

// add null terminator


*(dst+dst_idx) = '\0';

EECS 280 Fall 2019 Midterm Answers Packet


4/11
1b) (8 points)

void readFromStream(istream &is, int N) {


string str;
while(is >> str) {
cout << str;
cout<<",";
for(int i=1; i<N; i++) is >> str;
}
}

int main(int argc, char **argv){


if(argc != 2 && argc != 3) return 1;

int N = atoi(argv[1]);

if(argc == 3) {
ifstream is(argv[2]);
readFromStream(is, N);
} else {
readFromStream(cin, N);
}

return 0;
}

EECS 280 Fall 2019 Midterm Answers Packet


5/11
Problem 2: Arrays and Pointers (18 Points)
2a) (8 points)

1. 42

2. Undefined

3.
rld
t

4. Compile error (also accept


“Undefined”)

2b) (3 points)

int range(const int * array, int length) {

int cur_max = array[0];


int cur_min = array[0];
for(int i=1; i<length; i++) {
if(array[i] > cur_max) cur_max = array[i];
if(array[i] < cur_min) cur_min = array[i];
}
return cur_max-cur_min;

EECS 280 Fall 2019 Midterm Answers Packet


6/11
2c) (7 points)

int search(int **arr, int length_outer, int length_inner) {

int cur_min_range = range(arr[0], length_inner);


int idx = 0;
for(int i=1; i<length_outer; i++) {
int cur_range = range(arr[i], length_inner);
if(cur_range < cur_min_range) {
cur_min_range = cur_range;
idx = i;
}
}
return idx;

EECS 280 Fall 2019 Midterm Answers Packet


7/11
Problem 3: Structs and C-Style ADTs (16 points)

3a) (2 points)

void Playlist_init(Playlist *p_in, string name_in) {

p_in->num_songs = 0;
p_in->name = name_in;

3b) (6 points)

Song *Playlist_find_song(Playlist *p_in, const Song *s_in) {

for(int i=0; i<p_in->num_songs; i++) {


if(song_title(&p_in->songs[i]) == song_title(s_in) &&
song_artist(&p_in->songs[i] == song_artist(s_in))
return &(p_in->songs[i]);
}
return nullptr;

EECS 280 Fall 2019 Midterm Answers Packet


8/11
3c) (8 points)

void Playlist_merge (Playlist *p_in1, const Playlist *p_in2) {

for(int i=0; i<p_in2->num_songs; i++) {


Song *s2 = &p_in2->songs[i];
Song *s1 = playlist_find_song(p_in1, s2);
if(s1)
Song_inc_plays(s1, Song_plays(s2));
else
Playlist_addSong(p_in1, *s2);

EECS 280 Fall 2019 Midterm Answers Packet


9/11
Problem 4: Inheritance and Polymorphism (19 Points)

4a) (3 points)

void Phone::depletePower(int amount) {

cout << “Phone “;

if(lowPowerModeOn)
Device:: depletePower(amount/2);
else
Device::depletePower(amount);

4b) (3 points)

int countChargedPhones(const std::vector<Device *> &devices) {

int count=0;
for(int i=0; i<devices.size(); i++) {
if(devices[i]->getBatteryPercentage() > 75 &&
devices[i]->getDeviceID() == Phone::DeviceID )
count++;
}
return count;

EECS 280 Fall 2019 Midterm Answers Packet


10/11
4c) (3 points)

Laptop::Laptop(double aspectRatio_in, const string &brand_in)


: Device(brand_in), hingeOpen(false), aspectRatio(aspectRatio_in)
{}

4d) (8 points)

1. Phone Depleted 2%
Phone Depleted 1%

2. Compile error

3. Pinging cell towers!

4. Compile error

4e) (2 points)

Can’t create an instance of Laptop because startUp() is not


implemented

(startUp() is a pure virtual function, therefore, Laptop is an


abstract class)

EECS 280 Fall 2019 Midterm Answers Packet


11/11

You might also like