1. Home
  2. MCQs by exam
  3. Campus Interviews
  4. Computer Science MCQs

Campus Interviews MCQs – Computer Science MCQs · Page 4

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

Showing 76–100 of 346

A destructor throws while the stack is already unwinding from another exception. What is the outcome in C++11 and later?

  1. Both exceptions are merged into a std::nested_exception and rethrown together
  2. The program continues normally, with the destructor's exception silently discarded
  3. std::terminate is called, because two active exceptions cannot propagate at once Correct answer
  4. The new exception replaces the old one and unwinding continues from that point
MCQSEXAM Read explanation

An iterator it points into a std::map. The statement it = m.erase(it); is executed. What does it hold afterwards?

  1. The value m.begin(), because erasing resets all stored iterators
  2. The iterator to the following element, which erase returned by value Correct answer
  3. An invalidated iterator, so incrementing it afterwards is undefined behaviour
  4. The iterator to the erased element, which remains allocated but detached
MCQSEXAM Read explanation

A single-argument constructor is marked explicit. What does that prevent?

  1. Direct-initialisation, so objects can only be created through a factory function
  2. Copy construction, so the type can no longer be stored inside standard containers
  3. Aggregate initialisation, since the class is no longer an aggregate after the change
  4. Implicit conversion from the argument type at call sites and in copy-initialisation Correct answer
MCQSEXAM Read explanation

A lambda captures a local int by reference with [&] and is stored in a std::function that outlives the enclosing function. What happens when it is invoked later?

  1. It reads a valid value, because std::function extends the lifetime of captures
  2. It reads a dangling reference, because the local died with its stack frame Correct answer
  3. It reads the value the local held, because capture by reference copies on escape
  4. It throws std::bad_function_call, since the captured reference has expired
MCQSEXAM Read explanation

Why can a function template not be partially specialised in C++?

  1. Function templates are chosen by overload resolution, so partial ordering fills that role Correct answer
  2. Partial specialisation of function templates was removed by the C++11 standard revision
  3. A function template needs a class template wrapper before it can be specialised at all
  4. Partial specialisation is allowed, but only when every template parameter is a reference type
MCQSEXAM Read explanation

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