Given:
public class Runner {
public static String name = “unknown”;
public void start() {
System.out.println(name);
}
public static void main(String[] args) {
name = “Daniel”;
start();
}
}
What is the result?
A.
Daniel
B.
Unknown
C.
It may print”unknown”or”Daniel”depending on the JVM implementation.
D.
Compilation fails.
E.
An exception is thrown at runtime.
Explanation:
Thecompilation fails at line start();Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – non-static method start() cannot be referenced from a static context
D because non-static method start() cannot be referenced from a static context
Object has to be created before calling start();
start() is instance method. D is correct ans.
Compile Error