ProgrammingModerateInfosys · Wipro NLTH
What is printed?
What is printed?
static void f(int x) { System.out.print("int"); }
static void f(long x) { System.out.print("long"); }
static void f(double x) { System.out.print("double"); }
public static void main(String[] a) { byte b = 5; f(b); }- A. int
- B. long
- C. double
- D. Compile error
Correct answer: A. int
A byte widens to int, long or double; the most specific applicable overload, f(int), is chosen.
More OOP with C++ and Java questions
- What happens when this C++ program is compiled? cpp struct B { int x = 5; }; class D : B { }; int main() { D d; cout << d.x; }
- What is printed? java static int f() { try { return 1; } finally { System.out.print("F"); } } public static void main(String[] a) {…
- In Java, an interface declares int MAX = 10;. What happens if an implementing class executes MAX = 20; ?
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct A { A() { cout << "c"; } A(const A&) { cout << "k"; } ~A() {…
- What is printed? java class P { static String w() { return "P"; } } class Q extends P { static String w() { return "Q"; } } // in main: P…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct…