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

NTS MCQs – Computer Science MCQs · Page 5

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

Showing 101–125 of 455

What is sizeof for union U { char c[5]; double d; } on a platform where double needs 8-byte alignment?

  1. 8, because the union is padded up to a multiple of its strictest member alignment Correct answer
  2. 5, because the union occupies only the raw byte count of its largest individual member
  3. 13, because the byte sizes of all the members are added together inside a union
  4. 16, because every union is rounded up to a power of two that doubles the alignment
MCQSEXAM Read explanation

A file already containing 500 bytes is opened with fopen(path, "w+"). What happens to the existing contents?

  1. They are discarded immediately, because w modes truncate a file that already exists Correct answer
  2. They are preserved until the first write, which then overwrites the file from its beginning
  3. They are preserved and writes append at the end, because + implies append access
  4. The open call fails with an error, since w+ is only valid for files that do not exist
MCQSEXAM Read explanation

int a[6] = {[4] = 7}; is compiled by a C99 compiler. What are the values of a[0] and a[4]?

  1. a[0] is indeterminate and a[4] is 7, since only the named element is touched
  2. a[0] is 7 and a[4] is 0, because the first initialiser fills the leading element
  3. Both are 7, because one designated initialiser applies to the whole aggregate
  4. a[0] is 0 and a[4] is 7, since omitted elements are initialised as if static Correct answer
MCQSEXAM Read explanation

Why does the standard library declare getchar to return int rather than char?

  1. Because the function reports read errors by returning a negative byte count
  2. So the out-of-band EOF value cannot be confused with any valid character code Correct answer
  3. Because a returned character may be a multi-byte sequence that is wider than 8 bits
  4. Because plain char is unsigned on most targets, which would otherwise discard the sign
MCQSEXAM Read explanation

A variable is defined as static int counter; at file scope in one translation unit. What does the static keyword change?

  1. Its storage duration becomes automatic, so it is created afresh on each function call
  2. Its initialisation is deferred until the variable is first used inside the unit
  3. Its value is placed in read-only memory, so only the defining unit can modify it
  4. Its linkage becomes internal, so other translation units cannot refer to it by name Correct answer
MCQSEXAM Read explanation

On a platform with 32-bit int and 32-bit unsigned int, what is the type of the literal 0x80000000?

  1. long, because the value exceeds the range of both int and unsigned int on this target
  2. Undefined, because no signed 32-bit type can represent that particular bit pattern
  3. unsigned int, because hexadecimal literals take the first type in their list that fits Correct answer
  4. int, because hexadecimal constants are signed by default unless a u suffix is supplied
MCQSEXAM Read explanation

A compound literal (int[]){1, 2, 3} appears inside a function body in C99. What is its storage duration?

  1. Register, because the literal is a constant expression folded at compile time
  2. Automatic, matching the enclosing block, so the array dies when that block exits Correct answer
  3. Static, so the array outlives the enclosing block and can safely be returned to its caller
  4. Dynamic, because the compiler reserves the array with an implicit malloc call at run time
MCQSEXAM Read explanation

int x = 5; x << 32; is compiled for a platform whose int is 32 bits wide. What is the status of that shift?

  1. Undefined behaviour, because the shift count is not less than the operand's width Correct answer
  2. Defined to yield zero, because every set bit is shifted out of the 32-bit range entirely
  3. Implementation-defined, so the compiler documents whether it wraps the count
  4. A constraint violation, so a strictly conforming compiler must reject the code
MCQSEXAM Read explanation

What goes wrong when the macro #define SQ(x) ((x)*(x)) is invoked as SQ(i++)?

  1. The extra parentheses force a temporary object, so the postfix increment is discarded
  2. The preprocessor evaluates i++ once and then passes the resulting value twice
  3. The argument is substituted twice, so i is incremented twice in one expression Correct answer
  4. The macro expands into a full statement, so it cannot legally appear inside an expression
MCQSEXAM Read explanation

A compiler must implement an enum whose enumerators fit comfortably inside a char. What does the C standard actually require of its size?

  1. Exactly one byte, chosen so the type is large enough for the largest enumerator value
  2. Exactly sizeof(int), because every enumerator is guaranteed to have type int in C
  3. Exactly sizeof(long), because enums are stored in the widest integer type
  4. An implementation-defined choice among char, signed int and unsigned int types Correct answer
MCQSEXAM Read explanation

Two struct objects hold identical member values but were filled separately. Why can memcmp on them be unreliable?

  1. Assignment performs a shallow copy, so the two structs end up sharing member storage
  2. Padding bytes may hold indeterminate values, so a byte comparison can report differences Correct answer
  3. The member-wise copy converts floating values, so bit patterns can differ in the last unit
  4. memcmp inspects only the first member of the struct, ignoring all the trailing members
MCQSEXAM Read explanation

What does the volatile qualifier actually guarantee for an object in a C program?

  1. Accesses become atomic between threads, so no torn reads or partial writes can ever occur
  2. A hardware memory barrier is inserted around each access to order it against other threads
  3. The object is placed in a memory section that hardware cannot write behind the program's back
  4. Every access written in the abstract machine is performed, so the compiler cannot cache it Correct answer
