ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
struct B { B& operator=(int) { cout << "B"; return *this; } };
struct D : B { };
int main() { D d; d = 5; }- A. B
- B. Nothing; 5 is converted to D
- C. Compile-time error: no operator= in D accepts an int
- D. Undefined behaviour
Correct answer: C. Compile-time error: no operator= in D accepts an int
D implicitly declares its own copy and move assignment operators, and they HIDE B::operator=(int). Neither accepts an int, so the call fails; using B::operator=; in D would make it print B.
More OOP with C++ and Java questions
- What is printed? cpp template <typename T> struct Box { static int count; Box() { count++; } }; template <typename T> int Box<T>::count =…
- What is printed? cpp struct N { int v; N(int x = 7) : v(x) { } }; void show(N n) { cout << n.v; } int main() { N a; N b = 3; show(4)…
- What is printed? java class P { void f(int x) { System.out.print("P-int "); } } class C extends P { void f(long x) {…
- What is printed? java BigDecimal a = new BigDecimal("1.0"), b = new BigDecimal("1.00"); Set<BigDecimal> h = new HashSet<>(List.of(a, b))…
- What is printed? java Stream.of("a", "bb", "ccc") .filter(s -> { System.out.print("f" + s.length()); return s.length() > 1; }) .map(s -> {…
- What is printed? java Stream.of(1, 2, 3).peek(System.out::print).map(x -> x 2); System.out.print("end");