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;
}
Answer:
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++?
Answer:
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++?
Answer:
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++?
Answer:
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?
Answer:
Explanation:
The std::vector is part of the <vector> header.
6. How can we dynamically allocate memory for an integer in C++?
Answer:
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?
Answer:
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++?
Answer:
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++?
Answer:
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;
}
Answer:
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++?
Answer:
Explanation:
Template specialization in C++ is defined with template <>.
12. In C++, what does the virtual keyword denote in a class method?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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++?
Answer:
Explanation:
The explicit keyword in C++ is used to prevent implicit conversions.
19. What is the function of the mutable keyword in C++?
Answer:
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++?
Answer:
Explanation:
Lambda expressions in C++ have the syntax [] (parameter_list) -> return_type { body_of_lambda }.
21. How is exception handling accomplished in C++?
Answer:
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++?
Answer:
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++?
Answer:
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;
}
Answer:
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?
Answer:
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.