1.
Declare an array to hold 10 integers.
Correct Answer
C. Int nums[10];
Explanation
The correct answer is "int nums[10];". This is because the question asks to declare an array to hold 10 integers. In C++, arrays are declared by specifying the data type followed by the name of the array and the size of the array in square brackets. In this case, "int nums[10];" declares an array named "nums" that can hold 10 integers.
2.
Everything you can do in C, you can do in C++.
Correct Answer
A. True
Explanation
C++ is an extension of the C programming language, which means that anything that can be done in C can also be done in C++. C++ includes all the features of C and adds additional features such as object-oriented programming, templates, and exception handling. Therefore, it is true that everything you can do in C, you can do in C++.
3.
The function "malloc" is used in C to dynamically allocate memory. What is the equivalent of "malloc" in C++?
Correct Answer
D. New
Explanation
In C++, the equivalent of "malloc" is the "new" keyword. "new" is used to dynamically allocate memory for objects in C++ and it also calls the constructor of the object being created. This is different from "malloc" in C, which only allocates memory without initializing it. Therefore, "new" is the correct answer as it performs the same functionality as "malloc" in C but with additional features specific to C++.
4.
Which of the following function prototypes declares a reference to the int "numPlayers"?
Correct Answer
B. Void startGame(int& numPlayers);
Explanation
The correct answer is "void startGame(int& numPlayers);" because it declares a reference to the int "numPlayers" by using the "&" symbol after the data type "int". This syntax indicates that the parameter "numPlayers" is passed by reference, allowing changes made to the parameter inside the function to affect the original variable outside the function.
5.
In C++ you must end a class declaration with what character?
Correct Answer
D. A semi-colon.
Explanation
In C++, a class declaration must be ended with a semi-colon. This is because the semi-colon is used to terminate statements in the C++ programming language. Ending the class declaration with a semi-colon indicates that the declaration has ended and separates it from the rest of the code.
6.
By default, class variables and methods in C++ are __________.
Correct Answer
C. Private
Explanation
In C++, class variables and methods are by default private. This means that they can only be accessed within the class itself and cannot be accessed from outside the class or from derived classes. Private members are used to encapsulate the internal implementation details of a class and provide data hiding, ensuring that they are not accidentally modified or accessed inappropriately.
7.
Bob and Alice are both C++ classes with a private member "secret" of type string. Pick the best option that allows Class Alice to access Class Bob's private member "secret".
Correct Answer
A. Have Class Bob "friend" Class Alice.
Explanation
Having Class Bob "friend" Class Alice allows Class Alice to access Class Bob's private member "secret". This means that Class Alice is granted access to the private members of Class Bob, including the "secret" member. As friends, the two classes can share and access each other's private members, enabling Class Alice to access Class Bob's "secret" member.
8.
Class Animal contains a virtual function "speak". Class Cat and Class Dog are derived from Class Animal. (e.g., They both inherit from Class Animal.) Which of the following best describes Class Animal?
Correct Answer
C. Class Animal is an abstract base class.
Explanation
Class Animal is an abstract base class because it contains a virtual function "speak" which is meant to be overridden by the derived classes, Class Cat and Class Dog. An abstract base class is a class that is designed to be inherited from and cannot be instantiated on its own. It provides a common interface and functionality that the derived classes can implement and customize according to their specific needs.
9.
The following is a code snippet from Class Dog:Dog::Dog(const Dog &D);What best describes this function prototype?
Correct Answer
B. Copy Constructor
Explanation
The given code snippet is a copy constructor. A copy constructor is a special constructor that creates a new object as a copy of an existing object. It takes an object of the same class as a parameter and creates a new object with the same values. In this case, the copy constructor for the class Dog is defined as Dog::Dog(const Dog &D), where "D" is the object to be copied.
10.
Line 1: Person q("Mickey");Line 2: Person r(p);Line 3: Person p = q;Line 4: p = q;For what lines above is a copy constructor invoked?
Correct Answer
C. 2,3
Explanation
A copy constructor is invoked when a new object is created as a copy of an existing object. In line 2, a copy constructor is invoked because the object "r" is being created as a copy of object "p". In line 3, a copy constructor is invoked because the object "p" is being created as a copy of object "q". Therefore, the correct answer is 2,3.
11.
When is a copy constructor implicitly called?
Correct Answer
C. Both a and b.
Explanation
A copy constructor is implicitly called when an object is passed by value to a function or when an object is returned by value from a function. In both cases, a copy of the object needs to be made, and the copy constructor is used for this purpose. Therefore, the correct answer is both a and b.
12.
Line 1: Person a;
Line 2: Person b = a;
Is line two an implicit or explicit call to the Person copy constructor?
Correct Answer
A. Implicit
Explanation
When an object is initialized with another object of the same class, the copy constructor is implicitly called. In the given example, Person b = a;, the copy constructor of the Person class is implicitly called to create a copy of the object a and initialize b with the values of a. This is a common scenario where the copy constructor is invoked automatically during object initialization.
13.
Int main(){Person a;Person b;Person c;return 0;}Which object's destructor is called first?
Correct Answer
C. Person c
Explanation
The object's destructor that is called first is Person c. In C++, when objects are created in the order of declaration, they are destroyed in the reverse order. Since Person c is declared last, its destructor will be called first.
14.
Destructors are only needed when you have pointers.
Correct Answer
B. False
Explanation
Destructors are special member functions in C++ that are used to clean up resources allocated by an object before it is destroyed. They are typically used to deallocate memory that was allocated using the new operator. Since pointers are commonly used to dynamically allocate memory, destructors are necessary to properly deallocate this memory and avoid memory leaks. Therefore, the statement "Destructors are only needed when you have pointers" is true.
15.
You can free a pointer in C++ using a "delete". Which keyword can you use to free an array?
Correct Answer
D. Delete[]
Explanation
In C++, when you allocate memory for an array using the "new" keyword, you need to use the "delete[]" keyword to free the memory. The "delete[]" keyword is specifically used for freeing dynamically allocated arrays, while the "delete" keyword is used for freeing dynamically allocated single objects. Therefore, the correct keyword to free an array in C++ is "delete[]".
16.
Review *r = newReview();How would you free the memory allocated above?
Correct Answer
B. Delete r;
Explanation
To free the memory allocated for the object "r", we use the "delete" keyword. In this case, "delete r;" is the correct choice because "r" was created using the "new" keyword, which requires "delete" to deallocate the memory. "free(r);" is not the correct choice because it is used to deallocate memory allocated with "malloc" or "calloc", not with "new". "delete[]" is used to deallocate memory allocated for arrays, so it is not applicable here. "~r();" is not the correct choice because it is the syntax for calling the destructor of an object, not for deallocating memory.
17.
Int*r= new int[5];How would you free the memory allocated above?
Correct Answer
C. Delete[] r;
Explanation
To free the memory allocated for the integer array "r", the correct option is "delete[] r;". This is because when memory is allocated using "new" for an array, it should be deallocated using "delete[]" to ensure that all elements of the array are properly released from memory. Using "delete" or "free()" would not correctly deallocate the memory for the array. The option "~r();" is incorrect because it suggests calling the destructor for the array, which is not the correct way to free memory.
18.
Destructors are implicity called on local variables when:
Correct Answer
D. Both a and c
Explanation
Destructors are implicitly called on local variables when exiting a function and also when exiting a statement such as an if or while loop. This means that when the execution of a function or a statement is finished, the destructor of any local variables within that function or statement will be automatically called.
19.
What is wrong with the following statement:Person a = new Person("noah");
Correct Answer
D. Person a is not a pointer.
Explanation
The statement "Person a is not a pointer" is the correct answer because it correctly identifies the issue with the given statement. The statement "Person a = new Person("noah");" is creating a new object of the Person class and assigning it to the variable "a". However, it is not declaring "a" as a pointer, which means it is not able to hold the memory address of another object.
20.
Create a prototype for a const function called "getName" that returns a string.
Correct Answer
B. String getName() const;
Explanation
The correct answer is "string getName() const;". In C++, the "const" keyword after a function declaration indicates that the function is a "const function", meaning it does not modify the object it is called on. In this case, the function "getName" returns a string and is declared as a const function, so it can be called on const objects of the class.
21.
Create a prototype for a function called "search" that takes a const string as a parameter and returns an int.
Correct Answer
C. Int search(const string);
Explanation
The correct answer is "int search(const string);" because it matches the prototype described in the question. The function is named "search", it takes a const string as a parameter, and it returns an int.
22.
Create a prototype for a const function called query that takes a const int as a parameter and returns an immutable Person object.
Correct Answer
D. Const Person (const int&) const;
Explanation
The correct answer is **D. const Person (const int&) const;**. This is a prototype for a const member function named `query` that takes a const reference to an int as a parameter and returns a const `Person` object. Here's how it would look in the context of a class definition:
```cpp
class SomeClass {
public:
const Person query(const int& param) const;
};
```
In this code, `query` is a member function of `SomeClass.` It takes a const reference to an int (`const int& param`) as a parameter and returns a const `Person` object (`const Person`). The `const` at the end of the function declaration means that `query` is a const member function, which means it cannot modify the object it is called on.
23.
Which of the following object-oriented features best describes the "is-a" class relationship?
Correct Answer
B. Inheritance
Explanation
Inheritance is the object-oriented feature that best describes the "is-a" class relationship. Inheritance allows a class to inherit the properties and behaviors of another class, creating a hierarchical relationship between classes. The "is-a" relationship signifies that one class is a specialized version of another class, representing a more specific type or subtype. This relationship allows for code reuse, as the subclass inherits the attributes and methods of the superclass, while also having the ability to add its own unique attributes and methods.
24.
__________ redefines a function (with exactly the same syntax) to do something else. Examples include overloading the copy constructor.
Correct Answer
C. Overloading
Explanation
Overloading is the correct answer because it allows redefining a function with the same syntax to perform different actions. This is commonly seen in cases like overloading the copy constructor, where multiple constructors with different parameters can be defined to create objects in different ways. Overloading enables the flexibility to use the same function name but with different arguments or types, providing different functionality based on the context.
25.
__________ implements the same function name but different parameters. The simplest example of this is having multiple constructors.
Correct Answer
C. Overloading
Explanation
Overloading in programming involves defining multiple functions with the same name but different parameters or different types of parameters. It allows you to use the same function name to perform different tasks based on the input arguments. In the context of the question, having multiple constructors with different parameters is an example of function overloading.