What is the result?

Given:

class X {
String str = “default”;
X(String s) { str = s;}
void print () { System.out.println(str); }
public static void main(String[] args) {
new X(“hello”).print();
}
}

What is the result?

Given:

class X {
String str = “default”;
X(String s) { str = s;}
void print () { System.out.println(str); }
public static void main(String[] args) {
new X(“hello”).print();
}
}

What is the result?

A.
hello

B.
default

C.
Compilation fails

D.
The program prints nothing

E.
An exception is thrown at run time

Explanation:
The program compiles fine.
The program runs fine.
The output is: hello



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Abed Abu Alhalawa

Abed Abu Alhalawa

A

James

James

The Answer is A.