1. Home
  2. MCQs by exam
  3. NTS
  4. Computer Science MCQs

NTS MCQs – Computer Science MCQs · Page 8

455 multiple choice questions typical of the NTS exam, with answers and explanations. Switch on practice mode to test yourself first.

Showing 176–200 of 455

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