Latest C++ Basics MCQs

1 min read

Created by Bjarne Stroustrup as an extension of the C programming language, C++ is a powerful, high-performance, statically typed language supporting both procedural and object-oriented programming paradigms. Mastery of C++ basics forms the critical foundation for software engineering, system architecture, game development, and high-performance applications. Core fundamentals include basic syntax, standard input/output streams, variable declarations, control structures, pointer manipulation, and fundamental object-oriented principles like classes and encapsulation.

1. Who is the creator of the C++ programming language?

a) Dennis Ritchie
b) Bjarne Stroustrup
c) James Gosling
d) Ken Thompson
Correct Answer: b) Bjarne Stroustrup
Explanation:
Bjarne Stroustrup created C++ at Bell Labs in 1979 as an extension of the C language.

2. What was the original name of C++ before it was renamed?

a) C Plus
b) Advanced C
c) C with Classes
d) Object C
Correct Answer: c) C with Classes
Explanation:
C++ was initially named C with Classes because it introduced object-oriented features to C.

3. Which of the following is the standard extension for C++ source files?

a) .cpp
b) .c
c) .cp
d) .cxx
Correct Answer: a) .cpp
Explanation:
While .cxx and .cc are sometimes used, .cpp is the most common and standard extension for C++ source files.

4. Which programming paradigm does C++ primarily support?

a) Procedural only
b) Object-Oriented only
c) Functional only
d) Multi-paradigm
Correct Answer: d) Multi-paradigm
Explanation:
C++ is a multi-paradigm language, supporting procedural, object-oriented, and generic programming.

5. Which function serves as the mandatory starting point of a C++ program?

a) start()
b) main()
c) init()
d) execute()
Correct Answer: b) main()
Explanation:
Every C++ program must contain a main() function, which serves as the entry point for execution.

6. Which header file is required for standard input and output operations in C++?

a) <stdio.h>
b)
c) <conio.h>
d)
Correct Answer: b)
Explanation:
The header file contains definitions for standard streams like cin and cout.

7. What is the standard output stream object in C++?

a) cin
b) print
c) cout
d) output
Correct Answer: c) cout
Explanation:
cout is the standard output stream object in C++, used to display output on the console.

8. Which operator is used alongside cout to display output?

a) >>
b) <<
c) ::
d) ->
Correct Answer: b) <<
Explanation:
The stream insertion operator (<<) is used to send data to the standard output stream.

9. What is the standard input stream object in C++?

a) cout
b) scan
c) cin
d) input
Correct Answer: c) cin
Explanation:
cin is the standard input stream object used to read input from the user.

10. Which operator is used alongside cin to read input?

a) <<
b) >>
c) ::
d) ==
Correct Answer: b) >>
Explanation:
The stream extraction operator (>>) is used to extract data from the standard input stream.

11. What does the endl manipulator do in C++?

a) Terminates the program
b) Ends the current loop
c) Inserts a newline character and flushes the output buffer
d) Deletes the last character printed
Correct Answer: c) Inserts a newline character and flushes the output buffer
Explanation:
endl inserts a newline character and ensures the output buffer is flushed to the console.

12. What is the primary purpose of the using namespace std statement?

a) To import external libraries
b) To avoid prefixing standard library elements with std::
c) To declare custom variables
d) To optimize code performance
Correct Answer: b) To avoid prefixing standard library elements with std::
Explanation:
It brings all identifiers from the standard namespace into the current scope, simplifying code syntax.

13. Which symbols denote a single-line comment in C++?

a) /*
b) #
c) //
d) --
Correct Answer: c) //
Explanation:
In C++, double slashes (//) are used to write single-line comments.

14. Which symbols denote a multi-line comment in C++?

a) /* comment */
b) // comment //
c)
d) # comment #
Correct Answer: a) /* comment */
Explanation:
Multi-line comments start with /* and end with */.

15. Which data type is used to store whole numbers in C++?

a) float
b) int
c) char
d) double
Correct Answer: b) int
Explanation:
The int data type is used to store whole numbers (integers) without decimals.

