ProgrammingHardGATE CS · TCS Digital · Advanced
What is printed? (C++17, assume <iostream> and using namespace std)
What is printed? (C++17, assume <iostream> and using namespace std)
struct T {
T() { cout << "c"; }
~T() { cout << "d"; }
};
int main() {
{
const T& r = T();
cout << "1";
}
{
T();
cout << "2";
}
}- A. c1dcd2
- B. cd1cd2
- C. c1cd2d
- D. c1dc2d
Correct answer: A. c1dcd2
A temporary bound to a const reference lives as long as the reference (destroyed after '1'); an unbound temporary dies at the end of its full expression, before '2' is printed.
More OOP with C++ and Java questions
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() { super()…
- What is printed? java Long l = 127L; Integer i = 127; System.out.print(l.equals(i) + " " + l.equals(127L) + " " + (l == 127) + " " +…
- What is printed? java static void f(long x) { System.out.print("L"); } static void f(Integer x) { System.out.print("I"); } static void…
- What is printed? java System.out.print(true ? 1 : 2.0); System.out.print(true ? 'a' : 0); int i = 0; System.out.print(true ? 'a' : i);
- What is printed? java static int f() { try { throw new RuntimeException(); } finally { return 1; } } static int g() { int x = 1; try {…
- What is printed? java class K { int v; K(int v) { this.v = v; } public int hashCode() { return v; } public boolean equals(Object o) {…