C++ Variables & Data Types MCQs

1 min read

Variables and data types form the core building blocks of any C++ application, defining how data is stored, represented, and manipulated in memory. C++ is a statically typed language, meaning every variable must be declared with a specific data type (such as int, float, char, or bool) before use. Understanding fundamental types, modifiers (signed, unsigned, short, long), type casting mechanisms, and modern C++ type deduction (auto) is essential for writing efficient, type-safe, and high-performance software.

1. Which of the following is a fundamental data type in C++?

a) String
b) Array
c) int
d) Class
Correct Answer: c) int
Explanation:
In C++, 'int' is a built-in fundamental data type used to store integer values.

2. What is the guaranteed size of the 'char' data type in C++?

a) 1 byte
b) 2 bytes
c) 4 bytes
d) Compiler dependent
Correct Answer: a) 1 byte
Explanation:
The C++ standard guarantees that the size of a 'char' is exactly 1 byte.

3. Which keyword is used to represent boolean values in C++?

a) boolean
b) bool
c) Bool
d) bit
Correct Answer: b) bool
Explanation:
The 'bool' keyword is used in C++ to declare boolean variables holding true or false.

4. What does the 'void' data type indicate in C++?

a) An empty set of values
b) An integer value
c) A memory address
d) A generic object
Correct Answer: a) An empty set of values
Explanation:
The 'void' type specifies that no value is available, often used for functions returning nothing.

5. Which data type is used for storing double-precision floating-point numbers?

a) float
b) double
c) decimal
d) long float
Correct Answer: b) double
Explanation:
The 'double' data type provides double-precision floating-point numbers, offering more precision than 'float'.

6. Which header file is required to use the std::string type?

a)
b)
c)
d)
Correct Answer: b)
Explanation:
The header must be included to use the C++ Standard Library std::string class.

7. What is the typical size of a 'float' variable on a 32-bit/64-bit architecture?

a) 2 bytes
b) 4 bytes
c) 8 bytes
d) 16 bytes
Correct Answer: b) 4 bytes
Explanation:
In most common architectures, a 'float' typically occupies 4 bytes (32 bits) in memory.

8. Which of the following generally determines the size of integer data types in C++?

a) The language standard only
b) The compiler and system architecture
c) The code editor
d) The runtime environment
Correct Answer: b) The compiler and system architecture
Explanation:
Aside from 'char', the exact sizes of types like 'int' depend on the compiler and hardware architecture.

9. What operator is used to find the size of a data type or variable in bytes?

a) size()
b) length()
c) sizeof()
d) bytes()
Correct Answer: c) sizeof()
Explanation:
The 'sizeof' operator evaluates to the size, in bytes, of a specific variable or data type.

10. Which character type is commonly used to store wide characters in C++?

a) char
b) wchar_t
c) wide_char
d) string16
Correct Answer: b) wchar_t
Explanation:
'wchar_t' is the wide-character type used to represent characters requiring more memory, like Unicode.

11. Which type modifier can be applied to both 'int' and 'char'?

a) short
b) long
c) unsigned
d) double
Correct Answer: c) unsigned
Explanation:
The 'unsigned' modifier can be applied to both integer and character types to restrict them to non-negative values.

12. What is the typical size of a 'short int'?

a) 1 byte
b) 2 bytes
c) 4 bytes
d) 8 bytes
Correct Answer: b) 2 bytes
Explanation:
A 'short int' typically takes up 2 bytes of memory, storing values up to 32,767 (if signed).

13. What is the maximum value of an 8-bit unsigned char?

a) 127
b) 128
c) 255
d) 256
Correct Answer: c) 255
Explanation:
An 8-bit unsigned char holds values from 0 to (2^8 - 1), which equals 255.

14. What happens if you assign a negative value to an unsigned integer variable?

a) Compilation error
b) Runtime exception
c) The value becomes zero
d) The value wraps around to a large positive number
Correct Answer: d) The value wraps around to a large positive number
Explanation:
Assigning a negative value to an unsigned integer causes an underflow, wrapping around to a large positive value.

