1)
public class Main{
int a=10;
public static void main(String[] args) {
int a=15;
System.out.println(this.a);
}
}
What is the result of attempting to compile and run the program?
A. Prints 10
B. Prints 15
C. Compile-time error
D. Runtime-error
Answer: C
This keyword can't be used inside the static methods.this keyword represent the object using which a non static function is called.
2)
public class Main{
public static void main(String[] args) {
B obj=new S();
obj.dis(); }}
class B{
static void dis(){
System.out.println("From Base");
}}
class S extends B{
void dis(){
System.out.println("From Sub");
}
}
What is the result of attempting to compile and run the program?
A. Prints From Sub
B. Prints From Base
C. Compile-time error
D. Run-time error
Answer: C
Static methods can't be override.So the declaration of function dis() within class S generate compile-time error.
3)
class A {
A(){
System.out.print("A ") ;
}
}
public class Main extends A {
Main(){
System.out.print("Main ") ;
}
public static void main(String[] args) {
new Main();
}
}
What is the result of attempting to compile and run the program?
A. Prints A Main
B. Prints Main A
C. Prints nothing
D. Compile-time error
Answer: A
When creating an object for the sub class the base class constructor will be called first,after that only sub class constructor will be invoked.Here class A's constructor will execute first ,then Main class constructor.
4)
public class Main{
public static void main(String[] args) {
S obj=new S();
obj.dis(); }}
final class B{ }
class S extends B{
void dis(){
System.out.println("Hello");
}
}
What is the result of attempting to compile and run the program?
A. Prints Hello
B. Compile-time error
C. Run-time error
D. None of the above
Answer: B
Final method's cant be inherited. So 'class S extends B' will generate compile-time error.
5)
class A {
static void dis() {
System.out.print("A"); }
}
public class Main extends A {
public static void main(String[] args) {
dis();
super.dis();
}
static void dis() {
System.out.println("Main");
}
}
What is the result of attempting to compile and run the program?
A. Prints Main A
B. Prints A Main
C. Compile-time error
D. Run-time error
Answer: C
Keyword super can't be used inside static context.It can be used only inside non static functions.
6)
class A {
static void dis() {
System.out.print("A "); }
}
public class Main extends A {
public static void main(String[] args) {
dis(); //line 1
Main obj=new Main();
obj.dis(); //line 3
}
}
What is the result of attempting to compile and run the program?
A. Prints A A
B. Compile-time error at line 1
C. Compile-time error at line 2
D. None of the above
Answer: A
Static methods can be called in two different ways.either using class name or using object of that class.
Always the best practice is using Class name.Here static method dis() is inherited from class A to Main.
Line 1 will call inherited dis() method.Line 3 is calling dis() using object of Main class.
7)
public class Main{
public static void main(String[] args) {
B obj=new S();
obj.dis(); }}
class B{
static void dis(){
System.out.println("From Base");
}}
class S extends B{
static void dis(){
System.out.println("From Sub");
}
}
What is the result of attempting to compile and run the program?
A. Prints From Base
B. Prints From Sub
C. Compile-time error
D. Run-time error
Answer: A
Static methods can't be override .So here class B's dis() method will be invoked.Usually static methods are invoked using the 'classname.function name' . ie B.dis() or S.dis() . If you are accessing static functions using objects ,functions will be invoked based on the object's reference type.ie B obj=new S();bj.dis();will invoke B's static dis() method not S's dis() method.
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 extends C {
void m1(D d) {System.out.print("D");}
public static void main(String[] args) {
A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D();
d1.m1(a1); d1.m1(b1); d1.m1(c1);
}}
What is the result of attempting to compile and run the program?
A. Prints: AAA
B. Prints: DDD
C. Prints: ABC
D. Compile-time error
Answer: C
The method invocation expression d1.m1(a1) uses reference d1 of type D to invoke method m1. Since the reference d1 is of type D, the class D is searched for an applicable implementation of m1. The methods inherited from the superclasses, C, B and A, are included in the search. The argument, a1, is a variable declared with the type A;so method A.m1(A a) is invoked
9)
class A {}
class B extends A {}
class C extends B {}
class D {
void m1(A a) {System.out.print("A");}
void m1(B b) {System.out.print("B");}
void m1(C c) {System.out.print("C");}
public static void main(String[] args) {
A c1 = new C(); B c2 = new C(); C c3 = new C(); D d1 = new D();
d1.m1(c1); d1.m1(c2); d1.m1(c3);
}}
What is the result of attempting to compile and run the program?
A. Prints: AAA
B. Prints: ABC
C. Prints: CCC
D. Compile-time error
Answer: B
Three methods overload the method name m1. Each has a single parameter of type A or B or C. For any method invocation expression of the form m1(referenceArgument), the method is selected based on the declared type of the variable referenceArgument--not the run-time type of the referenced object.
10)
class GFC216 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
char a1 = 1; long 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: float,double
C. Prints: double,float
D. Prints: double,double
Answer: A
The arguments of the method invocation expressions, m(a1) and m(b1), are of types char and long respectively. A method invocation conversion can widen an argument of type char or long to match either of the two method parameter types float or double; so both methods, m(float i) and m(double i), are applicable to the two method invocation expressions. Since both methods are applicable, the more specific of the two, m(float i) is chosen rather than the less specific, m(double i).
No comments:
Post a Comment