16. What is the typical size of an int data type on a 32-bit or 64-bit architecture?

a) 1 byte
b) 2 bytes
c) 4 bytes
d) 8 bytes
Correct Answer: c) 4 bytes
Explanation:
On most modern systems, the int data type occupies 4 bytes of memory.

17. Which data type stores a single character?

a) string
b) char
c) text
d) character
Correct Answer: b) char
Explanation:
The char data type is used to store a single character, enclosed in single quotes.

18. How many bytes does a char data type typically occupy in memory?

a) 1 byte
b) 2 bytes
c) 4 bytes
d) 8 bytes
Correct Answer: a) 1 byte
Explanation:
The char data type typically takes exactly 1 byte of memory.

19. Which data type is used to store single-precision floating-point numbers?

a) int
b) double
c) float
d) decimal
Correct Answer: c) float
Explanation:
The float data type represents single-precision floating-point numbers.

20. Which data type provides double precision for floating-point numbers?

a) float
b) double
c) long float
d) real
Correct Answer: b) double
Explanation:
The double data type represents double-precision floating-point numbers.

21. Which data type is used to store true or false values?

a) boolean
b) bool
c) bit
d) flag
Correct Answer: b) bool
Explanation:
The bool data type in C++ holds one of two values: true or false.

22. Which keyword specifies that a function does not return a value?

a) null
b) empty
c) void
d) none
Correct Answer: c) void
Explanation:
The void keyword is used as a return type for functions that do not return a value.

23. Which keyword is used to declare a constant variable in C++?

a) constant
b) const
c) final
d) static
Correct Answer: b) const
Explanation:
The const keyword ensures that the value of a variable cannot be changed after initialization.

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

a) my_var
b) _myVar
c) 1stVar
d) var1
Correct Answer: c) 1stVar
Explanation:
Variable names in C++ cannot begin with a number. They must start with a letter or an underscore.

25. What is the purpose of the sizeof operator in C++?

a) To find the length of a string
b) To determine the size of a variable or data type in bytes
c) To count the number of elements in an array
d) To determine the memory address of a variable
Correct Answer: b) To determine the size of a variable or data type in bytes
Explanation:
The sizeof operator evaluates to the number of bytes required to store a type or a variable in memory.

26. Which operator is used for addition in C++?

a) -
b) *
c) +
d) /
Correct Answer: c) +
Explanation:
The + operator is an arithmetic operator used for addition.

27. Which operator returns the remainder of an integer division?

a) /
b) %
c) //
d) mod
Correct Answer: b) %
Explanation:
The modulo operator (%) returns the remainder of a division operation.

28. What does the ++ operator do?

a) Adds two variables
b) Increments the value of a variable by 1
c) Doubles the value of a variable
d) Raises the variable to the power of 2
Correct Answer: b) Increments the value of a variable by 1
Explanation:
The increment operator (++) increases the integer value of its operand by exactly 1.

29. What is the difference between ++x and x++?

a) No difference, they behave exactly the same
b) ++x increments the value before it is used, x++ increments it after
c) x++ increments the value before it is used, ++x increments it after
d) ++x adds 2, x++ adds 1
Correct Answer: b) ++x increments the value before it is used, x++ increments it after
Explanation:
Pre-increment (++x) increments the variable and then returns the new value. Post-increment (x++) returns the original value and then increments.

30. Which of the following is the equality operator in C++?

a) =
b) ==
c) ===
d) !=
Correct Answer: b) ==
Explanation:
The == operator checks if the values of two operands are equal. The = operator is for assignment.

31. Which operator is used to check if two values are not equal?

a) <>
b) !==
c) !=
d) not=
Correct Answer: c) !=
Explanation:
The != operator is the standard relational operator in C++ for inequality checking.

32. What is the logical AND operator in C++?

a) and
b) &
c) &&
d) ||
Correct Answer: c) &&
Explanation:
The && operator is the logical AND operator. It evaluates to true only if both operands are true.

33. What is the logical OR operator in C++?

a) or
b) |
c) ||
d) &&
Correct Answer: c) ||
Explanation:
The || operator is the logical OR operator. It evaluates to true if at least one of its operands is true.