15. Which of the following is NOT a valid data type modifier in C++?

a) signed
b) unsigned
c) short
d) medium
Correct Answer: d) medium
Explanation:
C++ supports 'short', 'long', 'signed', and 'unsigned'. 'medium' does not exist.

16. Which header file provides constants for the limits of integer types?

a)
b)
c)
d)
Correct Answer: b)
Explanation:
The header contains macros that define the minimum and maximum limits of integer types.

17. Which data type was standardized in C++11 to guarantee at least 64 bits?

a) long
b) long double
c) long long int
d) int64
Correct Answer: c) long long int
Explanation:
C++11 standardized 'long long int' to ensure a minimum width of 64 bits across platforms.

18. Can the 'unsigned' modifier be applied to standard floating-point types?

a) Yes, always
b) No, it is a compilation error
c) Yes, but it has no effect
d) Yes, only in C++11
Correct Answer: b) No, it is a compilation error
Explanation:
The 'unsigned' modifier is strictly for integral types. Applying it to 'float' or 'double' causes a compilation error.

19. Is the plain 'char' type signed or unsigned by default?

a) Signed
b) Unsigned
c) Implementation-defined
d) Neither
Correct Answer: c) Implementation-defined
Explanation:
The C++ standard leaves whether plain 'char' is signed or unsigned up to the compiler implementation.

20. Which combination of modifiers is syntactically invalid?

a) unsigned long int
b) signed short int
c) long short int
d) long long int
Correct Answer: c) long short int
Explanation:
You cannot combine 'long' and 'short' together on the same variable, as they are conflicting size requirements.

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

a) 1stPlace
b) *myVar
c) my-var
d) my var
Correct Answer: b) *myVar
Explanation:
Variables can start with an underscore or letter. They cannot start with a number, contain spaces, or use hyphens.

22. Are variable names in C++ case-sensitive?

a) Yes
b) No
c) Only for global variables
d) Only for constants
Correct Answer: a) Yes
Explanation:
C++ is highly case-sensitive. Variables named 'count' and 'Count' are treated as completely distinct identifiers.

23. Which of the following cannot be used as a variable name?

a) main
b) String
c) class
d) count
Correct Answer: c) class
Explanation:
'class' is a reserved keyword in C++ and cannot be used as a programmer-defined identifier.

24. What happens if a variable name starts with two consecutive underscores?

a) It is a syntax error
b) It is reserved for compiler implementations
c) It is perfectly normal
d) It makes the variable static
Correct Answer: b) It is reserved for compiler implementations
Explanation:
Identifiers containing double underscores are reserved for the C++ standard library and compiler usage.

25. What is the maximum strict limit for the length of a variable name in the C++ standard?

a) 31 characters
b) 63 characters
c) 255 characters
d) No strict standard limit
Correct Answer: d) No strict standard limit
Explanation:
The C++ standard defines no strict maximum length for identifiers, though individual compilers might impose practical limits.

26. Which symbol is allowed within a C++ variable name?

a) $
b) @
c) *
d) #
Correct Answer: c) *
Explanation:
Only letters, digits, and underscores (*) are permitted in standard C++ variable names.

27. Is 'int return = 5;' a valid C++ statement?

a) Yes
b) No, because 'return' is a keyword
c) No, because 5 is not an int
d) Yes, but only locally
Correct Answer: b) No, because 'return' is a keyword
Explanation:
Keywords are reserved words in C++ and cannot be used as variable names.

28. Which of these is considered standard naming convention for local variables in modern C++?

a) camelCase or snake_case
b) PascalCase
c) UPPER_CASE
d) kebab-case
Correct Answer: a) camelCase or snake_case
Explanation:
camelCase and snake_case are standard for variables, while PascalCase is used for classes, and UPPER_CASE for macros.

