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

Q. A company that makes Computer Assisted Design (CAD) software has, within its application, some utility classes that are used to perform 3D rendering tasks. The company’s chief scientist has just improved the performance of one of the utility classes’ key rendering algorithms, and has assigned a programmer to replace the old algorithm with the new algorithm. When the programmer begins researching the utility classes, she is happy to discover that the algorithm to be replaced exists in only one class. The programmer reviews that class’s API, and replaces the old algorithm with the new

algorithm, being careful that her changes adhere strictly to the class’s API. Once testing has begun, the programmer discovers that other classes that use the class she changed are no longer working properly. What design flaw is most likely the cause of these new bugs?

A. Inheritance

B. Tight coupling

C. Low cohesion

D. High cohesion

E. Loose coupling

F. Object immutability

Answer: B

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

Q.Given:

1. class ClassA {

2. public int numberOfInstances;

3. protected ClassA(int numberOfInstances) {

4. this.numberOfInstances = numberOfInstances;

5. }

6. }

7. public class ExtendedA extends ClassA {

8. private ExtendedA(int numberOfInstances) {

9. super(numberOfInstances);

10. }

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

12. ExtendedA ext = new ExtendedA(420);

13. System.out.print(ext.numberOfInstances);

14. }

15. }

Which statement is true?

A. 420 is the output.

B. An exception is thrown at runtime.

C. All constructors must be declared public.

D. Constructors CANNOT use the private modifier.

E. Constructors CANNOT use the protected modifier.

Answer: A

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

Q. Given:

11. class ClassA {}

12. class ClassB extends ClassA {}

13. class ClassC extends ClassA {}

and:

21. ClassA p0 = new ClassA();

22. ClassB p1 = new ClassB();

23. ClassC p2 = new ClassC();

24. ClassA p3 = new ClassB();

25. ClassA p4 = new ClassC();

Which three are valid? (Choose three.)

A. p0 = p1;

B. p1 = p2;

C. p2 = p4;

D. p2 = (ClassC)p1;

E. p1 = (ClassB)p3;

F. p2 = (ClassC)p4;

Answer: A,E,F

Explanation :up cast can and down cast concepts.

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

Q. Given:

5. class Thingy { Meter m = new Meter(); }

6. class Component { void go() { System.out.print(“c”); } }

7. class Meter extends Component { void go() { System.out.print(“m”); } }

8.

9. class DeluxeThingy extends Thingy {

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

11. DeluxeThingy dt = new DeluxeThingy();

12. dt.m.go();

13. Thingy t = new DeluxeThingy();

14. t.m.go();

15. }

16. }

Which two are true? (Choose two.)

A. The output is mm.

B. The output is mc.

C. Component is-a Meter.

D. Component has-a Meter.

E. DeluxeThingy is-a Component.

F. DeluxeThingy has-a Component.

Answer: A,F

Explanation :Meter and Thingy has Has-A relationship.

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

Q. Given:

10. interface Jumper { public void jump(); }

20. class Animal {}

30. class Dog extends Animal {

31. Tail tail;

32. }

40. class Beagle extends Dog implements Jumper{

41. public void jump() {}

42. }

50. class Cat implements Jumper{

51. public void jump() {}

52. }

Which three are true? (Choose three.)

A. Cat is-a Animal

B. Cat is-a Jumper

C. Dog is-a Animal

D. Dog is-a Jumper

E. Cat has-a Animal

F. Beagle has-a Tail

G. Beagle has-a Jumper

Answer: B,C,F

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

Q. Given:

1. import java.util.*;

2. public class WrappedString {

3. private String s;

4. public WrappedString(String s) { this.s = s; }

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

6. HashSet<Object> hs = new HashSet<Object>();

7. WrappedString ws1 = new WrappedString(“aardvark”);

8. WrappedString ws2 = new WrappedString(“aardvark”);

9. String s1 = new String(“aardvark”);

10. String s2 = new String(“aardvark”);

11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);

12. System.out.println(hs.size()); } }

What is the result?

A. 0

B. 1

C. 2

D. 3

E. 4

F. Compilation fails.

G. An exception is thrown at runtime.

Answer: D

Explanation :Hashset does not allow to add duplicate value.Here String s2 has same value as well as si so second s2 can’t be add to hashset.

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

Q. Given:

11. // insert code here

12. private N min, max;

13. public N getMin() { return min; }

14. public N getMax() { return max; }

15. public void add(N added) {

16. if (min == null || added.doubleValue() < min.doubleValue())

17. min = added;

18. if (max == null || added.doubleValue() > max.doubleValue())

19. max = added;

20. }

21. }

Which two, inserted at line 11, will allow the code to compile? (Choose two.)

