C++ Certified Professional Programmer Practice Test

C++ Certified Professional Programmer (CPP) certification is an esteemed recognition in the field of computer science, underlining the coder’s proficiency in leveraging the power and flexibility of C++. For those aspiring to earn this certification, we’ve curated a set of 25 practice questions with answers and explanations to help hone your skills and gauge your readiness.

1. What is the output of the following C++ code?

#include <iostream>
int main() {
    std::cout << 10 + 20 / 2 << std::endl;
    return 0;
}
A) 25
B) 15
C) 20
D) 10

Answer:

A) 25

Explanation:

The division has higher precedence than addition. So, 20/2 is calculated first, which equals 10, and then added to 10.

2. Which of the following statements is true about pointers and references in C++?

A) References can be NULL while pointers cannot.
B) Pointers can be NULL while references cannot.
C) Both pointers and references can be NULL.
D) Neither pointers nor references can be NULL.

Answer:

B) Pointers can be NULL while references cannot.

Explanation:

References in C++ must always be initialized to refer to a valid object and can’t be NULL, whereas pointers can have a NULL value.

3. What is the purpose of a destructor in C++?

A) To destroy the object.
B) To allocate memory for the object.
C) To initialize the object's attributes.
D) To create a copy of the object.

Answer:

A) To destroy the object.

Explanation:

A destructor in C++ is used for cleaning up when an object goes out of scope or is explicitly deleted.

4. What is the default access modifier for class members in C++?

A) Public
B) Private
C) Protected
D) None

Answer:

B) Private

Explanation:

The default access modifier for class members in C++ is private.

5. Which of the following C++ headers must be included to use the std::vector container?

A) #include <vector>
B) #include <array>
C) #include <list>
D) #include <container>

Answer:

A) #include <vector>

Explanation:

The std::vector is part of the <vector> header.

6. How can we dynamically allocate memory for an integer in C++?

A) int *ptr = new int;
B) int *ptr = malloc(sizeof(int));
C) int *ptr = alloc int;
D) int *ptr = int();

Answer:

A) int ptr = new int;

Explanation:

In C++, new is used for dynamic memory allocation.

7. What will happen if an exception is thrown but not caught in a C++ program?

A) The program continues execution.
B) The program terminates.
C) The exception is ignored.
D) The program asks for user input.

Answer:

B) The program terminates.

Explanation:

If an exception is thrown but not caught, the C++ program terminates.

8. Which of the following statements is true regarding function overloading in C++?

A) Functions can be overloaded based on return type.
B) Functions can’t be overloaded based on parameter type.
C) Functions can be overloaded in different scopes.
D) Only member functions can be overloaded.

Answer:

C) Functions can be overloaded in different scopes.

Explanation:

Functions can be overloaded in different scopes, and the overloads can have different parameter types or numbers of parameters.

9. How can a constant object of a class be created in C++?

A) const Object obj;
B) Object const obj;
C) Object obj const;
D) Both A and B

Answer:

D) Both A and B

Explanation:

Both const Object obj; and Object const obj; are valid ways to declare a constant object in C++.

10. What is the output of the following code snippet?

#include <iostream>
int main() {
    int a = 10;
    int b = a++;
    std::cout << a << " " << b << std::endl;
    return 0;
}
A) 10 10
B) 11 10
C) 10 11
D) 11 11

Answer:

B) 11 10

Explanation:

The postfix increment operator a++ returns the value of a before incrementing it.

11. Which of the following is a correct syntax for template specialization in C++?

A) template <> class MyTemplate<int> {…};
B) template <class T> class MyTemplate<int> {…};
C) class MyTemplate<int> {…};
D) template <int> class MyTemplate {…};

Answer:

A) template <> class MyTemplate<int> {…};

Explanation:

Template specialization in C++ is defined with template <>.

12. In C++, what does the virtual keyword denote in a class method?

A) The method cannot be overridden.
B) The method must be implemented in the derived class.
C) The method can be overridden in the derived class.
D) The method is abstract.

Answer:

C) The method can be overridden in the derived class.

Explanation:

The virtual keyword in a class method denotes that the method can be overridden in a derived class.

13. Which of the following containers in the C++ Standard Template Library (STL) provides constant time insertion and deletion at both ends?

A) std::vector
B) std::list
C) std::deque
D) std::array

Answer:

C) std::deque

Explanation:

The std::deque container provides constant time insertion and deletion at both ends, while std::vector provides constant time insertion and deletion only at the end.

14. What is the purpose of std::move in C++11 and above?

A) To move the contents of a container to another.
B) To perform right shift operations.
C) To convert lvalues to rvalues.
D) To move the execution of the program to a different location.

