Data Structures Midterm Review
If an exception is not caught, the program will abort execution. True False
True
In C++, you can use vector object just like an array, but a vector's size can grow automatically if needed. True False
True
Template definition can be template<typename T> or template<class T> True False
True
Templates provide the capability to parameterize types in functions and classes. True False
True
The final keyword can be used to prevent a function from being overridden or to declare a constant. True False
True
The line containing a throw statement is known as the throw point. True False
True
To create a vector with initial values: 5.5, 6.6, 7.7, you can use the statement: vector<float> v = {5.5, 6.6, 7.7}; True False
True
To redefine a function, the function must be defined in the derived class using the same signature and return type as its base class. True False
True
You have to use the template prefix for each function in the class definition and class implementation True False
True
Which of the following is the correct syntax for defining a class named Table, which inherits from the class named Furniture? class Furniture: public Table{ ... }; class Table(Furniture){ .... }; class Table(Furniture); class Table: public Furniture { ... };
class Table: public Furniture { ... };
The ______ constructor is executed when an object is declared and initialized by using the value of another object. class copy default struct
copy
Which of the following statements is incorrect to invoke the maxValue template function below? template<typename T> T maxValue(const T& value1, const T& value2) { if(value1 > value2) return value1; else return value2; } cout << maxValue(1.5, 2); cout << maxValue(1.5, 2.5); cout << maxValue('1', '2'); cout << maxValue(1, 2);
cout << maxValue(1.5, 2);
If an exception is thrown by a member function of a class object, then the class ________ is called. handler constructor None of these destructor catcher
destructor
Run-time binding is also known as _______ binding. static deep shallow dynamic
dynamic
To invoke the swap function below, use ______. template<typename T> void swap(T& v1, T& v2) { T temp = v1; v1 = v2; v2 = temp; } swap(1, 2) int i1 = 1; int i2 = 2; swap(i1, i2); int i1= 1; double d2 = 2; swap(i1, d2); int v1 = 1; int v2 = 2; swap(&v1, &v2)
int i1 = 1; int i2 = 2; swap(i1, i2);
Consider the following statement: ptrMemberVarType objectThree(objectOne); The values of the member variables of objectOne are being copied into the corresponding member variables of objectThree. This initialization is called the _________. default assignment member-wise initialization member-wise assignment default initialization
member-wise initialization
When the ________ operator fails to allocate memory, C++ throws a bad_alloc exception. None of these catch new alloc
new
Which of the following code is correct? (a) vector<int> v; v[0] = 4; (b) vector<int> v(5); v[0] = 4; both a and b are incorrect both a and b are correct only code b is correct only code a is correct
only code b is correct
A(n) ________ is a collection of distinct elements of the same type class pointer list ordered set
ordered set
In C++, ________ means that a variable of a supertype can refer to a subtype object, and the capability of determining which function to invoke is known as __________. polymorphism, dynamic binding polymorphism, inheritance inheritance, polymorphism polymorphism, encapsulation
polymorphism, dynamic binding
Which of the following would be appropriate syntax for the heading of a copy constructor for a class called rulerType? copy rulerType(int inches, int centimeters) rulerType (const rulerType& myRuler) rulerType() rulerType(int inches, int centimeters)
rulerType (const rulerType& myRuler)
Match the following: size() vector<typename>() vector<typename>(size) push_back(e) at(index) vector<typename>(size,value) clear() empty() returns the size of the vector creates an empty vector creates a vector with the specified size appends the specified element into the vector. returns the element at the specified index creates a vector with specified size and default value removes all elements from the vector returns true if the vector is empty
size() - returns the size of the vector vector<typename>() - creates an empty vector vector<typename>(size) - creates a vector with the specified size push_back(e) - appends the specified element into the vector. at(index) - returns the element at the specified index vector<typename>(size,value) - creates a vector with specified size and default value clear() - removes all elements from the vector empty() - returns true if the vector is empty
In _______ binding, the necessary code to call a specific function is generated by the compiler dynamic shallow static deep
static
When there are several classes that have many common data attributes/fields, it is better to write a(n) _______ to hold all the general data. object method superclass subclass
superclass
An exception is thrown using a ________ statement and caught in a _________ block. throw, trial-catch trial, throw-catch throw, try-catch throw, trial-hold None of these
throw, try-catch
Which of the following statements creates a smart pointer for an array of 10 int values? unique<int[]> p(new int[10]); int* p = new int[10]; unique_ptr<int> p(new int[10]); unique_ptr<int[]> p(new int[10]);
unique_ptr<int[]> p(new int[10]);
Which of the following vector represents the list: int arr[4] = {1,2,3,4}; (Choose all that apply) vector<int> arr = {1,2,3,4}; vector<int> arr{1,2,3,4}; int vector arr{1,2,3,4}; int vector arr = {1,2,3,4};
vector<int> arr = {1,2,3,4}; vector<int> arr{1,2,3,4};
Which of the following is an abstract function? double getArea() = 0; double getArea(); virtual double getArea(); virtual double getArea() = 0;
virtual double getArea() = 0;
Given the minValue template function below, what would be the return value from invoking minValue(1.5, 2)? template<typename T1, typename T2> T1 minValue(T1 value1, T2 value2) { if (value1 < value2) return value1; else return value2; } 2 1 1.5 All answer choices are incorrect
1.5
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 81,12 12,12 81,81
12,81
Suppose vector v contains integers: 1, 2, 3, 4. What is in the output of the following code? cout << *max_element(v.begin() + 1, v.end() -1); 3 1 4 2
3
What is the output of the following statements? int x = 33; int *q; q = &x; cout << *q << endl; 33 NULL 3 0
33
What is the output of the following code? int *p; int x; x = 76; p = &x; *p = 43; cout << x << ", " << *p << endl; 76,76 76,43 43,76 43,43
43,43
What is the value of x after the following statements execute? int x = 25; int *p; p = &x; *p = 46; 0 NULL 46 25
46
Suppose vector v contains integers: 1,2,3,4. What is in the vector after the following statements? v.insert(v.begin(), 5); v.erase(v.begin() + 1); 5,2,3,4 1,5,1,2,3,4 5,1,3,4 5,1,2,3,4
5,2,3,4
Which of the following statements is true? An abstract class is like a regular class and you can create objects from it. You can declare a class abstract even though it does not contain abstract functions. A class is abstract if it contains a pure virtual function. An abstract class is declared using keyword function
A class is abstract if it contains a pure virtual function.
Which of the following statements is false? You cannot have a protected constructor. A public data field or function in a class can be accessed by name is any other program. A protected data field or a protected function in a base class can be accessed by name in its derived classes. Private members can only be accessed from inside of the class unless from a friend class or a friend function.
A public data field or function in a class can be accessed by name is any other program.
What is the output of the following code? class B { public: ~B() { cout << "B"; } }; class A: public B { public: ~A() { cout << "A"; } }; int main() { A a; return 0; } BB A B BA AA AB
AB
What is a function called when it is defined as a pure virtual function (being set to = 0)
Abstract function
Which of the following statements is correct for using the template class below? template<typename T, int quantity> class Stack { Stack(); .... private: T elements[quantity]; int number; } Stack<string, 50> s; Stack<double, 40> s; Stack<int, 50> s; Stack<char, 40> s; All answer choices are correct
All answer choices are correct
Which of the following statements is correct to invoke the printArray template function below? template<typename T> T printArray(T a[], int s) { for(int i=0; i < s; i++) cout << a[i] << " "; } int arr[] = {1, 2, 3, 4}; printArray(arr, 4); double arr[] = {1, 2.5, 3, 4}; printArray(arr, 4); char arr[] = {'A', 'B', 'C', 'D'}; printArray(arr, 4); int arr[] = {1, 2.5, 3, 4}; printArray(arr, 4); All answer choices are correct
All answer choices are correct
Which of the following statements is correct? a[0] = 4; v[0] = 3; vector<int> v = {1, 2}; All answer choices are correct int a[] = {1, 2};
All answer choices are correct
In OOP terminology, a derived or child class is called _________, and a base or parent class is called __________. Inheritance, polymorphism a top class, a bottom class a superclass, a subclass All answer choices are incorrect a low class, a high class
All answer choices are incorrect
Of the two classes, Cherry and Flavor, which would most likely be the subclass? neither; these are inappropriate class or subclass names either one Cherry Flavor
Cherry
Suppose you declared GeometricObject* p = &object. To cast p to Circle, use _________. Circle* p1 = dynamic_cast<Circle>(p); Circle* p1 = dynamic_cast<Circle*>(p); Circle p1 = dynamic_cast<Circle>(p); Circle p1 = dynamic_cast<Circle*>(p);
Circle* p1 = dynamic_cast<Circle*>(p);
A constructor can be redefined. True False
False
A custom exception class must always be derived from the class run_time error True False
False
Suppose the template prefix for the Stack class is: template<typename T = string>. You can create a stack of strings using: Stack stack; True False
False
The try block of a try/catch construct is used to display the definition of an exception parameter. True False
False
Which of the following about Inheritance is not true? Inheritance is the best way to design classes to avoid redundancy. Inheritance enables you to define a superclass and extend it to subclasses. Inheritance is a GUI library used for developing GUI programs. Inheritance extends the power of OOP by adding the feature for reusing software. Inheritance makes the system easy to comprehend and maintain.
Inheritance is a GUI library used for developing GUI programs.
In the following class definition, what is the name of the subclass? class Rose: public Flower { .... }; None of these Rose Flower Rose Rose Flower Flower
Rose
Which of the following statements is incorrect for using the template class below? template<typename T = int> class Stack { Stack(); .... } Stack s; Stack<int> s; Stack<double> s; Stack<> s;
Stack s;
Suppose that statement3 throws an exception of type Execution3 in the code below, which statements will be executed after statement3? try { statement1; statement2; statement3; } catch (Exception1& ex1) { } catch (Exception2& ex2){ } catch (Exception3& ex3){ statement4; throw; } statement5; Statement4 Statement3 Statement1 Statement2
Statement4
Which of the following statements about templates is incorrect? Templates provide the capability to parameterize types in functions and classes. Templates improve performance. Templates facilitate developing reusable software. With templates, you can define a function or a class with a generic type that can be substituted for a concrete type by the compiler.
Templates improve performance.
Which of the following statements is incorrect? class Fruit { public: Fruit (int id) { } }; class Apple: public Fruit { public: Apple() { } }; The program has a compile error because Fruit does not have a no-arg constructor The program will compile if you add a no-arg constructor for Fruit. The program will compile fine. The program will compile if you replace Apple() by Apple() : Fruit(4). The program will compile if you delete the constructor in Fruit.
The program will compile fine