ProgrammingVery hardCapgemini · Infosys
What is printed? (C++, assume <iostream> and using namespace std)
What is printed? (C++, assume <iostream> and using namespace std)
struct S {
void f() { cout << "n"; }
void f() const { cout << "c"; }
};
int main() {
S s;
const S& r = s;
s.f();
r.f();
}- A. nn
- B. cc
- C. nc
- D. cn
Correct answer: C. nc
Overloads differing in const are chosen by the object's constness: non-const s uses f(), the const reference uses f() const.
More OOP with C++ and Java questions
- What does this Java method do when called? java static int f() { try { throw new RuntimeException("a"); } finally { return 5; } }
- What is printed? java String a = "ja" + "va"; String b = "java"; String c = "ja"; String d = c + "va"; System.out.print((a == b) + "," +…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct A { A() { cout << "A"; } }; struct B : virtual A { }; struct…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f(int x = 1) { cout <<…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void p() { cout << "A"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct X { string n; X(const char s) : n(s) {…