C++ MCQ

C++ is a versatile and widely used programming language. Whether you’re a beginner just starting with C++ or looking for a refresher, this quiz will test your basic understanding of the language.

Quiz Outline: 

  • Basics & Syntax
  • Data Types & Variables
  • Control Structures
  • Functions
  • Arrays & Strings
  • Pointers & References
  • OOP Concepts (Classes, Inheritance, Polymorphism)
  • STL (Standard Template Library)
  • Memory Management
  • More

1. Which symbol marks the beginning and end of a C++ statement?

a) ;
b) .
c) :
d) ,

Answer:

a) ;

Explanation:

In C++, statements are terminated using a semicolon.

2. Which of the following is a valid variable name in C++?

a) 1var
b) var!
c) _var
d) var#

Answer:

c) _var

Explanation:

Variables in C++ can start with an underscore or a letter, but not with a number or special character.

3. Which of the following is the correct way to add comments in C++?

a) //
b) /* */
c) —
d) Both a & b

Answer:

d) Both a & b

Explanation:

// is used for single-line comments, while /* */ is used for multi-line comments.

4. Which header file is required for input and output operations?

a) <iostream>
b) <io>
c) <input>
d) <output>

Answer:

a) <iostream>

Explanation:

The header defines the standard stream objects for input and output.

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

a) float
b) double
c) char
d) boolean

Answer:

d) boolean

Explanation:

The correct data type for boolean values in C++ is bool, not boolean.

6. What is the size (in bytes) of an int data type on most 32-bit systems?

a) 1
b) 2
c) 4
d) 8

Answer:

c) 4

Explanation:

On most 32-bit systems, an int is 4 bytes.

7. What data type would you use to store a single character?

a) string
b) char
c) text
d) word

Answer:

b) char

Explanation:

The char data type is used to store a single character.

8. Which keyword is used to declare a constant?

a) const
b) static
c) final
d) define

Answer:

a) const

Explanation:

In C++, the const keyword is used to declare a constant value.

9. What will be the output of cout << 5/2;?

a) 2.5
b) 2
c) 5
d) 2.0

Answer:

b) 2

Explanation:

Both 5 and 2 are integers, so the division will be integer division, which discards the remainder.

10. Which of the following can be a valid value for a bool data type?

a) 1
b) 0
c) true
d) Both a & c

Answer:

d) Both a & c

Explanation:

In C++, bool can take values true or false, which are equivalent to 1 and 0, respectively.

11. Which of the following control structures allows the execution of a block of statements multiple times based on a condition?

a) if
b) switch
c) loop
d) goto

Answer:

c) loop

Explanation:

Loops, like for, while, and do-while, allow statements to be executed multiple times based on a condition.

12. Which keyword is used to exit a loop prematurely?

a) break
b) exit
c) stop
d) return

Answer:

a) break

Explanation:

The break statement is used to exit a loop before its normal completion.

13. Which of the following is NOT a loop in C++?

a) for
b) foreach
c) while
d) do-while

Answer:

b) foreach

Explanation:

C++ is used for for traditional looping, and while and do-while for conditional looping. There isn’t a foreach loop in standard C++, but there’s a range-based for loop.

14. Which keyword is used to define a function in C++?

a) def
b) fun
c) func
d) None of the above

Answer:

d) None of the above

Explanation:

In C++, functions are defined by specifying the return type followed by the function name and parameters. No specific keyword like def or fun is required.

15. If a function doesn’t return any value, what should be its return type?

a) null
b) void
c) none
d) empty

Answer:

b) void

Explanation:

Functions that don’t return a value have a return type of void.

16. Which of the following is NOT a valid function declaration?

a) int func();
b) void func(int a, int b);
c) double func(double a, double b);
d) func();

Answer:

d) func();

Explanation:

The return type is missing in option d, making it an invalid function declaration.

17. What is the first index of an array in C++?

a) 1
b) 0
c) Depends on the declaration
d) None of the above

Answer:

b) 0

Explanation:

In C++, arrays are zero-indexed, meaning the first element has an index of 0.

18. How do you find the length of a string named str?

a) str.length()
b) length(str)
c) sizeof(str)
d) str.len()

Answer:

a) str.length()

Explanation:

The length() function is a member function of the string class in C++.

19. Which of the following is the correct way to declare an array of 5 integers?

a) int array[5];
b) int array;
c) array[5] int;
d) int[5] array;

Answer:

a) int array[5];

Explanation:

Arrays in C++ are declared with the type, followed by the name and size in square brackets.

20. If string str = “Hello”;, what is str[1]?

a) H
b) e
c) l
d) o

Answer:

b) e

Explanation:

Strings in C++ are zero-indexed, so str[1] would return the second character, which is ‘e’.

21. What is encapsulation in C++?

a) Function overloading
b) Inheritance of classes
c) Wrapping data and methods into a single unit
d) Using multiple inheritance

Answer:

c) Wrapping data and methods into a single unit

