You are on page 1of 6

COSC 1437 (DL)- Spring 2017

Final Exam - Chapters 13-17, 19 & Notes


Total Points: 60

Due: Tuesday, May 9th @ 11:59PM. Look at Syllabus/ICR about late work.

Directions: For Questions 1-21, clearly mark answers on a separate word (or notepad) document. See
sample file/directions provided by your professor and submit to the appropriate location under the
MyTCC (BlackBoard) site.

Assume all variables are properly declared- unless otherwise mentioned.

Multiple Choice. Mark the one best answer for each question. (2 pts. each)

1. Complete the following function template:

____
T reverse(T x)
{
return -x;
}

A. template : class T C. template (class T)


B. template <class T> D. template {class T}
2. Consider the accompanying definition of a recursive function:

void printNum(int num) //Line 1


{ //Line 2
if (n < 0) //Line 3
cout << "Num is negative" << endl; //Line 4
else if (num == 0) //Line 5
cout << "Num is zero" << endl; //Line 6
else //Line 7
{ //Line 8
cout << num << " "; //Line 9
printNum(num 1); //Line 10
} //Line 11
} //Line 12

Which of the statements represent the base case?


A. Statements in Lines 3 and 4 C. Statements in Lines 3-6
B. Statements in Lines 5 and 6 D. Statements in Lines 5-10
3. The try block is followed by one or more ____ blocks.
A. catch C. do
B. finally D. throw
4. Two types of container classes in the STL are
A. box and cylinder C. sequential and associative
B. array and struct D. constant and literal

1
5. To catch an exception, a program must
A. first experience an unrecoverable error. C. have a try/catch construct.
B. have a throw() function. D. first experience a fatal error.
6. A linked list is a collection of components called ____.
A. elements C. members
B. nodes D. pointers
7. What is special about the last node in a dynamic linked list?
A. Its component (data) member is empty.
B. Its component (data) member contains the value 0.
C. Its link member is empty.
D. Its link member contains the value NULL.
8. Given the template:

template<class T>
T Half(T n)
{
return n / 2;
}

Which of the following does not contain a valid function call?


A. anInt = Half<int>(324); C. anInt = Half(324);
B. aFloat = Half<float>(98.6); D. anInt = Half<324>;
9. You typically do not throw an exception
A. when invalid data is detected
B. to test an exception handler
C. when the processing thats done by a method cant be completed
D. after catching an exception and performing some processing
10. The ____ keyword specifies that the class members following it can be accessed only by using the
class functions.
A. private C. protected
B. public D. restricted
11. Objects are referred to as ____ of a class.
A. state C. built-in types
B. members D. instances
12. A(n) ____ is any nonconstructor class method that changes an objects data values.
A. access method C. get function
B. mutator method D. return function
13. ____ is the capability of deriving one class from another class.
A. Overloading C. Polymorphism
B. Redefinition D. Inheritance
14. Overriding a base member function by using an overloaded derived member function is an example
of ____.
A. inheritance C. polymorphism
B. scoping D. derivation

2
15. If a base class member has a protected access and the derived classs access specifier is public,
the derived class member is ____ to its class.
A. public C. protected
B. global D. private
16. Dynamic binding determines which function should be called at runtime, based on the ____ type
making the call.
A. object C. function
B. class D. operator
17. Consider the accompanying class definition, and declaration:

class rectangleType
{
public:
void setLengthWidth(double x, double y);
//Postcondition: length = x; width = y;
void print() const;
//Output length and width;
double area();
//Calculate and return the area of the rectangle;
double perimeter();
//Calculate and return the parameter;
rectangleType();
//Postcondition: length = 0; width = 0;
rectangleType(double x, double y);
//Postcondition: length = x; width = y;

private:
double length;
double width;
};

rectangleType bigRect;

Which of the following statements is correct?


A. rectangleType.print(); C. bigRect.print();
B. rectangleType::print(); D. bigRect::print();

18. Which of the following class definitions makes the public members of the class aClass become the
public members of the class bClass?
A. class aClass: public bClass {
//...
};
B. class bClass: public aClass {
//...
};
C. class bClass: aClass {
//...
};
D. class aClass: bClass {
//...
};

3
19. What is the output of the following program?

#include <iostream>
using namespace std;

class bClass {
public:
void print() const;
bClass(int a = 0, int b = 0);
//Postcondition: x = a; y = b;
private:
int x;
int y;
};

class dClass: public bClass {


public:
void print() const;
dClass(int a = 0, int b = 0, int c = 0);
//Postcondition: x = a; y = b; z = c;
private:
int z;
};

int main() {
bClass bObject(2, 3);
dClass dObject(3, 5, 8);

bObject.print();
cout << endl;
dObject.print();
cout << endl;

return 0 ;
}

void bClass::print() const {


cout << x << " " << y << endl;
}

bClass::bClass(int a, int b) {
x = a;
y = b;
}

void dClass::print() const {


bClass:print();
cout << " " << z << endl;
}

dClass::dClass(int a, int b, int c)


: bClass(a, b){
z = c;
}

4
A. 2 3 C. 3 5 8
2 3 3 5 8
B. 2 3 D. 5 8
3 5 8 3 5 8

20. Which of the following function prototypes overloads the != operator for the class rectangleType?
A. bool operator!=(rectangle&) const;
B. bool operator!=(const rectangleType&) const;
C. int operator!=(const rectangleType) const;
D. int operator!=(rectangle&) const;

Short Answer. Clearly mark answers as directed. Partial Credit will be given. (10 @ 2 each)

21. Consider the following class definition and variable declaration:

class BagType
{
public:
void set(string, double, double, double, double);
void print() const;
string getStyle() const;
void getPrice() const;

BagType();
BagType(string, double, double, double, double);

private:
string style;
double l;
double w;
double h;
double price;
};

BagType newBag;

Answer the following:

A. How many total members does class BagType have?

B. How many private members class BagType have?

C. How many constructors does class BagType have?

D. How many constant functions does class BagType have?

E. Which constructor is used to initialize the object newBag?

5
Course Orientation- This part has already been taken. Your score will be added to the exam. (10 pts.)

=============================================================================================

Extra Credit: Implement the following program. Follows same program guidelines and graded on the
same scale as program sets. Submit only your .cpp file- no test runs/folder required. Partial credit given.
(10 points)

A maze is a two-dimensional structure with an entrance and sometimes an exit. Write a C++
program to determine if any maze has an exit. All the mazes will have the entrance point at the top left
corner. The exit point can be anywhere in the far-right column. A valid path will be a continuous block
of 1s that connect the top left corner to any spot on the far-right column. A valid path can only be
connected horizontally or vertically. Diagonal connections are not legal. The 1s in the file represent the
path and 0s represent the walls of the maze. Output "Exit found" if there is a path leading to an exit out
of the maze, otherwise output "Exit not found" if there is not a path leading out of the maze. If it is a
valid path output the path coordinates as well. Input will be from a data file consisting of a series of
matrices consisting of 1s and 0s along with its size. The 1s represent the path and 0s show that there is no
path in that area. Let the user input the name of the file from the keyboard. Output should be user
friendly.

Name the program: MazeNumbersXX.cpp, where XX are your initials.

You might also like