29. Why is 'int num-ber = 10;' invalid?

a) Hyphens are parsed as the subtraction operator
b) Number is a reserved word
c) Variables cannot be assigned immediately
d) It lacks a pointer
Correct Answer: a) Hyphens are parsed as the subtraction operator
Explanation:
Hyphens are interpreted by the compiler as the minus (subtraction) operator, making them invalid inside identifier names.

30. Can a variable name consist of only a single underscore '* '?

a) No, it must contain letters
b) No, it is a syntax error
c) Yes, it is a valid identifier
d) Yes, but only for integers
Correct Answer: c) Yes, it is a valid identifier
Explanation:
A single underscore is syntactically a valid identifier, though generally avoided for clarity.

31. What is the value of an uninitialized local integer variable?

a) 0
b) Garbage value
c) NULL
d) -1
Correct Answer: b) Garbage value
Explanation:
Uninitialized local variables contain whatever raw data was previously left at their memory location, known as a garbage value.

32. What is the default value of an uninitialized global integer variable?

a) 0
b) Garbage value
c) NULL
d) -1
Correct Answer: a) 0
Explanation:
Global and static variables are zero-initialized by default before main() runs.

33. What does the statement 'extern int a;' do?

a) Defines a variable 'a'
b) Allocates memory for 'a'
c) Declares variable 'a' without defining it
d) Initializes 'a' to zero
Correct Answer: c) Declares variable 'a' without defining it
Explanation:
The 'extern' keyword tells the compiler that the variable 'a' exists and its memory allocation is in another translation unit.

34. Which of the following demonstrates C++11 uniform (list) initialization?

a) int x = 5;
b) int x(5);
c) int x{5};
d) int x = (5);
Correct Answer: c) int x{5};
Explanation:
Using curly braces {} for initialization is known as list initialization or uniform initialization, introduced in C++11.

35. What is a major safety advantage of brace initialization 'int x{5.5};'?

a) It is faster
b) It prevents narrowing conversions by throwing an error
c) It automatically rounds the number
d) It uses less heap space
Correct Answer: b) It prevents narrowing conversions by throwing an error
Explanation:
Brace initialization forbids narrowing conversions; trying to initialize an int with a float via braces causes a compilation error.

36. Which of the following is a valid multiple declaration?

a) int a, b = 10;
b) int a; float b;
c) int a = 10; b = 10;
d) int a = b = 10;
Correct Answer: a) int a, b = 10;
Explanation:
Multiple variables of the same type can be declared on a single line by separating them with commas.

37. What is an L-value in C++?

a) A value strictly on the right side of an assignment
b) An expression that refers to an identifiable memory location
c) A temporary literal value
d) A deprecated variable
Correct Answer: b) An expression that refers to an identifiable memory location
Explanation:
An L-value represents an object that occupies an identifiable location in memory, allowing it to appear on the left side of an assignment.

38. What is an R-value in C++?

a) A persistent memory location
b) A variable holding an array
c) A temporary value that does not persist beyond the expression
d) A pointer variable
Correct Answer: c) A temporary value that does not persist beyond the expression
Explanation:
An R-value is typically a temporary literal or expression result that does not have a persistent memory address.

39. In the statement 'int x = y;', 'y' must be declared before this line. True or False?

a) True
b) False
c) Only if y is an object
d) Depends on the compiler
Correct Answer: a) True
Explanation:
C++ strictly requires all identifiers (variables) to be declared before they are used in any expression.

40. Which initialization syntax is explicitly known as direct initialization?

a) int x = 5;
b) int x(5);
c) int x{5};
d) int x = {5};
Correct Answer: b) int x(5);
Explanation:
Direct initialization uses parentheses to initialize a variable, invoking its constructor directly.

41. What is the scope of a standard local variable?

a) The entire program
b) The file it is declared in
c) The block or function it is declared in
d) The class it belongs to
Correct Answer: c) The block or function it is declared in
Explanation:
Local variables are only accessible within the specific code block {} or function where they are defined.

