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

Q. Given:

11. public class PingPong implements Runnable {

12. synchronized void hit(long n) {

13. for(int i = 1; i < 3; i++)

14. System.out.print(n + “-” + i + ” “);

15. }

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

17. new Thread(new PingPong()).start();

18. new Thread(new PingPong()).start();

19. }

20. public void run() {

21. hit(Thread.currentThread().getId());

22. }

23. }

Which two statements are true? (Choose two.)

A. The output could be 8-1 7-2 8-2 7-1

B. The output could be 7-1 7-2 8-1 6-1

C. The output could be 8-1 7-1 7-2 8-2

D. The output could be 8-1 8-2 7-1 7-2

Answer: C,D

Explanation :For answer to this questions we will have to check options sequence as per hit method.

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

Q. Which two code fragments will execute the method doStuff() in a separate thread?

(Choose two.)

A. new Thread() {

public void run() { doStuff(); }

};

B. new Thread() {

public void start() { doStuff(); }

};

C. new Thread() {

public void start() { doStuff(); }

}.run();

D. new Thread() {

public void run() { doStuff(); }

}.start();

E. new Thread(new Runnable() {

public void run() { doStuff(); }

}).run();

F. new Thread(new Runnable() {

public void run() { doStuff(); }

}).start();

Answer: D,F

Explanation :To make seprate thread we have to use run method of thread class or Runnable interface.

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

Q. Given:

11. public class Person {

12. private String name;

13. public Person(String name) {

14. this.name = name;

15. }

16. public boolean equals(Object o) {

17. if ( ! ( o instanceof Person) ) return false;

18. Person p = (Person) o;

19. return p.name.equals(this.name);

20. }

21. }

Which statement is true?

A. Compilation fails because the hashCode method is not overridden.

B. A HashSet could contain multiple Person objects with the same name.

C. All Person objects will have the same hash code because the hashCode method is not

overridden.

D. If a HashSet contains more than one Person object with name=”Fred”, then removing

another Person, also with name=”Fred”, will remove them all.

Answer: B

Explanation :D etermining a match in a hashset is a two step process,

1) Calculate hashcode, to find out which ‘bucket’ the item should be in

2) Compare each items in that bucket to see if they are equal to the item you want to add

The hashcode() method is not overriden in the question, therefore Object hashcode() method will be used, which will return a unique hashcode for every object. Each object added will be added to its own bucket, therefore the equals() method will not return a match.

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

Q. Given:

5. import java.util.*;

6. public class SortOf {

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

8. ArrayList<Integer> a = new ArrayList<Integer>();

9. a.add(1); a.add(5); a.add(3);

11. Collections.sort(a);

12. a.add(2);

13. Collections.reverse(a);

14. System.out.println(a);

15. }

16. }

What is the result?

A. [1, 2, 3, 5]

B. [2, 1, 3, 5]

C. [2, 5, 3, 1]

D. [5, 3, 2, 1]

E. [1, 3, 5, 2]

F. Compilation fails.

G. An exception is thrown at runtime.

Answer: C

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

Q. Given:

11. public class Person {

12. private name;

13. public Person(String name) {

14. this.name = name;

15. }

16. public int hashCode() {

17. return 420;

18. }

19. }

Which statement is true?

A. The time to find the value from HashMap with a Person key depends on the size of the map.

B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.

C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate.

D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.

Answer: A

Explanation : Here we are generating same hashcode for each objects that’s why it will take time.

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

Q. Given:

1. public class Drink implements Comparable {

2. public String name;

3. public int compareTo(Object o) {

4. return 0;

5. }

6. }

and:

20. Drink one = new Drink();

21. Drink two = new Drink();

22. one.name= “Coffee”;

23. two.name= “Tea”;

24. TreeSet set = new TreeSet();

25. set.add(one);

26. set.add(two);

A programmer iterates over the TreeSet and prints the name of each Drink object.

What is the result?

A. Tea

B. Coffee

C. Coffee

Tea

D. Compilation fails.

E. The code runs with no output.

F. An exception is thrown at runtime.

Answer: B

Explanation :From the Set javadoc

A collection that contains no duplicate elements

From the TreeSet javadoc help

a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal…

From the TreeSet.add() method javadoc

Adds the specified element to this set if it is not already present

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

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>