34. What is the logical NOT operator in C++?

a) !
b) ~
c) not
d) -
Correct Answer: a) !
Explanation:
The ! operator is the logical NOT operator. It reverses the logical state of its operand.

35. Which of the following is a bitwise AND operator?

a) &&
b) &
c) |
d) ^
Correct Answer: b) &
Explanation:
The single & is the bitwise AND operator, which performs a logical AND operation on each corresponding bit.

36. Which of the following represents the bitwise XOR operator?

a) ~
b) !
c) ^
d) |
Correct Answer: c) ^
Explanation:
The ^ symbol is the bitwise XOR (exclusive OR) operator in C++.

37. What does the statement x += 5 do?

a) Assigns 5 to x
b) Adds 5 to x and stores the result in x
c) Checks if x is equal to 5
d) Creates a new variable x with value 5
Correct Answer: b) Adds 5 to x and stores the result in x
Explanation:
The compound assignment operator += is shorthand for x = x + 5.

38. Which operator is known as the ternary operator in C++?

a) ::
b) ->
c) ?:
d) <<
Correct Answer: c) ?:
Explanation:
The conditional operator (?:) is the only ternary operator in C++, taking three operands.

39. What is the scope resolution operator in C++?

a) .
b) ->
c) ::
d) :
Correct Answer: c) ::
Explanation:
The scope resolution operator (::) is used to specify the context to which an identifier belongs.

40. Which statement is used to execute a block of code only if a specified condition is true?

a) if
b) while
c) for
d) switch
Correct Answer: a) if
Explanation:
The if statement evaluates a boolean condition and executes the subsequent block of code if the condition is true.

41. What is used to execute an alternative block of code when the if condition is false?

a) elseif
b) else
c) default
d) catch
Correct Answer: b) else
Explanation:
The else keyword defines a block of code to be executed when the preceding if condition evaluates to false.

42. Which control structure is used to select one of many code blocks to be executed based on the value of a variable?

a) if
b) for
c) switch
d) do-while
Correct Answer: c) switch
Explanation:
The switch statement evaluates an expression and branches to the case label matching the expression's value.

43. In a switch statement, what is the purpose of the break keyword?

a) To terminate the program
b) To skip the current iteration
c) To exit the switch block and prevent fall-through
d) To pause the execution
Correct Answer: c) To exit the switch block and prevent fall-through
Explanation:
Without a break, execution will fall through to subsequent cases. The break statement exits the switch statement entirely.

44. Which keyword specifies the block of code to execute if no cases match in a switch statement?

a) else
b) otherwise
c) finally
d) default
Correct Answer: d) default
Explanation:
The default case in a switch statement acts as a catch-all if none of the explicit case values match.

45. Which loop executes a block of code a specified number of times using an initialization, condition, and increment?

a) while loop
b) for loop
c) do-while loop
d) if loop
Correct Answer: b) for loop
Explanation:
The for loop is specifically structured with initialization, condition, and iteration statements in one line.

46. Which loop evaluates its condition before executing its code block?

a) do-while loop
b) switch loop
c) while loop
d) goto loop
Correct Answer: c) while loop
Explanation:
A while loop checks its condition at the beginning. If the condition is false initially, the loop body never executes.

47. Which loop guarantees that its code block will be executed at least once?

a) for loop
b) while loop
c) do-while loop
d) nested loop
Correct Answer: c) do-while loop
Explanation:
A do-while loop evaluates its condition at the end of the iteration, meaning the body executes at least once.

48. Which statement forces an immediate exit from a loop?

a) exit
b) return
c) continue
d) break
Correct Answer: d) break
Explanation:
The break statement terminates the innermost enclosing loop or switch immediately.

49. Which statement skips the remaining code in the current loop iteration and moves to the next iteration?

a) skip
b) continue
c) next
d) break
Correct Answer: b) continue
Explanation:
The continue statement stops execution of the current iteration and jumps back to evaluate the loop's condition or increment.

50. What is an infinite loop?

