1)
public class Test {
final int a;
public static void main(String[] args) {
System.out.println(a);
}
}
//What is the result of attempting to compile and run the program?
A. Prints 0
B. Prints garbage value
C. Prints null
D. compile-time error
Answer: D
non static final variables should be initialized either at compile-time(ie final int a=10;) or runtime (ie
within each constructor declared).static final variable should be initialized either at compile-time(ie
static final int a=10;) or at runtime(within static blocks).Local final variable should be initialised before
use.
2)
public class Test {
static final int a;
Test(int a){
this.a=a;
}
public static void main(String[] args) {
Test t=new Test(10);
System.out.println(t.a);
}
}
//What is the result of attempting to compile and run the program?
A. Prints 0
B. Prints 10
C. Compile-time error
D. Run-time error
Answer: C
Static final variables should be initialised either at the time of declaration or at the static blocks.
3)
public class Test {
public static void main(String[] args) {
new A();
new A();
}
}
class A {
static {
System.out.print("static ");
}
}
// What is the result of attempting to compile and run the program?
A. static static
B. static
C. compile-time error
D. run-time error
Answer: B
Static block will be executed only once. ie during the class loading only.It won't execute for each instance
creation.
4)
class Base {
Base(int x) {
System.out.println(x);
}}
public class Main extends Base {
int x = 10;
Main() {
super(x);
}
public static void main(String[] args) {
Main m = new Main();
}
}
// What is the result of attempting to compile and run the program?
A. Prints 10
B. Prints Nothing
C. Compile-time error
D. Run-time error
Answer: C
You can't use non static fields and functions of one class and it's super classes as parameters of super() and this().The reason is you can use non static fields and functions only after the creation of an object.
5)
class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B(); A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}
What is the result of attempting to compile and run the program?
A. Prints B.s1 A.s2 A.s1 A.s2
B. Prints B.s1 A.s2 B.s1 A.s2
C. compile-time error
D. Run-time error
Answer: A
The variables of a subclass can hide the variables of a superclass or interface. The variable that is accessed is determined at compile-time based on the type of the reference--not the run-time type of the object. The two references x and y refer to the same instance of type B. The name x.s1 uses a reference of type B; so it refers to the variable s1 declared in class B. The name y.s1 uses a reference of type A; so it refers to the variable s1 declared in class A.
6)
abstract class D {String s1 = "D"; String getS1() {return s1;}}
class E extends D {String s1 = "E"; String getS1() {return s1;}}
class F {
public static void main (String[] s) {
D x = new E(); System.out.print(x.s1 + x.getS1());
}}
What is the result of attempting to compile and run the program?
A. Prints: DD
B. Prints: DE
C. Prints: EE
D. Prints: ED
Answer: B
At run-time, the actual field that is accessed depends on the compile-time type of the reference--not the
run-time type of the object.The compile-time type of the reference x in the name x.s1 is D; so the selected field is the one declared in class D. A non-static method is selected based on the run-time type of the object--not the compile-time type of the reference. The same reference variable x is used in the method invocation expression
7)
class GFC217 {
static String m(int i) {return "int";}
static String m(float i) {return "float";}
public static void main (String[] args) {
long a1 = 1; double b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}
What is the result of attempting to compile and run the program?
A. Prints: float,float
B. Prints: int,float
C. Compile-time error
D. Run-time error
Answer: C
The method invocation expression, m(b1), contains an argument of type double. A method invocation conversion will not implicitly narrow the argument to match the parameter type of the method, m(float i). The method invocation expression, m(a1), contains an argument of type long. A method invocation conversion will widen the argument to match the parameter type of the the method, m(float i).
8)
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A c1 = new C(); C c2 = new C(); c1.m1(c2);
}}
What is the result of attempting to compile and run the program?
A. Prints A
B. Prints B
C. Prints C
D. Compile-time error
Answer: A
The reference c1 is of the superclass type, A; so it can be used to invoke only the method m1 declared in class A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using the reference c1.
9)
class GFC218 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {m(null);}
}
What is the result of attempting to compile and run the program?
A. Prints: Object
B. Prints: String
C. Compile-time error
D. Run-time error
Answer: B
The argument of the method invocation expression, m(null), is of type null and can be converted to either type String or Object by method invocation conversion, so both methods, m(String x) and m(Object x), are applicable.The more specific of the two, m(String x), is chosen over the less specific, m(Object x).
10)
interface I10 {String name = "I10"; String s10 = "I10.s10";}
interface I20 {String name = "I20"; String s20 = "I20.s20";}
class C10 implements I10, I20 { // 1
public static void main(String[] args) {
System.out.print(s10+","); // 2
System.out.print(s20+","); // 3
System.out.print(name); // 4
}}
What is the result of attempting to compile and run the program?
A. Prints: I10.s10,I20.s20,I10
B. Prints: I10.s10,I20.s20,I20
C. compile-time error
D. run-time error
Answer: C
Class C10 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C10; then, no compile-time error occurs. Line 4 generates the compile-time error, because it is the first to access the name field as a member of class C10.
No comments:
Post a Comment