C++ Final Exam Questions And Answers

Reviewed by Godwin Iheuwa
Godwin Iheuwa, MS (Computer Science) |
Database Administrator
Review Board Member
Godwin Iheuwa, a Database Administrator at MTN Nigeria, holds an MS in Computer Science, specializing in Agile Methodologies and Database Administration from the University of Bedfordshire and a Bachelor's in Computer Science from the University of Port Harcourt. His proficiency in SQL Server Integration Services (SSIS) and SQL Server Management Studio contributes to his expertise in database management.
, MS (Computer Science)
By Noah.belcher
N
Noah.belcher
Community Contributor
Quizzes Created: 1 | Total Attempts: 6,932
| Attempts: 6,932 | Questions: 25
Please wait...
Question 1 / 25
0 %
0/100
Score 0/100
1. Declare an array to hold 10 integers. 

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.

Submit
Please wait...
About This Quiz
C++ Final Exam Questions And Answers - Quiz

C++ is a popular programming language used to create computer programs. Have you studied this subject? Check out these C++ final exam questions and answers and get to... see morerevise your concepts. This interactive quiz consists of well-researched questions and answers about the C++ concepts. So, give it a try and see how much score you can make. So, are you ready to take this quiz? Let's start then. All the best! see less

2. In C++ you must end a class declaration with what character?

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.

Submit
3. Everything you can do in C, you can do in C++.

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++.

Submit
4. Destructors are implicity called on local variables when:

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.

Submit
5. Which of the following object-oriented features best describes the "is-a" class relationship?

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.

Submit
6. Which of the following function prototypes declares a reference to the 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.

Submit
7. You can free a pointer in C++ using a "delete". Which keyword can you use to free an array?

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[]".

Submit
8. The function "malloc" is used in C to dynamically allocate memory. What is the equivalent of "malloc" in C++?

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++.

Submit
9. Int*r= new int[5];
How would you free the memory allocated above?

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.

Submit
10. By default, class variables and methods in C++ are __________.

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.

Submit
11. 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?

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.

Submit
12. Destructors are only needed when you have pointers. 

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.

Submit
13. Create a prototype for a function called "search" that takes a const string as a parameter and returns an int.

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.

Submit
14. The following is a code snippet from Class Dog:
Dog::Dog(const Dog &D);

What best describes this function prototype?

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.

Submit
15. When is a copy constructor implicitly called?

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.

Submit
16. __________ redefines a function (with exactly the same syntax) to do something else. Examples include overloading the copy constructor.

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.

Submit
17. Review *r = newReview();
How would you free the memory allocated above?

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.

Submit
18. Line 1: Person a; Line 2: Person b = a;   Is line two an implicit or explicit call to the Person copy constructor?

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.

Submit
19. What is wrong with the following statement:
Person a = new Person("noah");

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.

Submit
20. __________ implements the same function name but different parameters. The simplest example of this is having multiple constructors. 

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.

Submit
21. Create a prototype for a const function called "getName" that returns a string.

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.

Submit
22. 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?

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.

Submit
23. Int main(){Person a;Person b;Person c;return 0;}
Which object's destructor is called first?

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.

Submit
24. 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". 

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.

Submit
25. Create a prototype for a const function called query that takes a const int as a parameter and returns an immutable Person object. 

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.
Submit
View My Results
Godwin Iheuwa |MS (Computer Science) |
Database Administrator
Godwin Iheuwa, a Database Administrator at MTN Nigeria, holds an MS in Computer Science, specializing in Agile Methodologies and Database Administration from the University of Bedfordshire and a Bachelor's in Computer Science from the University of Port Harcourt. His proficiency in SQL Server Integration Services (SSIS) and SQL Server Management Studio contributes to his expertise in database management.

Quiz Review Timeline (Updated): Jan 16, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jan 16, 2024
    Quiz Edited by
    ProProfs Editorial Team

    Expert Reviewed by
    Godwin Iheuwa
  • May 10, 2010
    Quiz Created by
    Noah.belcher
Cancel
  • All
    All (25)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Declare an array to hold 10 integers. 
In C++ you must end a class declaration with what character?
Everything you can do in C, you can do in C++.
Destructors are implicity called on local variables when:
Which of the following object-oriented features best describes the...
Which of the following function prototypes declares a reference to the...
You can free a pointer in C++ using a "delete". Which keyword can you...
The function "malloc" is used in C to dynamically allocate memory....
Int*r= new int[5];How would you free the memory allocated above?
By default, class variables and methods in C++ are __________.
Class Animal contains a virtual function "speak". Class Cat and Class...
Destructors are only needed when you have pointers. 
Create a prototype for a function called "search" that takes a const...
The following is a code snippet from Class Dog:Dog::Dog(const Dog...
When is a copy constructor implicitly called?
__________ redefines a function (with exactly the same syntax) to do...
Review *r = newReview();How would you free the memory allocated above?
Line 1: Person a; ...
What is wrong with the following statement:Person a = new...
__________ implements the same function name but different...
Create a prototype for a const function called "getName" that returns...
Line 1: Person q("Mickey");Line 2: Person r(p);Line 3: Person p =...
Int main(){Person a;Person b;Person c;return 0;}Which object's...
Bob and Alice are both C++ classes with a private member "secret" of...
Create a prototype for a const function called query that takes a...
Alert!

Advertisement