Variables and Constants in C ++ Programming MCQ Questions and Answers
1. Which of the following is a valid variable name in C++?
A) 9count
B) total$sum
C) count_9
D) int
Answer: C) count_9
2. What is the correct keyword to define a constant variable in C++?
A) static
B) final
C) define
D) const
Answer: D) const
3. What is the storage class specifier that retains the value of a variable between function calls?
A) register
B) extern
C) static
D) auto
Answer: C) static
4. Which of the following variables has a local scope in C++?
A) Global variable
B) Static variable
C) Variable declared inside a function
D) Extern variable
Answer: C) Variable declared inside a function
5. Which of the following keywords is used to define a variable that cannot be modified?
A) static
B) mutable
C) volatile
D) const
Answer: D) const
6. Which of the following constants in C++ has type double by default?
A) 10
B) ‘A’
C) 10.5
D) 10U
Answer: C) 10.5
7. Which of the following is a valid declaration of a floating-point constant?
A) float num = 25.0;
B) float num = 25;
C) float num = 25.0f;
D) float num = ‘25’;
Answer: C) float num = 25.0f;
8. A variable declared as extern means:
A) It is defined in another file
B) It is local to the current function
C) It is initialized automatically
D) It cannot be modified
Answer: A) It is defined in another file
9. Which keyword allows a class member to be changed even if it is part of a const object?
A) const_cast
B) mutable
C) static
D) volatile
Answer: B) mutable
10. Which of the following is NOT a valid integer constant?
A) 015
B) 0xA
C) 10u
D) 12.0
Answer: D) 12.0
11. What is the correct way to define a constant integer with value 100?
A) define int a = 100;
B) int a = const 100;
C) const int a = 100;
D) static const a = 100;
Answer: C) const int a = 100;
12. Which of the following best describes a constexpr variable?
A) It is always initialized at runtime.
B) It must be initialized with a compile-time constant expression.
C) It cannot be used in constant expressions.
D) It is evaluated during program execution.
Answer: B) It must be initialized with a compile-time constant expression.
13. The lifetime of a static local variable is:
A) Till the function returns
B) Till the block exits
C) Till program termination
D) Till the next function call
Answer: C) Till program termination
14. Which of the following statements about constants is TRUE?
A) Constants can be changed later
B) Constants must be initialized when declared
C) Constants occupy no memory
D) Constants are stored on the stack
Answer: B) Constants must be initialized when declared
15. What is the default value of an uninitialized local variable in C++?
A) 0
B) Undefined (garbage value)
C) NULL
D) -1
Answer: B) Undefined (garbage value)
16. Which operator can modify a const variable indirectly?
A) *
B) const_cast
C) static_cast
D) reinterpret_cast
Answer: B) const_cast
17. Which of the following represents a character constant in C++?
A) “A”
B) ‘A’
C) 65.0
D) A
Answer: B) ‘A’
18. What is the difference between const and constexpr in C++?
A) No difference
B) constexpr ensures compile-time evaluation
C) const ensures compile-time evaluation
D) const is faster
Answer: B) constexpr ensures compile-time evaluation
19. The scope of a global variable is:
A) Function
B) Class
C) Namespace
D) Entire program
Answer: D) Entire program
20. Which of the following keywords can be used to define a variable whose value can change even in a const object?
A) const
B) register
C) mutable
D) extern
Answer: C) mutable
21. The prefix 0x in a numeric constant denotes which number system?
A) Octal
B) Hexadecimal
C) Binary
D) Decimal
Answer: B) Hexadecimal
22. Which keyword ensures that the compiler does not optimize access to a variable?
A) static
B) mutable
C) volatile
D) extern
Answer: C) volatile
23. In C++, what is the size of a char variable?
A) 2 bytes
B) 8 bytes
C) 1 byte
D) Depends on compiler
Answer: C) 1 byte
24. Which of the following constants is a valid hexadecimal constant?
A) 078
B) 0x1A
C) 0b1010
D) 1AH
Answer: B) 0x1A
25. Which of the following cannot appear on the left-hand side of an assignment?
A) Variable
B) Pointer
C) Constant
D) Reference
Answer: C) Constant
26. What is the type of constant 0xFF in C++ by default?
A) long
B) short
C) int
D) unsigned int
Answer: C) int
27. What happens if a constant variable is not initialized during declaration?
A) It takes zero by default
B) It can be assigned later
C) It causes a compilation error
D) It takes garbage value
Answer: C) It causes a compilation error
28. Which of the following statements is correct about extern variables?
A) They are stored in stack memory
B) They provide a reference to a global variable defined elsewhere
C) They are used to create constants
D) They are used only inside classes
Answer: B) They provide a reference to a global variable defined elsewhere
29. What will be the output of the following code?
const int x = 10;
int *p = (int*)&x;
*p = 20;
cout << x;
A) 10
B) 20
C) 10 (undefined behavior)
D) Compilation error
Answer: C) 10 (undefined behavior)
30. Which keyword defines a constant expression that can be used in array bounds?
A) static
B) const
C) register
D) constexpr
Answer: D) constexpr
31. Which of the following constants represents a binary literal in C++14?
A) 0x1010
B) 01010
C) 0b1010
D) 0d1010
Answer: C) 0b1010
32. What is the value type of a string constant “ABC” in C++?
A) char*
B) const char
C) const char[]
D) string
Answer: C) const char[]
33. The keyword register suggests that:
A) Variable should be stored in hard disk
B) Variable has global lifetime
C) Variable cannot be accessed
D) Variable should be stored in CPU register for faster access
Answer: D) Variable should be stored in CPU register for faster access
34. Which of the following keywords allows the same variable name to be reused in nested scopes?
A) extern
B) shadowing
C) volatile
D) static
Answer: B) shadowing
35. Which of the following is an invalid variable declaration in C++?
A) int num1 = 10;
B) float _rate = 5.0;
C) char #ch = ‘A’;
D) int new = 5;
Answer: D) int new = 5;
36. Which of the following types can be used with the constexpr specifier?
A) Runtime evaluated expressions
B) Compile-time evaluable expressions
C) Input/output stream objects
D) Dynamic memory allocations
Answer: B) Compile-time evaluable expressions
37. What is the lifetime of a global variable declared outside all functions?
A) Function scope
B) Block scope
C) Entire program duration
D) Static duration
Answer: C) Entire program duration
38. Which of the following is a valid declaration of multiple constants in one line?
A) const int a = 5, b = 10;
B) const int a = 5, b = 10;
C) const int a; b;
D) int const = 5, b = 10;
Answer: B) const int a = 5, b = 10;
39. Which of the following types cannot be declared as constexpr?
A) int
B) ifstream
C) char
D) double
Answer: B) ifstream
40. Which of the following correctly declares a constant pointer to an integer?
A) const int p;
B) int * const p;
C) int * const p = &x;
D) const int p;
Answer: C) int * const p = &x;
41. In the expression const int *p, what is constant?
A) The pointer p
B) The value pointed by p
C) Both pointer and value
D) Neither pointer nor value
Answer: B) The value pointed by p
42. Which of the following correctly declares a constant object in C++?
A) MyClass obj;
B) const MyClass *obj;
C) const MyClass obj;
D) MyClass const *obj;
Answer: C) const MyClass obj;
________________________________________
43. Which constant represents the null pointer in modern C++ (C++11 and later)?
A) 0
B) NULL
C) nullptr
D) ‘\0’
Answer: C) nullptr
44. The volatile keyword tells the compiler that:
A) The variable should be constant
B) The variable is unused
C) The variable may change unexpectedly
D) The variable must not be initialized
Answer: C) The variable may change unexpectedly
45. Which of the following correctly declares a constant reference?
A) int & const ref;
B) const &int ref;
C) const int &ref = value;
D) int const ref;
Answer: C) const int &ref = value;
46. What does the static keyword mean when applied to a global variable?
A) It is shared among all files
B) It is read-only
C) It has internal linkage (visible only within the file)
D) It has limited lifetime
Answer: C) It has internal linkage (visible only within the file)
47. The type of a string literal like “hello” is:
A) char*
B) string
C) const char[6]
D) char[]
Answer: C) const char[6]
48. What will happen if you declare two variables with the same name in the same scope?
A) The compiler will merge them
B) The program runs with a warning
C) The second variable overwrites the first
D) Compilation error
Answer: D) Compilation error
49. Which of the following constants has type unsigned long?
A) 10L
B) 10UL
C) 10LU
D) 10U
Answer: B) 10UL
50. Which of the following declarations creates a read-only reference to an integer?
A) int &r = x;
B) int &&r = x;
C) const int &r = x;
D) int const *r = &x;
Answer: C) const int &r = x;
51. Which of the following can be modified in a const member function?
A) All member variables
B) Static members only
C) Mutable members
D) None of the above
Answer: C) Mutable members
52. Which statement correctly defines a constant pointer to a constant integer?
A) int const * const p;
B) const int *p;
C) int * const p;
D) const int * const p;
Answer: D) const int * const p;
53. Which of the following is a valid use of the auto keyword in C++11?
A) To define constants only
B) To declare runtime constants
C) To automatically deduce the variable type
D) To create global variables
Answer: C) To automatically deduce the variable type
54. Which of the following constants represents a wide character literal?
A) ‘A’
B) “A”
C) L’A’
D) u8″A”
Answer: C) L’A’
55. What is the scope of a variable declared inside a block {} in C++?
A) Program scope
B) File scope
C) Block scope
D) Function scope
Answer: C) Block scope
56. Which of the following statements about constexpr functions is TRUE?
A) They cannot return any value
B) They cannot have parameters
C) They can be evaluated at compile time
D) They must contain loops
Answer: C) They can be evaluated at compile time
57. What does the keyword static do when used in a function?
A) Preserves variable value between function calls
B) Creates temporary variables
C) Allocates variable on the heap
D) Limits variable lifetime to one call
Answer: A) Preserves variable value between function calls
58. Which of the following is an example of a user-defined constant?
A) const double PI = 3.14159;
B) 5.5
C) ‘A’
D) 10U
Answer: A) const double PI = 3.14159;
59. The const keyword ensures that:
A) Variable can be reassigned only once
B) Variable value cannot change after initialization
C) Variable must be declared static
D) Variable will have global scope
Answer: B) Variable value cannot change after initialization
60. What happens when a const variable is assigned a value later?
A) It changes successfully
B) It is redefined
C) It results in a compilation error
D) It gets initialized
Answer: C) It results in a compilation error
61. Which of the following suffixes indicates an unsigned constant?
A) F
B) U
C) L
D) LL
Answer: B) U
62. Which of the following is the correct declaration of a long double constant?
A) long double val = 12.34L;
B) double val = 12.34L;
C) float val = 12.34L;
D) long val = 12.34L;
Answer: A) long double val = 12.34L;
63. Which of the following storage classes has the smallest lifetime?
A) static
B) extern
C) auto
D) register
Answer: C) auto
64. What is the correct statement about variable initialization?
A) Variables can be used before initialization
B) Variables must be initialized before use
C) Initialization is optional
D) Variables are auto-initialized
Answer: B) Variables must be initialized before use
65. Which of the following is an invalid way to declare a variable?
A) int num = 5;
B) float 1value;
C) char ch = ‘A’;
D) double rate = 2.5;
Answer: B) float 1value;
66. What will be the output of the following code?
const int a = 5;
int b = a + 10;
cout << b;
A) Compilation error
B) Undefined behavior
C) 15
D) 5
Answer: C) 15
67. Which of the following is a valid declaration for a volatile pointer to integer?
A) *volatile int ptr;
B) int *volatile ptr;
C) int volatile *ptr;
D) const volatile int &ptr;
*Answer: A) volatile int ptr;
68. Which of the following is NOT a valid constant type in C++?
A) Character constant
B) Floating-point constant
C) Boolean constant
D) Identifier constant
Answer: D) Identifier constant
69. Which type of variable has its memory allocated on the heap?
A) Local
B) Global
C) Dynamic
D) Static
Answer: C) Dynamic
70. The constexpr specifier was introduced in which version of C++?
A) C++98
B) C++03
C) C++11
D) C++17
Answer: C) C++11
71. Which of the following allows constants to be defined at global scope?
A) const keyword
B) #define inside a function
C) static keyword
D) mutable keyword
Answer: A) const keyword
72. Which of the following is true about #define constants?
A) They are replaced by the preprocessor before compilation
B) They are stored in memory
C) They are initialized at runtime
D) They can be modified
Answer: A) They are replaced by the preprocessor before compilation
73. Which of the following correctly defines a constexpr function?
A) constexpr void print() { cout << “Hi”; }
B) constexpr int square(int x) { return x * x; }
C) constexpr auto = 10;
D) const int func(int x) { return x * x; }
Answer: B) constexpr int square(int x) { return x * x; }
74. Which of the following is NOT true about static variables inside functions?
A) They are initialized only once
B) They retain their values between calls
C) They are destroyed when the function ends
D) They have function scope
Answer: C) They are destroyed when the function ends
75. Which of the following constants has a boolean value of true?
A) 0
B) NULL
C) 1
D) ‘\0’
Answer: C) 1
76. Which of the following is a constant pointer to a constant data?
A) const int *p;
B) int * const p;
C) const int * const p;
D) int const p;
Answer: C) const int * const p;
77. Which storage class in C++ gives a variable internal linkage by default?
A) extern
B) auto
C) static
D) register
Answer: C) static
78. Which keyword in C++ specifies that a variable may be modified by hardware or another thread?
A) const
B) static
C) volatile
D) mutable
Answer: C) volatile
79. What is the correct syntax to declare a compile-time constant integer?
A) constexpr int n = getInput();
B) constexpr int n = 10;
C) const int n = read();
D) int constexpr n;
Answer: B) constexpr int n = 10;
80. The static keyword applied to a global variable means:
A) It is accessible only within the same translation unit
B) It is accessible across all files
C) It must be constant
D) It is reinitialized every time
Answer: A) It is accessible only within the same translation unit
81. What is the scope of a variable declared as register?
A) Global
B) Block
C) File
D) Function prototype
Answer: B) Block
82. Which of the following correctly declares a variable whose type will be automatically deduced?
A) auto value = 10;
B) deduce value = 10;
C) typeof(value) = 10;
D) var value = 10;
Answer: A) auto value = 10;
83. Which of the following is not a valid constant literal in C++?
A) 0x1F
B) 075
C) 0b128
D) 1.2e3
Answer: C) 0b128
84. The default storage class of local variables in C++ is:
A) register
B) extern
C) auto
D) static
Answer: C) auto
85. Which of the following cannot be declared as constexpr?
A) int
B) void
C) char
D) double
Answer: B) void
86. Which of the following modifiers cannot be used together in C++?
A) const and volatile
B) static and const
C) register and static
D) extern and const
Answer: C) register and static
87. Which keyword allows defining a constant expression in C++ that replaces macros?
A) final
B) const
C) static
D) constexpr
Answer: D) constexpr
88. Which statement correctly describes a const reference parameter?
A) It copies the argument value
B) It prevents modification of the argument
C) It prevents the parameter from being passed by reference
D) It stores a new value in the parameter
Answer: B) It prevents modification of the argument
89. Which of the following is true about variable names in C++?
A) Can begin with a number
B) Can begin with an underscore
C) Can contain special symbols
D) Are case-insensitive
Answer: B) Can begin with an underscore
90. Which keyword specifies that a constant should be evaluated during compilation and not at runtime?
A) const
B) static
C) register
D) constexpr
Answer: D) constexpr
91. Which of the following integer constants is in octal format?
A) 0x8
B) 0b1000
C) 010
D) 10L
Answer: C) 010
92. Which of the following correctly represents a wide string literal?
A) “Hello”
B) L”Hello”
C) u8″Hello”
D) R”Hello”
Answer: B) L”Hello”
93. Which of the following cannot be declared as a variable name?
A) int _main;
B) int for;
C) int total_sum;
D) int n1;
Answer: B) int for;
94. Which of the following constants is of type unsigned long long?
A) 100L
B) 100UL
C) 100ULL
D) 100LU
Answer: C) 100ULL
95. Which of the following statements about static data members in classes is true?
A) They are unique for each object
B) They are shared among all objects
C) They cannot be initialized
D) They are stored on stack
Answer: B) They are shared among all objects
96. Which of the following is NOT a valid floating-point literal?
A) 1.23
B) 1.2e3
C) 1.2fF
D) 0.0
Answer: C) 1.2fF
97. What is the effect of declaring a global variable as const?
A) It is visible only to functions
B) It can be changed anywhere
C) It is accessible across files using extern const
D) It becomes private to a class
Answer: C) It is accessible across files using extern const
98. Which of the following is true about the #define directive?
A) It does not allocate memory
B) It defines typed constants
C) It can be used within classes
D) It cannot define constants
Answer: A) It does not allocate memory
99. What is the correct declaration for a constant pointer to a variable integer?
A) const int *p;
B) int * const p;
C) const int const *p;
D) const int const p;
Answer: B) int * const p;
100. Which of the following correctly describes extern const variables?
A) They are reinitialized every time
B) They are local to a function
C) They are stored on stack
D) They are defined elsewhere but constant in nature
Answer: D) They are defined elsewhere but constant in nature
