Sei sulla pagina 1di 13

Part I: 12 Multiple choice questions (2 points each):

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!

1. Consider the following program.

#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;
}

Is this a legal C++ program and does it execute without errors?

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

2. Suppose we have the following partial class definitions:

class Time { // wall clock time


public:
// construct Time object with initial time hh:mm
Time(int hh, int mm);

// return hour part of this Time object


int hours();

// return minute part of this Time object


int min();
...
private:
// representation of Time objects
int hr, min; // hours and minutes, where 0 <= min <= 60
};

class Event { // an event in the calendar


public:
// Construct Event with description d and Time t
Event(string d, Time t);

// Advance time of this Event by given number of minutes


void advanceTime(int minutes);
...
private:
// representation of an event
string what; // event description
Time when; // time of event
};

Now, here is a proposed implementation of the advanceTime method, which adds the given
number of minutes to the Time part of an event

// Advance time of this Event by given number of minutes


void Event::advanceTime(int minutes) {
when.min = when.min + minutes;
while (when.min > 60) {
when.hr++;
when.min = when.min - 60;
}
}

Is this function

A. Correct, works fine


B. The calculation is correct, but the variable when in Event::advanceTime has not been
declared properly (i.e., the compiler will complain about an undeclared variable).
C. The calculation is correct, but the function is illegal because it attempts to access private
variable when, which is not accessible inside function advanceTime.
D. The calculation is correct, but the function is illegal because it attempts to access the fields
of variable when.
E. None of the above
CSE 143 2000WI Midterm 1 Version B Page 3 of 13

3. Suppose we have a class Thing declared in file thing.h as follows.

class Thing {
public:
// initialize this Thing when it is constructed
Thing();
// other public operations
...
private:
// private representation
...
};

Now suppose we execute the following main program

#include "thing.h"

int main() {
Thing it;
Thing them[2];

// quit without doing anything else


return 0;
}

How many times is the default (null) constructor for class Thing executed?
A. 0
B. 1
C. 2
D. 3
E. 4

4. Consider the following program fragment:

#include <iostream>
using namespace std;

int* a = new int[10];


for(int i = 0; i<10; i++) a[i] = i;
int* b = &a[2];

cout << b[3];

What will this program fragment display?

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 mystery(int* pInt)


{
pInt = new int;
*pInt = 143;
cout << *pInt << " ";
}

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

6. Consider the following class definition which represents a geometric Point:

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);
};

bool Employee::equals(Employee& other) { return name == other.name; }

Is there anything wrong with the implementation of Employee::equals?


A. There is no problem
B. The equals method as implemented does not return value of type bool.
It returns a 1, 0, or 1.
C. This method is useless since one Employee object could never equal another.
D. The use of other.name is not valid since other is a reference parameter.
E. The use of other.name is not valid because it does not belong to the local Employee object
associated with the function call.

8. Suppose you have the following program.

#include <iostream>
using namespace std;

int main() {
int i, j, k;
i = 1; j = 2; k = 3;

cin >> i >> j >> k; // Input Statement

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

9. Suppose we see the following data structure definition in a program:

const int MAX_INPUT = 100; // maximum number of items in input

struct InputList { // buffer containing numbers to be processed


int input[MAX_INPUT]; // input numbers
int current; // current input position
int nItems; // number of input items
};

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.

10. Consider the following program:

#include <iostream>
using namespace std;

int foo(int& x, int& y) {


x = 2*y;
y = y + x;
return x + 5;
}

void main() {
int a = 3, b = 7;
a = foo(a, b+4);
cout << a << << b;
}

What does this program output?


A. 27 33
B. 27 7
C. 19 14
D. 19 18
E. Nothing. It is not a legal C++ program.
CSE 143 2000WI Midterm 1 Version B Page 7 of 13

11. What output does the following program produce?

#include <iostream>
using namespace std;

void f(int &a, int b, int &c) {


b = a - c;
a = b * c;
}

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

12. Consider the following program:

#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] << ;
}

What will this program output?


A. 0123
B. 1234
C. 3210
D. 4321
E. Cannot tell from the given program.
CSE 143 2000WI Midterm 1 Version B Page 8 of 13

Part II: Programming Question 1 (10 points)


This question does not involve C++ classes with member functions. It does, however, use the C++ string
class.

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;