MCQSEXAM Read explanation

A variable must hold a pointer to a function taking an int and returning int. Which declaration is right, and how does it compare with the alternative?

  1. int (*fp)(int); which means exactly the same thing as int *fp(int) under the C declarator precedence rules
  2. int fp(*)(int); which places the pointer inside the parameter list rather than on the name
  3. int (*fp)(int); which declares a function pointer, unlike int *fp(int) which declares a function Correct answer
  4. int *fp(int); which declares a function pointer, while int (*fp)(int) declares a function
MCQSEXAM Read explanation

A buffer's contents must be shifted three bytes to the right inside the same array. Why is memmove required instead of memcpy?

  1. memmove handles overlap by choosing a safe copy direction, while memcpy does not Correct answer
  2. memcpy refuses overlapping ranges at run time and returns a null pointer instead
  3. memmove is needed only when the two ranges are exactly equal, not when they overlap
  4. memcpy copies through a hidden temporary buffer, so it needs twice the stack space
MCQSEXAM Read explanation

Why does the loop while (!feof(fp)) { fgets(line, n, fp); puts(line); } often print the last line twice?

  1. fgets rewinds the file when it reaches the last record, so the final line is read again
  2. feof returns true at the start of the last line, so the guard terminates far too early
  3. puts flushes the buffer twice when the final newline is missing from the file
  4. feof only becomes true after a read hits end of file, so one extra iteration runs Correct answer
MCQSEXAM Read explanation

A line is read with fgets(buf, sizeof buf, stdin). What does buf contain when the whole input line fits in the buffer?

  1. Only the characters before the newline, since the newline stays in the input stream
  2. The line including its trailing newline character, if one was present in the input Correct answer
  3. The line with the newline stripped away, because fgets always truncates at the line terminator
  4. The line with the newline replaced by a NUL plus the previous buffer contents kept
MCQSEXAM Read explanation

char dst[4]; strncpy(dst, "abcd", 4); is executed. What is the state of dst afterwards?

  1. The copy is truncated to three bytes and a NUL fills the last position of dst
  2. The call is rejected at run time and dst keeps its previous uninitialised bytes
  3. The four bytes are copied with no terminating NUL, so dst is not a valid string Correct answer
  4. Three bytes are copied and a NUL is appended, since space is always reserved
MCQSEXAM Read explanation

After longjmp returns control to the setjmp point, what is guaranteed about non-volatile automatic variables of the function that called setjmp?

  1. They keep their latest modified values because the stack frame is never touched
  2. They are reset to zero, since longjmp reinitialises the function's automatic objects
  3. Their values are indeterminate if they were modified between setjmp and longjmp Correct answer
  4. Their values are reliably restored to the values they held at the setjmp call
MCQSEXAM Read explanation

Given uint8_t a = 200, b = 100; what is the type and value of the expression a + b in a typical C implementation?

  1. uint8_t with value 44, because the sum wraps at 256 and is truncated back down
  2. unsigned int with value 300, because uint8_t promotes to unsigned int on all targets
  3. int with value 44, because the sum is computed in 8 bits and then sign-extended up
  4. int with value 300, because both operands are promoted to int before the addition Correct answer
MCQSEXAM Read explanation

In C99 and later, the expression -7 % 3 is evaluated. What value results, and which rule fixes it?

  1. 2, because the remainder operator always normalises the result to a non-negative value
  2. -2, because the quotient is floored downward, which shifts the remainder in the negative direction
  3. 1, because the sign of a remainder is taken from the divisor rather than from the dividend
  4. -1, because division truncates toward zero, so the remainder keeps the dividend's sign Correct answer
MCQSEXAM Read explanation

What fundamentally distinguishes a container from a virtual machine?

  1. A container includes a hypervisor of its own, while a VM does not, so a container needs more privileged hardware
  2. Containers cannot be networked, unlike virtual machines, which each receive their own network stack
  3. A VM shares the host kernel while a container virtualises the hardware, which is why a VM starts faster
  4. Containers share the host kernel and isolate at the process level, whereas each VM runs its own guest kernel Correct answer
MCQSEXAM Read explanation

Declarative infrastructure-as-code tools are described as idempotent. What does that mean in practice?

  1. Resources are created only in the first apply of each day, and later runs are silently skipped
  2. The tool rolls back every change made by a failed run, restoring the previous state automatically and safely
  3. Applying the same template repeatedly converges to the same state instead of creating duplicate resources Correct answer
  4. Each apply must be run exactly once, since repeated runs change resource identifiers every time
MCQSEXAM Read explanation

Which response header primarily controls how long a CDN edge may serve a cached object before revalidating?

  1. Cache-Control with max-age, and s-maxage which overrides it for shared caches Correct answer
  2. Content-Length, which sets the freshness window in seconds for the response
  3. ETag, which fixes the cache lifetime and forces revalidation on every single request
  4. Vary, which sets a separate cache lifetime per client and per user agent string
MCQSEXAM Read explanation