CPSC 1020 Final Clemson
True
(T/F) A class member labeled protected is accessible to member functions of derived classes as well as to member functions of the same class
True
(T/F) Access specifiers will determine availability of members
True
(T/F) C++ uses the declared type of a pointer to determine access to the members of the pointed-to object
True
(T/F) Declaring a function virtual will make the compiler check the type of each object to see if it defines a more specific version of the virtual function
True
(T/F) In C++, polymorphism is implemented through virtual functions using keyword virtual
True
(T/F) Inheritance models the 'is-a' relationship between classes
True
(T/F) Overloaded functions have the same name but different parameter lists.
False
(T/F) Private base class members are accessible in derived classes.
True
(T/F) Protected base class members are accessible in derived classes.
True
(T/F) Public base class members are accessible in derived classes.
False
(T/F) The existing class is called the derived class
True
(T/F) The new class is called the derived class
True
(T/F) When a function is overridden, all objects of a derived class use the overriding function.
an accessor
A C++ member function that uses, but does not change, the value of a member variable is called A) an accessor. B) a constant. C) a mutator. D) a user. E) a constructor.
is used to ensure that memory that is no longer in use is automatically deleted.
A C++ smart pointer: A) can only be used to point to smart objects. B) is useful when writing software for intelligent systems. C) is used to ensure that memory that is no longer in use is automatically deleted. D) none of these. E) automatically allocates memory whenever memory is needed by the program.
none of these.
A constructor may have a return type of A) void B) bool C) int D) any of these. E) none of these.
default
A constructor that does not require that any arguments be passed to it is called a(n) ________ constructor. A) zero-element B) useless C) empty D) stand-alone E) default
to a block of memory that has been deleted.
A dangling pointer is a pointer: A) that has not been initialized. B) that has been declared but has not yet been assigned a value. C) that has been assigned the value of null or null_ptr. D) to a block of memory that has been deleted. E) none of these
is automatically called when an object is destroyed.
A destructor is a member function that: A) none of these. B) is automatically called when an object is destroyed. C) can only be called by the main function of a program. D) is used to remove old unneeded objects. E) causes the program to end
whenever it is called
A function other than the main function is executed: A) when it is first defined. B) when its function prototype is encountered. C) only once. D) whenever it is called. E) when the main function finishes executing.
a vector can be resized
A key difference between arrays and vectors in C++ is that: A) a vector can be returned from a function B) a vector can be created dynamically C) a vector can hold different data types D) a vector can be passed to a function E) a vector can be resized F) a vector can store objects
static
A member function that is declared ________ cannot use the this pointer. A) inline B) public C) none of these D) private E) static
static
A member function that is declared ________ cannot use the this pointer. A) none of these B) static C) private D) inline E) public
Exception
An _______ is a value or an object that indicates that a runtime error has occurred
one that has at least one pure virtual function.
An abstract class is: A) none of these B) one that supports polymorphism. C) one that has all of its member variables declared public. D) one that has at least one pure virtual function. E) one that has all of its member variables declared private.
a memory location that is associated with a name that can be used by different parts of the program to access the memory location.
An lvalue is: A) a memory location that is associated with a name that can be used by different parts of the program to access the memory location. B) none of these C) An expression denoting a value that appears as the left operand of a binary operator. D) a value of type long. E) a value of type long returned from a function.
instance
An object is a(n) ________ of a class. A) member B) attribute C) example D) copy E) instance
public member functions
An object typically hides its data, but allows outside code to access it through its: A) none of these B) private member functions. C) public member functions. D) access specifiers. E) public data members.
a temporary value that cannot be accessed from a different part of the program.
An rvalue is A) a memory location that is associated with a name that can be used by different parts of the program to access the memory location. B) a value passed as the rightmost parameter of a function. C) the right operand of a binary operator. D) none of these E) a temporary value that cannot be accessed from a different part of the program.
nullptr
Beginning with C++ 11, the recommended value for a pointer whose value is invalid is: A) invalid_ptr B) none of these C) nullptr D) 0 E) NULL
(*this).val = val;
Consider the following class: class SomeClass { private: int val; public: void setVal(int val) { [ MISSING CODE ] } }; Which of the following code completes the class definition? A) (*this).val = val; B) *(this.val) = val; C) (*this.val) = val; D) val = val; E) this.val = val; F) val = r.val; G) val = &val;
Season favoriteSeason = Season::Spring;
Consider the following code that creates an enumerated class named 'Season': enum class Season {Spring, Summer, Fall, Winter}; Which of the following code correctly creates a variable of type Season? A) Season favoriteSeason = "Spring"; B) Season favoriteSeason = Season::Spring; C) Season favoriteSeason = Season.Spring; D) Season favoriteSeason = Season(Spring); E) Season favoriteSeason = Spring;
virtual void move() = 0;
Consider the following declaration of a class Robot. class Robot { private: std::string name; int id; public: Robot() = default;[MISSING MOVE FUNCTION]}; Add a pure virtual function 'void move()' (type in the function below) .
A and D
Consider the following function which passes an object of type SomeClass as a constant reference int fun(const SomeClass& c) Which of the following statements is correct. (Select all that apply.) A) the function accesses the original object, rather than a copy of it B) the function can only access member functions of the object declared as const C) the function can only access member functions of the object declared as static D) the function cannot make any changes to the member variables of the object E) the function can only access member variables of the object declared as const F) the function makes a copy of the object, rather than accessing the original object
A base class pointer: <Animal *>
Consider the following program. #include <iostream> #include <vector> using namespace std; class Animal { public: virtual void makeSound() { cout << "Animal!" << endl; } }; class Dog : public Animal { public: void makeSound() { cout << "Woof" << endl; } }; int main() { Dog dog; vector<MISSING TYPE> animal {&dog}; animal.at(0)->makeSound(); return 0; } What type must vector 'animal' be declared so that the program uses polymorphism and prints "Woof"? A) A derived class pointer: <Dog *> B) A base class pointer: <Animal *> C) A derived class: <Dog> D) A base class reference: <&Animal> E) A derived class reference: <&Dog> F) A base class: <Animal >
10 40 23 42
Consider the following program: #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1 {4, 8, 15, 16, 23, 42}; vector<int> v2 {10, 40}; for (int num : v1) if ( num > 20){ v2.push_back(num); } for (int num : v2) { cout << num << " "; } return 0; } What does cout print to the terminal?
The copy constructor
Consider the following two lines of code that create three instances of class "Box" Box box1{40,20}, box2{60,30}; Box box3 = box1; What internal function is called in line 2 when box3 is created? A) The move constructor B) The assignment operator C) The copy constructor D) The destructor
virtual function
Declaring a member function of a class to be a ________ will cause the C++ compiler to use dynamic binding. A) none of these B) static function C) constructor function D) virtual function E) destructor function
when memory is allocated at run-time.
Dynamic memory allocation occurs: A) when memory is allocated at compile time. B) when a pointer is copied from one object to another. C) when memory is allocated at run-time. D) none of these E) when a pointer is used to access a memory location.
instance member variables
Each object of a class has its own copy of the class's ________ A) none of these B) instance member variables. C) all of these D) static member variables. E) static member functions.
non-static member variables.
Each object of a class has its own copy of the class's ________: A) static member functions. B) static member variables. C) non-static member variables. D) all of these E) none of these F) non-static member functions.
Circle myCircle;
If Circle is the name of a class, which of the following statements would create a Circle object named myCircle? A) Circle myCircle(); B) Circle myCircle; C) myCircle Circle(); D) none of these E) myCircle Circle;
Circle myCircle;
If Circle is the name of a class, which of the following statements would create an instance of Circle named myCircle? A) none of these B) Circle myCircle; C) Circle myCircle(); D) myCircle Circle; E) myCircle Circle();
static
If a member variable is declared ________, all objects of that class share access to that variable. A) inline B) const C) dynamic D) static E) none of these F) default
employee[5].setHoursWorked(40);
If employee is an array of objects with a public member function named setHoursWorked, which of the following statements correctly calls that function for the employee object in array element 5? A) employee[5].setHoursWorked = 40; B) employee.setHoursWorked[5](40); C) setHoursWorked(employee[5], 40); D) employee.setHoursWorked[5] = 40; E) employee[5].setHoursWorked(40);
myCircle.setRadius(2.5);
If setRadius is a Circle class function and myCircle is a Circle object, which of the following statements would set myCircle's radius to 2.5? A) none of these B) Circle.setRadius(2.5); C) Circle(setRadius(2.5)); D) setRadius(2.5); E) Circle::setRadius(2.5); F) myCircle::setRadius(2.5); G) myCircle.setRadius(2.5);
box.setSide(5)
If setSide is a Square class function and box is a Square object, which of the following statements would set the length of box's side to 5? A) box.setSide(5); B) Square.setSide = 5; C) Square.setSide(5); D) setSide(5); E) none of these
9
If the scores array is defined like this: int scores[ ]= {4, 7, 4, 8, 9}; what will the following statement display? cout << scores[4]; A) 9 B) the first four scores C) 4 D) 8 E) 7
All of the above
In a function header, in addition to the name of the function, you are required to furnish (select all that apply): A) the data type of the return value. B) a data type for each parameter. C) an identifier name for each parameter. D) All of the above
A and B
In a function prototype, in addition to the name of the function, you are required to furnish (select all that apply) A) the data type of the return value. B) a data type for each parameter. C) an identifier name for each parameter. D) A and B E) B and C F) All of the above
the iostream header file.
In any program that uses the cin object, you must #include: A) none of these. B) named constants. C) the iostream header file. D) the fstream header file. E) >> and << operators.
Base class members
In the statement class Car : protected Vehicle, what is being protected? A) none of these B) Derived class data C) Future inherited classes D) Base class members E) Derived class functions
Structs members are public by default
In what way are structs different from classes in C++? A) none of these B) all of these C) Structs members are public by default D) Structs cannot have private members E) Structs cannot have a constructor
has-a
Object composition is useful for creating a ________ relationship between classes. A) friend B) has-a C) conditional D) static E) none of these
an array value.
On each iteration of the following range-based for loop for (int element : myArray) cout << element << endl; the variable element holds: A) an array location. B) an array value. C) an array name. D) an array subscript. E) none of these.
pointers or references are being used.
Polymorphism in C++ will not work unless: A) the header file is included. B) pointers or references are being used. C) the members of the class are public. D) all of these E) none of these
double calcArea(double r) { area = 3.14159 * pow(r, 2); return area; } double calcArea(double w, double h) { area = w * h; return area; }
Programming Question: Area Calculation Below is an incomplete program that uses an overloaded function calcArea() to return the areas of a circle and a rectangle. Write the missing code to define calcArea() that will complete this program. Type your code into the below text editor. Please set the editor to "Preformatted" in the drop-down menu next to the font size. This formatting makes it easier to read and write code in the Canvas editor. #include <iostream> #include <cmath> using namespace std; // Function definitions[ -- YOUR CODE HERE -- ] // Main program int main() { double radius = 2.0, // radius of circle width = 1.5, // width of rectangle height = 3.0; // height of rectangle cout << "Area of circle: "; cout << calcArea(radius) << endl; cout << "Area of rectangle: "; cout << calcArea(width, height) << endl; return 0; } // Output from above program: // Area of circle: 12.56 // Area of rectangle: 4.5
private members
Protected members of a base class are like ________, with the exception that they may be accessed by derived classes. A) constructor functions B) static members C) private members D) public members E) none of these
dot operator
Public members of a class object can be accessed from outside the class by using the A) extraction operator. B) member access operator. C) class name. D) dot operator. E) get function.
composition
Suppose a class A contains a member that is an object of class B, and suppose B is created and destroyed at the same time that A is created and destroyed. What term describes this particular form of class relationship? A) has-a relationship B) nested C) dependence D) composition E) aggregation
composition
Suppose a class A contains a member that is an object of class B, and suppose B is created and destroyed at the same time that A is created and destroyed. What term describes this particular form of relationship between objects? A) dependence B) composition C) nested D) "is-a" relationship E) aggregation
inaccessible
Suppose a class A has a private member variable a1 and a protected member variable a2. Suppose class B is publicly inherited from A, and class C is publicly inherited from B. What will be the access specifiers of a1 in class C? A) a1 will not be inherited B) private C) inaccessible D) protected E) public
protected
Suppose a class A has a private member variable a1 and a protected member variable a2. Suppose class B is publicly inherited from A, and class C is publicly inherited from B. What will be the access specifiers of a2 in class C? A) a2 will not be inherited B) private C) inaccessible D) protected E) public
the program will suffer from memory leaks.
Suppose that a function dynamically allocates a block of memory with a local pointer variable p pointing to the allocated block. Suppose further that there are no other pointers referencing that block of memory, and the function returns without doing a delete on p. Then : A) the compiler will automatically deallocate the memory pointed to by p. B) the returning function will throw the bad_alloc exception. C) none of these D) the program will suffer from memory leaks. E) the pointer p becomes a dangling pointer.
B can access the private members of A, but A cannot access the private members of B
Suppose there are two classes, A and B. Inside A's declaration, B is listed as a friend. Which of the following statements is correct? A) A and B share their private members; changing a private variable in an instance of class A will also change that variable in an instance of class B B) A can access the private members of B, but B cannot access the private members of A C) A can access the private members of B, and B can access the private members of A D) B can access the private members of A, but A cannot access the private members of B
The function in the derived class overrides the function in the base class
Suppose you have a function 'int fun(int x)' in a base class. What happens if you implement the same function 'int fun(int x)' in a derived class? A) The program will not compile B) The function in the derived class overloads the function in the base class C) The program will cause a runtime error when the function is called D) An object of the derived class cannot be instantiated E) The function in the derived class overrides the function in the base class
base, derived
The ________ class constructor is called before the ________ class constructor. A) none of these B) private, public C) public, private D) base, derived E) derived, base
any of these.
The elements of an array can be: A) objects. B) none of these. C) structures. D) any of these. E) strings.
2
The expression 5 / 2 evaluates to: A) 1 B) 10 C) 5.2 D) 2.5 E) 2
All three variables 'radius', 'pi' and 'area'
The following class "Square" has a friend function "print". Which member variables will "print" have access to? class Circle { private: double radius = 2.0; double const pi = 3.14; public: double area; friend void print(const Circle&); ... }; A) Only the non-const variables 'radius' and 'area" B) Only the non-const private variable 'radius' C) Only the public variable 'area' D) None of the variables E) Only the private variables 'radius' and 'pi' F) Only the const variable 'pi' G) All three variables 'radius', 'pi' and 'area'
operator +
To overload the + operator, you would write a function called A) operator.overload(+). B) overload +. C) function +. D) none of these E) operator +.
cmath
To use the pow() function, or other mathematical library functions, you must #include the ________ header file in your program. A) mathlib B) iostream C) algebra D) cmath E) iomanip
True
True/False: A function can have zero to many parameters and either zero or one return value(s).
True
True/False: A private member variable may only be accessed from a function that is a member of the same class.
True
True/False: In C++ and other object-oriented programming languages, Abstract Data Types (ADTs) are normally implemented as classe
True
True/False: Object-oriented programming is centered around objects that include both data and the functions that operate on them.
True
True/False: When a function just needs to use a copy of an argument passed to it, the argument should normally be passed by value.
$(CXX) $(OPTIONS) program.cpp funct.cpp -o $(TARGET)
Type in the command that completes the following Makefile: CXX = g++ FLAGS = -Wall TARGET = program.out all: $(TARGET)$(TARGET): program.cpp funct.cpp [--- Your Command Here ---] clean: rm -f $(TARGET)
1
What does the following program print? #include <iostream> #include <vector> using namespace std; class Square { private: int side {1}; public: Square() = default; Square(int s) : side{s} {} int calcArea() { return side * side; } }; int main() { vector<Square> squares{2}; cout << squares.size(); return 0; }
5 5
What does the following program print? #include <iostream> using namespace std; class SomeClass { private: int x {0}; public: SomeClass() = default; SomeClass(int val) : x(val) {} int fun(SomeClass &r) { if (this != &r) { return r.x; } else { return this->x; } } }; int main() { SomeClass c1{5}, c2{10}; cout << c1.fun(c1) << " " << c2.fun(c1) << endl; return 0; }
10 10
What does the following program print? #include <iostream> using namespace std; int main() { int *num1, *num2, x{5}; num1 = &x; x = 10; num2 = &x; cout << *num1 << " " << *num2; return 0; }
3.14 (the area of the circle)
What value does cout in the following program print? // Calculate area of a square and a circle #include <iostream> #include <cmath> using namespace std; double calcArea(double side, double radius) { // return circle area return 3.1416 * pow(radius, 2); // return square area return side * side; } int main() { double squareSide = 2, circleRadius = 1, area = 0; area = calcArea(squareSide, circleRadius); cout << area << endl; return 0; } What value does cout in the program print? A) NULL B) 3.14 (the area of the circle) C) First 3.14 (the area of the circle), then 4 (the area of the square) D) First 4 (the area of the square), then 3.14 (the area of the circle) E) The program will not compile F) 4 (the area of the square)
7
What value will be assigned to the variable number by the following statement? int number = 7.8; A) 8 B) It's unpredictable. That's the problem. C) 7.8 D) 7 E) none of these.
20
What value will the following program print? #include <iostream> using namespace std; class Match { private: static int totalPoints; int matchPoints {0}; public: Match() = default; Match(int p) : matchPoints(p){ totalPoints += p; } int getPoints() { return matchPoints + totalPoints; } }; int Match::totalPoints {0}; int main() { Match game1 {5}, game2 {10}; cout << game1.getPoints() << endl; return 0; } A) 0 B) 15 C) 25 D) 5 E) 10 F) 20
25
What value will the following program print? #include <iostream> using namespace std; class Match { private: static int totalPoints; int matchPoints {0}; public: Match() = default; Match(int p) : matchPoints(p){ totalPoints += p; } int getPoints() { return matchPoints + totalPoints; } }; int Match::totalPoints {0}; int main() { Match game1 {10}, game2 {5}; cout << game1.getPoints() << endl; return 0; } A) 25 B) 20 C) 5 D) 0 E) 10 F) 15
Assign 1 to answer.
What will the following statement do if x equals 17 and answer = 20? answer = x > 100 ? 0 : 1; A) Assign 1 to x. B) Assign 0 to answer. C) Assign 17 to answer. D) Assign 1 to answer. E) Assign 0 to x.
a copy constructor.
When a class contains a pointer to dynamically allocated memory, it is a good idea to equip the class with A) a dynamically allocated constructor. B) a static constructor and an overloaded comparison operator. C) an inline constructor. D) a copy constructor. E) none of these
the scope resolution operator (::).
When a member function is defined outside of the class declaration, the function name must be qualified with the class name, followed by A) the public access specifier. B) the scope resolution operator (::). C) a tilde (~). D) the private access specifier. E) a semicolon(;).
overloaded
When more than one function has the same name they are called ________ functions. A) parallel B) renamed C) overloaded D) member E) identical
static
When the compiler binds a call to a member function using only information available at compile time, the compiler is said to use ________ binding. A) none of these B) safe C) dynamic D) local E) static
reference
When used as a parameter, a ________ variable allows a function to access and modify the original argument passed to it. A) value B) static C) floating-point D) reference E) default value
can add both new variables and new functions.
When you derive a class from an existing class, you: A) none of these B) can add both new variables and new functions. C) can add new variables, but cannot add new functions. D) can add new functions, but cannot add new variables. E) must add both new variables and new functions.
delete [ ] p;
Which of the following statements correctly deletes a dynamically-allocated array pointed to by p? A) delete array p; B) delete p; C) none of these D) p delete[ ]; E) delete [ ] p;
Static Binding
Which type of binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually pointed to, and is performed at compile time. A) Static Binding B) Dynamic Binding C) Function Binding
Dynamic Binding
Which type of binding invokes the function to be invoked and is determined at execution time, and Can look at the actual class of the object pointed to and choose the most specific version of the function? A) Static Binding B) Dynamic Binding C) Function Binding
Dynamic Binding
Which type of binding is used to bind virtual function? A) Static Binding B) Dynamic Binding C) Function Binding
try
followed by a block { }, is used to invoke code that may throw an exception. A) try B) throw C) catch
throw
followed by an argument, is used to signal an exception. A) try B) throw C) catch