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

Q. Given:

11. class Snoochy {

12. Boochy booch;

13. public Snoochy() { booch = new Boochy(this); }

14. }

15.

16. class Boochy {

17. Snoochy snooch;

18. public Boochy(Snoochy s) { snooch = s; }

19. }

And the statements:

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

22. Snoochy snoog = new Snoochy();

23. snoog = null;

24. // more code here

25. }

Which statement is true about the objects referenced by snoog, snooch, and booch

immediately after line 23 executes?

A. None of these objects are eligible for garbage collection.

B. Only the object referenced by booch is eligible for garbage collection.

C. Only the object referenced by snoog is eligible for garbage collection.

D. Only the object referenced by snooch is eligible for garbage collection.

E. The objects referenced by snooch and booch are eligible for garbage collection.

Answer: E

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

Q. Given:

3. interface Animal { void makeNoise(); }

4. class Horse implements Animal {

5. Long weight = 1200L;

6. public void makeNoise() { System.out.println(“whinny”); }

7. }

8. public class Icelandic extends Horse {

9. public void makeNoise() { System.out.println(“vinny”); }

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

11. Icelandic i1 = new Icelandic();

12. Icelandic i2 = new Icelandic();

13. Icelandic i3 = new Icelandic();

14. i3 = i1; i1 = i2; i2 = null; i3 = i1;

15. }

16. }

When line 15 is reached, how many objects are eligible for the garbage collector?

A. 0

B. 1

C. 2

D. 3

E. 4

F. 6

Answer: E

Explanation :When we declare srting like “winny” this is also object but not refrenced by any one.

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

Q. Given:

5. class Payload {

6. private int weight;

7. public Payload (int w) { weight = w; }

8. public void setWeight(int w) { weight = w; }

9. public String toString() { return Integer.toString(weight); }

10. }

11. public class TestPayload {

12. static void changePayload(Payload p) { /* insert code */ }

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

14. Payload p = new Payload(200);

15. p.setWeight(1024);

16. changePayload(p);

17. System.out.println(“p is ” + p);

18. } }

Which code fragment, inserted at the end of line 12, produces the output p is 420?

A. p.setWeight(420);

B. p.changePayload(420);

C. p = new Payload(420);

D. Payload.setWeight(420);

E. p = Payload.setWeight(420);

Answer: A

Explanation :At line 12 we have to call setWeight method of Payload class.

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

Q. Given:

11. public static void test(String str) {

12. int check = 4;

13. if (check = str.length()) {

14. System.out.print(str.charAt(check -= 1) +”, “);

15. } else {

16. System.out.print(str.charAt(0) + “, “);

17. }

18. }

and the invocation:

21. test(“four”);

22. test(“tee”);

23. test(“to”);

What is the result?

A. r, t, t,

B. r, e, o,

C. Compilation fails.

D. An exception is thrown at runtime.

Answer: C

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

Q. A UNIX user named Bob wants to replace his chess program with a new one, but he is

not sure where the old one is installed. Bob is currently able to run a Java chess program

starting from his home directory /home/bob using the command:

java -classpath /test:/home/bob/downloads/*.jar games.

Chess Bob’s CLASSPATH is set (at login time) to:

/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jar

What is a possible location for the Chess.class file?

A. /test/Chess.class

B. /home/bob/Chess.class

C. /test/games/Chess.class

D. /usr/lib/games/Chess.class

E. /home/bob/games/Chess.class

F. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)

G. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)

Answer: C

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

Q. Given classes defined in two different files:

1. package util;

2. public class BitUtils {

3. private static void process(byte[] b) {}

4. }

1. package app;

2. public class SomeApp {

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

4. byte[] bytes = new byte[256];

5. // insert code here

6. }

7. }

What is required at line 5 in class SomeApp to use the process method of BitUtils?

A. process(bytes);

B. BitUtils.process(bytes);

C. app.BitUtils.process(bytes);

D. util.BitUtils.process(bytes);

E. import util.BitUtils.*; process(bytes);

F. SomeApp cannot use the process method in BitUtils.

Answer: F

Explanation :To access a class in other pakage class :

1.That class should import that pakage

2.Extends that class

Here no condition is present.

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

Q. Given:

15. public class Pass2 {

16. public void main(String [] args) {

17. int x = 6;

18. Pass2 p = new Pass2();

19. p.doStuff(x);

20. System.out.print(” main x = ” + x);

21. }

22.

23. void doStuff(int x) {

24. System.out.print(” doStuff x = ” + x++);

25. }

26. }

And the command-line invocations:

javac Pass2.java

java Pass2 5

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. doStuff x = 6 main x = 6

D. doStuff x = 6 main x = 7

E. doStuff x = 7 main x = 6

F. doStuff x = 7 main x = 7

Answer: B

Explanation :In java program there should be a static main method.Here main method is not static so at runtime exception will be thrown when jvm firstly try to call static main method.

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

Q. Given:

12. public class Test {

13. public enum Dogs {collie, harrier};

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

15. Dogs myDog = Dogs.collie;

16. switch (myDog) {

17. case collie:

18. System.out.print(“collie “);

19. case harrier:

20. System.out.print(“harrier “);

21. }

22. }

23. }

What is the result?

A. collie

B. harrier

C. Compilation fails.

D. collie harrier

E. An exception is thrown at runtime.

Answer: D

Explanation :In Enum,when we declare it make these values 0,1,2 etc.Here collie, harrier is like 0,1.Then switch conditions work normally.

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

Q. Given:

11. static void test() {

12. try {

13. String x = null;

14. System.out.print(x.toString() + ” “);

15. }

16. finally { System.out.print(“finally “); }

17. }

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

19. try { test(); }

20. catch (Exception ex) { System.out.print(“exception “); }

21. }

What is the result?

A. null

B. finally

C. null finally

D. Compilation fails.

E. finally exception

Answer: E

Explanation :Here String x = null; means string object pointing to garbage memory.Then in print method we are trying to access string object so it will throw exception.

In test method we haven’t catch exception so firstly run finally then run catch of main mthod.

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

Q. Given:

11. static void test() throws Error {

12. if (true) throw new AssertionError();

13. System.out.print(“test “);

14. }

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

16. try { test(); }

17. catch (Exception ex) { System.out.print(“exception “); }

18. System.out.print(“end “);

19. }

What is the result?

A. end

B. Compilation fails.

C. exception end

D. exception test end

E. A Throwable is thrown by main.

F. An Exception is thrown by main.

Answer: E

Explanation :From test method we are trying to throw Error.That is not Exception type so at runtime it will show Throwable

Do Remember the following hirerachy :

  1.                        Object  
  2.    
  3.                       Throwable  
  4.    
  5.             Error                 Exception  
  6.    
  7.                             RuntimeExceptions    Checked Exceptions  
  8.    

or you can say that
Object —-> Throwable —-> Error

So if an instance of type Error is thrown then throwable can catch that, but
Exception can not.

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

This entry was posted in Freshers, Java Certifications, Java Programing, Programing, Technical Interviews Questions 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>