- Home
- Subjects
- Computer Science MCQs
- C Programming
C Programming MCQs · Page 2
146 multiple choice questions with answers in Computer Science MCQs.
-
A
-4 to 3, because a three-bit field is treated as signed unless unsigned is repeated
-
B
0 to 7, with 8 stored as 0 because the value is kept modulo 8 by the field
Correct answer
-
C
0 to 7, with 8 raising a run-time overflow trap on conforming implementations
-
D
0 to 255, because bit-fields are always widened to a full byte when stored
-
A
b only, because the matching case block ends automatically at the next label
-
B
a, b and c, because every case label above the match is also evaluated in sequence
-
C
Nothing, because no case matches once the fall-through behaviour is detected
-
D
b then c, because execution continues into the later labels without any break
Correct answer
-
A
Because the increment operator is not defined for any pointer type in the language
-
B
Because a cast to char * is needed before assigning any value to a void pointer
-
C
Because pointer arithmetic needs a complete object type to know the element size
Correct answer
-
D
Because void pointers may only be compared and never assigned a fresh address
-
A
Inline functions may not appear in shared headers, because each unit expands its own copy
-
B
The compiler inlines both copies and thereby creates two symbols with the same name
-
C
C99 requires inline functions to be static, and the header omits the static keyword
-
D
An inline definition is not an external definition, so no unit emits a callable body
Correct answer
-
A
8, because the union is padded up to a multiple of its strictest member alignment
Correct answer
-
B
5, because the union occupies only the raw byte count of its largest individual member
-
C
13, because the byte sizes of all the members are added together inside a union
-
D
16, because every union is rounded up to a power of two that doubles the alignment
-
A
They are discarded immediately, because w modes truncate a file that already exists
Correct answer
-
B
They are preserved until the first write, which then overwrites the file from its beginning
-
C
They are preserved and writes append at the end, because + implies append access
-
D
The open call fails with an error, since w+ is only valid for files that do not exist
-
A
a[0] is indeterminate and a[4] is 7, since only the named element is touched
-
B
a[0] is 7 and a[4] is 0, because the first initialiser fills the leading element
-
C
Both are 7, because one designated initialiser applies to the whole aggregate
-
D
a[0] is 0 and a[4] is 7, since omitted elements are initialised as if static
Correct answer
-
A
Because the function reports read errors by returning a negative byte count
-
B
So the out-of-band EOF value cannot be confused with any valid character code
Correct answer
-
C
Because a returned character may be a multi-byte sequence that is wider than 8 bits
-
D
Because plain char is unsigned on most targets, which would otherwise discard the sign
-
A
Its storage duration becomes automatic, so it is created afresh on each function call
-
B
Its initialisation is deferred until the variable is first used inside the unit
-
C
Its value is placed in read-only memory, so only the defining unit can modify it
-
D
Its linkage becomes internal, so other translation units cannot refer to it by name
Correct answer
-
A
long, because the value exceeds the range of both int and unsigned int on this target
-
B
Undefined, because no signed 32-bit type can represent that particular bit pattern
-
C
unsigned int, because hexadecimal literals take the first type in their list that fits
Correct answer
-
D
int, because hexadecimal constants are signed by default unless a u suffix is supplied
-
A
Register, because the literal is a constant expression folded at compile time
-
B
Automatic, matching the enclosing block, so the array dies when that block exits
Correct answer
-
C
Static, so the array outlives the enclosing block and can safely be returned to its caller
-
D
Dynamic, because the compiler reserves the array with an implicit malloc call at run time
-
A
Undefined behaviour, because the shift count is not less than the operand's width
Correct answer
-
B
Defined to yield zero, because every set bit is shifted out of the 32-bit range entirely
-
C
Implementation-defined, so the compiler documents whether it wraps the count
-
D
A constraint violation, so a strictly conforming compiler must reject the code
-
A
The extra parentheses force a temporary object, so the postfix increment is discarded
-
B
The preprocessor evaluates i++ once and then passes the resulting value twice
-
C
The argument is substituted twice, so i is incremented twice in one expression
Correct answer
-
D
The macro expands into a full statement, so it cannot legally appear inside an expression
-
A
Exactly one byte, chosen so the type is large enough for the largest enumerator value
-
B
Exactly sizeof(int), because every enumerator is guaranteed to have type int in C
-
C
Exactly sizeof(long), because enums are stored in the widest integer type
-
D
An implementation-defined choice among char, signed int and unsigned int types
Correct answer
-
A
Assignment performs a shallow copy, so the two structs end up sharing member storage
-
B
Padding bytes may hold indeterminate values, so a byte comparison can report differences
Correct answer
-
C
The member-wise copy converts floating values, so bit patterns can differ in the last unit
-
D
memcmp inspects only the first member of the struct, ignoring all the trailing members
-
A
Accesses become atomic between threads, so no torn reads or partial writes can ever occur
-
B
A hardware memory barrier is inserted around each access to order it against other threads
-
C
The object is placed in a memory section that hardware cannot write behind the program's back
-
D
Every access written in the abstract machine is performed, so the compiler cannot cache it
Correct answer
-
A
int (*fp)(int); which means exactly the same thing as int *fp(int) under the C declarator precedence rules
-
B
int fp(*)(int); which places the pointer inside the parameter list rather than on the name
-
C
int (*fp)(int); which declares a function pointer, unlike int *fp(int) which declares a function
Correct answer
-
D
int *fp(int); which declares a function pointer, while int (*fp)(int) declares a function
-
A
4 and 3, because sizeof counts the terminating NUL while strlen does not
Correct answer
-
B
3 and 3, because both operators measure only the visible characters
-
C
3 and 4, because sizeof excludes the NUL that strlen includes in its count
-
D
4 and 4, because strlen also counts the implicit terminating NUL byte
-
A
memmove handles overlap by choosing a safe copy direction, while memcpy does not
Correct answer
-
B
memcpy refuses overlapping ranges at run time and returns a null pointer instead
-
C
memmove is needed only when the two ranges are exactly equal, not when they overlap
-
D
memcpy copies through a hidden temporary buffer, so it needs twice the stack space
-
A
fgets rewinds the file when it reaches the last record, so the final line is read again
-
B
feof returns true at the start of the last line, so the guard terminates far too early
-
C
puts flushes the buffer twice when the final newline is missing from the file
-
D
feof only becomes true after a read hits end of file, so one extra iteration runs
Correct answer
-
A
Only the characters before the newline, since the newline stays in the input stream
-
B
The line including its trailing newline character, if one was present in the input
Correct answer
-
C
The line with the newline stripped away, because fgets always truncates at the line terminator
-
D
The line with the newline replaced by a NUL plus the previous buffer contents kept
-
A
The copy is truncated to three bytes and a NUL fills the last position of dst
-
B
The call is rejected at run time and dst keeps its previous uninitialised bytes
-
C
The four bytes are copied with no terminating NUL, so dst is not a valid string
Correct answer
-
D
Three bytes are copied and a NUL is appended, since space is always reserved
-
A
They keep their latest modified values because the stack frame is never touched
-
B
They are reset to zero, since longjmp reinitialises the function's automatic objects
-
C
Their values are indeterminate if they were modified between setjmp and longjmp
Correct answer
-
D
Their values are reliably restored to the values they held at the setjmp call
-
A
uint8_t with value 44, because the sum wraps at 256 and is truncated back down
-
B
unsigned int with value 300, because uint8_t promotes to unsigned int on all targets
-
C
int with value 44, because the sum is computed in 8 bits and then sign-extended up
-
D
int with value 300, because both operands are promoted to int before the addition
Correct answer
-
A
2, because the remainder operator always normalises the result to a non-negative value
-
B
-2, because the quotient is floored downward, which shifts the remainder in the negative direction
-
C
1, because the sign of a remainder is taken from the divisor rather than from the dividend
-
D
-1, because division truncates toward zero, so the remainder keeps the dividend's sign
Correct answer