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

Q. Given:

11. Float pi = new Float(3.14f);

12. if (pi > 3) {

13. System.out.print(“pi is bigger than 3. “);

14. }

15. else {

16. System.out.print(“pi is not bigger than 3. “);

17. }

18. finally {

19. System.out.println(“Have a nice day.”);

20. }

What is the result?

A. Compilation fails.

B. pi is bigger than 3.

C. An exception occurs at runtime.

D. pi is bigger than 3. Have a nice day.

E. pi is not bigger than 3. Have a nice day.

Answer: A

Explanation :Finally block can’t come without try/catch.So it will not compile.

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

Q. Given:

1. public class Boxer1{

2. Integer i;

3. int x;

4. public Boxer1(int y) {

5. x = i+y;

6. System.out.println(x);

7. }

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

9. new Boxer1(new Integer(4));

10. }

11. }

What is the result?

A. The value “4″ is printed at the command line.

B. Compilation fails because of an error in line 5.

C. Compilation fails because of an error in line 9.

D. A NullPointerException occurs at runtime.

E. A NumberFormatException occurs at runtime.

F. An IllegalStateException occurs at runtime.

Answer: D

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

Q. Given:

1. public class Person {

2. private String name;

3. public Person(String name) { this.name = name; }

4. public boolean equals(Person p) {

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

6. }

7. }

Which statement is true?

A. The equals method does NOT properly override the Object.equals method.

B. Compilation fails because the private attribute p.name cannot be accessed in line 5.

C. To work correctly with hash-based data structures, this class must also implement the

hashCode method.

D. When adding Person objects to a java.util.Set collection, the equals method in line 4

will prevent duplicates.

Answer: A

Explanation :When we use override method then we should be careful to overide rule.Here we haven’t override equals method properly.

Equals syntax : boolean equals (Object obj)

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

Q. Which two statements are true about the hashCode method? (Choose two.)

A. The hashCode method for a given class can be used to test for object equality and

object inequality for that class.

B. The hashCode method is used by the java.util.SortedSet collection class to order the

elements within that set.

C. The hashCode method for a given class can be used to test for object inequality, but

NOT object equality, for that class.

D. The only important characteristic of the values returned by a hashCode method is that

the distribution of values must follow a Gaussian distribution.

E. The hashCode method is used by the java.util.HashSet collection class to group the

elements within that set into hash buckets for swift retrieval.

Answer: C,E

Explanation :If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.

It is NOT required that if two objects are unequal according to the

equals(java.lang.Object) method, then calling the hashCode() method

on each of the two objects must produce distinct integer results. However,

the programmer should be aware that producing distinct integer results for

unequal objects may improve the performance of hashtables.

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

Q. Given:

1. public class Score implements Comparable<Score> {

2. private int wins, losses;

3. public Score(int w, int l) { wins = w; losses = l; }

4. public int getWins() { return wins; }

5. public int getLosses() { return losses; }

6. public String toString() {

7. return “<” + wins + “,” + losses + “>”;

8. }

9. // insert code here

10. }

Which method will complete this class?

A. public int compareTo(Object o){/*more code here*/}

B. public int compareTo(Score other){/*more code here*/}

C. public int compare(Score s1,Score s2){/*more code here*/}

D. public int compare(Object o1,Object o2){/*more code here*/}

Answer: B

Explanation :For Comparable interface,we have to override compareTo method.Then option B has proper syntax.

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

Q. Given a pre-generics implementation of a method:

11. public static int sum(List list) {

12. int sum = 0;

13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {

14. int i = ((Integer)iter.next()).intValue();

15. sum += i;

16. }

17. return sum;

18. }

What three changes allow the class to be used with generics and avoid an unchecked

warning? (Choose three.)

A. Remove line 14.

B. Replace line 14 with “int i = iter.next();”.

C. Replace line 13 with “for (int i : intList) {“.

D. Replace line 13 with “for (Iterator iter : intList) {“.

E. Replace the method declaration with “sum(List<int> intList)”.

F. Replace the method declaration with “sum(List<Integer> intList)”.

Answer: A,C,F

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

Q. Given:

23. Object [] myObjects = {

24. new Integer(12),

25. new String(“foo”),

26. new Integer(5),

27. new Boolean(true)

28. };

29. Arrays.sort(myObjects);

30. for(int i=0; i<myObjects.length; i++) {

31. System.out.print(myObjects[i].toString());

32. System.out.print(” “);

33. }

What is the result?

A. Compilation fails due to an error in line 23.

B. Compilation fails due to an error in line 29.

C. A ClassCastException occurs in line 29.

D. A ClassCastException occurs in line 31.

E. The value of all four objects prints in natural order.

Answer: C

Explanation :For Arrays.sort(),all objects indside myObjects should be comparable otherwise will throw ClassCastException

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

Q. Given a class Repetition:

1. package utils;

2.

3. public class Repetition {

4. public static String twice(String s) { return s + s; }

5. }

and given another class Demo:

1. // insert code here

2.

3. public class Demo {

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

5. System.out.println(twice(“pizza”));

6. }

7. }

Which code should be inserted at line 1 of Demo.java to compile and run Demo to print

“pizzapizza”?

A. import utils.*;

B. static import utils.*;

C. import utils.Repetition.*;

D. static import utils.Repetition.*;

E. import utils.Repetition.twice();

F. import static utils.Repetition.twice;

G. static import utils.Repetition.twice;

Answer:F

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

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 the following directory structure:

bigProject

|–source

| |–Utils.java

|

|–classes

|–

And the following command line invocation:

javac -d classes source/Utils.java

Assume the current directory is bigProject, what is the result?

A. If the compile is successful, Utils.class is added to the source directory.

B. The compiler returns an invalid flag error.

C. If the compile is successful, Utils.class is added to the classes directory.

D. If the compile is successful, Utils.class is added to the bigProject directory.

Answer: C

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

Q. Given:

1. package com.company.application;

2.

3. public class MainClass {

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

5. }

And MainClass exists in the /apps/com/company/application directory. Assume the

CLASSPATH environment variable is set to “.” (current directory).Which two java

commands entered at the command line will run MainClass? (Choose two.)

A. java MainClass if run from the /apps directory

B. java com.company.application.MainClass if run from the /apps directory

C. java -classpath /apps com.company.application.MainClass if run from any directory

D. java -classpath . MainClass if run from the /apps/com/company/application directory

E. java -classpath /apps/com/company/application:. MainClass if run from the /apps

directory

F. java com.company.application.MainClass if run from the

/apps/com/company/application directory

Answer: B,C

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

Q. Which statement is true?

A. A class’s finalize() method CANNOT be invoked explicitly.

B. super.finalize() is called implicitly by any overriding finalize() method.

C. The finalize() method for a given object is called no more than once by the garbage

collector.

D. The order in which finalize() is called on two objects is based on the order in which

the two objects became finalizable.

Answer: C

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

This entry was posted in C,C++ Programing, Programing and tagged , , . Bookmark the permalink.

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

  1. Nikhil Arora says:

    Why the answer of the Question is option C?
    Question is : 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…….

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>