42. Which operator allows accessing a global variable when a local variable has the exact same name?

a) ->
b) .
c) ::
d) &
Correct Answer: c) ::
Explanation:
The scope resolution operator (::) tells the compiler to access the global scope version of the variable.

43. What is the lifetime of a variable declared with the 'static' keyword inside a function?

a) It dies when the function returns
b) It persists for the duration of the entire program
c) It resets every time the function is called
d) It is dynamically allocated
Correct Answer: b) It persists for the duration of the entire program
Explanation:
A local static variable is initialized once and retains its value between function calls until the program terminates.

44. What happens when an inner block declares a variable with the same name as a variable in an outer block?

a) Compilation error
b) The outer variable overwrites the inner one
c) The inner variable shadows (hides) the outer variable
d) They share the same memory
Correct Answer: c) The inner variable shadows (hides) the outer variable
Explanation:
Variable shadowing occurs when a variable in an inner scope has the same name as one in an outer scope, temporarily hiding it.

45. What defines a global variable?

a) A variable defined inside main()
b) A variable defined outside of all functions
c) A variable passed across multiple files via pointer
d) A variable with the volatile keyword
Correct Answer: b) A variable defined outside of all functions
Explanation:
Global variables are defined outside any function or block, making them globally accessible to the program.

46. What is the scope of a 'static' global variable?

a) The entire program
b) Only the file it is declared in
c) Only the function it is used in
d) It has no scope
Correct Answer: b) Only the file it is declared in
Explanation:
When 'static' is applied to a global variable, it gives it internal linkage, making it visible only within its own translation unit.

47. Variables dynamically allocated using the 'new' keyword are stored where?

a) The Stack
b) The Data Segment
c) The Heap
d) The Text Segment
Correct Answer: c) The Heap
Explanation:
Dynamic memory allocation using 'new' creates variables on the heap (free store).

48. Automatic variables created normally inside a function are stored where?

a) The Stack
b) The Heap
c) The BSS Segment
d) CPU Registers only
Correct Answer: a) The Stack
Explanation:
Automatic local variables are pushed onto the call stack and popped when they go out of scope.

49. What did the 'register' keyword originally suggest to the compiler?

a) Store the variable on the heap
b) Store the variable in CPU registers for fast access
c) Make the variable read-only
d) Share the variable between threads
Correct Answer: b) Store the variable in CPU registers for fast access
Explanation:
Historically, 'register' requested the compiler to store the variable in a CPU register for speed (deprecated in modern C++).

50. Which keyword specifies that each thread has its own separate instance of a variable?

a) static
b) volatile
c) thread_local
d) extern
Correct Answer: c) thread_local
Explanation:
The 'thread_local' keyword specifies thread storage duration, creating a unique instance of the variable per thread.

51. What does the 'const' keyword do to a variable?

a) Makes it global
b) Prevents it from being modified after initialization
c) Allocates it dynamically
d) Prevents it from being cached
Correct Answer: b) Prevents it from being modified after initialization
Explanation:
'const' creates a constant value, causing a compile error if code attempts to modify it.

52. When must a 'const' variable be initialized?

a) Any time before use
b) Exactly at the time of its declaration
c) Inside a constructor only
d) It does not need initialization
Correct Answer: b) Exactly at the time of its declaration
Explanation:
Because a const variable cannot be assigned a value later, it must be initialized exactly when declared.

53. What is the primary difference between 'const' and 'constexpr'?

a) They are identical
b) constexpr is evaluated at compile-time; const can be runtime
c) const is compile-time; constexpr is runtime
d) constexpr is only for classes
Correct Answer: b) constexpr is evaluated at compile-time; const can be runtime
Explanation:
'constexpr' guarantees that the variable is evaluated at compile time, while 'const' just guarantees immutability, even if initialized at runtime.

54. What does 'const int* ptr' mean?

