MCQS C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Basics of C++

1.What is the correct syntax to output "Hello,


World" in C++?
a) printf("Hello, World");
b) cout << "Hello, World";
c) Console.WriteLine("Hello,
World");
d) print("Hello, World");
Answer: b
2.What header file is required for using cin
and cout?
a) <stdio.h>
b) <iostream>
c) <conio.h>
d) <string>
Answer: b
3.Which of the following is a valid C++
comment?
a) /* This is a comment */
b) // This is a comment
c) Both a and b
d) None of the above
Answer: c
4.Which escape character is used for a new
line in C++?
a) \n
b) \t
c) \\
d) \r
Answer: a
5.What is the output of the following code?
cpp
Copy code
int x = 5;
cout << x++;
a) 5
b) 6
c) Compilation error
d) Undefined behavior
Answer: a

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

Pointers and Arrays


11. What does the following code print?
int arr[] = {10, 20, 30};
cout << *(arr + 1);
a) 10
b) 20
c) 30
d) Compilation error
Answer: b
12. If p is a pointer to an integer, what does
p++ do?
a) Moves the pointer to the next integer
b) Increments the value pointed by p
c) Compilation error
d) None of the above
Answer: a
Memory Management
13. Which of the following is used to
allocate memory dynamically in C++?
a) malloc()
b) new
c) calloc()
d) realloc()
Answer: b
14. Which keyword is used to deallocate
memory allocated by new?
a) delete
b) free
c) release
d) clear
Answer: a

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

Control Structures (Continued)


16. What is the output of the following
code?
int i = 0;
while (i < 3) {
cout << i;
i++;
}
a) 012
b) 123
c) 0123
d) Infinite loop
Answer: a
17. Which of the following statements about
the switch statement is true?
a) Every case must end with a break.
b) default is mandatory in a switch
statement.
c) switch evaluates an integer or character
expression.
d) Multiple cases cannot have the same code
block.
Answer: c
18. What happens if there is no break in a
switch case?
a) Compilation error
b) Only the matching case executes
c) Fallthrough occurs to subsequent cases
d) The program crashes
Answer: c
19. What will the following code output?

for (int i = 0; i < 5; i++) {


if (i == 2) continue;
cout << i;
}
a) 01234
b) 0134
c) 0234
d) Compilation error
Answer: b

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

Pointers and Arrays (Continued)


24. What does * signify in C++?
a) Pointer declaration
b) Dereferencing a pointer
c) Multiplication operator
d) All of the above
Answer: d
25. What is the correct way to declare a
pointer to an integer?
a) int *p;
b) int p*;
c) int p;
d) pointer<int> p;
Answer: a
26. What will the following code output?
int x = 10;
int *p = &x;
cout << *p;
a) 10
b) Address of x
c) Undefined behavior
d) Compilation error
Answer: a
27. Which of the following statements about
arrays is true?
a) The size of an array can be changed
during runtime.
b) Array elements are stored in contiguous
memory locations.
c) Arrays in C++ are dynamic by default.
d) The index of the first element is 1.
Answer: b
28. If int arr[5] = {1, 2, 3, 4,
5};, what is the value of arr[2]?
a) 1
b) 2
c) 3
d) 4
Answer: c

Memory Management (Continued)


29. Which operator is used to allocate
memory dynamically for an array?
a) new
b) malloc()
c) calloc()
d) new[]
Answer: d
30. What happens if delete is used for a
pointer that was not allocated using new?
a) Memory leak
b) Undefined behavior
c) Compilation error
d) Program crashes
Answer: b
31. How do you deallocate memory
allocated for an array?
a) delete
b) delete[]
c) free()
d) clear()
Answer: b

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?

int a = 10, b = 20;


cout << (a > b ? a : b);
a) 10
b) 20
c) 30
d) Compilation error
Answer: b
45. Which of the following is the modulus
operator in C++?
a) %
b) #
c) mod
d) @
Answer: a
46. Which of the following is not a logical
operator in C++?
a) &&
b) ||
c) !
d) =
Answer: d

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

53. How do you explicitly convert a float to


an int in C++?

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.

Control Structures (Advanced)


61. What will the following code output?
cpp
Copy code
int i = 0;
for (;;) {
if (i == 3) break;
cout << i;
i++;
}
a) 012
b) Infinite loop
c) Compilation error
d) 0123
Answer: a
62. What is the output of the following
code?
cpp
Copy code
int x = 0;
for (int i = 0; i < 3; i++) {
x += i;
}
cout << x;
a) 0
b) 1
c) 3
d) 6
Answer: c
63. Which of the following loops does not
support a condition at its start or end?
a) for loop
b) while loop
c) do-while loop
d) None of the above
Answer: c
64. What does the break statement do?
a) Exits the current loop
b) Skips the rest of the current iteration
c) Exits the program
d) Continues the next iteration of the loop
Answer: a
65. What is the correct syntax for a for
loop?
a) for (int i; i < 10; i++)
b) for (int i = 0, i < 10, i++)
c) for (int i = 0; i < 10; i++)
d) for i = 0; i < 10; i++
Answer: c

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