a) A loop with too many variables
b) A loop that never terminates because its condition is never false
c) A loop that runs exactly 1000 times
d) A loop without a body
Correct Answer: b) A loop that never terminates because its condition is never false
Explanation:
An infinite loop occurs when the terminating condition is never met or the condition is explicitly set to always be true.

51. Which of the following is the correct syntax to create an infinite for loop?

a) for(;;)
b) for(true)
c) for()
d) for(1 to infinity)
Correct Answer: a) for(;;)
Explanation:
In a for loop, omitting the initialization, condition, and increment leaves for(;;). An empty condition evaluates to true.

52. What is a function in C++?

a) A data type
b) A block of code designed to perform a particular task
c) A type of loop
d) A preprocessor directive
Correct Answer: b) A block of code designed to perform a particular task
Explanation:
Functions help organize code into reusable blocks that execute a specific task when called.

53. What does a function signature in C++ consist of?

a) The function name and return type
b) The function name and parameter list
c) The return type and parameter list
d) The function body
Correct Answer: b) The function name and parameter list
Explanation:
In C++, a function's signature includes its name and the sequence of its parameter types. The return type is not part of the signature.

54. What is the keyword used to return a value from a function?

a) give
b) yield
c) return
d) value
Correct Answer: c) return
Explanation:
The return keyword terminates function execution and specifies the value to be returned to the caller.

55. If a function is defined as int sum(int a, int b), what must it return?

a) A floating-point number
b) An integer
c) Nothing
d) A character
Correct Answer: b) An integer
Explanation:
The int preceding the function name dictates that the function must return an integer value.

56. What happens when parameters are passed by value to a function?

a) The original variable is modified
b) A copy of the value is passed, so the original variable is unchanged
c) The memory address is passed
d) A reference to the variable is passed
Correct Answer: b) A copy of the value is passed, so the original variable is unchanged
Explanation:
Pass by value means the function works with a copy of the argument. Changes made inside the function do not affect the original variable.

57. How do you specify that a parameter should be passed by reference in C++?

a) By using the * operator before the parameter name
b) By using the & operator before the parameter name
c) By using the ref keyword
d) By wrapping the parameter in quotes
Correct Answer: b) By using the & operator before the parameter name
Explanation:
The ampersand (&) in the parameter list indicates that the variable is passed by reference.

58. What are default arguments in a C++ function?

a) Arguments that cannot be changed
b) Values automatically provided if the caller does not supply an argument
c) Arguments that are passed by default to every function
d) The first argument of any function
Correct Answer: b) Values automatically provided if the caller does not supply an argument
Explanation:
Default arguments allow a function to be called with fewer arguments than declared. The compiler fills in missing arguments with default values.

59. In what order must default arguments be assigned in a function declaration?

a) From left to right
b) From right to left
c) In any order
d) Only the first parameter can be default
Correct Answer: b) From right to left
Explanation:
Default arguments must be specified from right to left. If a parameter has a default argument, all parameters to its right must also have default arguments.

60. What is function overloading in C++?

a) Writing functions that consume too much memory
b) Multiple functions with the same name but different parameters
c) Multiple functions with different names doing the same thing
d) Defining a function inside another function
Correct Answer: b) Multiple functions with the same name but different parameters
Explanation:
Function overloading allows you to use the same function name for multiple functions, provided they have a different number or type of parameters.

61. Can two functions be overloaded if they differ only in their return type?

a) Yes
b) No
c) Yes, but only if one is void
d) Yes, but only in classes
Correct Answer: b) No
Explanation:
C++ does not allow function overloading based solely on return type because the compiler cannot determine which function to call if the return value is ignored.

62. What is an inline function in C++?

a) A function that is executed sequentially
b) A function whose body is inserted at the place where it is called, to reduce function call overhead
c) A function that only contains one line of code
d) A function inside a loop
Correct Answer: b) A function whose body is inserted at the place where it is called, to reduce function call overhead
Explanation:
The inline keyword requests the compiler to expand the function code at the call site to eliminate the overhead of a function call.

63. What is an array in C++?

