Sun Certified Programmer for the Java
2 Platform Exam Question And Answer
QUESTION: 41
Given:
1.
public class Donkey2 {
2.
public static void main(String[] args) {
3.
boolean assertsOn = true;
4.
assert (assertsOn) : assertsOn = true;
5.
if(assertsOn) {
6.
System.out.println("assert is on");
7.
}
8.
}
9.
}
If
class Donkey is invoked twice, the first time without assertions enabled, and
the second time with assertions enabled, what are the results?
A.
no output
B.
no output
assert
is on
C.
assert is on
D.
no output
An
AssertionError is thrown.
E.
assert is on
An
AssertionError is thrown.
Answer: C
QUESTION: 42
Given:
31.
public void method() {
32.
A a = new A();
33.
a.method1();
34.
}
Which
statement is true if a TestException is thrown on line 3 of class B?
1.public
class A{
2. public void method1(){
3. try {
4. B b=new B();
5. b.method2();
6. // more code here
7. } catch(TestException te{
8. throw new RuntimeException(te);
9. }
10. }
11. }
1.Public
class B {
2. Public void method2() throws TestException {
3. //more code here
4. }
5. }
1.Public
class TextException extends Exception {
2.}
A.
Line 33 must be called within a try block.
B.
The exception thrown by method1 in class A is not required to be caught.
C.
The method declared on line 31 must be declared to throw a RuntimeException.
D.
On line 5 of class A, the call to method2 of class B does not need to be placed
in a try/catch block.
Answer: B
QUESTION: 43
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
QUESTION: 44
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
QUESTION: 45
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
QUESTION: 46
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
QUESTION: 47
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
QUESTION: 48
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
QUESTION: 49
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
QUESTION: 50
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
QUESTION: 51
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
QUESTION: 52
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
QUESTION: 53
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
QUESTION: 54
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
QUESTION: 55
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
QUESTION: 56
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
QUESTION: 57
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
QUESTION: 58
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
QUESTION: 59
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
QUESTION: 60
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
No comments:
Post a Comment