C++ Language
• Interview questions are important for evaluating a candidate's proficiency in the C++ programming language and assessing their coding and problem-solving skills.
Interview QnA
Here are some tricky interview questions in C++ that can help you prepare for technical interviews:
- 1 • What is C++?
⤏ C++ is a general-purpose programming language that was developed as an extension of the C programming language. It was created by Bjarne Stroustrup at Bell Labs in the early 1980s.
- 2 • Difference between 'new' and 'malloc' in C++.
int *arr1 = new int[5]; int *arr2 = (int*)malloc(5 * sizeof(int));
⤏ Both 'new' and 'malloc' are used for dynamic memory allocation, but 'new' is an operator in C++, and 'malloc' is a function in C.
- 3 • What is a virtual destructor?
⤏ A virtual destructor is used in a base class to ensure that the correct destructor is called when deleting a derived class object through a pointer to the base class. It allows proper cleanup of resources in polymorphic class hierarchies.
- 4 • Concept of smart pointers in C++.
std::unique_ptr<int> uniquePtr(new int); std::shared_ptr<int> sharedPtr = std::make_shared<int>();
⤏ Smart pointers in C++ (e.g., 'std::unique_ptr', 'std::shared_ptr') are objects that act like pointers but provide automatic memory management. They automatically release the allocated memory when it's no longer needed, helping to prevent memory leaks.
- 5 • Difference between 'delete' and 'delete[]' in C++.
⤏ In C++, 'delete' is used to deallocate memory for a single object created using 'new', while 'delete[]' is used to deallocate memory for an array of objects created using 'new[]'. Using 'delete' for an array or 'delete[]' for a single object can lead to undefined behavior.
- 6 • What is the "Rule of Three" in C++?
⤏ The Rule of Three states that if a class defines any one of the following three special member functions ('destructor', 'copy constructor', or 'copy assignment operator'), then it should define all three. This is to ensure proper resource management when a class involves dynamic memory allocation or other resources.
- 7 • What is the purpose of the 'explicit' keyword?
⤏ The 'explicit' keyword is used to prevent automatic type conversion by the compiler. It can be applied to a single-argument constructor to prevent implicit type conversion during object creation.
- 8 • The 'volatile' keyword in C++.
⤏ The 'volatile' keyword in C++ is used to indicate that a variable may be changed by some external factor, such as an interrupt or another thread, and the compiler should not optimize or reorder access to that variable. It ensures that every access to the variable corresponds to a genuine read or write operation.
- 9 • Find the output: 'int i = 5; std::cout << (++i) + (++i) + (i++);'
⤏ Output: 21
- 10 • Find the output: 'int x = 10; std::cout << x++ << ++x << x--;'
⤏ Output: 101212
- 11 • Find the output of the following code below:
int a = 5, b = 2; int result = a++ - --b * 2; std::cout << result;
⤏ Output: 3
- 12 • How does the 'typeid' operator work?
⤏ The 'typeid' operator returns information about the type of an expression. It is often used in conjunction with dynamic_cast to check the actual type of an object in polymorphic scenarios.
- 13 • Difference between 'const' and constexpr in C++?
⤏ The 'const' is used to declare constants and is evaluated at runtime, and the 'constexpr' is used to declare constants that are evaluated at compile time. Also, 'constexpr' enables more opportunities for optimization by the compiler.
- 14 • Difference between 'override' and 'final' in C++11 onwards.
⤏ In C++11 and later, 'override' is used to explicitly indicate that a function is intended to override a virtual function in a base class, and the 'final' is used to indicate that a virtual function should not be overridden in any derived class.
- 15 • Explain the purpose of lambda expressions in C++.
⤏ Lambda expressions in C++ provide a concise way to create anonymous functions or function objects. They are particularly useful in situations where a small, short-lived function is needed, such as in algorithms or as arguments to higher-order functions.
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.