Common Debugging Scenarios


75. What happens if you dereference a null
pointer in C++?
a) Undefined behavior
b) 0 is returned
c) Compilation error
d) Segmentation fault
Answer: a
76. What does a segmentation fault indicate?
a) Syntax error in the code
b) Division by zero
c) Illegal memory access
d) Infinite loop
Answer: c
77. Which of the following can cause a
buffer overflow?
a) Writing beyond the end of an array
b) Division by zero
c) Infinite loop
d) None of the above
Answer: a
78. What is the purpose of a core dump?
a) Displays program output
b) Helps debug program crashes by saving
program state
c) Optimizes code performance
d) None of the above
Answer: b

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

Let me know if you'd like further expansion or


specific areas to focus on.
4o
You said:
more
ChatGPT said:
ChatGPT
Here’s an additional set of C++ MCQs to
further aid your preparation:
Functions (Continued)
83. What is the purpose of the void return
type in a function?
a) To return a value
b) To signify no return value
c) To return null
d) To indicate an error in the function
Answer: b
84. Which of the following is a valid
function declaration in C++?
a) void functionName(int a);
b) int functionName[5];
c) int functionName() int;
d) functionName void(int a);
Answer: a
85. Which of the following can be used to
pass parameters by reference in C++?
a) int &x
b) int x
c) int *x
d) int x&
Answer: a
86. Which of the following statements is
correct?
a) Function overloading occurs when
functions have the same name but different
return types.
b) Function overloading occurs when
functions have the same name and number
of arguments.
c) Function overloading occurs when
functions have the same name and different
argument types.
d) Function overloading requires the
functions to have different names.
Answer: c

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

Preprocessor Directives (Continued)


91. What is the purpose of the #define
directive in C++?
a) To define constants
b) To include header files
c) To start the main function
d) To mark the beginning of the program
Answer: a
92. What will be the output of the following
code?
cpp
Copy code
#define MAX 100
cout << MAX;
a) 100
b) MAX
c) Undefined behavior
d) Compilation error
Answer: a
93. Which of the following directives is used
to conditionally include a file?
a) #include
b) #define
c) #ifdef
d) #else
Answer: c
94. What is the function of the #ifdef
directive in C++?
a) It includes a header file if a condition is
true.
b) It defines a constant.
c) It checks whether a macro is defined.
d) It removes all comments from the code.
Answer: c

Type Conversion (Advanced)


95. What is the result of the following code?
cpp
Copy code
int x = 10;
float y = 3.14;
x = y;
cout << x;
a) 3
b) 3.14
c) 10
d) Compilation error
Answer: a
96. What is the correct way to convert a
float to int?
a) (int) myFloat;
b) int(myFloat);
c) static_cast<int>(myFloat);
d) All of the above
Answer: d
97. What happens when a double is
converted to int?
a) The fractional part is truncated.
b) The fractional part is rounded to the
nearest integer.
c) It throws a runtime error.
d) It results in a compilation error.
Answer: a

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

Memory Management (Continued)


116. What will the following code do?
cpp int* ptr = new int(10);
delete ptr;
a) Allocates memory and then frees it
b) Allocates memory and causes a memory
leak
c) Throws an exception
d) Deletes the pointer but not the memory
Answer: a
117. Which of the following is the correct
way to free a dynamically allocated array?
a) delete ptr[];
b) free(ptr);
c) delete[] ptr;
d) clear(ptr);
Answer: c
118. What is the main advantage of using
new and delete over malloc and free
in C++?
a) new and delete do not require casting
b) malloc and free are more efficient
c) new automatically initializes variables
d) malloc and free are not used in C++
Answer: a
119. What happens when memory is allocated
dynamically but not freed?
a) Memory is freed automatically
b) Memory leak occurs
c) Stack memory is used instead
d) None of the above
Answer: b
120. What is the correct way to allocate an
array dynamically in C++?
a) int arr[5];
b) int* arr = new int[5];
c) int* arr = malloc(5 *
sizeof(int));
d) int arr = new int[5];
Answer: b

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

Arrays and Vectors


126. What is the default size of an array in
C++?
a) 5
b) 10
c) It depends on the compiler
d) Arrays require a fixed size upon
declaration
Answer: d
127. Which of the following is the correct
way to declare an array of 10 integers in
C++?
a) int arr[10];
b) int arr = new int[10];
c) int arr[10] = {};
d) Both a and b
Answer: d
128. Which of the following is used to resize
a vector dynamically in C++?
a) resize()
b) reallocate()
c) resizeArray()
d) extend()
Answer: a
129. What is the output of the following
code?
cpp vector<int> v = {1, 2,
3}; v.push_back(4); cout <<
v[3];
a) 4
b) 3
c) 0
d) Undefined behavior
Answer: a
130. How do you access the last element of a
vector in C++?
a) v[last()]
b) v.end()
c) v.back()
d) v.last()
Answer: c

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

You might also like