a) The pointer itself cannot change
b) The integer being pointed to cannot be modified through this pointer
c) Both are constant
d) Invalid syntax
Correct Answer: b) The integer being pointed to cannot be modified through this pointer
Explanation:
This is a pointer to a constant integer. The memory address can change, but the value it points to cannot be altered via the pointer.

55. What does 'int* const ptr' mean?

a) The pointer itself cannot change, but the value it points to can
b) The integer cannot be modified
c) Both are constant
d) Invalid syntax
Correct Answer: a) The pointer itself cannot change, but the value it points to can
Explanation:
This is a constant pointer. The address it holds is locked, but the actual integer data can be modified.

56. Which of the following is NOT an advantage of 'const' over '#define'?

a) const respects scope
b) const is type-safe
c) const is evaluated by the compiler, not the preprocessor
d) #define cannot handle strings
Correct Answer: d) #define cannot handle strings
Explanation:
#define can handle string literals easily. The other statements are true advantages of using const.

57. What does the 'volatile' keyword signify?

a) The variable will crash the program
b) The variable's value may change externally, so the compiler shouldn't optimize it
c) It forces storage in cache
d) It prevents thread access
Correct Answer: b) The variable's value may change externally, so the compiler shouldn't optimize it
Explanation:
'volatile' warns the compiler that a variable might be modified by hardware or other threads, preventing aggressive caching optimizations.

58. Which keyword allows a class member to be modified even if the class instance is 'const'?

a) volatile
b) mutable
c) static
d) extern
Correct Answer: b) mutable
Explanation:
The 'mutable' keyword overrides 'const', allowing specific members to be modified inside a constant object.

59. By default, what linkage does a global 'const' variable have in C++?

a) External linkage
b) Internal linkage
c) No linkage
d) Dynamic linkage
Correct Answer: b) Internal linkage
Explanation:
In C++, global const variables have internal linkage by default, meaning they are restricted to their own translation unit unless declared extern.

60. Which specific C++ cast is used to cast away the 'const' qualifier?

a) const_cast
b) static_cast
c) dynamic_cast
d) reinterpret_cast
Correct Answer: a) const_cast
Explanation:
'const_cast' is uniquely designed to strip or add 'const' and 'volatile' qualifiers from pointers or references.

61. What happens when you assign a 'float' value to an 'int' variable?

a) It rounds to the nearest integer
b) The fractional part is simply truncated
c) Compilation error
d) Crash at runtime
Correct Answer: b) The fractional part is simply truncated
Explanation:
Implicit conversion from float/double to int strictly truncates (drops) the decimal portion without rounding.

62. Which casting operator is used for standard, compile-time type conversions?

a) dynamic_cast
b) const_cast
c) reinterpret_cast
d) static_cast
Correct Answer: d) static_cast
Explanation:
'static_cast' performs standard conversions (like float to int) and checks safety at compile time.

63. Which cast is designed specifically for safe downcasting in class hierarchies with virtual functions?

a) dynamic_cast
b) static_cast
c) reinterpret_cast
d) const_cast
Correct Answer: a) dynamic_cast
Explanation:
'dynamic_cast' performs runtime type checking to safely cast pointers/references within polymorphic class hierarchies.

64. What happens if a 'dynamic_cast' fails on a pointer?

a) It throws an exception
b) It returns nullptr
c) It causes a segmentation fault
d) It returns a void pointer
Correct Answer: b) It returns nullptr
Explanation:
If a dynamic_cast fails when casting pointers, it safely returns nullptr. (If it fails on a reference, it throws std::bad_cast).

65. Which cast is the most dangerous, converting between unrelated pointers or integer types?

a) static_cast
b) dynamic_cast
c) reinterpret_cast
d) const_cast
Correct Answer: c) reinterpret_cast
Explanation:
'reinterpret_cast' overrides the type system, interpreting the raw memory as a completely different type.

66. What is integer promotion in C++?

