ProgrammingVery hardMicrosoft/Google SDE MCQ · GATE CS
What is printed?
What is printed?
struct B {
B() { cout << "B"; }
B(const B&) { cout << "c"; }
};
struct D : B {
D() { cout << "D"; }
D(const D&) { cout << "k"; }
};
int main() {
D x;
D y = x;
}- A. BDck
- B. BDBk
- C. BDk
- D. BDcD
Correct answer: B. BDBk
A user-written copy constructor that does not mention the base in its initializer list default-constructs the base, so copying y prints B (not c) and then k. Writing D(const D& o) : B(o) would copy the base.
More OOP with C++ and Java questions
- What is printed? cpp struct P { int x; explicit P(int v) : x(v) { } explicit operator bool() const { return x != 0; } }; int main() { P…
- Which of the marked lines are compile errors? cpp int g(int x) { return x; } double g(int x) { return 2.0 x; } // line 1 void h(int x) { }…
- What is printed? java Map<String, Integer> m = new HashMap<>(); System.out.print(m.put("a", 1)); System.out.print(m.put("a", 2))…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.merge("k", 5, Integer::sum); m.merge("k", 5, Integer::sum); m.merge("k"…
- What is printed? java TreeMap<Integer, String> t = new TreeMap<>(); for (int k : new int[]{40, 10, 30, 20}) t.put(k, "v")…
- What is printed? java Deque<Integer> d = new ArrayDeque<>(); d.push(1); d.push(2); d.offer(3); d.addFirst(4); System.out.print(d.pop() +…