1. Home
  2. Subjects
  3. Computer Science MCQs
  4. C Programming

C Programming MCQs · Page 3

146 multiple choice questions with answers in Computer Science MCQs.

Showing 51–75 of 146

int a[5]; int *p = a + 5; is this pointer valid?

  1. No; any pointer beyond the last element is undefined behaviour even if not dereferenced
  2. Yes, and *p reads the value immediately after the array
  3. No; p must be assigned a[4] because arrays are zero-based
  4. Yes; forming a pointer one past the last element is allowed, but dereferencing it is not Correct answer
MCQSEXAM Read explanation

int x = INT_MAX; x = x + 1; what does the C standard specify?

  1. x wraps around to INT_MIN, exactly as unsigned arithmetic does, because two's complement hardware wraps
  2. The behaviour is implementation-defined, so each compiler must document the value that x receives in this case
  3. A runtime trap is guaranteed on every conforming implementation, so the program aborts instead of wrapping
  4. Signed overflow is undefined behaviour, so the compiler may assume it never happens and optimise accordingly Correct answer
MCQSEXAM Read explanation

What is the practical difference between #include <stdio.h> and #include "myheader.h"?

  1. 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
  2. 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
  3. 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
  4. 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
MCQSEXAM Read explanation

What does the restrict qualifier on a pointer parameter promise the compiler?

  1. That the object it points to is accessed only through that pointer and pointers derived from it within the block Correct answer
  2. That the pointer is never null and always points to a valid object for the whole duration of the call
  3. That the pointed-to object is read-only for the duration of the call, so the compiler may cache its value in registers
  4. That the pointer variable itself cannot be reassigned inside the function, although the pointee may change
MCQSEXAM Read explanation

A C99 struct is declared as struct Buf { size_t len; char data[]; }; on a 64-bit LP64 system. What does sizeof(struct Buf) evaluate to?

  1. The size of size_t plus any trailing padding, because the flexible array member holds no elements Correct answer
  2. The size of size_t plus one, because a flexible array member must always hold at least one element
  3. It is rejected at compile time, because an array member may not be declared with an empty subscript
  4. The size of a pointer, because data decays to a char pointer stored inside the struct
MCQSEXAM Read explanation

On a platform where plain char is signed, char c = 200; is followed by int i = c; what is i?

  1. 200, because a char always holds the value assigned to it at initialisation
  2. 56, because the sign bit is cleared during promotion and only the low seven bits survive
  3. -56, because 200 does not fit in a signed 8-bit char and wraps to 200 minus 256 Correct answer
  4. Indeterminate, because the conversion is undefined behaviour the compiler may optimise away
MCQSEXAM Read explanation

size_t n = 10; printf("%d", n); is compiled on LP64. What is the problem?

  1. The compiler must reject the program as a constraint violation, since the argument types do not match
  2. The format expects int while size_t is an 8-byte unsigned type, so behaviour is undefined; %zu is correct Correct answer
  3. It is correct, because size_t is a typedef for int on every platform, so the widths always agree
  4. It prints 10 reliably, because variadic arguments are promoted to int before printf reads the value passed
MCQSEXAM Read explanation

A qsort comparator for int returns (a - b) and is used on values close to INT_MIN and INT_MAX. Why is this a defect?

  1. The comparator must return only 0 or 1, and any other return value is ignored by the qsort implementation
  2. Signed overflow in the subtraction is undefined behaviour, so the sign of the result can be wrong Correct answer
  3. qsort requires the comparator to return a long rather than an int, so the subtraction must be widened
  4. qsort calls the comparator once per element, so ties between equal keys are never detected at all
MCQSEXAM Read explanation

A buffer is grown with p = realloc(p, 2 * n); and the call fails. What is the correct statement?

  1. The original block is automatically freed before the call returns, so p is left as a null pointer
  2. The original block is truncated to half its size before the failure, so the buffer keeps only its data
  3. realloc cannot fail when the new size is larger than the old one, because growth always happens in place
  4. The original block is untouched and still allocated, so assigning the result directly to p leaks it Correct answer
MCQSEXAM Read explanation

int a[3] = {10, 20, 30}; int *p = a; int x = *p++; what are x and the new value of *p?

  1. x is 11, because the increment applies to the pointed-to int rather than to the pointer itself
  2. x is 10 and *p is now 20, because postfix ++ binds tighter than * and yields the old pointer value Correct answer
  3. x is 10 and p still points at a[0], because the unary * is evaluated before the postfix increment
  4. x is 20, because the pointer is incremented before it is dereferenced and the old value is discarded
MCQSEXAM Read explanation

In C (not C++), what is the status of storing a value in one union member and then reading a different member of the same size?

  1. Undefined behaviour in C as well, exactly as it is in C++, so the compiler may assume it never happens
  2. A constraint violation, so a conforming compiler must reject the program instead of silently reinterpreting it
  3. Permitted; the bytes are reinterpreted and the result is implementation-defined, which is called type punning Correct answer
  4. A conversion that applies the usual arithmetic conversions to the stored bytes before reading them back
MCQSEXAM Read explanation

What does the C standard guarantee about the return value of malloc(0)?

  1. Always a null pointer, because zero bytes can never be allocated by any conforming malloc
  2. Always a valid pointer to exactly one byte, because malloc rounds every request up to one byte
  3. Undefined behaviour, so the program is invalid whatever pointer the call happens to return
  4. A null pointer or a unique pointer that can be passed to free are both conforming outcomes Correct answer
MCQSEXAM Read explanation

char *p = "hello"; p[0] = 'H'; is executed. What is the status of this program under the C standard?

  1. Undefined behaviour, because the string literal has static storage and must not be modified Correct answer
  2. It is well defined, but only on little-endian machines and only when the optimisation is disabled
  3. It works as intended, because p is a non-const pointer that points at a writable array
  4. It is a syntax error, because string literals cannot be indexed or assigned through any pointer
MCQSEXAM Read explanation

For int n = -1; size_t sz = 4; what is the value of (n < sz) in C, and why?

  1. True, because size_t is promoted to int before the comparison under the usual arithmetic conversions in C here
  2. A compile-time error, since signed and unsigned operands may never be compared without an explicit cast
  3. False, because n is converted to size_t in the usual arithmetic conversions and becomes a very large value Correct answer
  4. True, because -1 is promoted to int and then compared as an ordinary signed value in this expression
MCQSEXAM Read explanation

Consider void f(int a[10]) { printf("%zu", sizeof a); } called with an int arr[10] on a 64-bit LP64 platform. What does the sizeof expression print?

  1. 8, because the parameter is adjusted to int * and sizeof then measures a pointer Correct answer
  2. 40, because the declared bound of 10 ints is part of the parameter type
  3. 4, because the array name always decays to a single int
  4. 80, because the decayed pointer is doubled for bounds checking
MCQSEXAM Read explanation