CS 052 Midterm
A class can have more than one destructor.
False
A class and its members can be described graphically using Unified Modeling Language (UML) notation.
True
A derived class can redefine a public member function of the base class.
True
Given the declarationint *p;The statementp = new int[50]; dynamically allocates an array of 50 elements of type int and p contains the address of the first element in the array.
True
By default, all members of a class are public.
False
Constructors are called like any other function.
False
In C++, &p, p, and *p all have the same meaning.
False
A ____ sign in front of a member name on a UML diagram indicates that this member is a privatemember.
-
A C++ implementation file has the extension ____.
.cpp
Assume that the typiclal linked list operations have been correctly implemented for this class. Further assume that the identifier for this linked list object is myList. After the operations below execute how many dangling pointers will exist? Draw the "state" of the linked list after the following operations have been executed. Identify any dangling pointers and/or memory leaks. (For this question do not try to draw an answer in Canvas. Draw your answer on paper and compare it to the hint attached to Homework 8.) Node *current = tail->previous->previous; delete current->next; tail->previous = NULL;
1
All operators can be overloaded as member functions of the class.
False
Exceptions are used to signal errors or unexpected events that occur at compile time.
False
Exceptions signal errors or unexpected events that occur when code is compiled.
False
The process of propagating uncaught exceptions from a function to its caller is called propagating the stack of function calls.
False
There is no way to definitively indicate the end of a linked list.
False
When a node is inserted into a linked list, all of the elements after the insertion point must be moved one position toward the end of the list.
False
Assume that the basic circular linked list operations have been correctly implemented for this class. Further assume that the identifier for this linked list object is myList. What output will be displayed on screen after this series of operations on the list? myList.intersection(); myList.bisection(); myList.associative(); myList.void(); cout << myList.cout() << " " << myList.cin() << endl;
Nothing. These operations are nonsensical.
A pointer variable is a variable that stores the address of a memory location.
True
A try block must be followed by one or more catch blocks.
True
An exception can propagate backwards along the chain of functions calls until the exception is thrown out of a try block that has a catch block that can handle it.
True
Dynamically allocated data structures may be linked together in memory to form a chain.
True
If p is a pointer variable, the statement p = p + 1; is valid in C++.
True
In C++, an array name is a constant pointer.
True
In many cases, a program will need to test for several different types of errors and signal which one has occured.
True
The preprocessor directive #ifndef is used to prevent multiple inclusions of a header file in a program.
True
Using operator overloading, it is possible to cange an operator's entire meaning.
True
When a throw statement is executed, control is passed to another part of the program known as an exception handler.
True
If I define a class that has a data member that is a pointer, what other members should I also include in the class?
copy constructor, overloaded assignment operator, destructor
There are ________ possible ways for a thrown exception to go uncaught.
two
In C++, ____ is called the address operator.
&
Consider the program below. What is output when this program executes? #include <iostream> #include <vector> #include <exception> #include <stdexcept> using namespace std; int main() { try { vector<int> a; a.push_back(132); a.push_back(126); a.push_back(49); a.push_back(13); a.push_back(20); int i = 40; if ( i > 4 ) throw "ERROR: index is out of range!"; cout << "a[i] = " << a.at(i) << endl; } catch(out_of_range) { cout << "caught out_of_range exception in function main...\n"; } catch(...) { cout << "unknown exception caught in function main...\n"; } return 0; }
unknown exception caught in function main...
A ____ sign in front of a member name on a UML diagram indicates that this member is a publicmember.
+
The drawing below represents the "state" of a linked list ofWidget objects. All objects on the list are instances of theWidget class. The class has a member function getWeight() that returns the value of the object's weightdata member. The class also has a member function getNext()that returns the address stored in the object's next data member. The next data member is a pointer to a Widget object. The number listed within the drawing of each Widget is the value of its weight data member. After the code below executes there will be _________ Widget objects deleted, _________ dangling pointers, and _________"lost objects" that represent memory leaks. Widget *current = widgetList; bool done = false; while ( !done && current != NULL ) { if ( (current->getWeight() ) > 7.9 ) { delete current; done = true; } else { current = current->getNext(); } }
1, 2, 2. Object with weight 8.0 is deleted, the next pointer of object with weight 7.6 is dangling and the current pointer is dangling; objects with weight 3.2 and 2.6 are "lost" and represent memory leaks.
What is the output of the following code?int *p;int x;x = 12;p = &x;cout << x << ", ";*p = 81;cout << *p << endl;
12, 81
Assume that the basic circular linked list operations have been correctly implemented for this class. Further assume that the identifier for this linked list object is myList. What output will be displayed on screen after this series of operations on the list? int total = 0; while (total < 200) { total += myList.front(); myList.advance(); } cout << total << endl;
227
Assume that the typiclal linked list operations have been correctly implemented for this class. Further assume that the identifier for this linked list object is myList. After the operations below execute how many dangling pointers will exist? Draw the "state" of the linked list after the following operations have been executed. Identify any dangling pointers and/or memory leaks. (For this question do not try to draw an answer in Canvas. Draw your answer on paper and compare it to the hint posted attached to Homework 8.) head = head->next->next->previous; Node *current = tail; delete current->next; while (current->previous != NULL) { if (current->info < 80) { current = current->previous; delete current->next; } else { current = current->previous; } }
4
What is the output of the following code?int *p;int x;x = 76;p = &x;*p = 43;cout << x << ", " << *p << endl;
43, 43
What is the value of x after the following statements execute?int x = 25;int *p;p = &x;*p = 46;
46
The drawing below represents the "state" of a linked list ofWidget objects. All objects on the list are instances of theWidget class. The class has a member function getWeight() that returns the value of the object's weightdata member. The class also has a member function getNext()that returns the address stored in the object's next data member. The next data member is a pointer to a Widget object. The number listed within the drawing of each Widget is the value of its weight data member. After the code below executes there will be _________ Widget objects deleted, _________ dangling pointers, and _________"lost objects" that represent memory leaks. Widget *current = widgetList; Widget *byebye = NULL; bool done = false; while ( !done && current != NULL ) { if ( (current->getWeight() ) > 1.9 ) { byebye = current current = current->getNext(); delete byebye; done = false; } else { current = current->getNext(); } }
5, 2, 0. All objects are deleted, there are no memory leaks, but there are two dangling pointers. The widgetList pointer was never moved, so it is still pointing to the address of the first object in the list, but that memory has been deallocated and is no longer valid. The currentpointer moved past the end of the list, so it is pointing to NULL and is valid. The pointer byebyestopped on the last object which was also deallocated so byebye is a dangling pointer.
What will the following program display on the screen? #include <iostream>using namespace std;class Tank{private: int gallons; public: Tank() { gallons = 50; } Tank( int gal ) { gallons = gal; } int getGallons() { return gallons; }};int main(int argc, const char * argv[]){ Tank storage1, storage2, storage3(20); cout << storage1.getGallons() << endl; cout << storage2.getGallons() << endl; cout << storage3.getGallons() << endl; return 0;}
50 50 20
Assume that the basic circular linked list operations have been correctly implemented for this class. Further assume that the identifier for this linked list object is myList. What output will be displayed on screen after this series of operations on the list? myList.advance(); myList.remove(); myList.remove(); myList.advance(); cout << myList.front() << " " << myList.back() << endl;
76 80
____ is the ability to combine data, and operations on that data, in a single unit.
Encapsulation
A derived class can directly access any member of the base class.
False
A derived class cannot directly access publicmembers of a base class.
False
A pointer variable is declared using an ampersand (&) character.
False
From the list below, select the three characteristics of Object Oriented Design (OOD) / Object Oriented Programming (OOP).
Polymorphism, Inheritance, Encapsulation
From the list below, select the three characteristics of Object Oriented Design (OOD) / Object Oriented Programming (OOP).
Polymorphism, inheritance, encapsulation
A data structure that points to an object of the same type as itself is known as a(n) ____________________ data structure.
Self-referential
A copy constructor is automatically called in the following situations:
When an object is being created and initialized with another object of the same class. An object is passed by value as an argument to a function. A function returns an instance of a class, by value.
Consider the program below. What is output when this program executes? #include <iostream> #include <vector> #include <exception> #include <stdexcept> using namespace std; int main() { try { vector<int> a; a.push_back(132); a.push_back(126); a.push_back(49); a.push_back(13); a.push_back(20); cout << "a[3] = " << a.at(3) << endl; } catch(out_of_range) { cout << "caught out_of_range exception in function main...\n"; } catch(...) { cout << "unknown exception caught in function main...\n"; } return 0; }
a[3] = 13
Consider the program below. What is output when this program executes? #include <iostream> #include <vector> #include <exception> #include <stdexcept> using namespace std; int main() { try { vector<int> a; a.push_back(132); a.push_back(126); a.push_back(49); a.push_back(13); a.push_back(20); int i = 4; if ( i > 4 ) throw "ERROR: index is out of range!"; cout << "a[i] = " << a.at(i) << endl; } catch(out_of_range) { cout << "caught out_of_range exception in function main...\n"; } catch(...) { cout << "unknown exception caught in function main...\n"; } return 0; }
a[i] = 20
The basic linked list operations are:
adding an element to a list, removing an element from the list, traversing the list, destroying the list
Existing classes, from which you create new classes, are called ____ classes.
base
The try block is immediately followed by one or more __________ blocks.
catch
Consider the program below. What is output when this program executes? #include <iostream> #include <vector> #include <exception> #include <stdexcept> using namespace std; int main() { try { vector<int> a; a.push_back(132); a.push_back(126); a.push_back(49); a.push_back(13); a.push_back(20); cout << "a[3] = " << a.at(30) << endl; } catch(out_of_range) { cout << "caught out_of_range exception in function main...\n"; } catch(...) { cout << "unknown exception caught in function main...\n"; } return 0; }
caught out_of_range exception in function main...
Inheritance is an example of a(n) ____ relationship.
is-a
If the programmer does not specify a copy constructor for a class, then the compiler calls a default copy constructor when necessary; resulting in a ________ assignment.
memberwise
If I write code that dynamically allocates memory with the operator new, I must remember to deallocate that memory using the operator delete. Failing to do so results in a(n) ___________________ .
memory leak
This linked list is an instance of the doubly linked list class having the class name DLL. This is a non-template class. Nodes on the linked list have an info data member of type int. Write a member function for class DLL that will output the contents of each Node's info data in reverse order. Use iteration (not recursion). (For this question do not try to write the code in Canvas, write your code by hand or type into an IDE, then compare your answer to the hint attached to Homework 8.) How many lines of code does your function require?
more than 1
A programmer-defined copy constructor must have a single parameter that is a reference to the _________ class.
same
A thrown exception could go uncaught if _______________________________________.
the program contains no catch blocks with an exception parameter of the correct data type. it is thrown from outside a try block..
The process of beginning at the head of a list and going through the entire list while doing some processing at each node is called ______________ the list.
traversing