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

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 :We use only implements keywords once for implementing one or more interfaces.

Q. Given:

11. public interface A111 {

12. String s = “yo”;

13. public void method1();

14. }

17. interface B { }

20. interface C extends A111, B {

21. public void method1();

22. public void method1(int x);

23. } What is the result?

A. Compilation succeeds.

B. Compilation fails due to multiple errors.

C. Compilation fails due to an error only on line 20.

D. Compilation fails due to an error only on line 21.

E. Compilation fails due to an error only on line 22.

F. Compilation fails due to an error only on line 12.

Answer: A

Explanation :An interface could extends other interfaces.

Q. Given:

11. class Alpha {

12. public void foo() { System.out.print(“Afoo “); }

13. }

14. public class Beta extends Alpha {

15. public void foo() { System.out.print(“Bfoo “); }

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

17. Alpha a = new Beta();

18. Beta b = (Beta)a;

19. a.foo();

20. b.foo();

21. }

22. }

What is the result?

A. Afoo Afoo

B. Afoo Bfoo

C. Bfoo Afoo

D. Bfoo Bfoo

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: D

Explanation :In both case actual object is of Beta class objects.

Q. Given:

1. public class Threads4 {

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

3. new Threads4().go();

4. }

5. public void go() {

6. Runnable r = new Runnable() {

7. public void run() {

8. System.out.print(“foo”);

9. }

10. };

11. Thread t = new Thread(r);

12. t.start();

13. t.start();

14. }

15. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes normally and prints “foo”.

D. The code executes normally, but nothing is printed.

Answer: B

Explanation :We can’t call start on one thread more than once.

Q. Given:

1. public class TestOne {

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

3. Thread.sleep(3000);

4. System.out.println(“sleep”);

5. }

6. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes normally and prints “sleep”.

D. The code executes normally, but nothing is printed.

Answer: C

Q. Given:

1. public class Threads3 implements Runnable {

2. public void run() {

3. System.out.print(“running”);

4. }

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

6. Thread t = new Thread(new Threads3());

7. t.run();

8. t.run();

9. t.start();

10. }

11. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes and prints “running”.

D. The code executes and prints “runningrunning”.

E. The code executes and prints “runningrunningrunning”.

Answer: E

Explanation :We can call run method more than once for a thread objects.

Q. Given:

public class NamedCounter {

private final String name;

private int count;

public NamedCounter(String name) { this.name = name; }

public String getName() { return name; }

public void increment() { count++; }

public int getCount() { return count; } public void reset() { count = 0;

}

Which three changes should be made to adapt this class to be used safely by multiple

threads? (Choose three.)

A. declare reset() using the synchronized keyword

B. declare getName() using the synchronized keyword

C. declare getCount() using the synchronized keyword

D. declare the constructor using the synchronized keyword

E. declare increment() using the synchronized keyword

Answer: A,C,E

Explanation :We use synchronized keywords to make methos safety purpose.

Q. Given that Triangle implements Runnable, and:

31. void go() throws Exception {

32. Thread t = new Thread(new Triangle());

33. t.start();

34. for(int x = 1; x < 100000; x++) {

35. //insert code here

36. if(x%100 == 0) System.out.print(“g”);

37. } }

38. public void run() {

39. try {

40. for(int x = 1; x < 100000; x++) {

41. // insert the same code here

42. if(x%100 == 0) System.out.print(“t”);

43. }

44. } catch (Exception e) { }

45. }

Which two statements, inserted independently at both lines 35 and 41, tend to allow both

threads to temporarily pause and allow the other thread to execute? (Choose two.)

A. Thread.wait();

B. Thread.join();

C. Thread.yield();

D. Thread.sleep(1);

E. Thread.notify();

Answer: C,D

Explanation :We use sleep and yield method to stop thread execution.

Q. Given:

1. public class TestSeven extends Thread {

2. private static int x;

3. public synchronized void doThings() {

4. int current = x;

5. current++;

6. x = current;

7. }

8. public void run() {

9. doThings();

10. }

11.}

Which statement is true?

A. Compilation fails.

B. An exception is thrown at runtime.

C. Synchronizing the run() method would make the class thread-safe.

D. The data in variable “x” are protected from concurrent access problems.

E. Declaring the doThings() method as static would make the class thread-safe.

F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block

would make the class thread-safe.

Answer: E

Q. Given:

11. public class Yikes {

12.

13. public static void go(Long n) {System.out.print(“Long “);}

14. public static void go(Short n) {System.out.print(“Short “);}

15. public static void go(int n) {System.out.print(“int “);}

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

17. short y = 6;

18. long z = 7;

19. go(y);

20. go(z);

21. }

22. }

What is the result?

A. int Long

B. Short Long

C. Compilation fails.

D. An exception is thrown at runtime.

Answer: A

Explanation :As short could be adjust in int but long can’t be so it will be autoboxed in Long object.

Q. Given:

12. Date date = new Date();

13. df.setLocale(Locale.ITALY);

14. String s = df.format(date);

The variable df is an object of type DateFormat that has been initialized in line 11. What

is the result if this code is run on December 14, 2000?

A. The value of s is 14-dic-2000.

B. The value of s is Dec 14, 2000.

C. An exception is thrown at runtime.

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

Answer: D

Q. Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder

object? (Choose two.)

A. When using versions of Java technology earlier than 5.0.

B. When sharing a StringBuffer among multiple threads.

C. When using the java.io class StringBufferInputStream.

D. When you plan to reuse the StringBuffer to build more than one string.

Answer: A,B

Explanation :StringBuffer’s all method are thread safe so if safety is our concern then we should not replace it.

Q. Given that c is a reference to a valid java.io.Console object, and:

11. String pw = c.readPassword(“%s”, “pw: “);

12. System.out.println(“got ” + pw);

13. String name = c.readLine(“%s”, “name: “);

14. System.out.println(” got “, name);

If the user types fido when prompted for a password, and then responds bob when

prompted for a name, what is the result?

A. pw: got fido name: bob got bob

B. pw: fido got fido name: bob got bob

C. pw: got fido name: bob got bob

D. pw: fido got fido name: bob got bob

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: E

Explanation :Syntax is not correct to read input.

Q. Given:

11. String test = “This is a test”;

12. String[] tokens = test.split(“\s”);

13. System.out.println(tokens.length);

What is the result?

A. 0

B. 1

C. 4

D. Compilation fails.

E. An exception is thrown at runtime.

Answer: D

Explanation :We have n’t use proper syntax for delimintor for string.

Q. Given:

10. import java.io.*;

11. class Animal {

12. Animal() { System.out.print(“a”); }

13. }

14. class Dog extends Animal implements Serializable {

15. Dog() { System.out.print(“d”); }

16. }

17. public class Beagle extends Dog { }

If an instance of class Beagle is created, then Serialized, then deSerialized, what is the

result?

A. ad

B. ada

C. add

D. adad

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: B

Explanation :When you serialize a Beagle object it will be serialized without any compiler error , because the subtypes of a serializable class are themselves serializable. when you deserialize it, then the constructor of Animal class will run because it is not serialized.

Q. Given:

1. public class Target {

2. private int i = 0;

3. public int addOne(){

4. return ++i;

5. }

6. }

And:

1. public class Client {

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

3. System.out.println(new Target().addOne());

4. }

5. }

Which change can you make to Target without affecting Client?

A. Line 4 of class Target can be changed to return i++;

B. Line 2 of class Target can be changed to private int i = 1;

C. Line 3 of class Target can be changed to private int addOne(){

D. Line 2 of class Target can be changed to private Integer i = 0;

Answer: D

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>