Explanation:

Encapsulation is a fundamental concept in object-oriented programming that binds together the data and functions that manipulate the data.

22. Which keyword is used to create an object of a class in C++?

a) new
b) this
c) class
d) object

Answer:

a) new

Explanation:

The new keyword is used to dynamically create an instance (object) of a class.

23. Which of the following is a member function?

a) A function declared outside a class
b) A function declared inside a class
c) A function that is static
d) A global function

Answer:

b) A function declared inside a class

Explanation:

A member function is a function that is declared inside a class and acts upon the data members of that class.

24. What is the main role of a constructor in a class?

a) To delete objects
b) To initialize the object’s data members
c) To perform operations
d) None of the above

Answer:

b) To initialize the object’s data members

Explanation:

Constructors are special class functions that are executed whenever an object of the class is created. They help in initializing the object’s data.

25. Which of the following is true about destructors?

a) A class can have multiple destructors
b) Destructors cannot have parameters
c) Destructors are used to allocate memory
d) Destructors can be inherited

Answer:

b) Destructors cannot have parameters

Explanation:

A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope or is explicitly deleted. A destructor will have the exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.

26. Which of the following is used for dynamic memory allocation?

a) malloc
b) free
c) delete
d) new

Answer:

d) new

Explanation:

In C++, a new operator is used for dynamic memory allocation which initializes the memory and returns its address.

27. Which of the following is used to free the memory allocated by the ‘new’ operator?

a) free
b) delete
c) remove
d) dealloc

Answer:

b) delete

Explanation:

The delete operator is used to free the memory that was previously allocated by the new operator.

28. What is the process by which a function can be made to behave differently based on its arguments?

a) Encapsulation
b) Abstraction
c) Polymorphism
d) Inheritance

Answer:

c) Polymorphism

Explanation:

Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

29. Function overriding is an example of:

a) Compile-time polymorphism
b) Run-time polymorphism
c) Both compile-time and run-time polymorphism
d) None of the above

Answer:

b) Run-time polymorphism

Explanation:

Function overriding is a concept where two or more functions have the same name, return type, and parameters. The function that is to be called is determined at runtime, hence it is considered run-time polymorphism.

30. Which of the following concepts allows you to reuse code?

a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism

Answer:

c) Inheritance

Explanation:

Inheritance is a mechanism wherein a new class is derived from an existing class. This allows the derived class to inherit features from the base class, allowing for code reuse.

31. What function is used to open a file in C++?

a) open()
b) fopen()
c) start()
d) begin()

Answer:

a) open()

Explanation:

In C++, the open() function is used to open a file.

32. Which of the following modes is used to open a file in read-only mode in C++?

a) r
b) w
c) a
d) rw

Answer:

a) r

Explanation:

The ‘r’ mode is used to open a file for reading.

33. What does a pointer variable store?

a) A memory address
b) A value
c) Both memory address and value
d) Name of a variable

Answer:

a) A memory address

Explanation:

A pointer variable in C++ stores the memory address of another variable.

34. What symbol is used to declare a pointer variable?

a) &
b) *
c) %
d) $

Answer:

b) *

Explanation:

The * symbol is used to declare a pointer variable.

35. What is the main use of templates in C++?

a) To increase the runtime of a program
b) To add more variables to a program
c) To add flexibility to classes and functions
d) To make the program look better

Answer:

c) To add flexibility to classes and functions

Explanation:

Templates provide a way to create generic classes and functions, thereby providing flexibility.

36. Which loop is best suited for going through an array?

a) for loop
b) while loop
c) do-while loop
d) for-each loop

Answer:

a) for loop

Explanation:

A for loop is typically best suited for iterating through an array because of its structure, with initialization, condition, and increment/decrement in one line.

37. Which of the following loops is executed at least once?

a) for loop
b) while loop
c) do-while loop
d) None of the above

Answer:

c) do-while loop

Explanation:

The do-while loop is executed at least once because the condition is checked after the loop body is executed.

38. What keyword is used for exception handling?

a) throw
b) throws
c) tackle
d) error

Answer:

a) throw

Explanation:

In C++, the throw keyword is used to signal that an exception has occurred.

39. Which of the following is not a type of exception in C++?

a) runtime_error
b) logic_error
c) input_error
d) overflow_error

Answer:

c) input_error

Explanation:

There isn’t an exception named input_error in the standard C++ library.

40. Which keyword is used to catch exceptions in C++?

a) capture
b) get
c) catch
d) trap

Answer:

c) catch

Explanation:

In C++, the catch block is used to handle the exceptions. It follows the try block which contains the code that might throw exceptions.

41. Which of the following keywords is used in C++ to allocate memory dynamically for a single value?

a) malloc()
b) alloc()
c) new
d) create

Answer:

c) new

Explanation:

In C++, the new operator is used to allocate memory dynamically on the heap for a single value or an array.

42. To deallocate memory allocated using the new operator, which operator is used?