a) Converting float to int
b) Automatically converting smaller integer types to 'int' for arithmetic
c) Making an integer global
d) Upgrading to a 64-bit OS
Correct Answer: b) Automatically converting smaller integer types to 'int' for arithmetic
Explanation:
Types smaller than int (like char or short) are implicitly promoted to int before performing arithmetic operations.

67. When casting an integer to a boolean, what does the number 0 become?

a) true
b) false
c) undefined
d) null
Correct Answer: b) false
Explanation:
In C++, the integer 0 implicitly converts to the boolean value 'false'.

68. When casting an integer to a boolean, what does the number -5 become?

a) true
b) false
c) undefined
d) Error
Correct Answer: a) true
Explanation:
Any non-zero integer (positive or negative) implicitly converts to 'true'.

69. What is '(int)x' an example of?

a) Implicit casting
b) Explicit C-style casting
c) Dynamic casting
d) Promotion
Correct Answer: b) Explicit C-style casting
Explanation:
Using parentheses with a type name before a variable is the syntax for an explicit C-style cast.

70. What does the 'typeid' operator do?

a) Returns a std::type_info object containing type information at runtime
b) Casts a type safely
c) Gets the size of a type
d) Creates a new type alias
Correct Answer: a) Returns a std::type_info object containing type information at runtime
Explanation:
'typeid' is part of RTTI (Run-Time Type Information) and returns type details of an expression.

71. What does the 'auto' keyword do in modern C++?

a) Creates an automatic variable in registers
b) Deduces the type of a variable from its initializer at compile time
c) Initializes to zero automatically
d) Frees memory automatically
Correct Answer: b) Deduces the type of a variable from its initializer at compile time
Explanation:
'auto' tells the compiler to deduce the variable's type based on the right-hand side expression.

72. Can you declare a variable with 'auto' without initializing it (e.g., auto x;)?

a) Yes
b) No
c) Only for global variables
d) Only for pointers
Correct Answer: b) No
Explanation:
Because 'auto' deduces the type from the initialization value, the variable must be initialized immediately.

73. If 'int x = 5;', what type does 'auto y = &x;' deduce?

a) int
b) auto
c) int*
d) int&
Correct Answer: c) int*
Explanation:
The '&' operator takes the address, returning a pointer. Thus, 'auto' deduces 'int*'.

74. Which keyword inspects the exact type of an expression without evaluating it?

a) typeid
b) typeof
c) auto
d) decltype
Correct Answer: d) decltype
Explanation:
'decltype' determines the exact type of an expression at compile time without executing the expression itself.

75. What is 'nullptr' introduced in C++11?

a) A macro defined as 0
b) A function that deletes pointers
c) A keyword representing a type-safe null pointer literal
d) A generic string type
Correct Answer: c) A keyword representing a type-safe null pointer literal
Explanation:
'nullptr' provides a type-safe pointer constant (of type std::nullptr_t), unlike NULL which is just an integer 0.

76. Which data type was introduced specifically for UTF-16 character encoding?

a) char16_t
b) wchar_t
c) string16
d) utf16_char
Correct Answer: a) char16_t
Explanation:
C++11 added 'char16_t' to explicitly support UTF-16 Unicode character representation.

77. Does 'auto' retain top-level 'const' qualifiers by default?

a) Yes
b) No
c) Only for pointers
d) Only for classes
Correct Answer: b) No
Explanation:
When copying a value, 'auto' strips top-level const qualifiers. You must write 'const auto' to keep it constant.

78. What feature allows unpacking tuples or structs into multiple variables easily (C++17)?

a) Template packing
b) Structured bindings
c) Multiple initialization
d) Tuple cracking
Correct Answer: b) Structured bindings
Explanation:
Structured bindings (e.g., auto [x, y] = myPair;) unpack structures or tuples into individual variable names.

79. What is 'std::byte' (introduced in C++17)?

