Tuesday 24 July 2012

Java Classes - Objective Questions - Part 1

1)

class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
  public static void main (String[] args) {
    Color color1 = new Red(); Red color2 = new Red();
    boolean b1 = color1 instanceof Color; //line 1
    boolean b2 = color1 instanceof Blue; //line 2
    boolean b3 = color2 instanceof Blue; //line 3
    System.out.print(b1+","+b2+","+b3);
}}

What is the result of attempting to compile and run the program?

A. false,false,false
B. true,false,false
C. true,true,true
D. Compile-time error

Answer: D

The type of the reference color2 is Red. Since Red is not a subclass or a superclass of Blue, the expression color2 instanceof Blue is rejected at compile-time. So compile time error at line number 3

2)

public class Main{
public static void main(String args[]){
Sub ob=new Sub();
ob.dis();
}}
  class Base{
public abstract void dis();
}
 class Sub extends Base {
public void dis() {
System.out.println("Hello");
}}

What is the output ?

A. Prints Hello
B. Compile-time error
C. Run-time error
D. None of the above

Answer: B

If a class contains one or more abstract methods ,the class must be declared as abstract .Here the Base class should be precede with the keyword 'abstract'.

3)

class A {
A(){
System.out.print("A") ;
}
}
public class Main extends A {
Main(){
System.out.print("Main") ;
}
public static void main(String[] args) {

}
 }
 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: C

A class constructor will be called only if the object of the class is created.Here no object is created for
class A or Main.

4)

class JSC102 {
  public static void main (String[] args) {
    private int x = 1; protected int y = 2; public int z = 3;
    System.out.println(x+y+z);
}}
What is the result of attempting to compile and run the program?

A. Prints: 6
B. Compile-time error
C. Run-time error
D. None of the above

Answer: B

We can use only 'final' modifier with the local variables.So here the usage of private,protected,public with local variables x,y,z resp. will generate compile time errors.

5)

public class Main {
public static void main(String[] args)  throws Exception{

Main m = new Sub();
m.dis();
}
public void dis() throws IOException {
System.out.println("From Main dis");
}
}
class Sub extends Main {
public void dis() throws Exception{
System.out.println("From Sub dis");
}
}

The output of the above code is

A. Prints From Main dis
B. Prints From Sub dis
C. Compile-time error
D. Run-time error

Answer: C

Sub class function,dis()'s throws clause Exception is not compatible with throws clause in Main dis() function.When overriding function,the sub class function can have no throws clause ,same throws clause as in base class or throws clause with narrowed exceptions compared to Base class function.In this question exception clause is widened from IOException to Exception during overriding of dis().It generates compile-time error.

6)

public class Main{
public static void main(String args[]){
Sub ob=new Sub(); //line 3
ob.dis(); // line 4
}}
abstract class Base{
public abstract void dis(){}; // line 7
}
 class Sub extends Base {
public void dis() {
System.out.println("Hello");
}}

The output of the above code is

A. Error at line 3
B. Error at line 4
C. Error at line 7
D. None of the above

Answer: C

Abstract methods do not specify a method body.ie public abstract void disp(); is the correct.

7)

class Red {
  public int a; public static int b;
  public static void main (String[] in) {
    Red r1 = new Red(), r2 = new Red(); r1.a++; r1.b++;
    System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
}}

What is the result of attempting to compile and run the program?

A. Prints 1 ,1, 0, 0
B. Prints 1 ,1, 1, 1
C. Prints 1, 1, 0, 1
D. Compile-time Error

Answer: C

Both instances of class Red share a single copy of the static field b. Although field b is only incremented using the r1 reference, the change is visible in the r2 instance of class Red.

8)

public class Main {

public static void main(String[] args) {

Main m = new Sub();
m.dis();
}
static void dis() {
System.out.println("From Main dis");
}
}
class Sub extends Main {
void dis() {
System.out.println("From Sub dis");
}
}

A. Prints From Main dis
B. Prints From Sub dis
C. Compile-time error
D. Run-time error

Answer: C

Static methods can't be override.So the overriding of dis() function in Sub class generate  Compile-time error.

9)

class A {
A(){
System.out.print("A ") ;
}
A(int a){
System.out.print("A with args") ;
}
}
public class Main{
public static void main(String[] args) {
new A();
}
 }

What is the result of attempting to compile and run the program?

A. Prints A
B. Prints A with args
C. Prints nothing
D. Compile-time error

Answer: A

Here when creating object of class A,no argument is passed.So the non argument constructor of A will be invoked.

10)

public class Main{
public static void main(String args[]){
Sub ob=new Sub(); // line 1
ob.dis(); //line 2
}}
 abstract class Base{
public abstract void dis();
}
 class Sub extends Base {

}
What is the result of attempting to compile and run the program?

A. Error at line 1
B. Error at line 2
C. Compile-time error
D. Run-time error

Answer: C

When you are inheriting an abstract class ,either the sub class should override all the abstract functions in base class or declare sub class itself as abstract.

No comments:

Post a Comment