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

Q. Given:

3. public class Batman {

4. int squares = 81;

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

6. new Batman().go();

7. }

8. void go() {

9. incr(++squares);

10. System.out.println(squares);

11. }

12. void incr(int squares) { squares += 10; }

13. }

What is the result?

A. 81

B. 82

C. 91

D. 92

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: B

Explanation :void incr() method will increase its local variable value.So in go() method squares value will be only 82.

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

Q. Given:

15. public class Yippee {

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

17. for(int x = 1; x < args.length; x++) {

18. System.out.print(args[x] + ” “);

19. }

20. }

21. }

and two separate command line invocations:

java Yippee

java Yippee 1 2 3 4

What is the result?

A. No output is produced.

1 2 3

B. No output is produced.

2 3 4

C. No output is produced.

1 2 3 4

D. An exception is thrown at runtime.

1 2 3

E. An exception is thrown at runtime.

2 3 4

F. An exception is thrown at runtime.

1 2 3 4

Answer: B

Explanation :Indexing of arguments will be start from 0.So it will start to print from 234.

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

Q. Given:

13. public class Pass {

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

15. int x = 5;

16. Pass p = new Pass();

17. p.doStuff(x);

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

19. }

20.

21. void doStuff(int x) {

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

23. }

24. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. doStuff x = 6 main x = 6

D. doStuff x = 5 main x = 5

E. doStuff x = 5 main x = 6

F. doStuff x = 6 main x = 5

Answer: D

Explanation :when p will call doStuff() then it will first print x value then increment.So there will be 5 print after that it will increase to 6.

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

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();

12. Icelandic i3 = new Icelandic();

13. i3 = i1; i1 = i2; i2 = null; i3 = i1;

14. }

15. }

When line 14 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 :Which object is not refrenced by any object refrence variable that will be eligible for garbage collection.

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

Q. Given two files, GrizzlyBear.java and Salmon.java:

1. package animals.mammals;

2.

3. public class GrizzlyBear extends Bear {

4. void hunt() {

5. Salmon s = findSalmon();

6. s.consume();

7. }

8. }

1. package animals.fish;

2.

3. public class Salmon extends Fish {

4. public void consume() { /* do stuff */ }

5. }

If both classes are in the correct directories for their packages, and the Mammal class

correctly defines the findSalmon() method, which change allows this code to compile?

A. add import animals.mammals.*; at line 2 in Salmon.java

B. add import animals.fish.*; at line 2 in GrizzlyBear.java

C. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java

D. add import animals.mammals.GrizzlyBear.*; at line 2 in Salmon.java

Answer: B

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

Q. Given:

11. String[] elements = { “for”, “tea”, “too” };

12. String first = (elements.length > 0) elements[0] : null;

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The variable first is set to null.

D. The variable first is set to elements[0].

Answer: D

Explanation :Here at line 12,firstly it will check array length then if it true assign array first element other wise null.

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

Q. A company has a business application that provides its users with many different reports:

receivables reports, payables reports, revenue projects, and so on. The company has just

purchased some new, state-of-the-art, wireless printers, and a programmer has been

assigned the task of enhancing all of the reports to use not only the company’s old

printers, but the new wireless printers as well. When the programmer starts looking into

the application, the programmer discovers that because of the design of the application, it

is necessary to make changes to each report to support the new printers.Which two design

concepts most likely explain this situation? (Choose two.)

A. Inheritance

B. Low cohesion

C. Tight coupling

D. High cohesion

E. Loose coupling

F. Object immutability

Answer: B,C

Explanation :For better application,there should be high cohesion and loose coupling.

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

Q. Given:

10. public class SuperCalc {

11. protected static int multiply(int a, int b) { return a * b;}

12. }

and:

20. public class SubCalc extends SuperCalc{

21. public static int multiply(int a, int b) {

22. int c = super.multiply(a, b);

23. return c;

24. }

25. }

and:

30. SubCalc sc = new SubCalc ();

31. System.out.println(sc.multiply(3,4));

32. System.out.println(SubCalc.multiply(2,2));

What is the result?

A. 12

4

B. The code runs with no output.

C. An exception is thrown at runtime.

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

E. Compilation fails because of an error in line 22.

F. Compilation fails because of an error in line 31.

Answer: E

Explanation :static methods can’t override.And super method use in case of override so line 22 is not right.

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

Q. Given:

31. class Foo {

32. public int a = 3;

33. public void addFive() { a += 5; System.out.print(“f “); }

34. }

35. class Bar extends Foo {

36. public int a = 8;

37. public void addFive() { this.a += 5; System.out.print(“b ” ); }

38. }

Invoked with:

Foo f = new Bar();

f.addFive();

System.out.println(f.a);

What is the result?

A. b 3

B. b 8

C. b 13

D. f 3

E. f 8

F. f 13

G. Compilation fails.

H. An exception is thrown at runtime.

Answer: A

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

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

One Response to SCJP Certification Questions With Answers Explanation :Java(Set-4)

  1. Saad says:

    Print this 3,9,81. By for in java

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>