Home
MCQs by exam
NTS
NTS MCQs · Page 114
5,604 multiple choice questions typical of the NTS exam, with answers and explanations. Switch on practice mode to test yourself first.
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
A
Always a null pointer, because zero bytes can never be allocated by any conforming malloc
B
Always a valid pointer to exactly one byte, because malloc rounds every request up to one byte
C
Undefined behaviour, so the program is invalid whatever pointer the call happens to return
D
A null pointer or a unique pointer that can be passed to free are both conforming outcomes
Correct answer
A
Undefined behaviour, because the string literal has static storage and must not be modified
Correct answer
B
It is well defined, but only on little-endian machines and only when the optimisation is disabled
C
It works as intended, because p is a non-const pointer that points at a writable array
D
It is a syntax error, because string literals cannot be indexed or assigned through any pointer
A
1 2 3
Correct answer
B
0 1 2
C
1 1 1
D
An error, because a static local may not have an initialiser
A
True, because size_t is promoted to int before the comparison under the usual arithmetic conversions in C here
B
A compile-time error, since signed and unsigned operands may never be compared without an explicit cast
C
False, because n is converted to size_t in the usual arithmetic conversions and becomes a very large value
Correct answer
D
True, because -1 is promoted to int and then compared as an ordinary signed value in this expression
A
sizeof(c) is 4 and sizeof('a') is 1, because the constant is narrower than the variable
B
sizeof(c) is 1 and sizeof('a') is 4, because a character constant has type int in C
Correct answer
C
Both are 1, since a character literal always has type char
D
Both are 4, since integer promotion also applies to the variable
A
8, because the parameter is adjusted to int * and sizeof then measures a pointer
Correct answer
B
40, because the declared bound of 10 ints is part of the parameter type
C
4, because the array name always decays to a single int
D
80, because the decayed pointer is doubled for bounds checking
Practise on paper
Preparing for NTS?
Make it your exam plan: see your progress in every subject and topic, set a daily goal and practise what you have not done yet.
Start my NTS plan
Test yourself
Take a timed online test and get a certificate, or play today's quick quiz.
Online tests
Daily quiz