ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
void f(int) { cout << "i"; }
void f(char) { cout << "c"; }
void f(double) { cout << "d"; }
int main() {
char a = 'a'; short s = 1; float x = 1;
f(a); f(s); f(x); f(a + a);
}- A. cidi
- B. cddc
- C. cici
- D. cidc
Correct answer: A. cidi
f(a) is an exact match (c). short has no exact overload, and promotion to int beats conversion to char or double (i). float promotes to double (d). a + a has type int after integral promotion (i).
More OOP with C++ and Java questions
- Which statement about this code is correct? cpp struct B { virtual void f() const { } }; struct D : B { void f() override { } };
- What is printed? cpp struct B { int x = 5; }; struct D : B { int x = 7; }; int main() { D d; B& b = d; cout << d.x << b.x << d.B::x; }
- What is printed? java class P { String f = "P"; static String s() { return "P"; } String i() { return "P"; } } class C extends P { String…
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("L"); } static void f(Integer x) {…
- What is printed? java String x = "a"; final String y = "a"; System.out.print(("ab" == x + "b") + " "); System.out.print(("ab" == y + "b")…
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static…