Midterm1 Solutions
Midterm1 Solutions
Answer all of the following questions. READ EACH QUESTION CAREFULLY. Fill the correct bubble
on your mark-sense sheet. Each correct question is worth 2 points. Choose the one BEST answer for each
question. Assume that all given C++ code is syntactically correct unless a possibility to the contrary is
suggested in the question.
Remember not to devote too much time to any single question, and good luck!
#include <iostream>
using namespace std;
int * f (int n) {
int a[10];
for (int i = 0; i < 10; i++)
a[i] = i*n;
return a;
}
void main() {
int j, k;
int * p;
j = 12;
p = f(j);
for (k = 0; k < 10; k++) {
cout << p[k];
}
cout << endl;
}
A. Fails to compile because variable p in the main program is not an array, and cannot be
subscripted.
B. Fails to compile because "p=f(j);" is an array assignment, and array assignments are not
supported in C/C++.
C. Fails during execution with a subscript-out-of-range error.
D. Might appear to work, but is not correct because it uses a dangling pointer (pointer to no-
longer-allocated storage).
E. None of the above (either the program works fine, or it fails for some reason not given here).
CSE 143 2000WI Midterm 1 Version B Page 2 of 13
Now, here is a proposed implementation of the advanceTime method, which adds the given
number of minutes to the Time part of an event
Is this function
class Thing {
public:
// initialize this Thing when it is constructed
Thing();
// other public operations
...
private:
// private representation
...
};
#include "thing.h"
int main() {
Thing it;
Thing them[2];
How many times is the default (null) constructor for class Thing executed?
A. 0
B. 1
C. 2
D. 3
E. 4
#include <iostream>
using namespace std;
A. 2
B. 3
C. 5
D. Nothing. It is not a legal C++ program.
E. This is a legal program, but the value printed cannot be determined from the code given here
CSE 143 2000WI Midterm 1 Version B Page 4 of 13
5. What does the following program output? (Hint: you may find it very helpful to diagram
the use of pointers and memory to follow what is happening here.)
#include <iostream>
using namespace std;
void main()
{
int i = 142;
int* pInt = &i;
mystery(pInt);
cout << *pInt << " ";
}
A. 143 142
B. 142 143
C. 142 142
D. 143 143
E. No output, the code is erroneous
class Point {
private:
// representation is here
public:
Point(double x, double y);
double getX();
double getY();
double distanceTo(Point other); // distance between this point and the other
void paint(); // paint the Point on the screen
};
From the definition of Point above we know that the representation of a Point must be:
A. A point in the x-y coordinate system.
B. Two variables of type double: one for the x coordinate, and one for the y coordinate.
C. A dynamically allocated array.
D. A polar representation: an angle and a distance from the origin.
E. There is no way of knowing.
CSE 143 2000WI Midterm 1 Version B Page 5 of 13
7. Consider the following class definition, which represents an Employee, and the
implementation of one of the methods:
class Employee {
private:
string name;
public:
bool equals(Employee& other);
};
#include <iostream>
using namespace std;
int main() {
int i, j, k;
i = 1; j = 2; k = 3;
return 0;
}
Suppose we execute this program and the user enters the following input
17 xvii 42
What are the values stored in variables i, j, and k after execution of the input
statement (cin >> ...)?
A. i = 1, j = 2, k = 3
B. i = 17, j = 2, k = 3
C. i = 17, j = unknown, k = 3
D. i = 17, j = 2, k = 42
E. i = 17, j = unknown, k = 42
CSE 143 2000WI Midterm 1 Version B Page 6 of 13
What do we know *for certain* about this data structure, given the above
definition?
A. Array elements input[current]..input[nItems] contain input items that have not yet been
processed.
B. Array elements input[current+1]..input[nItems] contain input items that have not yet
been processed.
C. Array elements input[current]..input[nItems-1] contain input items that have not yet been
processed.
D. Array elements input[current+1]..input[nItems-1] contain input items that have not yet
been processed.
E. It is impossible to answer the question with certainty given the data structure and supplied
comments.
#include <iostream>
using namespace std;
void main() {
int a = 3, b = 7;
a = foo(a, b+4);
cout << a << << b;
}
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 15;
f(b,a,a);
cout << a << ' ' << b << endl;
}
A. 10 50
B. 50 10
C. 10 15
D. 10 150
E. none of the above
#include <iostream>
using namespace std;
void bar(int* a) {
for(int j = 0; j < 4; j++)
a[j] = 4 j;
}
void main() {
int* a = new int[4];
bar(a);
for(int i = 3; i >= 0; i--)
cout << a[i] << ;
}
In this problem, you are to write some code to process a data structure containing information about the
animals in various zoos around the world. The data structures are defined as follows:
#include <string>
#include <iostream>
using namespace std;
(a) Complete the definition of the function makeEmptyZooList that sets its ZooList parameter to an
empty list (containing no Zoos). Your code should perform all of the initialization actually needed to
solve the problem, and no more.
z.nZoos = 0;
}
CSE 143 2000WI Midterm 1 Version B Page 9 of 13
An Interesting Zoo is one whose collection contains at least one each of lions, tigers, and bears (i.e., one or
more of each of these three kinds of animals). Complete the following function definition so it prints on
cout the name(s) of all interesting zoos in its ZooList parameter, one zoo name per output line.
// print names of zoos from list z that have at least one lion, tiger, and bear
void printInterestingZoos(ZooList &z) {
}
CSE 143 2000WI Midterm 1 Version B Page 10 of 13
The boss would like to add a feature to the help-desk scheduling program to print a report listing people
who are working too many shifts.
For this problem, complete the definition of function reportOverworked, which, when it is executed, prints
a list of all employees who are working six or more shifts during the week.
With this schedule, reportOverworked should print the following (exact format and order of names does not
matter).
Your code should use the class definitions for Homework 2, which are printed for reference at the end of
this problem. Notice that these are the same as the ones given in the sample solution, except that the shifts
in the schedule are not necessarily sorted when reportOverworked is called.
Hint: Be sure to take advantage of any available functions in the classes to simplify your code.
void Schedule::reportOverworked() {
bool knowsUnix();
void setUnix(bool b);
bool knowsWindows();
void setWindows(bool b);
bool knowsMac();
void setMac(bool b);
private:
struct Skills { // skills of one Employee. values are true
bool unix; // for each skill if the employee has that skill
bool windows;
bool mac;
};
class Shift {
public:
// Construct new Shift with unknown uninitialized values
Shift();
// Remove shift from this schedule given the employee name and (int)
// day of the shift. Return true if that shift was successfully located
// and removed, else return false.
bool removeShift(string name, int day);
// Read schedule information from the text file named file_name, and
// store the information in it in this Schedule, replacing any previous
// information in this Schedule
void readScheduleFile(string file_name);
// Print report of shifts without full coverage, and the skills that are missing
void reportCoverageGaps();
private:
// representation of the Schedule
Shift shifts[MAX_SHIFTS]; // Shifts of this Schedule are stored in
int size; // shifts[0..size-1], in no particular order
// private functions
CSE 143 2000WI Midterm 1 Version B Page 13 of 13