struct Zoo { // information about a single zoo:


string name; // zoo name
int llama; // number of llamas in this zoo
int giraffe; // number of giraffes in this zoo
int lion; // number of lions, etc.
int tiger; // (similarly for each named animal)
int kangaroo;
int elephant;
int bear;
int donkey;
int wombat;
};

const int MAX_ZOOS = 1742; // maximum # of zoos in list

struct ZooList { // information about a collection of zoos:


int nZoos; // # of zoos currently in the list
Zoo z[MAX_ZOOS]; // zoo information is stored in z[0..nZoos-1]
};

(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.

// set ZooList z to an empty list


void makeEmptyZooList(ZooList &z) {

z.nZoos = 0;

}
CSE 143 2000WI Midterm 1 Version B Page 9 of 13

(b) ["Lions and tigers and bears, oh my!!"]

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) {

for (int k = 0; k < z.nZoos; k++)


if (z.z[k].lion > 0 && z.z[k].tiger > 0 && z.z[k].bear > 0)
cout<< z.z[k].name << endl;

}
CSE 143 2000WI Midterm 1 Version B Page 10 of 13

Programming Question 2 (10 points)

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.

Example: Suppose the shift schedule contains the following:

Monday: Alice Bob


Tuesday: Bob Ralph
Wednesday: Alice Fred Bob
Thursday: Alice Susan Bob
Friday: George Bob Alice
Saturday: Alice Ralph
Sunday: Alice Bob

With this schedule, reportOverworked should print the following (exact format and order of names does not
matter).

Overworked Employees: Alice Bob

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.

// reportOverworked - print names of employees working 6 or more shifts

void Schedule::reportOverworked() {

string currentEmployee; // name of and # of shifts worked by


int nShifts; // current employee in the list
string employee; // name of employee for next shift in list

// If schedule is empty, quit without examining it


if (size <= 0)
return;

// ensure schedule sorted


sortByName();

// count first shift for first employee


currentEmployee= shifts[0].getEmployee().getName();
nShifts = 1;

// examine and count remaining shifts


for (int k = 1; k < size; k++) {

// if this is another shift for the same employee, count it


// and print name if this is the 6th shift for this person;
// otherwise, set count to 1 for new employee.
employee = shifts[k].getEmployee().getName();
if (employee == currentEmployee) {
nShifts++;
if (nShifts == 6)
cout << employee << ' ';
} else {
nShifts = 1;
currentEmployee = employee;
}
CSE 143 2000WI Midterm 1 Version B Page 11 of 13

Reference information: Class Definitions for help-desk program

// description of one help desk employee and his/her skills


class Employee {
public:
// Construct new Employee object with no skills and null name
Employee();

// Retrieve or set name of this Employee


string getName();
void setName(string new_name);

// Retrieve or set this Employee's skills:


// "knowsXyzzy" is true if this Employee has skills on system xyzzy
// "setXyzzy(b)" sets this Employee's skills on system xyzzy to b (true of false)

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;
};

string name; // name of this Employee


Skills skills; // skills of this Employee
};

// Representation of one help desk work shift

class Shift {
public:
// Construct new Shift with unknown uninitialized values
Shift();

// Store employee as this Shift's employee


void setEmployee(Employee employee);

// = this Shift's employee


Employee getEmployee();

// Store day of this Shift


void setDay(int day);
CSE 143 2000WI Midterm 1 Version B Page 12 of 13

// = day of this Shift


int getDay();

private: // Shift representation


Employee employee; // Employee information
int day; // day of Shift
};

const int MAX_SHIFTS = 100; // max # shifts in a schedule

// Weekly help desk schedule containing employee shift information


class Schedule {
public:
// Construct an empty Schedule
Schedule();

// add given shift to this Schedule


void addShift(Shift 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 the shifts in this schedule


void printShifts();

// Print a list of employees in this Schedule along with their


// skills, sorted by employee name.
void printEmployees();

// Print this Schedule in order from Monday to Sunday showing a list of


// employees working on each day.
void printSchedule();

// Print report of shifts without full coverage, and the skills that are missing
void reportCoverageGaps();

// Print report of shifts with more than 3 employees on duty


void reportOverStaffedShifts();

// Print report of Employees working 6 or more shifts.


void reportOverworked();

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

// Sort the shifts in this schedule alphabetically by employee name


void sortByName();
};

Potrebbero piacerti anche