a) A collection of variables of different data types
b) A collection of variables of the same data type stored in contiguous memory locations
c) A dynamic list of functions
d) A single variable that can hold infinite values
Correct Answer: b) A collection of variables of the same data type stored in contiguous memory locations
Explanation:
An array is a fixed-size sequential collection of elements of the same type stored in contiguous memory blocks.

64. What is the index of the first element in a C++ array?

a) 1
b) -1
c) 0
d) Empty
Correct Answer: c) 0
Explanation:
In C++, array indexing is zero-based, meaning the first element is accessed using index 0.

65. How do you declare an array of 5 integers in C++?

a) int arr(5);
b) int arr[5];
c) array int[5];
d) int[5] arr;
Correct Answer: b) int arr[5];
Explanation:
The correct syntax places the size inside square brackets after the variable name: type arrayName[arraySize];.

66. What happens if you access an array out of its bounds in C++?

a) A compile-time error occurs
b) The program safely returns null
c) Undefined behavior, which may cause a crash or garbage value to be read/written
d) The array automatically expands
Correct Answer: c) Undefined behavior, which may cause a crash or garbage value to be read/written
Explanation:
C++ does not perform bounds checking on arrays. Accessing out of bounds leads to undefined behavior.

67. Which of the following defines a 2D array of integers with 3 rows and 4 columns?

a) int arr[4][3];
b) int arr(3,4);
c) int arr[3][4];
d) int arr{3}{4};
Correct Answer: c) int arr[3][4];
Explanation:
A 2D array is declared using two sets of square brackets: type name[rows][columns].

68. What does the name of an array (without brackets) represent in C++?

a) The value of the first element
b) The size of the array
c) The memory address of the first element
d) The entire array structure
Correct Answer: c) The memory address of the first element
Explanation:
An array name decays into a pointer to its first element in most contexts, holding the base memory address of the array.

69. In C-style strings, what character is used to mark the end of the string?

a) New line
b) Null character
c) Tab character
d) End of file
Correct Answer: b) Null character
Explanation:
C-style strings are null-terminated, meaning they end with a special null character to signify the string's conclusion.

70. How much memory does a C-style string 'cat' occupy?

a) 3 bytes
b) 4 bytes
c) 8 bytes
d) Depends on the system
Correct Answer: b) 4 bytes
Explanation:
It takes 4 bytes: 3 bytes for the characters 'c', 'a', 't', and 1 byte for the implicit null terminator.

71. Which standard library class provides a safer and more flexible way to handle strings in C++?

a) cstring
b) char array
c) std::string
d) StringArray
Correct Answer: c) std::string
Explanation:
The std::string class from the standard library is the standard and recommended way to manage dynamic strings in C++.

72. What is a pointer in C++?

a) A variable that stores an integer value
b) A variable that stores the memory address of another variable
c) A keyword used to point to the main function
d) An operator used for addition
Correct Answer: b) A variable that stores the memory address of another variable
Explanation:
A pointer is a variable whose value is the physical address of another variable in memory.

73. Which operator is used to obtain the memory address of a variable?

a) *
b) &&
c) &
d) ->
Correct Answer: c) &
Explanation:
The address-of operator (&) precedes a variable to return its memory address.

74. Which operator is used to declare a pointer variable?

a) &
b) *
c) ->
d) ::
Correct Answer: b) *
Explanation:
The asterisk (*) is used in a declaration to indicate that the variable is a pointer.

75. Which operator is used to access the value stored at the memory address pointed to by a pointer?

a) &
b) ->
c) *
d) ::
Correct Answer: c) *
Explanation:
The dereference operator (*) accesses the value located at the memory address held by the pointer.

76. What is a null pointer?

a) A pointer pointing to a freed memory location
b) A pointer that does not point to any valid memory location
c) A pointer storing the string null
d) A pointer pointing to index 0 of an array
Correct Answer: b) A pointer that does not point to any valid memory location
Explanation:
A null pointer is explicitly assigned a value of 0 or nullptr to indicate it is not pointing to any valid object or function.

77. Which keyword was introduced in C++11 to represent a null pointer constant?

