1.
-
Given:
-
11. public interface Status {
-
12. /* insert code here */ int MY_VALUE = 10;
-
13. }
-
Which three are valid on line 12? (Choose three.)
Correct Answer(s)
A. Final
B. Static
D. Public
Explanation
The given code snippet is declaring a public interface named "Status". On line 12, the code is declaring an integer constant named "MY_VALUE" and assigning it a value of 10. In Java, interface constants are implicitly public, static, and final. Therefore, the valid options for line 12 are final, static, and public.
2.
-
Given:
-
10. public class Bar {
-
11.static void foo(int...x) {
-
12. // insert code here
-
13. }
-
14. }
-
Which two code fragments, inserted independently at line 12, will allow
-
the class to compile? (Choose two.)
Correct Answer(s)
B. for(int z : x) System.out.println(z);
D. For( int i=0; i< x.length; i++ ) System.out.println(x[i]);
Explanation
The first code fragment, "for(int z : x) System.out.println(z);", uses an enhanced for loop to iterate over the elements in the array "x" and print each element. This is a valid way to iterate over an array in Java.
The second code fragment, "for( int i=0; i< x.length; i++ ) System.out.println(x[i]);", uses a traditional for loop to iterate over the array "x" using an index variable "i". It prints each element in the array by accessing it using the index. This is also a valid way to iterate over an array in Java.
3.
-
Given:
-
11. public class Test {
-
12. public static void main(String [] args) {
-
13. int x =5;
-
14. boolean b1 = true;
-
15. boolean b2 = false;
-
16.
-
17.if((x==4) && !b2)
-
18. System.out.print(”l “);
-
19. System.out.print(”2 “);
-
20. if ((b2 = true) && b1)
-
21. System.out.print(”3 “);
-
22. }
-
23. }
-
What is the result?
Correct Answer
D. 2 3
Explanation
The code first checks if the condition (x==4) is true and b2 is false. Since x is not equal to 4 and b2 is false, the condition is not satisfied and the code skips the print statement on line 18.
Then, the code assigns true to b2 and checks if b1 is true. Since b1 is true, the condition is satisfied and the code prints "3" on line 21.
Therefore, the result is "2 3".
4.
-
4. Given:
-
31. // some code here
-
32. try {
-
33. // some code here
-
34. } catch (SomeException se) {
-
35. // some code here
-
36. } finally {
-
37. // some code here
-
38. }
-
Under which three circumstances will the code on line 37 be executed?
-
(Choose three.)
Correct Answer(s)
B. The code on line 33 throws an exception.
C. The code on line 35 throws an exception.
E. The code on line 33 executes successfully.
Explanation
The code on line 37 will be executed under the following three circumstances:
1) The code on line 33 throws an exception.
2) The code on line 35 throws an exception.
3) The code on line 33 executes successfully.
5.
-
Given:
-
10. interface Foo {}
-
11. class Alpha implements Foo { }
-
12. class Beta extends Alpha {}
-
13. class Delta extends Beta {
-
14. public static void main( String[] args) {
-
15. Beta x = new Beta();
-
16. // insert code here
-
17. }
-
18. }
-
Which code, inserted at line 16, will cause a
-
java.lang.ClassCastException?
Correct Answer
C. Foo f= (Delta)x;
Explanation
The code "Foo f= (Delta)x;" will cause a java.lang.ClassCastException because it is trying to cast an object of type Beta to type Delta, which is not a valid casting operation.
6.
-
Given:
-
• d is a valid, non-null Date object
-
• df is a valid, non-null DateFormat object set to the
-
current locale
-
What outputs the current locales country name and the appropriate
-
version of d’s date?
Correct Answer
B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ “ “ + df.format(d));
Explanation
The correct answer is `Locale loc = Locale.getDefault(); System.out.println(loc.getDisplayCountry() + " " + df.format(d));`. This code retrieves the default locale using `Locale.getDefault()`, which represents the current locale of the system. It then uses the `getDisplayCountry()` method of the `Locale` class to get the name of the country in the current locale. Finally, it uses the `format()` method of the `DateFormat` object `df` to format the date `d` according to the current locale. The output will be the current locale's country name followed by the formatted date.
7.
-
Given:
-
20. public class CreditCard {
-
21.
-
22. private String cardlD;
-
23. private Integer limit;
-
24. public String ownerName;
-
25.
-
26. public void setCardlnformation(String cardlD,
-
27. String ownerName,
-
28. Integer limit) {
-
29. this.cardlD = cardlD;
-
30. this.ownerName = ownerName;
-
31. this.limit = limit;
-
32. }
-
33. }
-
Which is true?
Correct Answer
C. The ownerName variable breaks encapsulation.
Explanation
The ownerName variable breaks encapsulation because it is declared as public, allowing direct access and modification from outside the class. Encapsulation is a principle of object-oriented programming that promotes data hiding and protects data from being accessed or modified directly. In this case, the ownerName variable should have been declared as private to ensure proper encapsulation.
8.
-
Assume that country is set for each class.
-
Given:
-
10. public class Money {
-
11. private String country, name;
-
12. public getCountry() { return country; }
-
13.}
-
and:
-
24. class Yen extends Money {
-
25. public String getCountry() { return super.country; }
-
26. }
-
27.
-
28. class Euro extends Money {
-
29. public String getCountry(String timeZone) {
-
30. return super.getCountry();
-
31. }
-
32. }
-
Which two are correct? (Choose two.)
Correct Answer(s)
B. Euro returns correct values.
E. Compilation fails because of an error at line 25.
Explanation
The statement "Euro returns correct values" is correct because the method getCountry() in the Euro class calls the super.getCountry() method, which returns the value of the country variable in the Money class.
The statement "Compilation fails because of an error at line 25" is also correct because the method getCountry() in the Yen class tries to access the private variable country directly using the super keyword, which is not allowed. The country variable in the Money class should have a protected or public access modifier in order to be accessible in the subclass.
9.
Which Man class properly represents the relationship “Man has a best
friend who is a Dog”?
Correct Answer
D. Class Man { private Dog bestFriend; }
Explanation
The correct answer is "class Man { private Dog bestFriend; }". This answer properly represents the relationship "Man has a best friend who is a Dog" by declaring a private variable "bestFriend" of type Dog within the Man class.
10.
-
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 is true?
Correct Answer
A. The time to find the value from HashMap with a Person key depends
on the size of the map.
Explanation
The time to find the value from a HashMap with a Person key depends on the size of the map because the HashMap uses a hashing algorithm to calculate the index of the key-value pair based on the key's hash code. As the size of the map increases, the number of elements that need to be checked for a matching hash code also increases, resulting in a longer search time. Therefore, the time complexity for finding a value in a HashMap is O(1) on average, but it can be O(n) in the worst case scenario when there are many collisions.
11.
-
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
-
31. System.out.print(myObjects[i].toString());
-
32. System.out.print(” “);
-
33. }
-
What is the result?
-
Correct Answer
C. A ClassCastException occurs in line 29.
12.
-
12. Given:
-
13. public class Pass {
-
14. public static void main(String [1 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?
-
Correct Answer
D. DoStuffx = 5 main x = 5
Explanation
The code will compile successfully. The main method initializes the variable x with a value of 5. The doStuff method is called with the value of x as an argument. Inside the doStuff method, the value of x is printed and then incremented by 1. The main method then prints the value of x again. Since the value of x is passed by value, the increment operation in the doStuff method does not affect the value of x in the main method. Therefore, the output will be "doStuff x = 5 main x = 5".
13.
-
Given:
-
10. package com.sun.scjp;
-
11. public class Geodetics {
-
12. public static final double DIAMETER = 12756.32; // kilometers
-
13. }
-
Which two correctly access the DIAMETER member of the Geodetics
-
class? (Choose two.)
Correct Answer(s)
A. import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway()
{ return Geodetics.DIAMETER/2.0; } }
C. import static com.sun.scjp.Geodetics. *;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
Explanation
The correct answers are "import com.sun.scjp.Geodetics;" and "public class TerraCarta { public double halfway() { return Geodetics.DIAMETER/2.0; } }".
In order to access the DIAMETER member of the Geodetics class, we need to import the com.sun.scjp.Geodetics package. This is done correctly in the first option "import com.sun.scjp.Geodetics;".
Then, in order to access the DIAMETER member, we need to use the syntax "Geodetics.DIAMETER". This is done correctly in the first option "public class TerraCarta { public double halfway() { return Geodetics.DIAMETER/2.0; } }".
Therefore, both options correctly access the DIAMETER member of the Geodetics class.
14.
-
Given:
-
10. class Nav{
-
11. public enum Direction { NORTH, SOUTH, EAST, WEST }
-
12. }
-
13. public class Sprite{
-
14. // insert code here
-
15. }
-
Which code, inserted at line 14, allows the Sprite class to compile?
Correct Answer
D. Nav.Direction d = Nav.Direction.NORTH;
Explanation
The code "Nav.Direction d = Nav.Direction.NORTH;" allows the Sprite class to compile because it specifies the fully qualified name of the Direction enum, which is "Nav.Direction". This ensures that the compiler can find and reference the enum correctly.
15.
-
Given:
-
10. interface Foo { int bar(); }
-
11. public class Sprite {
-
12. public int fubar( Foo foo) { return foo.bar(); }
-
13. public void testFoo() {
-
14. fubar(
-
15. // insert code here
-
16.);
-
17. }
-
18. }
-
Which code, inserted at line 15, allows the class Sprite to compile?
Correct Answer
C. NewFoo() { public int bar(){return 1; } }
Explanation
The code "newFoo() { public int bar(){return 1; } }" allows the class Sprite to compile because it creates a new instance of the interface Foo and provides an implementation for the bar() method. This allows the fubar() method to be called with a valid argument, satisfying the requirements of the method signature.
16.
-
Click the Exhibit button.
-
10. interface Foo {
-
11. int bar();
-
12. }
-
13.
-
14. public class Beta {
-
15.
-
16. class A implements Foo {
-
17. public int bar() { return 1; }
-
18. }
-
19.
-
20. public int fubar( Foo foo) { return foo.bar(); }
-
21.
-
22. public void testFoo() {
-
23.
-
24. class A implements Foo {
-
25. public int bar() { return 2; }
-
26. }
-
27.
-
28. System.out.println( fubar( new A()));
-
29. }
-
30.
-
31. public static void main( String[] argv) {
-
32. new Beta().testFoo();
-
33. }
-
34. }
-
Which three statements are true? (Choose three.)
Correct Answer(s)
B. The code compiles and the output is 2.
E. If lines 16, 17 and 18 were removed, the code would compile and
the output would be 2.
F. If lines 24, 25 and 26 were removed, the code would compile and
the output would be 1.
Explanation
The code compiles and the output is 2 because in line 28, the method fubar is called with a new instance of class A as the argument, which implements the Foo interface and overrides the bar() method to return 2. If lines 16, 17, and 18 were removed, the code would still compile and the output would be 2 because the A class inside the Beta class would be used. If lines 24, 25, and 26 were removed, the code would still compile and the output would be 1 because the A class outside the Beta class would be used.
17.
-
Given:
-
1. package sun.scjp;
-
2. public enum Color { RED, GREEN, BLUE }
-
1. package sun.beta;
-
2. // insert code here
-
3. public class Beta {
-
4. Color g = GREEN;
-
5. public static void main( String[] argv)
-
6. { System.out.println( GREEN); }
-
7. }
-
The class Beta and the enum Color are in different packages.
-
Which two code fragments, inserted individually at line 2 of the Beta
-
declaration, will allow this code to compile? (Choose two.)
Correct Answer(s)
C. Import sun.scjp.Color; import static sun.scjp.Color.*;
E. Import sun.scjp.Color; import static sun.scjp.Color.GREEN
Explanation
The code is trying to access the GREEN constant from the Color enum in the Beta class. In order to do this, the Color enum must be imported. The first option "import sun.scjp.Color; import static sun.scjp.Color.*;" imports the Color enum and all its static members, allowing the code to access the GREEN constant. The second option "import sun.scjp.Color; import static sun.scjp.Color.GREEN;" imports only the Color enum and the GREEN constant directly, allowing the code to access the GREEN constant.
18.
-
Given:
-
1. public interface A {
-
2. String DEFAULT_GREETING = “Hello World”;
-
3. public void method1();
-
4. }
-
A programmer wants to create an interface called B that has A as its
-
parent. Which interface declaration is correct?
Correct Answer
A. public interface B extends A { }
Explanation
The correct answer is "public interface B extends A { }". This is the correct syntax for declaring an interface B that extends interface A. The "extends" keyword is used to indicate that interface B inherits from interface A.
19.
-
Given:
-
1. class TestA {
-
2. public void start() { System.out.println(”TestA”); }
-
3. }
-
4. public class TestB extends TestA {
-
5. public void start() { System.out.println(”TestB”); }
-
6. public static void main(String[] args) {
-
7. ((TestA)new TestB()).start();
-
8. }
-
9. }
-
What is the result?
Correct Answer
B. TestB
Explanation
The result of the given code is "TestB". This is because the start() method in the TestB class overrides the start() method in the TestA class. When the start() method is called on the TestA object created from the TestB class, it executes the overridden method in TestB, which prints "TestB".
20.
-
Given:
-
1. interface TestA { String toString(); }
-
2. public class Test {
-
3. public static void main(String[] args) {
-
4. System.out.println(new TestA() {
-
5. public String toString() { return “test”; }
-
6. });
-
7. }
-
8. }
-
What is the result?
Correct Answer
A. Test
Explanation
The code defines an anonymous inner class that implements the TestA interface and overrides the toString() method. In the main method, an instance of this anonymous inner class is created and its toString() method is called. The overridden toString() method returns the string "test". Therefore, the result of the code is "test", which will be printed to the console.
21.
-
Question 21
-
Given:
-
11. public abstract class Shape {
-
12. int x;
-
13. int y;
-
14. public abstract void draw();
-
15. public void setAnchor(int x, int y) {
-
16. this.x = x;
-
17. this.y = y;
-
18. }
-
19. }
-
and a class Circle that extends and fully implements the Shape class.
-
Which is correct?
Correct Answer
C. Shape s = new Circle();
s.setAnchor(10,10);
s.draw();
Explanation
The correct answer is "Shape s = new Circle(); s.setAnchor(10,10); s.draw();". This is because the Shape class is an abstract class and cannot be instantiated. However, the Circle class extends and fully implements the Shape class, so we can create an instance of Circle and assign it to a variable of type Shape. This allows us to call the setAnchor() and draw() methods on the Shape variable, which will be executed based on the implementation in the Circle class.
22.
-
Given:
-
10. abstract public class Employee {
-
11. protected abstract double getSalesAmount();
-
12. public double getCommision() {
-
13. return getSalesAmount() * 0.15;
-
14. }
-
15. }
-
16. class Sales extends Employee {
-
17. // insert method here
-
18. }
-
Which two methods, inserted independently at line 17, correctly
-
complete the Sales class? (Choose two.)
-
.
Correct Answer(s)
B. Public double getSalesAmount() { return 1230.45; }
D. Protected double getSalesAmount() { return 1230.45; }
Explanation
The correct answer is "public double getSalesAmount() { return 1230.45; }" and "protected double getSalesAmount() { return 1230.45; }". These two methods correctly complete the Sales class because they override the abstract method "getSalesAmount()" from the Employee class. The Sales class is a subclass of Employee and it must provide an implementation for the abstract method in the superclass. The access modifiers of the methods in the subclass can be the same or more accessible than the corresponding method in the superclass, so both public and protected access modifiers are valid in this case.
23.
-
Given:
-
10. interface Data { public void load(); }
-
11. abstract class Info { public abstract void load(); }
-
Which class correctly uses the Data interface and Info class?
-
A
Correct Answer
A. public class Employee extends Info implements Data {
public void load() { /*do something*/ }
}
Explanation
The correct answer is option A. This is because the class "Employee" correctly extends the abstract class "Info" and implements the interface "Data". It provides the implementation for the "load()" method defined in both the abstract class and the interface.
24.
-
Given:
-
11. public abstract class Shape {
-
12. private int x;
-
13. private int y;
-
14. public abstract void draw();
-
15. public void setAnchor(int x, int y) {
-
16. this.x = x;
-
17. this.y = y;
-
18. }
-
19. }
-
Which two classes use the Shape class correctly? (Choose two.)
Correct Answer(s)
B. Public abstract class Circle extends Shape {
private int radius;
}
E. public class Circle extends Shape {
private int radius;
public void draw() {/* code here */}
}
Explanation
The first class, "public abstract class Circle extends Shape", correctly uses the Shape class by extending it and implementing the abstract method "draw()". This class provides a concrete implementation for the "draw()" method.
The second class, "public class Circle extends Shape", also correctly uses the Shape class by extending it and providing its own implementation for the "draw()" method.
Both classes properly inherit from the Shape class and implement the necessary methods, making them correct uses of the Shape class.
25.
Which two classes correctly implement both the java.lang.Runnable
and the java.lang.Clonable interfaces? (Choose two.)
Correct Answer(s)
C. . public class Session
implements Runnable, Clonable {
public void run() { / do something */ }
public Object clone() { /* make a copy */ }
}
D. . public abstract class Session
implements Runnable, Clonable {
public void run() { / do something */ }
public Object clone() { /*make a copy */ }
}
Explanation
The correct answers are the first and fourth options.
The first option correctly implements both the Runnable and Clonable interfaces by providing the run() and clone() methods.
The fourth option also correctly implements both interfaces by providing the run() and clone() methods, but it is an abstract class.
The second, third, and fifth options are incorrect because they either have syntax errors (e.g., double "implements" in the fifth option) or do not provide the necessary methods for both interfaces.