Friday, 9 March 2012

Sun Certified Java Programmer (SCJP) Questions

Question 1

The following code will give

1:    class Test

2:    {

3:         void show()

4:         {

5:             System.out.println("non-static method in Test");

6:         }

7:    }

8:    public class Q3 extends Test

9:    {

10:      static void show()

11:      {

12:          System.out.println("Overridden non-static method in Q3");

13:      }

14:

15:      public static void main(String[] args)

16:      {

17:          Q3 a = new Q3();

18:      }

19:   }

 

A) Compilation error at line 3.

B) Compilation error at line 10.

C) No compilation error, but runtime exception at line 3.

D) No compilation error, but runtime exception at line 10.

Answer : B

Explanation : You can't override a non-static method with static method.

 

Question 2

The following code will give

1:    class Test

2:    {

3:      static void show()

4:      {

5:          System.out.println("Static method in Test");

6:      }

7:    }

8:    public class Q4 extends Test

9:    {

10:       void show()

11:       {

12:           System.out.println("Overridden static method in Q4");

13:       }

14:       public static void main(String[] args)

15:       {

16:       }

17:    }

 

A) Compilation error at line 3.

B) Compilation error at line 10.

C) No compilation error, but runtime exception at line 3.

D) No compilation error, but runtime exception at line 10.

Answer : B

Explanation : You can't override a static method with non-static method.

For More Questions Keep Tuning………

No comments:

Post a Comment