a) NULL
b) null
c) nullptr
d) None
Correct Answer: c) nullptr
Explanation:
The nullptr keyword was introduced in C++11 as a type-safe null pointer constant, replacing the macro NULL.

78. What is a dangling pointer?

a) A pointer pointing to a null address
b) A pointer pointing to a memory location that has been deallocated or freed
c) An uninitialized pointer
d) A pointer that points to itself
Correct Answer: b) A pointer pointing to a memory location that has been deallocated or freed
Explanation:
Dangling pointers arise when an object is deleted or goes out of scope, but the pointer still holds the address of that freed memory.

79. What is the primary difference between a pointer and a reference in C++?

a) References can be reassigned to point to different objects, pointers cannot
b) Pointers can be null, but references must be initialized to a valid object and cannot be null
c) Pointers are safer to use than references
d) References consume more memory than pointers
Correct Answer: b) Pointers can be null, but references must be initialized to a valid object and cannot be null
Explanation:
A reference acts as an alias and must be bound to an existing object upon creation, meaning it cannot logically be null, unlike pointers.

80. How do you declare a reference variable r to an existing integer variable x?

a) int *r = x;
b) int r = &x;
c) int &r = x;
d) ref int r = x;
Correct Answer: c) int &r = x;
Explanation:
The & symbol in a declaration specifies a reference. It makes r an alias for x.

81. Which operator is used to allocate dynamic memory in C++?

a) alloc
b) malloc
c) new
d) create
Correct Answer: c) new
Explanation:
The new operator allocates memory on the heap and calls the constructor if an object is being created.

82. What does the new operator return?

a) The value of the allocated memory
b) A pointer to the beginning of the allocated memory block
c) A reference to the memory block
d) The size of the allocated memory
Correct Answer: b) A pointer to the beginning of the allocated memory block
Explanation:
The new operator requests memory from the OS and returns a pointer holding the address of the allocated block.

83. Which operator is used to free dynamically allocated memory in C++?

a) free
b) remove
c) clear
d) delete
Correct Answer: d) delete
Explanation:
The delete operator deallocates memory previously allocated by new, preventing memory leaks.

84. How do you deallocate an array that was dynamically allocated using new int[10]?

a) delete ptr;
b) delete[] ptr;
c) free(ptr);
d) delete(ptr);
Correct Answer: b) delete[] ptr;
Explanation:
When memory is allocated for an array using new[], it must be released using the array delete operator delete[].

85. What happens if dynamically allocated memory is not freed using delete?

a) The program automatically frees it upon exit
b) A memory leak occurs
c) A segmentation fault immediately occurs
d) The compiler issues a warning
Correct Answer: b) A memory leak occurs
Explanation:
If delete is not called, the memory remains occupied even after it is no longer used, resulting in a memory leak.

86. Which memory segment is used for dynamic memory allocation (using new)?

a) Stack
b) Heap
c) Data Segment
d) Code Segment
Correct Answer: b) Heap
Explanation:
Dynamic memory allocation at runtime happens in the Heap memory segment.

87. Where are local variables and function parameters typically stored in memory?

a) Stack
b) Heap
c) BSS Segment
d) Registers
Correct Answer: a) Stack
Explanation:
The Stack memory segment is used for static memory allocation, managing local variables, and function call frames.

88. What is a struct in C++?

a) A function that builds classes
b) A user-defined data type that groups different variables under a single name
c) A built-in data type for strings
d) A keyword to start a loop
Correct Answer: b) A user-defined data type that groups different variables under a single name
Explanation:
A structure (struct) allows you to combine data items of different kinds into a single custom type.

89. By default, the members of a struct in C++ are:

a) private
b) protected
c) public
d) static
Correct Answer: c) public
Explanation:
Unlike classes where members are private by default, all members of a struct are public by default in C++.

90. How do you access a member named age of a struct variable person?

a) person->age
b) person::age
c) person.age
d) person(age)
Correct Answer: c) person.age
Explanation:
The dot operator is used to access members of a struct or class instance directly.

