Interview questions are a necessary part of the selection process of any company or organization. To face the interviewer, one needs to be fully prepared for the interview questions. In this C++ interview questions tutorial, you will look at the most commonly asked C++ interview questions that one should know.
In this C++ interview questions tutorial, you will go through some conceptual questions, multiple-choice, output-based, and programming questions that you can expect in any C++ interview.
So let’s begin with the top 40 commonly asked C++ interview questions.
C++ Interview Questions For Freshers
The C++ Interview Questions addressed in this section cover the fundamentals of the C++ language, an aspect to be known mandatorily.
1. What is the difference between C and C++?
C is a procedure-oriented programming language
C++ is a partially object-oriented programming language
It follows a top-down approach
It follows a bottom-up approach
C doesn’t support function or operator overloading
C++ supports function as well as function overloading
C language doesn’t support virtual and friend function
C++ language supports both virtual and friend functions.
C language has 32 keywords
C++ language contains 52 keywords
2. What are classes and objects in C++?
A class is like a blueprint of an object. It is a user-defined data type with data members and member functions and is defined with the keyword class.
You define objects as an instance of a class. Once it creates the object, then it can operate on both data members and member functions.
3. What are access modifiers?
You use access modifiers to define accessibility for the class members. It defines how to access the members of the class outside the class scope.
There are three types of access modifiers:
4. Difference between equal to (==) and assignment operator(=)?
The equal to operator == checks whether two values are equal or not. If equal, then it’s true; otherwise, it will return false.
The assignment operator = allots the value of the right-side expression to the left operand.
5. What is the difference between a while loop and a do-while loop?
The while loop verifies the condition; if it’s true, then it iterates the loop till the condition becomes false.
The do-while loop first iterates the loop body once, then it checks for the condition.
If the condition is false in a while loop, then not a single statement will execute inside the loop.
If the condition in a do-while loop is false, then the body will also execute once.
6. What is the size of the int data type?
- 4 bytes
- 1 byte
- 8 bytes
- 2 bytes
1 - 4 bytes, the integer data type is 4 bytes.
7. Which among the following operators cannot be overloaded?
3 - ?: operator cannot be overloaded because it is not syntactically possible.
8. What among these is used to return the number of characters in the string?
- Both size and length
3. Both size and length are used to return the number of characters in the string.
9. Discuss the difference between prefix and postfix?
In prefix (++i), first, it increments the value, and then it assigns the value to the expression.
In postfix (i++), it assigns the value to the expression, and then it increments the variable's value.
10. Can you compile a program without the main function?
Yes, you can compile a program without the main function, but you cannot run or execute the program because the main() function is the entry point, from where all the execution begins. And without the entry point, then you can execute the program.
11. What is std in C++?
- std is a standard class in C++
- std is a standard file reading header
- std is a standard header file
- std is a standard namespace
4 - std is a standard namespace in C++
12. What are the four different data types in C++?
- Primitive/Basic: Char, int, short, float, double, long, bool, etc.
- Derived: Array, pointer, etc.
- Enumeration: Enum
- User-defined: Structure, class, etc.
13. How is struct different from class?
Its members are public by default.
Its members are private by default.
The default access specifiers are public when deriving a struct from a class/struct.
The default access specifiers are private when deriving a class.
14. What do you understand about polymorphism in C++?
Polymorphism relates to many forms that differ in different situations. It occurs when the hierarchy of multiple classes is related to one another by inheritance. In C++, the two types of polymorphism are
- Compile Time Polymorphism
- Runtime Polymorphism
15. Compare compile time and runtime polymorphism.
The method to be executed is known at compile time. And the call is resolved by the compiler.
The method to be executed is known at run time. The compiler does not resolve the call.
Provides quicker execution because it is known at the compile time.
Provides slower execution because it is known at the run time.
Achieved by operation or function overloading.
Achieved by function overriding.
16. What is a constructor in C++?
An automatically executed member function whenever an object is created is called a constructor. It has the same name as the class it belongs to. Thus, the compiler knows that the member function is a constructor. No return type is used for constructors.
17. What is a virtual function?
A member function in the base class redefined in a derived class is a virtual function. It is declared using the virtual keyword. It ensures that the correct function is called for an object, irrespective of the type of reference/pointer used for the function call. Virtual functions are mainly used for runtime polymorphism.
18. What do you understand about friend class and friend function?
A friend class is capable of accessing protected, private, and public members of other classes in which it is declared as friends.
A friend function can also access protected, private, and public members, but it is not a member function.
19. What are the three different types of C++ access specifiers?
- Public: All member functions and data members are accessible outside the class.
- Protected: All member functions and data members are accessible within the class and to the derived class.
- Private: All member functions and data members cannot be accessed outside the class.
20. What is an abstraction in C++?
Abstraction means displaying the essential details to the user while hiding the irrelevant or particular details that you don’t want to show to a user. It is of two types:
- Control abstraction
- Data abstraction
21. What are destructors in C++?
In C++, a deconstructor (function) is automatically called when an object is destroyed. It has the same name as the constructor but is preceded by a tilde. Destructors are used to deallocate memory and perform the cleanup.
22. Is it possible to overload a deconstructor? Give reasons for your answer.
No, it is impossible as destructors do not take arguments or return anything. There has to be only one empty destructor per class. It should have a void parameter list.
23. What is an abstract class? When is it used?
An abstract class is a class whose objects cannot be created. It serves as a parent for the derived classes. Placing a pure virtual function in the class makes it an abstract class.
24. What do you understand about static members and static member functions?
A variable in a class declared as static has its space allocated for the lifetime of the program. Regardless of the number of objects of that class created, there is only a single copy of the static member. The same static member is accessible to all the objects of that class.
A static member function can be called even if no class objects exist. It is accessed using only the class name and the scope resolution operator (::).
C++ Interview Questions For Intermediate
This set of C++ Interview Questions are bit more complex and advanced and form the next stage of your preparation for the C++ interview.
25. What is the C++ OOPs concept?
Object: Anything that exists physically in the real world is called an object.
Class: The collection of objects is called class.
Inheritance: Properties of parent class inherited into child class is known as inheritance.
Polymorphism: It is the ability to exist in more than one form.
Encapsulation: Binding of code and data together into a single unit.
Abstraction: Hiding internal details and showing functionality to the user.
26. When is void() return type used?
You use the void() return type when you don’t want to return any value. It specifies that the function doesn’t return a value. A function with a void return type completes its task and then returns the control to the caller.
27. What is call by value and call by reference in C++?
In the call by value method, you pass the copies of actual parameters to the function's formal parameters. This means if there is any change in the values inside the function, then that change will not affect the actual values.
In the call-by-reference method, the reference or address of actual parameters is sent to the function's formal parameters. This means any change in values inside the function will be reflected in the actual values.
28. What is an inline function?
An inline function when called expands in line. When you call this function, the whole code of the inline function gets inserted or substituted at the inline function call.
Inline return-type function-name(parameters)
29. What are pointers in C++?
Pointers are the variables that store the memory address of another variable. The type of the variable must correspond with the type of pointer.
Syntax: type *name
30. What is a scope resolution operator?
A scope resolution operator is represented as ::
This operator is used to associate function definition to a particular class.
The scope operator is used for the following purposes:
- To access a global variable when you have a local variable with the same name.
- To define a function outside the class.
31. What should be the output of the following C++ program?
3 - 012, the value of i is 0 and i % 5 is equal to 0, so x is displayed and incremented and i is also incremented, then 1 % 5 is not zero, so the condition is not met. Similarly, the process will repeat till 4 % 5 because 5 % 5 is zero. So the value of x is one and gets incremented to two. Then, the process will repeat for 6,7,8,9, but 10 % 10 will be zero again, and the value of x is printed, which is 2.
32. What is a constructor?
A constructor is defined as a member function that is invoked whenever you create an object; it has the same name as that of the class.
There are two types of constructors:
- Default constructor: This auto-generated constructor doesn’t take any arguments.
- Parameterized constructor: In this constructor, it can pass arguments.
33. What is operator overloading and function overloading?
Operator overloading lets the operators have an extended meaning apart from their predefined meaning.
Function overloading defines a method in such a way that there are multiple ways to call them.
34. How to input strings in C++ with spaces?
35. Discuss the difference between new and malloc
new is an operator
malloc() is a function
It calls the constructor
The malloc function doesn’t call the constructor
There is no need to specify memory size while using new()
You have to specify the memory size
new operator can be overloaded
malloc() can never be overloaded
36. What is operator overloading?
Operator overloading is a mechanism in which a special meaning is given to an operator.
For example, you can overload the ‘+’ operator in a class-like string to concatenate two strings by only using ‘+.’
37. What is the output of the below C++ program?
- 0 3
- 0 8
- 0 6
- 0 2
3 - 0 6. In enum, the element's value is one greater than the previous element. The value of blue is 0 by default, and the value of green is five, so the value of GREAT will become six automatically.
38. What among these is used to return the number of characters in the string?
Both size and length
3 - Both size and length are used to return the number of characters in the string
39. Which among the following statements is correct about the program given below?
- The output will be 7
- The output will be 14
- The output will be 0
- The output will be 1
1 - Output will be 7. Pointer p has the memory address of x, and you display the pointer with a dereference operator that will display the value 7.
40. Which among the following statements is correct about the program given below?
- The output will be 245
- The output will be 222
- The output will be 4810
- The output will be 4812
3 - Output will be 4810, because you are passing the references of the function here.
41. What is a friend function?
You can define a friend function as a function that can access private, public and protect members of the class. You declare the friend function with the help of the friend keyword. You declare this function inside the class.
42. Which of the following will give the size of object or type?
1 - The sizeof operator is used to give the size of object or type
43. Which of the following is not a member of a class?
- Static function
- Virtual function
- Const function
- Friend function
4 - Among the following, friend function is not a member of the class
44. What is STL?
STL stands for standard template library. It is a library of container templates that provide generic classes and functions.
STL components are containers, algorithms, iterators, and function objects.
Now, have a look at some advanced-level C++ Interview Questions.
C++ Interview Questions For Experienced
These C++ Interview Questions test your application skills in C++ and explore practical problems that might be posed to you in a C++ interview.
45. How to write a program to check if a number is a palindrome or not?
46. What is a copy constructor?
A copy constructor is a member function that initializes an object using another object within the same class.
This constructor is of two types - Default copy constructor and User-defined copy constructor.
47. Write a program to find the factorial of a number?
48. What is inheritance?
Inheritance is the mechanism in which you can create a new class i.e. child class from the existing class i.e. parent class. This child class is also known as a derived class and the parent class is also called Base class.
49. What is Abstraction?
Abstraction can be defined as a technique in which you only show functionality to the user i.e., the details that you want the user to see, hiding the internal details or implementation details.
50. What should be the output of the below code?
4 - 6, Ternary operator is used, the value of a is less than b which violates the condition that is why 6 is the answer.
51. How to find the frequency of a number in C++?
52. What should be the output of the below code?
1 - 1010, the value of j is incremented to 11, then j value is added to 100 but not assigned, and at last the value of j i.e 11 is added to 999 which gives us 1010.
53. What should be the correct statement about string objects in C++?
- String objects should necessarily be terminated by a null character
- String objects have a static size
- String objects have a dynamic size
- String objects use extra memory than required
3 - String objects have a dynamic size.
54. How is a shallow copy different from a deep copy?
It stores the references of objects to the original memory address.
It makes a fresh and separate copy of an entire object and its unique memory address.
It reflects changes made to the new/copied object in the original object.
It doesn’t reflect changes made to the new/copied object in the original object.
55. How are virtual functions different from pure virtual functions?
Virtual function: A member function in the base class redefined in a derived class. It is declared with a virtual keyword.
Pure virtual function: A function with no implementation. It is declared by assigning 0. It does not have a body.
56. Class D is derived from a base class B. If creating an object of type D, what order will the constructors of these classes get called?
The derived class consists of two parts: the base part and the derived part. C++ constructs derived objects in phases. The process begins with constructing the most-base class (at the top of the inheritance tree), followed by each child class construction in order, and then the most-child class. Thus, first, the Constructor of class B will be called, and then the constructor of class D.
57. Can a virtual function be called from a constructor?
Yes, a virtual function can be called from a constructor but carefully because it might not do as per the expectations. When a virtual function is called, the virtual call gets resolved at runtime when a virtual function is called.
58. What are void pointers?
A pointer with no associated data type is called a void pointer. It can hold addresses of any type. Also, you can assign a pointer of any type to a void pointer, but the reverse is not true unless it is typecast as follows:
59. What is this pointer in C++?
Every object’s member functions have a pointer named this. It points to the object itself. Its value is set to the address of the object for which it is called. This pointer is suitable for accessing the data in the object it points to.
60. How would you deallocate and allocate memory in C++?
Use the new operator for memory allocation and the delete operator for memory deallocation in C++.
Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!
After reading these C++ interview questions you must have got an insight into various essential C++ topics. Along with the conceptual questions, you also got the idea of output-based questions, multiple-choice, and programming questions.
If you are looking to build a career in software development, please check the Post Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you build your career in the right way.
Do you have any questions regarding this tutorial on C++ Interview Questions? If you do, then please put them in the comments section. We’ll help you solve your queries.