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.
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++?
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++
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.
12. 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.
13. 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.
14. 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.
15. 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)
16. 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
17. 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.
18. 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.
19. 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.
20. 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.
21. How to input strings in C++ with spaces?
22. 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
23. 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 ‘+.’
24. 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.
25. 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
26. 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.
27. 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.
28. 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.
29. 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
30. 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
31. 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.
32. How to write a program to check if a number is a palindrome or not?
33. 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.
34. Write a program to find the factorial of a number?
35. 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.
36. 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.
37. 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.
25. How to find the frequency of a number in C++?
39. 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.
40. 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.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll 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.