a) A typedef for unsigned char
b) A fast integer type
c) A distinct type implementing raw memory without arithmetic operations
d) A boolean alias
Correct Answer: c) A distinct type implementing raw memory without arithmetic operations
Explanation:
std::byte provides a type-safe representation of memory bytes, supporting bitwise operations but intentionally lacking math operations.

80. Unlike 'auto', does 'decltype' retain 'const' and reference qualifiers?

a) Yes
b) No
c) Only references
d) Only const
Correct Answer: a) Yes
Explanation:
Unlike auto, decltype precisely deduces the exact type, retaining all const and reference qualifiers.

81. What is the primary purpose of the 'typedef' keyword?

a) To define a hardware type
b) To create an alias for an existing data type
c) To explicitly cast variables
d) To define a macro
Correct Answer: b) To create an alias for an existing data type
Explanation:
'typedef' allows programmers to create custom name aliases for existing, often complex, types.

82. Which C++11 keyword provides a modern, template-compatible alternative to 'typedef'?

a) alias
b) define
c) using
d) decltype
Correct Answer: c) using
Explanation:
The 'using' keyword (e.g., using Integer = int;) acts as a modern alternative to typedef and supports templates.

83. What is an 'enum' in C++?

a) A loop structure
b) A function array
c) A user-defined type consisting of a set of named integer constants
d) A decimal data type
Correct Answer: c) A user-defined type consisting of a set of named integer constants
Explanation:
An enumeration defines a custom type restricted to a specific set of named integer values.

84. Unless explicitly assigned, what integer value does the first item in a standard enum take?

a) 0
b) 1
c) -1
d) Garbage value
Correct Answer: a) 0
Explanation:
By default, enumerators begin at 0 and increment by 1.

85. What is the main safety advantage of an 'enum class' over a traditional 'enum'?

a) It uses less memory
b) It supports floating point values
c) It prevents implicit conversion to integers and scopes the names
d) It can be instantiated with 'new'
Correct Answer: c) It prevents implicit conversion to integers and scopes the names
Explanation:
Enum classes provide strong type safety by not implicitly casting to int and by requiring scoped access (e.g., Color::Red).

86. Can you specify the underlying data type of an enum class (e.g., to save memory)?

a) No, it is always int
b) Yes, by adding a colon and the type (e.g., enum class Color : char)
c) No, the compiler decides automatically
d) Yes, but only in C
Correct Answer: b) Yes, by adding a colon and the type (e.g., enum class Color : char)
Explanation:
C++11 allows explicit specification of the underlying integer type for an enum to optimize memory usage.

87. What is the fundamental difference between a 'struct' and a 'class' in C++?

a) Structs cannot have methods
b) Struct members are public by default; class members are private by default
c) Structs cannot use inheritance
d) There is strictly no difference
Correct Answer: b) Struct members are public by default; class members are private by default
Explanation:
In C++, the only functional difference between a struct and a class is default visibility.

88. What is a 'union' data type?

a) A type that joins two arrays
b) A combination of struct and class
c) A type where all members share the exact same memory location
d) A concurrent data type
Correct Answer: c) A type where all members share the exact same memory location
Explanation:
A union holds different data types but stores them in the exact same memory space, meaning only one member is valid at a time.

89. What dictates the memory size of a 'union'?

a) The sum of all member sizes
b) The size of its largest member
c) The size of its smallest member
d) It is always dynamically sized
Correct Answer: b) The size of its largest member
Explanation:
To ensure it can hold any of its members safely, a union's size is determined by its largest data member.

90. What are bit-fields in C++ structs?

a) Boolean arrays
b) Variables specifying their exact memory size in bits
c) Pointers to individual bits
d) Encrypted struct fields
Correct Answer: b) Variables specifying their exact memory size in bits
Explanation:
Bit-fields allow you to tightly pack data by specifying the exact number of bits an integer member will consume.

91. What is the size of an entirely empty struct or class in C++?