a) delete
b) free
c) dealloc
d) remove

Answer:

a) delete

Explanation:

The delete operator is used to free up the memory that was previously allocated using the new operator.

43. What is the process of defining multiple functions with the same name but different parameters?

a) Function Overriding
b) Function Overloading
c) Function Duplication
d) Function Multiplicity

Answer:

b) Function Overloading

Explanation:

Function overloading allows multiple functions to have the same name but different parameters, possibly with different types.

44. Which of the following is a container in the Standard Template Library (STL)?

a) int
b) char
c) vector
d) main

Answer:

c) vector

Explanation:

A vector is a dynamic array provided by the C++ Standard Template Library.

45. What is the primary use of the algorithm header in the STL?

a) To define basic data types
b) To implement classes
c) To provide function templates for algorithms
d) To handle errors

Answer:

c) To provide function templates for algorithms

Explanation:

The algorithm header provides a set of function templates for common algorithms like sorting, searching, etc.

46. What is the full form of OOP?

a) Object Oriented Programming
b) Official Operational Program
c) Object Operated Programming
d) Official Operator Program

Answer:

a) Object Oriented Programming

Explanation:

OOP stands for Object Oriented Programming, a paradigm that uses “objects” – data structures consisting of data fields and methods.

47. In C++, what is the scope resolution operator?

a) ->
b) .
c) ::
d) <>

Answer:

c) ::

Explanation:

The scope resolution operator (::) in C++ is used to define a function outside a class or to access a global variable within another variable.

48. Which of the following is the correct syntax to comment multiple lines in C++?

a) //…//
b)
c) /…/
d) –…–

Answer:

c) /…/

Explanation:

In C++, /*…*/ is used to comment out multiple lines.

49. What does STL stand for in C++?

a) Standard Template Language
b) Simple Template Library
c) Standardized Type Library
d) Standard Template Library

Answer:

d) Standard Template Library

Explanation:

STL stands for Standard Template Library in C++.

50. What is the correct way to declare a constant variable in C++?

a) const int x;
b) int constant x;
c) constant int x;
d) int const x;

Answer:

a) const int x;

Explanation:

In C++, the const keyword is used before the datatype to declare a variable as constant.

51. What is a special type of function that has access to private members of a class but isn’t a member of that class?

a) Private function
b) Member function
c) Friend function
d) Public function

Answer:

c) Friend function

Explanation:

A friend function is not a member of the class, but it can access private and protected members of the class. It is declared using the keyword friend.

52. Which keyword is used in C++ to include a particular namespace in our program?

a) import
b) include
c) use
d) using

Answer:

d) using

Explanation:

The using keyword is used to specify that a program will be using a particular namespace, like using namespace std;.

53. Which function cannot be inline in C++?

a) Static function
b) Virtual function
c) Constant function
d) Friend function

Answer:

b) Virtual function

Explanation:

A virtual function in C++ is used for runtime polymorphism and it cannot be inline because its address is not known at compile time.

54. What is it called when a function calls itself?

a) Looping
b) Iterating
c) Recursion
d) Nesting

Answer:

c) Recursion

Explanation:

When a function calls itself, it’s known as recursion. It’s a powerful technique but one must ensure it has a base case to prevent infinite recursion.

55. Which among the following is a valid way to define a data member of a class as static?

a) static varName;
b) static dataType varName;
c) varName static;
d) dataType varName = static;

Answer:

b) static dataType varName;

Explanation:

In C++, static data members of a class need to be declared within the class using the static keyword followed by data type and variable name.

56. What can be used to point to a function in C++?

a) Variable pointer
b) Reference
c) Function pointer
d) Data pointer

Answer:

c) Function pointer

Explanation:

A function pointer points to the address of a function and can be used to call a function indirectly.

57. What characterizes the initialization of variables inside a class constructor?

a) Dynamic Initialization
b) Static Initialization
c) Constructor Initialization
d) Direct Initialization

Answer:

a) Dynamic Initialization

Explanation:

When the value of a variable is set during runtime (like inside a class constructor), it’s called dynamic initialization.

58. Which access specifier allows class members to be accessible only within the same class and by friend functions?

a) Private
b) Public
c) Protected
d) External

Answer:

a) Private

Explanation:

Private access specifier ensures that class members are only accessible within the class and by friend functions.

59. What is a class called that cannot be instantiated and is meant to be subclassed?

a) Incomplete class
b) Subclass
c) Abstract class
d) Superclass

Answer:

c) Abstract class

Explanation:

An abstract class is a class that cannot be instantiated and is intended to be subclassed. It can have abstract (without implementation) as well as concrete methods.

60. To read command line arguments in C++, which parameters should the main() function have?

a) void
b) ()
c) (args[])
d) (int argc, char *argv[])

Answer:

d) (int argc, char *argv[])

Explanation:

Command line arguments in C++ are read using (int argc, char *argv[]) where argc denotes the number of arguments and argv is an array of pointers to those arguments.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top