SCJP Certification Questions With Answers Explanation :Java(Set-10)

 Q. Given:

1. class Alligator {

2. public static void main(String[] args) {

3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};

4. int [][]y = x;

5. System.out.println(y[2][1]);

6. }

7. }

What is the result?

A. 2

B. 3

C. 4

D. 6

E. 7

F. Compilation fails.

Answer: E

Explanation :We are accessing third part with second position of array.

—————————————————————————————-

Q. Given:

21. abstract class C1 {

22. public C1() { System.out.print(1); }

23. }

24. class C2 extends C1 {

25. public C2() { System.out.print(2); }

26. }

27. class C3 extends C2 {

28. public C3() { System.out.println(3); }

29. }

30. public class Ctest {

31. public static void main(String[] a) { new C3(); }

32. }

What is the result?

A. 3

B. 23

C. 32

D. 123

E. 321

F. Compilation fails.

G. An exception is thrown at runtime.

Answer: D

Explanation :When we make object then constructor of that class is called but before that super class constructor will be called until final super class.

—————————————————————————————-

Q. Given:

10. class One {

11. public One foo() { return this; }

12. }

13. class Two extends One {

14. public One foo() { return this; }

15. }

16. class Three extends Two {

17. // insert method here

18. }

Which two methods, inserted individually, correctly complete the Three class? (Choose

two.)

A. public void foo() {}

B. public int foo() { return 3; }

C. public Two foo() { return this; }

D. public One foo() { return this; }

E. public Object foo() { return this; }

Answer: C,D

—————————————————————————————-

Q. Which two classes correctly implement both the java.lang.Runnable and the java.lang.

Cloneable interfaces? (Choose two.)

A. public class Session

implements Runnable, Cloneable {

public void run();

public Object clone();

}

B. public class Session

extends Runnable, Cloneable {

public void run() { /* do something */ }

public Object clone() { /* make a copy */ }

C. public class Session

implements Runnable, Cloneable {

public void run() { /* do something */ }

public Object clone() { /* make a copy */ }

D. public abstract class Session

implements Runnable, Cloneable {

public void run() { /* do something */ }

public Object clone() { /*make a copy */ }

E. public class Session

implements Runnable, implements Cloneable {

public void run() { /* do something */ }

public Object clone() { /* make a copy */ }

Answer: C,D

Explanation :When we implement any interface then its necessary to give definition to all undefined mehtod in interface.

—————————————————————————————-

Q. Given:

11. public interface A { public void m1(); }

12.

13. class B implements A { }

14. class C implements A { public void m1() { } }

15. class D implements A { public void m1(int x) { } }

16. abstract class E implements A { }

17. abstract class F implements A { public void m1() { } }

18. abstract class G implements A { public void m1(int x) { } }

What is the result?

A. Compilation succeeds.

B. Exactly one class does NOT compile.

C. Exactly two classes do NOT compile.

D. Exactly four classes do NOT compile.

E. Exactly three classes do NOT compile.

Answer: C

Explanation :When we implement any interface then its necessary to give definition to all undefined mehtod in interface.

—————————————————————————————-

Q. Given:

10. class Line {

11. public class Point { public int x,y;}

12. public Point getPoint() { return new Point(); }

13. }

14. class Triangle {

15. public Triangle() {

16. // insert code here

17. }

18. }

Which code, inserted at line 16, correctly retrieves a local instance of a Point object?

A. Point p = Line.getPoint();

B. Line.Point p = Line.getPoint();

C. Point p = (new Line()).getPoint();

D. Line.Point p = (new Line()).getPoint();

Answer: D

—————————————————————————————-

Q. Given:

1. class TestA {

2. public void start() { System.out.println(“TestA”); }

3. }

4. public class TestB extends TestA {

5. public void start() { System.out.println(“TestB”); }

6. public static void main(String[] args) {

7. ((TestA)new TestB()).start();

8. }

9. }

What is the result?

A. TestA

B. TestB

C. Compilation fails.

D. An exception is thrown at runtime.

Answer: B

Explanation :Below line

((TestA)new TestB()).start(); //actual object is of class TestB so it will print TestB.

—————————————————————————————-

Q. Given:

11. public static void main(String[] args) {

12. Object obj = new int[] { 1, 2, 3 };

13. int[] someArray = (int[])obj;

14. for (int i : someArray) System.out.print(i + ” “);

15. }

What is the result?

A. 1 2 3

B. Compilation fails because of an error in line 12.

C. Compilation fails because of an error in line 13.

D. Compilation fails because of an error in line 14.

E. A ClassCastException is thrown at runtime.

Answer: A

—————————————————————————————-

Q. Given:

foo and bar are public references available to many other threads. foo refers to a Thread and bar is an Object. The thread foo is currently executing bar.wait(). From another thread, what provides the most reliable way to ensure that foo will stop executing wait()?

A. foo.notify();

B. bar.notify();

C. foo.notifyAll();

D. Thread.notify();

E. bar.notifyAll();

F. Object.notify();

Answer: E

Explanation :To notify more than one thread we use notifyAll method.Here Foo is waiting so bar will notify.

—————————————————————————————-

This entry was posted in Java Certifications, Java Programing, Programing and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>