Answer:

C) To convert lvalues to rvalues.

Explanation:

std::move is used to convert lvalues to rvalues, thereby indicating that a resource may be moved from one object to another.

15. Which of the following smart pointers in C++ takes ownership of the pointed object and automatically deletes it when the smart pointer goes out of scope?

A) std::auto_ptr
B) std::unique_ptr
C) std::shared_ptr
D) std::weak_ptr

Answer:

B) std::unique_ptr

Explanation:

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope.

16. Which of the following C++ headers must be included to perform file input and output operations?

A) #include <fstream>
B) #include <filestream>
C) #include <fileio>
D) #include <file>

Answer:

A) #include <fstream>

Explanation:

To perform file input and output operations in C++, you need to include the <fstream> header.

17. Which of the following is the correct way to create a thread in C++11 and above?

A) std::thread myThread(functionName);
B) std::create_thread(functionName);
C) std::execute_thread(functionName);
D) std::run_thread(functionName);

Answer:

A) std::thread myThread(functionName);

Explanation:

In C++11 and above, you can create a thread by constructing an instance of std::thread and passing the function name as an argument.

18. What does the explicit keyword prevent in C++?

A) Copy Constructor
B) Default Constructor
C) Implicit Conversion
D) Function Overriding

Answer:

C) Implicit Conversion

Explanation:

The explicit keyword in C++ is used to prevent implicit conversions.

19. What is the function of the mutable keyword in C++?

A) It allows a data member of a const object to be modified.
B) It ensures that a data member remains constant.
C) It makes the object mutable.
D) It is used to mutate the state of the object.

Answer:

A) It allows a data member of a const object to be modified.

Explanation:

The mutable keyword in C++ allows a data member of a const object to be modified.

20. Which of the following is a correct lambda expression syntax in C++?

A) [] (int x, int y) -> int { return x + y; }
B) lambda (int x, int y) : int { return x + y; }
C) func [int x, int y] -> int { return x + y; }
D) [] int x, int y -> int { return x + y; }

Answer:

A) [] (int x, int y) -> int { return x + y; }

Explanation:

Lambda expressions in C++ have the syntax [] (parameter_list) -> return_type { body_of_lambda }.

21. How is exception handling accomplished in C++?

A) Using the try and catch keywords.
B) Using the error and handle keywords.
C) Using the exception keyword only.
D) Using the throw keyword only.

Answer:

A) Using the try and catch keywords.

Explanation:

In C++, exceptions are handled using try, catch, and throw keywords. The try block contains the code that might throw an exception, and the catch blocks contain the code to handle the exception.

22. Which of the following is a correct way to perform static_cast in C++?

A) static_cast<int>(variable);
B) int.static_cast(variable);
C) cast<int>.static(variable);
D) static<int>.cast(variable);

Answer:

A) static_cast<int>(variable);

Explanation:

The correct syntax for static_cast in C++ is static_cast<Type>(expression).

23. What is the purpose of the this pointer in C++?

A) To store the address of a static member function.
B) To store the address of the current object.
C) To store the address of a constant.
D) To store the address of a temporary variable.

Answer:

B) To store the address of the current object.

Explanation:

The this pointer in C++ is used to store the address of the current object inside a non-static member function.

24. What will be the output of the following code snippet?

#include <iostream>
using namespace std;
class Base {
public:
    virtual void show() { cout << "Base\n"; }
};
class Derived : public Base {
public:
    void show() { cout << "Derived\n"; }
};
int main() {
    Base* b;
    Derived d;
    b = &d;
    b->show();
    return 0;
}
A) Base
B) Derived
C) Base Derived
D) Compilation Error

Answer:

B) Derived

Explanation:

The show() function is virtual in the Base class and overridden in the Derived class. When a base class pointer points to a derived class object and we call the virtual function, the derived class version of the function is invoked.

25. In C++, what is the correct way to declare a multidimensional array with 3 rows and 4 columns?

A) int array[3][4];
B) int array[3, 4];
C) int array[][] = {3, 4};
D) int array = new int[3, 4];

Answer:

A) int array[3][4];

Explanation:

In C++, a multidimensional array can be declared using the syntax Type arrayName[size1][size2]…[sizeN]. Option A is the correct way to declare a 2D array with 3 rows and 4 columns.


Completing this C++ Certified Professional Programmer practice test should help you assess your understanding of various C++ concepts and functionalities. Remember, practicing regularly and addressing any areas of uncertainty is key to preparing effectively for the certification exam. We wish you the very best in your journey to becoming a C++ Certified Professional Programmer! Keep coding and exploring the myriad of possibilities that C++ offers.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top