a) 0 bytes
b) 1 byte
c) 4 bytes
d) 8 bytes
Correct Answer: b) 1 byte
Explanation:
C++ requires every distinct object to have a unique memory address, so an empty class/struct is assigned a size of at least 1 byte.

92. What is the size of a standard pointer variable on a 64-bit system?

a) 2 bytes
b) 4 bytes
c) 8 bytes
d) 16 bytes
Correct Answer: c) 8 bytes
Explanation:
Memory addresses on a 64-bit system are 64 bits wide, which equates to 8 bytes of memory.

93. What does a 'void*' (void pointer) represent?

a) A pointer to an empty function
b) A generic pointer that can hold the address of any data type
c) A null pointer
d) An invalid pointer
Correct Answer: b) A generic pointer that can hold the address of any data type
Explanation:
A void pointer is a generic pointer type that can store the address of any object without knowing its type.

94. Can you directly dereference a 'void*' pointer?

a) Yes
b) No, it must be explicitly cast to a specific type first
c) Only if it points to an int
d) Yes, using auto
Correct Answer: b) No, it must be explicitly cast to a specific type first
Explanation:
Because the compiler doesn't know the size or type of the underlying data, a void pointer cannot be dereferenced directly.

95. What is a reference variable ('&') in C++?

a) A pointer that can be reassigned
b) An alternative name (alias) for an already existing variable
c) A variable stored purely in cache
d) A duplicate memory copy
Correct Answer: b) An alternative name (alias) for an already existing variable
Explanation:
A reference acts as an alias. Changes made through the reference directly modify the original aliased variable.

96. Can a reference variable be left uninitialized upon declaration?

a) Yes
b) No
c) Only global references
d) Only const references
Correct Answer: b) No
Explanation:
A reference must always be initialized to alias an existing variable exactly at the time of its declaration.

97. Can a reference be changed to alias a completely different variable later in the program?

a) Yes
b) No, it is bound permanently upon initialization
c) Yes, using pointers
d) Yes, in C++11 onwards
Correct Answer: b) No, it is bound permanently upon initialization
Explanation:
Once a reference is bound to an object, it cannot be reseated. Any assignment modifies the bound object, not the reference itself.

98. What happens when a raw array is passed into a C++ function?

a) The entire array is deeply copied
b) It throws an error
c) It decays into a pointer to its first element
d) It passes by reference inherently keeping its size
Correct Answer: c) It decays into a pointer to its first element
Explanation:
Raw arrays 'decay' into a pointer to their first element, which is why size information is lost when passing to functions.

99. What is the primary difference between a C-string and a C++ std::string?

a) They are exactly the same
b) std::string manages its own memory and length, while a C-string is just a null-terminated char array
c) C-strings are faster in all operations
d) std::string cannot be printed
Correct Answer: b) std::string manages its own memory and length, while a C-string is just a null-terminated char array
Explanation:
C-strings require manual memory and terminator management, whereas std::string is an object managing its own dynamic memory.

100. Can you declare an array of references in C++ (e.g., int& arr[5];)?

a) Yes
b) No, it is explicitly illegal in C++
c) Only for primitive types
d) Only inside a struct
Correct Answer: b) No, it is explicitly illegal in C++
Explanation:
C++ forbids arrays of references because references are not distinct objects and do not take up standalone memory in an array context.
← Previous: C++ Templates - MCQs
Next →: Latest C++ Basics MCQs
NewC++ Classes and Objects - MCQs

C++ Classes and Objects - MCQs

In C++, a class is a fundamental building block of Object-Oriented Programming (OOP) that acts as a user-defined blueprint for…

By MCQs Generator
New
No Image

C++ Exception / Error Handling - MCQs

1. Which of the following is a fundamental data type in C++? a) String b) Array c) int d) Class…

By MCQs Generator
NewLatest C++ Basics MCQs

Latest C++ Basics MCQs

Created by Bjarne Stroustrup as an extension of the C programming language, C++ is a powerful, high-performance, statically typed language…

By MCQs Generator