A. public class MinMax<?> {

B. public class MinMax<? extends Number> {

C. public class MinMax<N extends Object> {

D. public class MinMax<N extends Number> {

E. public class MinMax<? extends Object> {

F. public class MinMax<N extends Integer> {

Answer: D,F

Hint : Generic based.N should be Number or Integer.

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

Q. Given:

3. import java.util.*;

4. public class G1 {

5. public void takeList(List<? extends String> list) {

6. // insert code here

7. }

8. }

Which three code fragments, inserted independently at line 6, will compile? (Choose

three.)

A. list.add(“foo”);

B. Object o = list;

C. String s = list.get(0);

D. list = new ArrayList<String>();

E. list = new ArrayList<Object>();

Answer: B,C,D

Explanation :List<? extends String> means that you can take any subtype of String;however, that subtype can be EITHER a subclass of a class (abstract or concrete) OR a type that implements the interface after the word extends.

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

Q. Given that the elements of a PriorityQueue are ordered according to natural ordering,

and:

2. import java.util.*;

3. public class GetInLine {

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

5. PriorityQueue<String> pq = new PriorityQueue<String>();

6. pq.add(“banana”);

7. pq.add(“pear”);

8. pq.add(“apple”);

9. System.out.println(pq.poll() + ” ” + pq.peek());

10. }

11. }

What is the result?

A. apple pear

B. banana pear

C. apple apple

D. apple banana

E. banana banana

Answer: D

Explanation :P eak :Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.

Poll : Retrieves and removes the head of this queue, or null if this queue is empty.

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

Q. Given:

enum Example { ONE, TWO, THREE }

Which statement is true?

A. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.

B. The expression (ONE < TWO) is guaranteed to be true andONE.compareTo(TWO) is guaranteed to be less than one.

C. The Example values cannot be used in a raw java.util.HashMap; instead, the

programmer must use a java.util.EnumMap.

D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated types do NOT implement java.lang.Comparable.

Answer: A

Explanation :When we store value in enum they convert integer series.

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

Q. Given:

3. import java.util.*;

4. public class Mapit {

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

6. Set<Integer> set = new HashSet<Integer>();

7. Integer i1 = 45;

8. Integer i2 = 46;

9. set.add(i1);

10. set.add(i1);

11. set.add(i2); System.out.print(set.size() + ” “);

12. set.remove(i1); System.out.print(set.size() + ” “);

13. i2 = 47;

14. set.remove(i2); System.out.print(set.size() + ” “);

15. }

16. }

What is the result?

A. 2 1 0

B. 2 1 1

C. 3 2 1

D. 3 2 2

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: B

Explanation :Hashset does not allow duplicate values.

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

Q. Given:

12. import java.util.*;

13. public class Explorer1 {

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

15. TreeSet<Integer> s = new TreeSet<Integer>();

16. TreeSet<Integer> subs = new TreeSet<Integer>();

17. for(int i = 606; i < 613; i++)

18. if(i%2 == 0) s.add(i);

19. subs = (TreeSet)s.subSet(608, true, 611, true);

20. s.add(609);

21. System.out.println(s + ” ” + subs);

22. }

23. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. [608, 609, 610, 612] [608, 610]

D. [608, 609, 610, 612] [608, 609, 610]

E. [606, 608, 609, 610, 612] [608, 610]

F. [606, 608, 609, 610, 612] [608, 609, 610]

Answer: F

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

Q. Given:

3. import java.util.*;

4. public class Quest {

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

6. String[] colors = {“blue”, “red”, “green”, “yellow”, “orange”};

7. Arrays.sort(colors);

8. int s2 = Arrays.binarySearch(colors, “orange”);

9. int s3 = Arrays.binarySearch(colors, “violet”);

10. System.out.println(s2 + ” ” + s3);

11. }

12. }

What is the result?

A. 2 -1

B. 2 -4

C. 2 -5

D. 3 -1

E. 3 -4

F. 3 -5

G. Compilation fails.

H. An exception is thrown at runtime.

Answer: C

Explanation :if value exists in array, the index of the element is returned. Otherwise, a negative value is returned.

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

Q. Given:

34. HashMap props = new HashMap();

35. props.put(“key45″, “some value”);

36. props.put(“key12″, “some other value”);

37. props.put(“key39″, “yet another value”);

38. Set s = props.keySet();

39. // insert code here

What, inserted at line 39, will sort the keys in the props HashMap?

A. Arrays.sort(s);

B. s = new TreeSet(s);

C. Collections.sort(s);

D. s = new SortedSet(s);

Answer: B

Explanation :TreeSet() sort the values.

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

Q. Which two statements are true? (Choose two.)

A. It is possible to synchronize static methods.

B. When a thread has yielded as a result of yield(), it releases its locks.

C. When a thread is sleeping as a result of sleep(), it releases its locks.

D. The Object.wait() method can be invoked only from a synchronized context.

E. The Thread.sleep() method can be invoked only from a synchronized context.

F. When the thread scheduler receives a notify() request, and notifies a thread, that thread

immediately releases its lock.

Answer: A,D

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

This entry was posted in Freshers, Java Certifications, Java Programing, Programing, Technical Interviews Questions and tagged , , . Bookmark the permalink.

3 Responses to SCJP Certification Questions With Answers Explanation :Java(Set-5)

  1. Aayush says:

    E. [606, 608, 609, 610, 612] [608, 610]
    is right answer.
    not
    F. [606, 608, 609, 610, 612] [608, 609, 610]
    I have already run this program.
    Plz Guide in right Direction.

    • papamal dhanuka says:

      Please check properly and then comment …..
      import java.util.*;

      public class Explorer1 {
      public static void main(String[] args) {

      TreeSet s = new TreeSet();

      TreeSet subs = new TreeSet();

      for(int i = 606; i < 613; i++)
      if(i%2 == 0) s.add(i);

      subs = (TreeSet)s.subSet(608, true, 611, true);
      //subs.add(611);

      //System.out.println(s +" " + subs);
      s.add(609);
      System.out.println(s +" " + subs);
      }

      }

      andoutput is:===
      [606, 608, 609, 610, 612] [608, 609, 610]

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>