Debug School

Akanksha
Akanksha

Posted on

Top 30 C++ Interview Questions with Answers multiple choice style

1. What does C++ stand for?

a) Common Compiler for Unix
b) Compact Code for Unified XML
c) C plus plus
d) Computer Communication Utility
Answer: c) C plus plus

2. What is the extension for C++ source files?

a) .cc
b) .cpp
c) .c++
d) .cxx
Answer: b) .cpp

3. In C++, which of the following is not a fundamental data type?

a) int
b) double
c) string
d) char
Answer: c) string

4. What is the correct syntax for declaring a constant in C++?

a) const int x = 10;
b) int constant x = 10;
c) int x = const 10;
d) int x = constant(10);
Answer: a) const int x = 10;

5. Which C++ keyword is used to create an alias for a data type?

a) typedef
b) typealias
c) alias
d) typedefname
Answer: a) typedef

6. In C++, how can you dynamically allocate memory for an array?

a) malloc()
b) new[]
c) alloc()
d) allocate()
Answer: b) new[]

7. Which operator is used to access the member of a class or structure in C++?

a) ::
b) .
c) ->
d) *
Answer: b) .

8. What is the purpose of the 'friend' keyword in C++?

a) It declares a function to be a global function.
b) It specifies a class as a friend of another class.
c) It defines a class.
d) It allocates memory for objects.
Answer: b) It specifies a class as a friend of another class.

9. What is function overloading in C++?

a) Writing multiple functions with the same name and different parameters in the same scope.
b) Writing multiple functions with different names and the same parameters in the same scope.
c) Writing a single function with multiple return statements.
d) Writing a function with a variable number of parameters.
Answer: a) Writing multiple functions with the same name and different parameters in the same scope.

10. Which of the following is not a C++ Standard Library container?

a) vector
b) stack
c) list
d) tree
Answer: d) tree

11. What is the purpose of the 'break' statement in a loop?

a) To exit the loop immediately.
b) To skip the current iteration and continue with the next.
c) To restart the loop.
d) To repeat the loop indefinitely.
Answer: a) To exit the loop immediately.

12. What is the difference between 'while' and 'do-while' loops in C++?

a) 'do-while' loops can execute their code block zero or more times.
b) 'while' loops always execute their code block at least once.
c) 'do-while' loops can only be used for infinite loops.
d) 'while' and 'do-while' loops are identical.
Answer: b) 'while' loops always execute their code block at least once.

13. What does the 'this' pointer refer to in C++?

a) It points to the previous object created.
b) It points to the current object itself.
c) It points to the next object in memory.
d) It points to a static member of the class.
Answer: b) It points to the current object itself.

14. Which C++ feature allows you to create a base class that can be inherited by multiple derived classes?

a) Templates
b) Polymorphism
c) Inheritance
d) Encapsulation
Answer: c) Inheritance

15. Which of the following is not a valid C++ access modifier?

a) private
b) protected
c) hidden
d) public
Answer: c) hidden

16. What is the purpose of the 'delete' operator in C++?

a) To remove a file from the disk.
b) To deallocate memory previously allocated with 'new'.
c) To delete a member variable from a class.
d) To delete a line of code from the program.
Answer: b) To deallocate memory previously allocated with 'new'.

17. What is the result of the bitwise AND operation (x & y) if x is 5 (binary: 0101) and y is 3 (binary: 0011)?

a) 5
b) 3
c) 7
d) 1
Answer: d) 1

18. In C++, what is the purpose of the 'const' qualifier when used with a member function?

a) It makes the function a constant function that can't modify the object's data members.
b) It makes the function a constructor.
c) It indicates that the function should be called only once.
d) It specifies that the function should take constant arguments.
Answer: a) It makes the function a constant function that can't modify the object's data members.

19. Which of the following is not a valid C++ data type?

a) long
b) float
c) double float
d) char
Answer: c) double float

20. What is the purpose of the 'static' keyword when used with a class member?

a) It makes the member a constant.
b) It indicates that the member is global and can be accessed from outside the class.
c) It specifies that the member is shared among all instances of the class.
d) It marks the member as deprecated.
Answer: c) It specifies that the member is shared among all instances of the class.

21. What is the correct way to define a C++ macro?

a) #define MACRO_NAME value
b) #macro MACRO_NAME value
c) define MACRO_NAME value
d) define MACRO_NAME as value
Answer: a) #define MACRO_NAME value

22. In C++, what does the 'typeid' operator return?

a) The type of an expression.
b) The size of a data type.
c) The memory address of a variable.
d) The name of a function.
Answer: a) The type of an expression.

23. What is the purpose of the 'try-catch' block in C++?

a) To handle exceptions and errors in a controlled manner.
b) To define custom data types.
c) To implement multi-threading.
d) To handle file input/output.
Answer: a) To handle exceptions and errors in a controlled manner.

24. What is the purpose of the 'volatile' keyword in C++?

a) It indicates that the variable can't be modified.
b) It indicates that the variable can't be accessed by multiple threads.
c) It informs the compiler that the variable may be modified outside the program's scope.
d) It specifies that the variable can't be accessed by functions outside the current file.
Answer: c) It informs the compiler that the variable may be modified outside the program's scope.

25. Which C++ operator is used to allocate memory for an object on the heap?

a) new
b) malloc
c) alloc
d) allocate
Answer: a) new

26. What is the purpose of 'inline' functions in C++?

a) To reduce the size of the code.
b) To specify that the function should be executed inline with the calling code.
c) To indicate that the function is private.
d) To specify that the function is a constructor.
Answer: b) To specify that the function should be executed inline with the calling code.

27. What does the 'sizeof' operator in C++ return?

a) The size of a data type in bytes.
b) The size of a memory block in bytes.
c) The size of a function in bytes.
d) The size of a file in bytes.
Answer: a) The size of a data type in bytes.

28. In C++, what is the purpose of the 'const_cast' operator?

a) To cast a constant pointer to a non-constant pointer.
b) To cast a non-constant pointer to a constant pointer.
c) To cast an integer to a character.
d) To cast a floating-point number to an integer.
Answer: a) To cast a constant pointer to a non-constant pointer.

29. In C++, what is the purpose of the 'std::endl'?

a) It inserts a new line into the output and flushes the buffer.
b) It ends the program.
c) It prints the current date and time.
d) It specifies the end of a class definition.
Answer: a) It inserts a new line into the output and flushes the buffer.

30. Which C++ operator is used to access the address of a variable?

a) *
b) &
c) ::
d) ->
Answer: b) &

31. What is the result of the bitwise OR operation (x | y) if x is 5 (binary: 0101) and y is 3 (binary: 0011)?

a) 5
b) 3
c) 7
d) 1
Answer: c) 7

32. What is the purpose of the 'virtual' keyword in C++?

a) It specifies that a function can be overridden in a derived class.
b) It indicates that a function should be called only once.
c) It marks a function as a pure virtual function.
d) It specifies that a variable can't be modified.
Answer: a) It specifies that a function can be overridden in a derived class.

33. Which C++ feature allows a function to have multiple definitions with the same name but different parameters?

a) Function overloading
b) Function overriding
c) Function polymorphism
d) Function templates
Answer: a) Function overloading

34. What is the purpose of the 'typeid' operator when used with dynamic_cast in C++?

a) It returns the type of the object pointed to by a base class pointer.
b) It returns the type of the derived class.
c) It returns the type of the base class.
d) It returns the type of the object pointed to by a derived class pointer.
Answer: a) It returns the type of the object pointed to by a base class pointer.

Top comments (0)