91. How do you access a member named age of a struct through a pointer ptr pointing to a struct instance?

a) ptr.age
b) ptr->age
c) ptr::age
d) *ptr.age
Correct Answer: b) ptr->age
Explanation:
The arrow operator (->) is used to access members of a struct or class via a pointer.

92. What is an enum in C++?

a) A loop type
b) A floating-point variable
c) A user-defined data type consisting of integral constants
d) A memory allocation function
Correct Answer: c) A user-defined data type consisting of integral constants
Explanation:
An enumeration (enum) assigns names to integer constants to make a program easier to read and maintain.

93. If enum Color { RED, GREEN, BLUE }; is declared, what is the default integer value of GREEN?

a) 0
b) 1
c) 2
d) Undefined
Correct Answer: b) 1
Explanation:
By default, enums start at 0 and increment by 1. RED is 0, GREEN is 1, and BLUE is 2.

94. Which of the following is a feature of C++ that C does not have?

a) Pointers
b) Functions
c) Classes and Objects
d) Structs
Correct Answer: c) Classes and Objects
Explanation:
C++ introduced Object-Oriented Programming (OOP) features like Classes and Objects, which are absent in C.

95. What is a class in C++?

a) A blueprint or template for creating objects
b) A specialized integer type
c) A memory management tool
d) A preprocessor command
Correct Answer: a) A blueprint or template for creating objects
Explanation:
A class defines a new data type containing data members and member functions. Objects are instances of a class.

96. By default, the members of a class in C++ are:

a) public
b) private
c) protected
d) virtual
Correct Answer: b) private
Explanation:
In a C++ class, if no access specifier is provided, all members default to private.

97. What is an object in C++?

a) A variable of type class
b) A function
c) A header file
d) A basic data type
Correct Answer: a) A variable of type class
Explanation:
An object is an instantiation of a class. It holds the data and methods defined by its class blueprint.

98. Which access specifier allows class members to be accessed from outside the class?

a) private
b) protected
c) public
d) internal
Correct Answer: c) public
Explanation:
Public members can be accessed from anywhere the object is visible.

99. What is a constructor in C++?

a) A function that destroys an object
b) A special member function automatically called when an object is instantiated
c) A variable that holds the class size
d) A keyword for inheritance
Correct Answer: b) A special member function automatically called when an object is instantiated
Explanation:
Constructors have the same name as the class and initialize objects when they are created.

100. What is the return type of a constructor?

a) void
b) int
c) bool
d) Constructors do not have a return type
Correct Answer: d) Constructors do not have a return type
Explanation:
Constructors are unique because they do not have any return type, not even void.

101. What is a destructor in C++?

a) A function to delete variables
b) A special member function automatically called when an object goes out of scope or is deleted
c) A compiler error
d) A memory allocation function
Correct Answer: b) A special member function automatically called when an object goes out of scope or is deleted
Explanation:
Destructors clean up resources before an object is destroyed. They have the class name preceded by a tilde.

102. Which character precedes a destructor name?

a) &
b) *
c) ~
d) !
Correct Answer: c) ~
Explanation:
A tilde precedes the class name to declare a destructor.

103. What does the static keyword do to a local variable inside a function?

a) Makes it accessible globally
b) Preserves its value between function calls
c) Prevents it from being modified
d) Deletes it immediately after use
Correct Answer: b) Preserves its value between function calls
Explanation:
A static local variable is initialized only once and retains its value across multiple calls to the function.
← Previous: C++ Variables & Data Types MCQs
New
No Image

C++ Templates - MCQs

1. Who is the creator of the C++ programming language? a) Dennis Ritchie b) Bjarne Stroustrup c) James Gosling d)…

By MCQs Generator
NewC++ Variables & Data Types MCQs

C++ Variables & Data Types MCQs

Variables and data types form the core building blocks of any C++ application, defining how data is stored, represented, and…

By MCQs Generator
New
No Image

C++ Inheritance and Polymorphism - MCQs

1. Who is the creator of the C++ programming language? a) Dennis Ritchie b) Bjarne Stroustrup c) James Gosling d)…

By MCQs Generator