1. Home
  2. MCQs by exam
  3. NTS

NTS MCQs · Page 89

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

Showing 2201–2225 of 5,604

A std::unordered_map is rehashed by an insertion. Which of its element references survive that operation?

  1. References and pointers to elements stay valid, but the iterators are invalidated Correct answer
  2. Nothing survives: all iterators, pointers and references are invalidated at once
  3. Only iterators survive, because buckets are relinked while the nodes are reallocated
  4. Everything survives, since rehashing only changes the size of the bucket array
MCQSEXAM Read explanation

int* p = new int[10]; delete p; is executed. What is the defect?

  1. It fails to compile, because delete cannot be applied to an array new expression
  2. The form of delete does not match the form of new, so the behaviour is undefined Correct answer
  3. Only the first element is destroyed and the remaining nine are leaked silently
  4. The delete is acceptable, but the ten ints stay allocated until the program exits
MCQSEXAM Read explanation

A const member function needs to update a cache counter. Which change is legitimate, and why?

  1. Cast away const, which is well defined even for an actually const object
  2. Mark the member function constexpr, since constant evaluation permits such writes
  3. Mark the member mutable, so that a const member function can still modify it Correct answer
  4. Mark the member static, because const-qualification then no longer applies to it
MCQSEXAM Read explanation

A std::function<void()> f wraps a lambda that captures a large array by value. What is the practical consequence?

  1. The capture is moved to the heap once and shared between copies, making copies cheap
  2. std::function rejects capturing lambdas, so the code fails to compile
  3. The closure object lives inside f, so copying f copies the whole captured array Correct answer
  4. The lambda decays to a plain function pointer, so the captured array is not kept alive
MCQSEXAM Read explanation

class D : public B1, public B2, where both B1 and B2 virtually inherit A. How many A subobjects does a D contain?

  1. It depends on the compiler, since merging of virtual bases is left implementation-defined
  2. Exactly one, shared by both paths, because virtual bases are merged into one subobject Correct answer
  3. Two, one reached through B1 and one through B2, addressed by distinct base offsets
  4. Zero, because virtual inheritance removes the base class entirely from the layout
MCQSEXAM Read explanation

Two objects hold std::shared_ptr to each other. Why is neither destructor called when the last external owner releases them?

  1. shared_ptr detects cycles and defers destruction to the next garbage collection
  2. The control blocks are leaked first, so the destructors run but the memory does not
  3. Each object's reference count stays at one because of the mutual strong references Correct answer
  4. The reference counts are merged into a single counter, so they never reach zero
MCQSEXAM Read explanation

What does std::array<int, 4> provide that a plain int[4] does not?

  1. Value semantics and a size() member, while the elements stay contiguous in memory Correct answer
  2. Heap allocation, so very large arrays do not consume space in the caller's stack frame
  3. Automatic bounds checking on every operator[] access in all build configurations
  4. Dynamic resizing, so extra elements can be appended after the object is constructed
MCQSEXAM Read explanation

std::sort(v.begin(), v.end(), [](int a, int b){ return a >= b; }); is called. What is the defect?

  1. std::sort needs a function pointer, so a lambda cannot be used as the predicate
  2. The predicate is fine, but it makes std::sort fall back to a stable bubble sort
  3. The predicate is not a strict weak ordering, so the sort behaviour is undefined Correct answer
  4. The predicate sorts descending, which std::sort rejects with a compile error
MCQSEXAM Read explanation

For int x = 0; what do decltype(x) and decltype((x)) yield?

  1. int and int, because parentheses are ignored entirely by the decltype specifier
  2. int& and int&, because decltype always adds a reference to any named variable
  3. int and int&, because a parenthesised lvalue expression produces a reference type Correct answer
  4. int& and int, because the extra pair of parentheses strips the reference from the type
MCQSEXAM Read explanation

A reference int& r = v[0]; is taken, then v.push_back(99) triggers a reallocation. What is the state of r?

  1. It becomes a reference to a default-constructed int placed in the new buffer
  2. It dangles, because growth allocates a new buffer and destroys the old elements Correct answer
  3. It remains valid, since vector guarantees reference stability across insertions
  4. It is rebound automatically to the new buffer at the same logical index position
MCQSEXAM Read explanation

For std::vector<std::string> v, how do v.push_back("abc") and v.emplace_back("abc") differ?

  1. emplace_back is only valid for trivially copyable types such as int and double values
  2. The two calls are identical in every respect, including the number of constructions
  3. emplace_back builds the string in place from the literal, push_back moves a temporary in Correct answer
  4. emplace_back copies an already-built string object, while push_back builds one from the literal
MCQSEXAM Read explanation

std::string_view sv = std::string("temp"); is stored as a class member. What is wrong with that?

  1. sv keeps the temporary alive by extending its lifetime, exactly like const& binding
  2. sv refers to a temporary destroyed at the end of the full expression, so it dangles Correct answer
  3. sv owns its own copy of the characters, so the member wastes memory but stays valid
  4. sv is rejected at compile time, because a view cannot bind to a temporary string
MCQSEXAM Read explanation

Why does declaring a move constructor noexcept change how std::vector relocates its elements when the buffer grows?

  1. vector then moves elements instead of copying them, which keeps the strong guarantee Correct answer
  2. vector refuses to grow the buffer at all once the move constructor is declared noexcept
  3. vector starts using memcpy on the elements, since noexcept proves they are trivial
  4. vector falls back to copying, because noexcept disables the move constructor entirely
MCQSEXAM Read explanation

A const std::string s is passed to a constructor call as std::move(s). Which constructor runs, and why?

  1. The move constructor, because std::move casts its argument to an rvalue reference
  2. A compile-time error, because std::move is deleted for const-qualified arguments by design
  3. The move constructor, because const objects are always treated as expiring values here
  4. The copy constructor, because std::move yields a const rvalue that cannot bind to T&& Correct answer
MCQSEXAM Read explanation

Given const int n = 5; const int& r = n; what types are deduced for auto a = r; and auto& b = r;?

  1. a is const int& and b is const int&, because auto copies the initialiser's type exactly
  2. a is int and b is int, because auto& also drops the const qualifier from the referent
  3. a is const int and b is const int&, because auto keeps const but removes the reference
  4. a is int and b is const int&, because plain auto strips references and top-level const Correct answer
MCQSEXAM Read explanation

struct F { unsigned flag : 3; }; is declared. What range can flag hold, and what happens when 8 is assigned?

  1. -4 to 3, because a three-bit field is treated as signed unless unsigned is repeated
  2. 0 to 7, with 8 stored as 0 because the value is kept modulo 8 by the field Correct answer
  3. 0 to 7, with 8 raising a run-time overflow trap on conforming implementations
  4. 0 to 255, because bit-fields are always widened to a full byte when stored
MCQSEXAM Read explanation

int x = 2; switch (x) { case 1: puts("a"); case 2: puts("b"); case 3: puts("c"); } What is printed?

  1. b only, because the matching case block ends automatically at the next label
  2. a, b and c, because every case label above the match is also evaluated in sequence
  3. Nothing, because no case matches once the fall-through behaviour is detected
  4. b then c, because execution continues into the later labels without any break Correct answer
MCQSEXAM Read explanation

A header defines inline int sq(int n) { return n*n; } and two translation units include it. Why can the link step fail?

  1. Inline functions may not appear in shared headers, because each unit expands its own copy
  2. The compiler inlines both copies and thereby creates two symbols with the same name
  3. C99 requires inline functions to be static, and the header omits the static keyword
  4. An inline definition is not an external definition, so no unit emits a callable body Correct answer
MCQSEXAM Read explanation

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