C++ Final Exam Questions And Answers

Reviewed by Godwin Iheuwa
Godwin Iheuwa, MS, Computer Science |
Computer Expert
Review Board Member
Godwin is a proficient Database Administrator currently employed at MTN Nigeria. He holds as MS in Computer Science from the University of Bedfordshire, where he specialized in Agile Methodologies and Database Administration. He also earned a Bachelor's degree in Computer Science from the University of Port Harcourt. With expertise in SQL Server Integration Services (SSIS) and SQL Server Management Studio, Godwin's knowledge and experience enhance the authority of our quizzes, ensuring accuracy and relevance in the realm of computer science.
, MS, Computer Science
Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Noah.belcher
N
Noah.belcher
Community Contributor
Quizzes Created: 1 | Total Attempts: 5,910
Questions: 25 | Attempts: 5,950

SettingsSettingsSettings
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 revise 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!


Questions and Answers
  • 1. 

    Declare an array to hold 10 integers. 

    • A.

      Int nums[9];

    • B.

      Int[9] nums;

    • C.

      Int nums[10];

    • D.

      Int[10] nums;

    • E.

      Int billy = new int[10];

    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.

    Rate this question:

  • 2. 

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

    • A.

      True

    • B.

      False

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

    Rate this question:

  • 3. 

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

    • A.

      Cpp_malloc

    • B.

      Create_new

    • C.

      Mem_create

    • D.

      New

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

    Rate this question:

  • 4. 

    Which of the following function prototypes declares a reference to the int "numPlayers"?

    • A.

      Void startGame(int numPlayers&);

    • B.

      Void startGame(int& numPlayers);

    • C.

      Void startGame(int* numPlayers);

    • D.

      Void startGame(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.

    Rate this question:

  • 5. 

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

    • A.

      A right curly bracket.

    • B.

      A right brace.

    • C.

      Double colons.

    • D.

      A semi-colon.

    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.

    Rate this question:

  • 6. 

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

    • A.

      Protected

    • B.

      Public

    • C.

      Private

    • D.

      Overloaded

    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.

    Rate this question:

  • 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". 

    • A.

      Have Class Bob "friend" Class Alice.

    • B.

      Have Class Alice "friend" Class Bob.

    • C.

      Make "secret" public.

    • D.

      Create a public member functions for Alice and Bob called getSecret().

    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.

    Rate this question:

  • 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?

    • A.

      Class Animal is a virtual block class.

    • B.

      Class Animal is a friend of Class Cat and Class Dog.

    • C.

      Class Animal is an abstract base class.

    • D.

      Class Animal is an interface.

    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.

    Rate this question:

  • 9. 

    The following is a code snippet from Class Dog:Dog::Dog(const Dog &D);What best describes this function prototype?

    • A.

      Class Constructor

    • B.

      Copy Constructor

    • C.

      Virtual Constructor

    • D.

      Overloaded Abstract Virtual Constructor

    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.

    Rate this question:

  • 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?

    • A.

      2

    • B.

      3

    • C.

      2,3

    • D.

      2,3,4

    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.

    Rate this question:

  • 11. 

    When is a copy constructor implicitly called?

    • A.

      Passing an object by value to a function.

    • B.

      Returning an object by value from a function.

    • C.

      Both a and b.

    • D.

      Passing an object by reference to a constructor.

    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.

    Rate this question:

  • 12. 

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

    • A.

      Implicit

    • B.

      Explicit

    • C.

      Both

    • D.

      None

    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.

    Rate this question:

  • 13. 

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

    • A.

      Person a

    • B.

      Person b

    • C.

      Person c

    • D.

      None of the above

    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.

    Rate this question:

  • 14. 

    Destructors are only needed when you have pointers. 

    • A.

      True

    • B.

      False

    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.

    Rate this question:

  • 15. 

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

    • A.

      Free

    • B.

      Free[]

    • C.

      Delete

    • D.

      Delete[]

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

    Rate this question:

  • 16. 

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

    • A.

      Free(r);

    • B.

      Delete r;

    • C.

      Delete[] r;

    • D.

      ~r();

    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.

    Rate this question:

  • 17. 

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

    • A.

      Free(r);

    • B.

      Delete r;

    • C.

      Delete[] r;

    • D.

      ~r();

    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.

    Rate this question:

  • 18. 

    Destructors are implicity called on local variables when:

    • A.

      Exiting a function

    • B.

      Restarting the operating system

    • C.

      Exiting a statement (if, while, etc.)

    • D.

      Both a and c

    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.

    Rate this question:

  • 19. 

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

    • A.

      There is nothing wrong.

    • B.

      You cannot instantiate an object on the same line as it is declared.

    • C.

      "noah" is a const string that cannot be reassigned inside the Person class.

    • D.

      Person a is not a pointer.

    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.

    Rate this question:

  • 20. 

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

    • A.

      Const string getName();

    • B.

      String getName() const;

    • C.

      String const getName();

    • D.

      Const std__str getName();

    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.

    Rate this question:

  • 21. 

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

    • A.

      Void search(const string);

    • B.

      Int search(const string) const;

    • C.

      Int search(const string);

    • D.

      Int const search(string);

    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.

    Rate this question:

  • 22. 

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

    • A.

      Const Person (const int) const;

    • B.

      Person const (const int) const;

    • C.

      Const Person const (const int) const;

    • D.

      Const Person (const int&) const;

    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.

    Rate this question:

  • 23. 

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

    • A.

      Composition

    • B.

      Inheritance

    • C.

      Aggregation

    • D.

      Polymorphism

    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.

    Rate this question:

  • 24. 

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

    • A.

      Abstraction

    • B.

      Overriding

    • C.

      Overloading

    • D.

      Extraction 

    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.

    Rate this question:

  • 25. 

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

    • A.

      Abstraction

    • B.

      Overriding

    • C.

      Overloading

    • D.

      None of the above 

    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.

    Rate this question:

Godwin Iheuwa |MS, Computer Science |
Computer Expert
Godwin is a proficient Database Administrator currently employed at MTN Nigeria. He holds as MS in Computer Science from the University of Bedfordshire, where he specialized in Agile Methodologies and Database Administration. He also earned a Bachelor's degree in Computer Science from the University of Port Harcourt. With expertise in SQL Server Integration Services (SSIS) and SQL Server Management Studio, Godwin's knowledge and experience enhance the authority of our quizzes, ensuring accuracy and relevance in the realm of computer science.

Quiz Review Timeline +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.