1. Home
  2. MCQs by exam
  3. Campus Interviews

Campus Interviews MCQs · Page 7

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

Showing 151–175 of 346

For objects a and b of class T, which special member is used by T b = a; and which by b = a; when b already exists?

  1. Copy-initialisation uses the copy constructor; assignment to an existing object uses the copy assignment operator Correct answer
  2. Both use the copy constructor, since the equals sign always means construction even for a live object
  3. Both use the copy assignment operator, since the equals sign always means assignment whatever the surrounding context
  4. T b = a; uses the default constructor followed by assignment, because b is an already constructed object
MCQSEXAM Read explanation

On an empty std::vector<int> v, what do v.reserve(100) and v.resize(100) each produce?

  1. Both set only capacity, differing solely in the exception guarantees offered when a reallocation fails
  2. reserve sets capacity to 100 with size still 0; resize sets size and capacity to 100 with value-initialised elements Correct answer
  3. Both set size to 100, but reserve leaves the new elements uninitialised until each element is written
  4. reserve sets size to 100 and resize only sets capacity, so writing v[0] directly after reserve is perfectly well defined
MCQSEXAM Read explanation

class D : public A, public B { Mem1 m1; Mem2 m2; public: D() : m2(2), m1(1), B(), A() {} }; In what order are bases and members initialised, and destroyed?

  1. m2, m1, B, A for initialisation, matching the initialiser list
  2. m1, m2, A, B for initialisation and A, B, m1, m2 for destruction
  3. A, B, m1, m2 for initialisation and the same order for destruction
  4. A, B, m1, m2 for initialisation and m2, m1, B, A for destruction Correct answer
MCQSEXAM Read explanation

class B { public: ~B() {} }; class D : public B { public: ~D() {} }; and then delete (B*)new D; is executed. What happens?

  1. Only D's destructor runs, because the dynamic type is D
  2. Undefined behaviour: only B's destructor runs, because B's destructor is not virtual Correct answer
  3. Both destructors run, since destructors are always virtual
  4. A compile error, because a base pointer cannot delete a derived object
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