Home
Subjects
Computer Science MCQs
Computer Science MCQs · Page 7
992 multiple choice questions with answers across 24 topics.
⚔️
Challenge a friend on Computer Science MCQs
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
A
Nothing; all special members are still generated normally
B
The move constructor and move assignment operator
Correct answer
C
The default constructor and the copy constructor
D
Only the destructor, which must then be defined by the user
A
Copy-initialisation uses the copy constructor; assignment to an existing object uses the copy assignment operator
Correct answer
B
Both use the copy constructor, since the equals sign always means construction even for a live object
C
Both use the copy assignment operator, since the equals sign always means assignment whatever the surrounding context
D
T b = a; uses the default constructor followed by assignment, because b is an already constructed object
A
Both set only capacity, differing solely in the exception guarantees offered when a reallocation fails
B
reserve sets capacity to 100 with size still 0; resize sets size and capacity to 100 with value-initialised elements
Correct answer
C
Both set size to 100, but reserve leaves the new elements uninitialised until each element is written
D
reserve sets size to 100 and resize only sets capacity, so writing v[0] directly after reserve is perfectly well defined
A
m2, m1, B, A for initialisation, matching the initialiser list
B
m1, m2, A, B for initialisation and A, B, m1, m2 for destruction
C
A, B, m1, m2 for initialisation and the same order for destruction
D
A, B, m1, m2 for initialisation and m2, m1, B, A for destruction
Correct answer
A
0, because the class has no members
B
4, because every object needs a vtable pointer
C
8 on 64-bit platforms, matching the pointer size
D
1, because distinct objects must have distinct addresses
Correct answer
A
Only D's destructor runs, because the dynamic type is D
B
Undefined behaviour: only B's destructor runs, because B's destructor is not virtual
Correct answer
C
Both destructors run, since destructors are always virtual
D
A compile error, because a base pointer cannot delete a derived object
A
No; any pointer beyond the last element is undefined behaviour even if not dereferenced
B
Yes, and *p reads the value immediately after the array
C
No; p must be assigned a[4] because arrays are zero-based
D
Yes; forming a pointer one past the last element is allowed, but dereferencing it is not
Correct answer
A
x wraps around to INT_MIN, exactly as unsigned arithmetic does, because two's complement hardware wraps
B
The behaviour is implementation-defined, so each compiler must document the value that x receives in this case
C
A runtime trap is guaranteed on every conforming implementation, so the program aborts instead of wrapping
D
Signed overflow is undefined behaviour, so the compiler may assume it never happens and optimise accordingly
Correct answer
A
Both forms behave identically in every compiler, and the difference between them is purely stylistic and has no effect on the search order at all
B
The quoted form searches the including file's directory or a configured path first, then the system paths, while the angle form searches only the system paths
Correct answer
C
The angle form is reserved for C++ standard library headers and the quoted form for C project headers, so the two never overlap in practice
D
The quoted form embeds the file as a string literal rather than including it, so any macros defined in that file are not visible to the rest of the translation unit
A
That the object it points to is accessed only through that pointer and pointers derived from it within the block
Correct answer
B
That the pointer is never null and always points to a valid object for the whole duration of the call
C
That the pointed-to object is read-only for the duration of the call, so the compiler may cache its value in registers
D
That the pointer variable itself cannot be reassigned inside the function, although the pointee may change
A
4
B
0
C
24
D
10
Correct answer
A
return (void)0;
B
return NULL;
C
return;
Correct answer
D
return 0;
A
12
Correct answer
B
6
C
9
D
16
A
The size of size_t plus any trailing padding, because the flexible array member holds no elements
Correct answer
B
The size of size_t plus one, because a flexible array member must always hold at least one element
C
It is rejected at compile time, because an array member may not be declared with an empty subscript
D
The size of a pointer, because data decays to a char pointer stored inside the struct
A
200, because a char always holds the value assigned to it at initialisation
B
56, because the sign bit is cleared during promotion and only the low seven bits survive
C
-56, because 200 does not fit in a signed 8-bit char and wraps to 200 minus 256
Correct answer
D
Indeterminate, because the conversion is undefined behaviour the compiler may optimise away
A
5
B
8
C
20
Correct answer
D
4
A
The compiler must reject the program as a constraint violation, since the argument types do not match
B
The format expects int while size_t is an 8-byte unsigned type, so behaviour is undefined; %zu is correct
Correct answer
C
It is correct, because size_t is a typedef for int on every platform, so the widths always agree
D
It prints 10 reliably, because variadic arguments are promoted to int before printf reads the value passed
A
Only the innermost loop that lexically encloses the break
Correct answer
B
Both loops, since break always leaves the outermost loop
C
The entire function, returning control to the caller
D
The current iteration of both loops, then continues with the next outer iteration
A
The number of format specifiers in the format string
B
EOF, matching the convention used by getchar
C
The number of characters transmitted to the output stream
Correct answer
D
Zero on success and a negative value on failure only
A
The comparator must return only 0 or 1, and any other return value is ignored by the qsort implementation
B
Signed overflow in the subtraction is undefined behaviour, so the sign of the result can be wrong
Correct answer
C
qsort requires the comparator to return a long rather than an int, so the subtraction must be widened
D
qsort calls the comparator once per element, so ties between equal keys are never detected at all
A
The original block is automatically freed before the call returns, so p is left as a null pointer
B
The original block is truncated to half its size before the failure, so the buffer keeps only its data
C
realloc cannot fail when the new size is larger than the old one, because growth always happens in place
D
The original block is untouched and still allocated, so assigning the result directly to p leaks it
Correct answer
A
volatile int * const p
B
const int *p
Correct answer
C
int * const p
D
const int * const p
A
x is 11, because the increment applies to the pointed-to int rather than to the pointer itself
B
x is 10 and *p is now 20, because postfix ++ binds tighter than * and yields the old pointer value
Correct answer
C
x is 10 and p still points at a[0], because the unary * is evaluated before the postfix increment
D
x is 20, because the pointer is incremented before it is dereferenced and the old value is discarded
A
5 and 5
B
5 and 6
C
6 and 6
D
6 and 5
Correct answer
A
Undefined behaviour in C as well, exactly as it is in C++, so the compiler may assume it never happens
B
A constraint violation, so a conforming compiler must reject the program instead of silently reinterpreting it
C
Permitted; the bytes are reinterpreted and the result is implementation-defined, which is called type punning
Correct answer
D
A conversion that applies the usual arithmetic conversions to the stored bytes before reading them back