MCQS C++
MCQS C++
MCQS C++
Control Structures
6.Which loop is guaranteed to execute at least
once?
a) for
b) while
c) do-while
d) None of the above
Answer: c
7.What will the following code snippet
output?
cpp
Copy code
for (int i = 0; i < 5; i++) {
if (i == 3) break;
cout << i;
}
a) 0123
b) 012
c) 01234
d) Compilation error
Answer: b
8.Which statement is used to skip the rest of
the current loop iteration?
a) break
b) continue
c) goto
d) return
Answer: b
Functions
9.What is the default return type of a C++
function if not explicitly mentioned?
a) int
b) void
c) float
d) Compiler error
Answer: a
10. How do you pass an array to a function
in C++?
a) By value
b) By reference
c) By pointer
d) Both b and c
Answer: d
Standard Library
15. Which library function can be used to
find the length of a string?
a) strlen()
b) size()
c) length()
d) All of the above
Answer: d
Functions (Continued)
20. What is the purpose of the return
statement in a function?
a) To exit a loop
b) To return a value to the calling function
c) To terminate the program
d) None of the above
Answer: b
21. Can a C++ function have default
arguments?
a) Yes
b) No
c) Only for int type
d) Only for void type
Answer: a
22. What is the correct way to declare a
function that does not return a value?
a) int functionName();
b) void functionName();
c) return functionName();
d) functionName();
Answer: b
23. What is function overloading?
a) Using the same function name with
different arguments
b) Using multiple functions with different
names
c) A function that calls another function
d) A function with a return type
Answer: a
Input/Output
32. What is the default behavior of cin
when input is separated by whitespace?
a) It reads until the first whitespace
character.
b) It reads the entire input line.
c) It throws an error.
d) It stores the whitespace in the variable.
Answer: a
33. What will the following code output?
string name;
cin >> name;
cout << name;
Input: John Doe
a) John
b) Doe
c) John Doe
d) Compilation error
Answer: a
34. What is the purpose of getline() in
C++?
a) To read an integer
b) To read a single character
c) To read an entire line including spaces
d) To terminate the program
Answer: c
Operators
35. Which operator has the highest
precedence in C++?
a) +
b) *
c) ++
d) =
Answer: c
36. What does the == operator do?
a) Assigns a value
b) Checks for equality
c) Compares memory addresses
d) None of the above
Answer: b
37. What is the result of 5 % 2 in C++?
a) 2.5
b) 1
c) 0
d) Compilation error
Answer: b
Data Types
38. Which of the following is a valid C++
data type?
a) int
b) float
c) bool
d) All of the above
Answer: d
39. What is the size of a char in C++?
a) 1 byte
b) 2 bytes
c) 4 bytes
d) Depends on the platform
Answer: a
40. What is the range of int on most
systems?
a) -32768 to 32767
b) -2147483648 to 2147483647
c) 0 to 65535
d) None of the above
Answer: b
41. What will the following code output?
float x = 5 / 2;
cout << x;
a) 2.5
b) 2.0
c) 5
d) Compilation error
Answer: b
42. What is the correct way to declare a
constant variable in C++?
a) const int x = 10;
b) int const x = 10;
c) Both a and b
d) #define x 10
Answer: c
Operators (Continued)
43. Which operator is used to access the
address of a variable?
a) *
b) &
c) ->
d) .
Answer: b
44. What is the output of the following
code?
Error Handling
47. What type of error occurs during
program execution?
a) Syntax error
b) Runtime error
c) Logical error
d) Compilation error
Answer: b
48. What happens if you divide a number by
zero in C++?
a) Program terminates
b) Undefined behavior
c) Outputs infinity
d) Compilation error
Answer: b
49. How can exceptions be handled in C++?
a) Using if-else
b) Using try-catch blocks
c) Using throw statements
d) Both b and c
Answer: d
Type Conversion
51. What is implicit type conversion in
C++?
a) Conversion done automatically by the
compiler
b) Conversion done explicitly by the
programmer
c) Casting a pointer
d) None of the above
Answer: a
52. What will the following code output?
float x = 3.14;
int y = x;
cout << y;
a) 3
b) 3.14
c) Undefined behavior
d) Compilation error
Answer: a
a) (int) x
b) int(x)
c) static_cast<int>(x)
d) All of the above
Answer: d
Preprocessor Directives
54. What is the purpose of the #include
directive?
a) To include standard libraries or header
files
b) To define macros
c) To include comments
d) None of the above
Answer: a
55. Which of the following is used to define
a macro in C++?
a) #include
b) #define
c) #macro
d) #ifdef
Answer: b
56. What will the following code output?
cpp
Copy code
#define PI 3.14
cout << PI * 2;
a) 6.28
b) 3.14
c) Compilation error
d) PI * 2
Answer: a
57. What is the purpose of the #ifdef
directive?
a) To check if a macro is defined
b) To include a file
c) To define a variable
d) None of the above
Answer: a
Miscellaneous
58. Which header file is required for input
and output in C++?
a) <stdio.h>
b) <iostream>
c) <conio.h>
d) <stdlib.h>
Answer: b
59. Which of the following is not a valid
variable name in C++?
a) int 1variable;
b) int _variable;
c) int variable1;
d) int variable_1;
Answer: a
60. What is the difference between == and =
in C++?
a) == is used for comparison, and = is used
for assignment.
b) Both are used for assignment.
c) Both are used for comparison.
d) There is no difference.
Answer: a
Would you like to expand on any specific topics
or proceed with further questions in different
categories?
4o
You said:
more
ChatGPT said:
ChatGPT
Here’s a continuation with more MCQs,
expanding further into advanced areas like
loops, libraries, type specifiers, and some
common debugging scenarios.
Libraries
66. Which of the following is a standard
C++ library?
a) <math.h>
b) <vector>
c) <algorithm>
d) All of the above
Answer: d
67. Which library is required for string
manipulation in C++?
a) <string.h>
b) <cstring>
c) <string>
d) <iostream>
Answer: c
68. What is the output of the following
code?
cpp
Copy code
#include <cmath>
cout << pow(2, 3);
a) 6
b) 8
c) 9
d) Compilation error
Answer: b
69. Which library function returns the size
of a container like vector?
a) size()
b) length()
c) sizeof()
d) count()
Answer: a
70. What is the correct way to sort an array
in ascending order using the C++ Standard
Library?
a) sort(arr.begin(),
arr.end());
b) std::sort(arr, arr + n);
c) sort(arr, n);
d) None of the above
Answer: b
Type Specifiers
71. Which type specifier forces a variable to
be read-only?
a) static
b) extern
c) const
d) volatile
Answer: c
72. What is the output of the following
code?
cpp
Copy code
const int x = 10;
// x = 20;
cout << x;
a) 10
b) 20
c) Compilation error due to reassignment of
x
d) Undefined behavior
Answer: c
73. Which type specifier ensures that a
variable is stored only once across multiple
files?
a) static
b) volatile
c) extern
d) register
Answer: c
74. What does the volatile keyword
indicate in C++?
a) The value of the variable can change
unexpectedly.
b) The variable is immutable.
c) The variable is optimized for faster
access.
d) None of the above.
Answer: a
Miscellaneous (Advanced)
79. Which of the following statements is
correct?
a) int x[5] = {1, 2, 3}; initializes
the first three elements, and the rest are set
to 0.
b) int x[5] = {1, 2, 3}; initializes
all elements to 1.
c) int x[5] = {1, 2, 3}; initializes
all elements to -1.
d) Compilation error.
Answer: a
80. What is the output of the following
code?
cpp
Copy code
int x = 10;
cout << (x > 5 ? "Yes" :
"No");
a) Yes
b) No
c) Compilation error
d) Undefined behavior
Answer: a
81. Which of the following is a correct
statement about variable scope?
a) Variables defined in a function are global.
b) static variables retain their values
between function calls.
c) Variables in main can be accessed from
any other function.
d) None of the above.
Answer: b
82. How do you represent a multi-line string
in C++?
a) Using \ at the end of each line
b) Using triple quotes """
c) Using separate cout statements
d) None of the above
Answer: a
Memory Management
87. Which of the following is the correct
syntax to allocate memory dynamically for a
single object?
a) int *ptr = new int;
b) int *ptr =
malloc(sizeof(int));
c) int *ptr = allocate int();
d) int *ptr = new int[];
Answer: a
88. What is the difference between new and
malloc in C++?
a) new is used for memory allocation in C,
while malloc is for C++.
b) malloc does not initialize memory, but
new does.
c) malloc can allocate memory for non-
primitive types, while new cannot.
d) Both are equivalent and can be used
interchangeably.
Answer: b
89. What will happen if you forget to call
delete after using new?
a) Memory leak
b) Compilation error
c) Undefined behavior
d) None of the above
Answer: a
90. Which of the following is the correct
way to free memory allocated for an array?
a) delete ptr[];
b) free(ptr);
c) delete[] ptr;
d) clear(ptr);
Answer: c
Input/Output (Continued)
98. What is the correct syntax to read a full
line of text (including spaces) from the user
in C++?
a) cin >> string;
b) getline(cin, string);
c) cin.getline(string);
d) input(cin, string);
Answer: b
99. Which of the following is the correct
syntax for writing output to a file?
a) ofstream file("file.txt");
b) fstream file;
file.open("file.txt");
c) ofstream file("file.txt");
file << "Hello, World!";
d) All of the above
Answer: d
100. What does the flush function do in
C++ output streams?
a) Clears the output buffer
b) Forces immediate output to the screen or
file
c) Removes all characters from the buffer
d) None of the above
Answer: b
Miscellaneous
101. What will the following code output?
cpp int a = 5; int *ptr = &a;
cout << *ptr;
a) 5
b) a
c) &a
d) Compilation error
Answer: a
102. What is the output of the following
code?
cpp int arr[5] = {1, 2, 3, 4,
5}; cout << arr[3];
a) 1
b) 2
c) 3
d) 4
Answer: d
103. Which of the following is true about the
const keyword in C++?
a) const variables cannot be modified after
initialization.
b) const variables can only be used inside
functions.
c) A const pointer cannot point to other
objects.
d) const objects must be declared as
static.
Answer: a
104. Which of the following is correct
regarding C++ arrays?
a) The size of an array must be defined
during runtime.
b) Arrays can be resized dynamically.
c) Arrays are zero-indexed.
d) None of the above
Answer: c
105. Which of the following is a valid pointer
expression in C++?
a) int *p = &x;
b) int *p = x;
c) int *p = new int();
d) Both a and c
Answer: d
Functions (Advanced)
106. Which of the following is a valid
function signature in C++?
a) int function(int, float);
b) int function(int x, float);
c) float function(int, float);
d) All of the above
Answer: d
107. Which function is used to get the
number of elements in an array in C++?
a) length()
b) size()
c) count()
d) sizeof()
Answer: d
108. What is a function pointer in C++?
a) A pointer that stores the address of a
function
b) A pointer to a variable
c) A reference to a function
d) A pointer to an array
Answer: a
109. What is the correct way to declare a
pointer to a function?
a) int* func(int);
b) int (*func)(int);
c) int func* (int);
d) void func(int*);
Answer: b
110. What is a lambda function in C++?
a) A function defined within a class
b) A function that can be passed as an
argument
c) A function with no parameters
d) A function that is automatically executed
Answer: b
Operators (Advanced)
111. What does the sizeof operator do in
C++?
a) Returns the number of elements in an
array
b) Returns the size of a type or variable in
bytes
c) Returns the size of an object in bits
d) All of the above
Answer: b
112. Which of the following operators has the
highest precedence in C++?
a) *
b) +
c) =
d) &
Answer: a
113. What is the result of the following
expression in C++?
cpp int x = 5, y = 10; cout
<< x < y;
a) 5
b) true
c) false
d) Compilation error
Answer: c
114. What is the purpose of the :: operator
in C++?
a) Used to access members of a class
b) Used to access global variables
c) Used for scope resolution
d) All of the above
Answer: d
115. Which of the following operators is used
to dereference a pointer in C++?
a) *
b) &
c) ->
d) .
Answer: a
File Handling
121. Which of the following is the correct
way to open a file for reading in C++?
a) fstream file("file.txt");
b) ifstream file("file.txt");
c) ofstream file("file.txt");
d) file.open("file.txt",
ios::in);
Answer: b
122. What is the correct way to read a single
character from a file in C++?
a) cin >> ch;
b) file >> ch;
c) file.get(ch);
d) file.getline(ch, 1);
Answer: c
123. What is the purpose of ofstream in
C++?
a) To read data from a file
b) To write data to a file
c) To open a file for both reading and
writing
d) To close a file
Answer: b
124. Which of the following operations is
valid for file handling in C++?
a) file.open("file.txt",
ios::app);
b) file.read("file.txt");
c) file.write();
d) file.close();
Answer: a
125. How do you check if a file opened
successfully in C++?
a) if (!file) {}
b) if (file.is_open()) {}
c) if (file.fail()) {}
d) All of the above
Answer: d
Advanced Concepts
131. What is a virtual function in C++?
a) A function that is executed in a loop
b) A function that is defined in a class but
overridden in derived classes
c) A function that is always executed when
called
d) A function that returns a pointer
Answer: b
132. What is the purpose of a destructor in
C++?
a) To initialize an object
b) To clean up memory when an object is
destroyed
c) To copy objects
d) To check the type of an object
Answer: b
133. What does the virtual keyword do to
a class member function?
a) It prevents the function from being
overridden
b) It allows the function to be overridden in
derived classes
c) It makes the function private
d) It makes the function public
Answer: b
134. What is the purpose of the new[]
operator in C++?
a) To allocate a single object dynamically
b) To allocate an array of objects
dynamically
c) To delete a dynamically allocated object
d) To deallocate an array
Answer: b
135. Which of the following will cause a
memory leak in C++?
a) Not calling delete after using new
b) Using delete twice on the same pointer
c) Accessing memory after it is freed
d) All of the above
Answer: a