Top Banner
1. Language Fundamentals Q: 1 Given 10. class Foo { 11. static void alpha() { /* more code here */ } 12. void beta() { /* more code here */ }Page 1 of 182Attention: 13. } Which two statements are true? (Choose two.) A. Foo.beta() is a valid invocation of beta(). B. Foo.alpha() is a valid invocation of alpha(). C. Method beta() can directly call method alpha(). D. Method alpha() can directly call method beta(). Answer: B, C Q: 2 Given: 12. public class Yippee2 { 13. 14. static public void main(String [] yahoo) { 15. for(int x = 1; x < yahoo.length; x++) { 16. System.out.print(yahoo[x] + " "); 17. } 18. } 19. } and the command line invocation: java Yippee2 a b c What is the result? A. a b B. b c C. a b c D. Compilation fails. E. An exception is thrown at runtime. Answer: B Q: 3 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.
42
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SCJP(DS)

 1. Language Fundamentals

Q: 1 Given10. class Foo {11. static void alpha() { /* more code here */ }12. void beta() { /* more code here */ }Page 1 of 182Attention:13. }                                                                                             

Which two statements are true? (Choose two.)A. Foo.beta() is a valid invocation of beta().B. Foo.alpha() is a valid invocation of alpha().C. Method beta() can directly call method alpha().D. Method alpha() can directly call method beta().Answer: B, C

Q: 2 Given:                                                                                  12. public class Yippee2 {13.14. static public void main(String [] yahoo) {15. for(int x = 1; x < yahoo.length; x++) {16. System.out.print(yahoo[x] + " ");17. }18. }19. }and the command line invocation:java Yippee2 a b c

What is the result?A. a bB. b cC. a b c                                                            D. Compilation fails.E. An exception is thrown at runtime.Answer: B

Q: 3 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 Yippeejava Yippee 1 2 3 4

What is the result?A. No output is produced.1 2 3B. No output is produced.2 3 4C. No output is produced.1 2 3 4D. An exception is thrown at runtime.1 2 3E. An exception is thrown at runtime.2 3 4F. An exception is thrown at runtime.1 2 3 4Answer: B

Page 2: SCJP(DS)

Q: 4 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 here2.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: 5  A JavaBeans component has the following field:11. private boolean enabled;Which two pairs of method declarations follow the JavaBeans standard for accessing this field? (Choose two.)A. public void setEnabled( boolean enabled )public boolean getEnabled()B. public void setEnabled( boolean enabled )public void isEnabled()C. public void setEnabled( boolean enabled )public boolean isEnabled()

D. public boolean setEnabled( boolean enabled )public boolean getEnabled()                           Answer: A, C

Q: 6Given classes defined in two different files:1. package util;2. public class BitUtils {3. public static void process(byte[]) { /* more code here */ }4. }1. package app;2. public class SomeApp {3. public static void main(String[] args) {4. byte[] bytes = new byte[256];5. // insert code here6. }7. }

What is required at line 5 in class SomeApp to use the process method of BitUtils?A. process(bytes);B. BitUtils.process(bytes);C. util.BitUtils.process(bytes);D. SomeApp cannot use methods in BitUtils.E. import util.BitUtils.*; process(bytes);Answer: C

Page 3: SCJP(DS)

Q: 7 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 and ONE.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

Q: 8 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.)

A. public class Circle implements Shape {private int radius;}B. public abstract class Circle extends Shape {private int radius;}C. public class Circle extends Shape {private int radius;public void draw();}D. public abstract class Circle implements Shape {private int radius;public void draw();}E. public class Circle extends Shape {private int radius;public void draw() {/* code here */}F. public abstract class Circle implements Shape {private int radius;public void draw() { /* code here */ }

Answer: B, E

Q: 09 Given:

10. class Nav{11. public enum Direction { NORTH, SOUTH, EAST, WEST }12. }13. public class Sprite{14. // insert code here15. }

Which code, inserted at line 14, allows the Sprite class to compile?A. Direction d = NORTH;B. Nav.Direction d = NORTH;

Page 4: SCJP(DS)

C. Direction d = Direction.NORTH;D. Nav.Direction d = Nav.Direction.NORTH;

Answer: D

Q: 10 Click the Exhibit button.Which three statements are true? (Choose three.)

A. Compilation fails.B. The code compiles and the output is 2.C. If lines 16, 17 and 18 were removed, compilation would fail.D. If lines 24, 25 and 26 were removed, compilation would fail.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.Answer: B, E, F

Page 5: SCJP(DS)

Q: 11 Click the Task button.

Solution:interface Reloadable{    public void reload();    }class Edit{    public void edit(){/* Edit Here*/}}  interface Displayable                        extends   Reloadable {      public void display();  } 

Q:12 Given:35. String #name = "Jane Doe";36. int $age = 24;37. Double _height = 123.5;38. double ~temp = 37.5;

Which two statements are true? (Choose two.)A. Line 35 will not compile.B. Line 36 will not compile.C. Line 37 will not compile.D. Line 38 will not compile.Answer: A, D

Q: 13 Given:55. int [] x = {1, 2, 3, 4, 5};56. int y[] = x;57. System.out.println(y[2]);

Which statement is true?A. Line 57 will print the value 2.B. Line 57 will print the value 3.C. Compilation will fail because of an error in line 55.D. Compilation will fail because of an error in line 56.Answer: B

Page 6: SCJP(DS)

Q: 14 A programmer needs to create a logging method that can accept anarbitrary number of arguments. For example, it may be called in these ways:logIt("log message1");logIt("log message2","log message3");logIt("log message4","log message5","log message6");Which declaration satisfies this requirement?A. public void logIt(String * msgs)B. public void logIt(String [] msgs)C. public void logIt(String... msgs)D. public void logIt(String msg1, String msg2, String msg3)Answer: C

Q: 15Which two code fragments correctly create and initialize a static array of intelements? (Choose two.)A. static final int[] a = { 100,200 };B. static final int[] a;static { a=new int[2]; a[0]=100; a[1]=200; }C. static final int[] a = new int[2]{ 100,200 };D. static final int[] a;static void init() { a = new int[3]; a[0]=100; a[1]=200; }Answer: A, B

Q: 16 Given:11. public static void main(String[] args) {12. String str = "null";13. if (str == null) {14. System.out.println("null");15. } else (str.length() == 0) {16. System.out.println("zero");17. } else {18. System.out.println("some");19. }20. }

What is the result?A. nullB. zeroC. someD. Compilation fails.E. An exception is thrown at runtime.Answer: D

Q: 17 Click the Exhibit button.Given:34. Test t = new Test();35. t.method(5);What is the output from line 5 of the Test class?

A. 5B. 10C. 12D. 17

Page 7: SCJP(DS)

E. 24   Answer: B

Q: 18 Given11. public interface Status {12. /* insert code here */ int MY_VALUE = 10;13. }

Which three are valid on line 12? (Choose three.)A. finalB. staticC. nativeD. publicE. privateF. abstractG. protectedAnswer: A, B, D

Question: 19 A programmer is designing a class to encapsulate the informationabout an inventory item. A JavaBeans component is needed todo this. The Inventoryltem class has private instance variables to storethe item information:10. private int itemId;11. private String name;12. private String description;Which method signature follows the JavaBeans naming standards formodifying the itemld instance variable?A. itemID(int itemId)B. update(int itemId)C. setItemId(int itemId)D. mutateItemId(int itemId)E. updateItemID(int itemId)

Answer: C

Question:20Given a file GrizzlyBear.java:1. package animals.mammals;2.3. public class GrizzlyBear extends Bear {4. void hunt() {5. Salmon s = findSalmon();6. s.consume();7. }8. }and another file, Salmon.java:1. package animals.fish;2.3. public class Salmon extends Fish {4. void consume() { /* do stuff */ }5. }Assume both classes are defined in the correct directories for theftpackages, and that the Mammal class correctly defines thefindSalmon() method. Which two changes allow this code to compilecorrectly? (Choose two.)A. add public to the start of line 4 in Salmon.javaB. add public to the start of line 4 in GrizzlyBear.javaC. add import animals.mammals.*; at line 2 in Salmon.javaD. add import animals.fish.*; at line 2 in GrizzlyBear.javaE. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.javaF. add import animals.mammals.GrizzlyBear.*;at line 2 in Salmon.java

Page 8: SCJP(DS)

Answer: AD21. Which are valid declarations? (Choose all that apply.)A. int $x;B. int 123;C. int _123;D. int #dim;E. int %percent;F. int *divide;G. int central_sales_region_Summer_2005_gross_sales;

Answer:-> A, C, and G are legal identifiers.->   B is incorrect because an identifier can't start with a digit.       D, E, and F are incorrect because identifiers must start with $, _, or a letter.

22. Which method names follow the JavaBeans standard? (Choose all that apply.)A. addSizeB. getCustC. deleteRepD. isColoradoE. putDimensions

Answer:-> B and D use the valid prefixes 'get' and 'is'.->A, C, and E are incorrect because 'add', 'delete' and 'put' are not standard   JavaBeans name prefixes.

23. Given:1. class Voop {2. public static void main(String[] args) {3. doStuff(1);4. doStuff(1,2);5. }6. // insert code here7. }Which, inserted independently at line 6, will compile? (Choose all that apply.)A. static void doStuff(int... doArgs) { }B. static void doStuff(int[] doArgs) { }C. static void doStuff(int doArgs...) { }D. static void doStuff(int... doArgs, int y) { }E. static void doStuff(int x, int... doArgs) { }

Answer:-> A and E use valid var-args syntax.-> B and C are invalid var-arg syntax, and D is invalid because the var-arg must be the last of a method's arguments. (Objective 1.4)

24. Which are legal declarations? (Choose all that apply.)A. short x [];B. short [] y;C. short[5] x2;D. short z2 [5];E. short [] z [] [];F. short [] y2 = [5];

Answer:-> A, B, and E are correct array declarations; E is a three dimensional array.-> C, D, and F are incorrect, you can't include the size of your array in a declaration unless you also instantiate the array object. F uses invalid instantiation syntax. (Objective 1.3)

Page 9: SCJP(DS)

 2. Declarations and Access Control

Q: 1 Click the Task button.

Solution:

enum Element{    EARTH,WIND,    FIRE{public String info(){return "Hot";}    };    public String info(){return "element";}}

Q: 2 Given:10. package com.sun.scjp;11. public class Geodetics {12. public static final double DIAMETER = 12756.32; // kilometers13. }

Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)A. import com.sun.scjp.Geodetics;public class TerraCarta {public double halfway(){ return Geodetics.DIAMETER/2.0; }B. import static com.sun.scjp.Geodetics;public class TerraCarta{public double halfway() { return DIAMETER/2.0; } }C. import static com.sun.scjp.Geodetics.*;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }D. package com.sun.scjp;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }

Page 10: SCJP(DS)

Answer: A, C

Q: 3 Click the Task button.

Solution:

package com.sun.cert;import java.util.*;public class AddressBook{    ArrayList entries;}

Q: 4 Which two classes correctly implement both the java.lang.Runnable and thejava.lang.Clonable interfaces? (ChooseA. public class Sessionimplements Runnable, Clonable {public void run();public Object clone();}B. public class Sessionextends Runnable, Clonable {public void run() { /* do something */ }public Object clone() { /* make a copy */ }C. public class Sessionimplements Runnable, Clonable {public void run() { /* do something */ }public Object clone() { /* make a copy */ }D. public abstract class Sessionimplements Runnable, Clonable {public void run() { /* do something */ }public Object clone() { /*make a copy */ }E. public class Sessionimplements Runnable, implements Clonable {public void run() { /* do something */ }public Object clone() { /* make a copy */ }

Answer: C, D

Q: 5 Given classes defined in two different files:1. package util;2. public class BitUtils {3. private static void process(byte[] b) {}

Page 11: SCJP(DS)

4. }1. package app;2. public class SomeApp {3. public static void main(String[] args) {4. byte[] bytes = new byte[256];5. // insert code here6. }7. }

What is required at line 5 in class SomeApp to use the process method of BitUtils?A. process(bytes);B. BitUtils.process(bytes);C. app.BitUtils.process(bytes);D. util.BitUtils.process(bytes);E. import util.BitUtils.*; process(bytes);F. SomeApp cannot use the process method in BitUtils.Answer: F

Q: 6 Given:11. class Cup { }12. class PoisonCup extends Cup { }...21. public void takeCup(Cup c) {22. if (c instanceof PoisonCup) {23. System.out.println("Inconceivable!");24. } else if (c instanceof Cup) {25. System.out.println("Dizzying intellect!");26. } else {27. System.exit(0);28. }29. }And the execution of the statements:Cup cup = new PoisonCup();takeCup(cup);What is the output?A. Inconceivable!B. Dizzying intellect!C. The code runs with no output.D. An exception is thrown at runtime.E. Compilation fails because of an error in line 22.

Answer: A

Q: 7 Click the Exhibit button.public class A{      private int counter=0;      public static int getInstanceCount()     {         return counter;     }     public A()     {        counter++;     } }Given this code from Class B:25. A a1 = new A();26. A a2 = new A();27. A a3 = new A();28. System.out.println(A.getInstanceCount());What is the result?A. Compilation of class A fails.

Page 12: SCJP(DS)

B. Line 28 prints the value 3 to System.out.C. Line 28 prints the value 1 to System.out.D. A runtime error occurs when line 25 executes.E. Compilation fails because of an error on line 28.

Answer: A

Q:8 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

Q:09 Given:11. interface DeclareStuff {12. public static final int EASY = 3;13. void doStuff(int t); }14. public class TestDeclare implements DeclareStuff {15. public static void main(String [] args) {16. int x = 5;17. new TestDeclare().doStuff(++x);18. }19. void doStuff(int s) {20. s += EASY + ++s;21. System.out.println("s " + s);22. }23. }

What is the result?A. s 14B. s 16C. s 10D. Compilation fails.E. An exception is thrown at runtime.Answer: D

Q: 10 Given:1. public class TestString1 {2. public static void main(String[] args) {3. String str = "420";4. str += 42;5. System.out.print(str);6. }7. }What is the output?A. 42B. 420C. 462D. 42042E. Compilation fails.F. An exception is thrown at runtime.Answer: D

Q: 6 Given:11. class Cup { }12. class PoisonCup extends Cup { }...

Page 13: SCJP(DS)

21. public void takeCup(Cup c) {22. if (c instanceof PoisonCup) {23. System.out.println("Inconceivable!");24. } else if (c instanceof Cup) {25. System.out.println("Dizzying intellect!");26. } else {27. System.exit(0);28. }29. }And the execution of the statements:Cup cup = new PoisonCup();takeCup(cup);What is the output?A. Inconceivable!B. Dizzying intellect!C. The code runs with no output.D. An exception is thrown at runtime.E. Compilation fails because of an error in line 22.

Answer: A

Q: 7 Click the Exhibit button.public class A{      private int counter=0;      public static int getInstanceCount()     {         return counter;     }     public A()     {        counter++;     } }Given this code from Class B:25. A a1 = new A();26. A a2 = new A();27. A a3 = new A();28. System.out.println(A.getInstanceCount());What is the result?A. Compilation of class A fails.B. Line 28 prints the value 3 to System.out.C. Line 28 prints the value 1 to System.out.D. A runtime error occurs when line 25 executes.E. Compilation fails because of an error on line 28.

Answer: A

Q:8 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

Q:09 Given:11. interface DeclareStuff {12. public static final int EASY = 3;

Page 14: SCJP(DS)

13. void doStuff(int t); }14. public class TestDeclare implements DeclareStuff {15. public static void main(String [] args) {16. int x = 5;17. new TestDeclare().doStuff(++x);18. }19. void doStuff(int s) {20. s += EASY + ++s;21. System.out.println("s " + s);22. }23. }

What is the result?A. s 14B. s 16C. s 10D. Compilation fails.E. An exception is thrown at runtime.Answer: D

Q: 10 Given:1. public class TestString1 {2. public static void main(String[] args) {3. String str = "420";4. str += 42;5. System.out.print(str);6. }7. }What is the output?A. 42B. 420C. 462D. 42042E. Compilation fails.F. An exception is thrown at runtime.Answer: D

Q: 11 Given:11. class Converter {12. public static void main(String[] args) {13. Integer i = args[0];14. int j = 12;15. System.out.println("It is " + (j==i) + " that j==i.");16. }17. }What is the result when the programmer attempts to compile the code and run it with the command java Converter 12?A. It is true that j==i.B. It is false that j==i.C. An exception is thrown at runtime.D. Compilation fails because of an error in line 13.Answer: D

Q: 12 Given:10. int x = 0;11. int y = 10;12. do {13. y--;14. ++x;15. } while (x < 5);16. System.out.print(x + "," + y);What is the result?A. 5,6B. 5,5

Page 15: SCJP(DS)

C. 6,5D. 6,6Answer: B

Q: 13 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?A. public interface B extends A {}B. public interface B implements A {}C. public interface B instanceOf A {}D. public interface B inheritsFrom A {}Answer: A

Q: 14 Given:11. public enum Title {12. MR("Mr."), MRS("Mrs."), MS("Ms.");13. private final String title;14. private Title(String t) { title = t; }15. public String format(String last, String first) {16. return title + " " + first + " " + last;17. }18. }19. public static void main(String[] args) {20. System.out.println(Title.MR.format("Doe", "John"));21. }What is the result?A. Mr. John DoeB. An exception is thrown at runtime.C. Compilation fails because of an error in line 12.D. Compilation fails because of an error in line 15.E. Compilation fails because of an error in line 20.Answer: A

Q: 15 Given:1. package test;2.3. class Target {4. public String name = "hello";5. }What can directly access and change the value of the variable name?A. any class                                    B. only the Target classC. any class in the test package      D. any class that extends TargetAnswer: C

Q: 16 Given:11. public class Ball{12. public enum Color { RED, GREEN, BLUE };13. public void foo(){14. // insert code here15. { System.out.println(c); }16. }17. }Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?A. for( Color c : Color.values() )B. for( Color c = RED; c <= BLUE; c++ )C. for( Color c ; c.hasNext() ; c.next() )D. for( Color c = Color[0]; c <= Color[2]; c++ )E. for( Color c = Color.RED; c <= Color.BLUE; c++ )Answer: A

Page 16: SCJP(DS)

Q: 17 Click the Task button.

Solution:package alpha;public class Alpha{private String alpha;public  Alpha( ){ this("A") ; }protected Alpha(String a){ alpha=a;  }}package beta;public class Beta extends alpha.Alpha{private Beta(String a){ super(a);  }}

Q: 18 Given:1. public class Target {2. private int i = 0;3. public int addOne(){4. return ++i;5. }6. }And:1. public class Client {2. public static void main(String[] args){3. System.out.println(new Target().addOne());4. }5. }Which change can you make to Target without affecting Client?A. Line 4 of class Target can be changed to return i++;B. Line 2 of class Target can be changed to private int i = 1;C. Line 3 of class Target can be changed to private int addOne(){D. Line 2 of class Target can be changed to private Integer i = 0;Answer: D

Page 17: SCJP(DS)

Q: 19 Click the Task button.

Solution:public class Single{      private static Single instance;      public static Single getInstance( ){ if(instance==null) instance = create( );      return instance;   }protectedSingle( ) {  }staticSingle create ( ) { return new Single ( ) ; }}class SingleSub extends Shape{  }

Q: 20 Given:12. public class Test {13. public enum Dogs {collie, harrier};14. public static void main(String [] args) {15. Dogs myDog = Dogs.collie;16. switch (myDog) {17. case collie:18. System.out.print("collie ");19. case harrier:20. System.out.print("harrier ");21. }22. }23. }What is the result?A. collie                                       B. harrierC. Compilation fails.                    D. collie harrierE. An exception is thrown at runtime.Answer: D

Q: 21 Click the Exhibit button.Given: ClassA a = new ClassA();a.methodA();

Page 18: SCJP(DS)

What is the result?

 

A. Compilation fails.B. ClassC is displayed.C. The code runs with no output.D. An exception is thrown at runtime.

Answer: D

Q: 22 Click the Task button.

Solution:public int update(int quantity,int adjust){

Page 19: SCJP(DS)

quantity=quantity+adjust;return quantity;}public void call Update( ) {int quant=100;quant=update(quant,320);System.out.println("the quantity is " +quant);}

Q: 23 Given:1. package sun.scjp;2. public enum Color { RED, GREEN, BLUE }1. package sun.beta;2. // insert code here3. 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 Betadeclaration, will allow this code to compile? (Choose two.)

A. import sun.scjp.Color.*;B. import static sun.scjp.Color.*;C. import sun.scjp.Color; import static sun.scjp.Color.*;D. import sun.scjp.*; import static sun.scjp.Color.*;E. import sun.scjp.Color; import static sun.scjp.Color.GREEN;

Answer: CE

Question 24Given:

10. public class Fabric11. public enum Color {12. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);13. private final int rgb;14. Color( int rgb) { this.rgb = rgb; }15. public int getRGB() { return rgb; }16. };17. public static void main( String[] argv) {18. // insert code here19. }20. }

Which two code fragments, inserted independently at line 18, allow theFabric class to compile? (Choose two.)

A. Color skyColor = BLUE;B. Color treeColor = Color.GREEN;C. Color purple = new Color( 0xff00ff);D. if( RED.getRGB() < BLUE.getRGB() ) {}E. Color purple = Color.BLUE + Color.RED;F. if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}

Answer: BF

25. Given the following,1. interface Base {

Page 20: SCJP(DS)

2. boolean m1 ();3. byte m2(short s);4. }

Which code fragments will compile? (Choose all that apply.)A. interface Base2 implements Base { }B. abstract class Class2 extends Base {public boolean m1() { return true; } }C. abstract class Class2 implements Base { }D. abstract class Class2 implements Base {public boolean m1() { return (true); } }E. class Class2 implements Base {boolean m1() { return false; }byte m2(short s) { return 42; } }

Answer:->   C and D are correct. C is correct because an abstract class doesn't have to implement any or all of its interface's methods. D is correct because the method is correctly implemented.->  A is incorrect because interfaces don't implement anything. B is incorrect because classes don't extend interfaces. E is incorrect because interface methods are implicitly public, so the methods being implemented must be public.

 26. Which declare a compilable abstract class? (Choose all that apply.)A. public abstract class Canine { public Bark speak(); }B. public abstract class Canine { public Bark speak() { } }C. public class Canine { public abstract Bark speak(); }D. public class Canine abstract { public abstract Bark speak(); }

Answer:->B is correct. abstract classes don't have to have any abstract methods.-> A is incorrect because abstract methods must be marked as such. C is incorrect becauseyou can't have an abstract method unless the class is abstract. D is incorrect because thekeyword abstract must come before the classname. (Objective 1.1)

27. Which is true? (Choose all that apply.)A. "X extends Y" is correct if and only if X is a class and Y is an interface.B. "X extends Y" is correct if and only if X is an interface and Y is a class.C. "X extends Y" is correct if X and Y are either both classes or both interfaces.D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.

Answer:->   C is correct.->   A is incorrect because classes implement interfaces, they don't extend them.       B is incorrect because interfaces only "inherit from" other interfaces.       D is incorrect based on the preceding rules.

28. Given:1. enum Animals {2. DOG("woof"), CAT("meow"), FISH("burble");3. String sound;4. Animals(String s) { sound = s; }5. }6. class TestEnum {7. static Animals a;8. public static void main(String [] args) {9. System.out.println(a.DOG.sound + " " + a.FISH.sound);10. }11. }

What is the result?A. woof burbleB. Multiple compilation errors

Page 21: SCJP(DS)

C. Compilation fails due to an error on line 2D. Compilation fails due to an error on line 3E. Compilation fails due to an error on line 4F. Compilation fails due to an error on line 9Answer:-> A is correct; enums can have constructors and variables.->B, C, D, E, and F are incorrect; these lines all use correct syntax.

29. Given:1. enum A { A }2. class E2 {3. enum B { B }4. void C() {5. enum D { D }6. }7. }Which statements are true? (Choose all that apply.)A. The code compiles.B. If only line 1 is removed the code compiles.C. If only line 3 is removed the code compiles.D. If only line 5 is removed the code compiles.E. If lines 1 and 3 are removed the code compiles.F. If lines 1, 3 and 5 are removed the code compiles.Answer->D and F are correct. Line 5 is the only line that will not compile, because enums cannot belocal to a method.->A, B, C and E are incorrect based on the above.

 3. Operators

Q: 01 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("1 ");19. System.out.print("2 ");20. if ((b2 = true) && b1 )21. System.out.print("3 ");22. }23. }

What is the result?A. 2B. 3C. 1 2D. 2 3E. 1 2 3F. Compilation fails.G. An exception is thrown at runtime.

Answer: D

Q: 02 Given the command line java Pass2 and:15. public class Pass2 {

Page 22: SCJP(DS)

16. public void main(String [] args) {17. int x = 6;18. Pass2 p = new Pass2();19. p.doStuff(x);20. System.out.print(" main x = " + x);21. }22.23. void doStuff(int x) {24. System.out.print(" doStuff x = " + x++);25. }26. }What is the result?A. Compilation fails.B. An exception is thrown at runtime.C. doStuff x = 6 main x = 6D. doStuff x = 6 main x = 7E. doStuff x = 7 main x = 6F. doStuff x = 7 main x = 7                        Answer: B

Q: 03 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 = 6D. doStuff x = 5 main x = 5E. doStuff x = 5 main x = 6F. doStuff x = 6 main x = 5Answer: D

Question: 04Given:42. public class ClassA {43. public int getValue() {44.int value=0;45. boolean setting = true;46. String title=”Hello”;47. if (value || (setting && title == “Hello”)) { return 1; }48. if (value == 1 & title.equals(”Hello”)) { return 2; }49. }50. }And:70. ClassA a = new ClassA();71. a.getValue();What is the result?A. 1B. 2C. Compilation fails.D. The code runs with no output.E. An exception is thrown at runtime.Answer: C

Page 23: SCJP(DS)

5. Given:class Hexy {public static void main(String[] args) {Integer i = 42;String s = (i<40)?"life":(i>50)?"universe":"everything";System.out.println(s);} }What is the result?A. nullB. lifeC. universeD. everythingE. Compilation fails.F. An exception is thrown at runtime.Answer:-> D is correct. This is a ternary nested in a ternary with a little unboxing thrown in.Both of the ternary expressions are false.-> A, B, C, E, and F are incorrect based on the above.

6. Given:1. class Example {2. public static void main(String[] args) {3. Short s = 15;4. Boolean b;5. // insert code here6. }7. }Which, inserted independently at line 5, will compile? (Choose all that apply.)

A. b = (Number instanceof s);B. b = (s instanceof Short);C. b = s.instanceof(Short);D. b = (s instanceof Number);E. b = s.instanceof(Object);F. b = (s instanceof String);Answer:->  B and D correctly use boxing and instanceof together.-> A is incorrect because the operands are reversed. C and E use incorrect instance   of syntax.F is wrong because Short isn't in the same inheritance tree as String.

Q: 01 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("1 ");19. System.out.print("2 ");20. if ((b2 = true) && b1 )21. System.out.print("3 ");22. }23. }

What is the result?A. 2B. 3C. 1 2D. 2 3E. 1 2 3F. Compilation fails.G. An exception is thrown at runtime.

Page 24: SCJP(DS)

Answer: D

Q: 02 Given the command line java Pass2 and:15. public class Pass2 {16. public void main(String [] args) {17. int x = 6;18. Pass2 p = new Pass2();19. p.doStuff(x);20. System.out.print(" main x = " + x);21. }22.23. void doStuff(int x) {24. System.out.print(" doStuff x = " + x++);25. }26. }What is the result?A. Compilation fails.B. An exception is thrown at runtime.C. doStuff x = 6 main x = 6D. doStuff x = 6 main x = 7E. doStuff x = 7 main x = 6F. doStuff x = 7 main x = 7                        Answer: B

Q: 03 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 = 6D. doStuff x = 5 main x = 5E. doStuff x = 5 main x = 6F. doStuff x = 6 main x = 5Answer: D

Question: 04Given:42. public class ClassA {43. public int getValue() {44.int value=0;45. boolean setting = true;46. String title=”Hello”;47. if (value || (setting && title == “Hello”)) { return 1; }48. if (value == 1 & title.equals(”Hello”)) { return 2; }49. }50. }And:70. ClassA a = new ClassA();71. a.getValue();What is the result?A. 1B. 2

Page 25: SCJP(DS)

C. Compilation fails.D. The code runs with no output.E. An exception is thrown at runtime.Answer: C

5. Given:class Hexy {public static void main(String[] args) {Integer i = 42;String s = (i<40)?"life":(i>50)?"universe":"everything";System.out.println(s);} }What is the result?A. nullB. lifeC. universeD. everythingE. Compilation fails.F. An exception is thrown at runtime.Answer:-> D is correct. This is a ternary nested in a ternary with a little unboxing thrown in.Both of the ternary expressions are false.-> A, B, C, E, and F are incorrect based on the above.

6. Given:1. class Example {2. public static void main(String[] args) {3. Short s = 15;4. Boolean b;5. // insert code here6. }7. }Which, inserted independently at line 5, will compile? (Choose all that apply.)

A. b = (Number instanceof s);B. b = (s instanceof Short);C. b = s.instanceof(Short);D. b = (s instanceof Number);E. b = s.instanceof(Object);F. b = (s instanceof String);Answer:->  B and D correctly use boxing and instanceof together.-> A is incorrect because the operands are reversed. C and E use incorrect instance   of syntax.F is wrong because Short isn't in the same inheritance tree as String.

7. Given:1. class Comp2 {2. public static void main(String[] args) {3. float f1 = 2.3f;4. float[][] f2 = {{42.0f}, {1.7f, 2.3f}, {2.6f, 2.7f}};5. float[] f3 = {2.7f};6. Long x = 42L;7. // insert code here8. System.out.println("true");9. }10. }And the following five code fragments:F1. if(f1 == f2)F2. if(f1 == f2[2][1])F3. if(x == f2[0][0])F4. if(f1 == f2[1,1])F5. if(f3 == f2[2])What is true?

Page 26: SCJP(DS)

A. One of them will compile, only one will be true.B. Two of them will compile, only one will be true.C. Two of them will compile, two will be true.D. Three of them will compile, only one will be true.E. Three of them will compile, exactly two will be true.F. Three of them will compile, exactly three will be true.Answer:-> D is correct. Fragments F2, F3, and F5 will compile, and only F3 is true.-> A, B, C, E, and F are incorrect. F1 is incorrect because you can’t compare a primitive   to an array. F4 is incorrect syntax to access an element of a two-dimensional array.

8. Given:class Fork {public static void main(String[] args) {if(args.length == 1 | args[1].equals("test")) {System.out.println("test case");} else {System.out.println("production " + args[0]);} } }And the command-line invocation:java Fork live2What is the result?A. test caseB. productionC. test case live2D. Compilation fails.E. An exception is thrown at runtime.Answer:->  E is correct. Because the short circuit (||) is not used, both operands are evaluated. Since args[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.-> A, B, C, and D are incorrect based on the above.

9. Given:class Foozit {public static void main(String[] args) {Integer x = 0;Integer y = 0;for(Short z = 0; z < 5; z++)if((++x > 2) || (++y > 2))x++;System.out.println(x + " " + y);} }What is the result?A. 5 1      B. 5 2      C. 5 3      D. 8 1E. 8 2      F. 8 3      G. 10 2     H. 10 3Answer:->  E is correct. The first two times the if test runs, both x and y are incremented once (the x++ is not reached until the third iteration). Starting with the third iteration of the loop, y is never touched again, because of the short-circuit operator.->  A, B, C, D, F, G, and H are incorrect based on the above.

10. Given:class Titanic {public static void main(String[] args) {Boolean b1 = true;boolean b2 = false;boolean b3 = true;if((b1 & b2) | (b2 & b3) & b3)System.out.print("alpha ");if((b1 = false) | (b1 & b3) | (b1 | b2))System.out.print("beta ");} }What is the result?A. beta

Page 27: SCJP(DS)

B. alphaC. alpha betaD. Compilation fails.E. No output is produced.F. An exception is thrown at runtime.Answer:-> E is correct. In the second if test, the leftmost expression is an assignment, nota comparison. Once b1 has been set to false, the remaining tests are all false.-> A, B, C, D, and F are incorrect based on the above.

11. Given:class Feline {public static void main(String[] args) {Long x = 42L;Long y = 44L;System.out.print(" " + 7 + 2 + " ");System.out.print(foo() + x + 5 + " ");System.out.println(x + y + foo());}static String foo() { return "foo"; }}What is the result?A. 9 foo47 86fooB. 9 foo47 4244fooC. 9 foo425 86fooD. 9 foo425 4244fooE. 72 foo47 86fooF. 72 foo47 4244fooG. 72 foo425 86fooH. 72 foo425 4244fooI. Compilation fails.Answer:-> G is correct. Concatenation runs from left to right, and if either operand is a String, the operands are concatenated. If both operands are numbers they are added together. Unboxing works in conjunction with concatenation.-> A, B, C, D, E, F, H, and I are incorrect based on the above.

12. Place the fragments into the code to produce the output 33. Note, you must use each fragment exactly once.CODE:class Incr {public static void main(String[] args) {Integer x = 7;int y = 2;x ___ ___;___ ___ ___;___ ___ ___;___ ___ ___;System.out.println(x);}}

FRAGMENTS:

Answer:class Incr {

Page 28: SCJP(DS)

public static void main(String[] args) {Integer x = 7;int y = 2;x *= x;y *= y;y *= y;x -= y;System.out.println(x);}}Yeah, we know it’s kind of puzzle-y, but you might encounter something like it on the real exam.

13. Given:1. class Maybe {2. public static void main(String[] args) {3. boolean b1 = true;4. boolean b2 = false;5. System.out.print(!false ^ false);6. System.out.print(" " + (!b1 & (b2 = true)));7. System.out.println(" " + (b2 ^ b1));8. }9. }Which are true?A. Line 5 produces true.B. Line 5 produces false.C. Line 6 produces true.D. Line 6 produces false.E. Line 7 produces true.F. Line 7 produces false.Answer:-> A , D, and F is correct. The ^ (xor) returns true if exactly one operand is true. The ! inverts the operand’s boolean value. On line 6 b2 = true is an assignment not a comparison, and it’s evaluated because & does not short-circuit it.-> B, C, and E are incorrect based on the above.

14. Given:class Sixties {public static void main(String[] args) {int x = 5;int y = 7;System.out.print(((y * 2) % x));System.out.print(" " + (y % x));}}What is the result?A. 1 1B. 1 2C. 2 1D. 2 2E. 4 1F. 4 2G. Compilation fails.H. An exception is thrown at runtime.Answer:->F is correct. The % (remainder a.k.a. modulus) operator returns the remainder of adivision operation.->A, B, C, D, E, G, and H are incorrect based on the above.

15. Given:class Scoop {static int thrower() throws Exception { return 42; }public static void main(String [] args) {try {int x = thrower();

Page 29: SCJP(DS)

} catch (Exception e) {x++;} finally {System.out.println("x = " + ++x);} } }

What is the result?A. x = 42B. x = 43C. x = 44D. Compilation fails.E. The code runs with no output.

Answer:->D is correct, the variable x is only in scope within the try code block, it’s not in scope inthe catch or finally blocks. ->A, B, C, and E is are incorrect based on the above.

16. Given:class Alien {String invade(short ships) { return "a few"; }String invade(short... ships) { return "many"; }}class Defender {public static void main(String [] args) {System.out.println(new Alien().invade(7));} }What is the result?A. manyB. a fewC. Compilation fails.D. The output is not predictable.E. An exception is thrown at runtime.

Answer:->C is correct, compilation fails. The var-args declaration is fine, but invade takes a short,so the argument 7 needs to be cast to a short. With the cast, the answer is B, 'a few'.->A, B, D, and E are incorrect based on the above. (Objective 1.3)

17. Given:1. class Dims {2. public static void main(String[] args) {3. int[][] a = {{1,2,}, {3,4}};4. int[] b = (int[]) a[1];5. Object o1 = a;6. int[][] a2 = (int[][]) o1;7. int[] b2 = (int[]) o1;8. System.out.println(b[1]);9. } }What is the result?

A. 2B. 4C. An exception is thrown at runtimeD. Compilation fails due to an error on line 4.E. Compilation fails due to an error on line 5.F. Compilation fails due to an error on line 6.G. Compilation fails due to an error on line 7.

Answer:->C is correct. A ClassCastException is thrown at line 7 because o1 refers to an int[][]not an int[]. If line 7

Page 30: SCJP(DS)

was removed, the output would be 4.->A, B, D, E, F, and G are incorrect based on the above. (Objective 1.3)

18. Given:class Eggs {int doX(Long x, Long y) { return 1; }int doX(long... x) { return 2; }int doX(Integer x, Integer y) { return 3; }int doX(Number n, Number m) { return 4; }public static void main(String[] args) {new Eggs().go();}void go() {short s = 7;System.out.print(doX(s,s) + " ");System.out.println(doX(7,7));} }What is the result?A. 1 1B. 2 1C. 3 1D. 4 1E. 2 3F. 3 3G. 4 3

Answer:-> G is correct. Two rules apply to the first invocation of doX(). You can’t widen and then box in one step, and var-args are always chosen last. Therefore you can’t widen shorts to either ints or longs, and then box them to Integers or Longs. But you can box shorts to Shorts and then widen them to Numbers, and this takes priority over using a var-args method. Thesecond invocation uses a simple box from int to Integer.-> A, B, C, D, E, and F are incorrect based on the above. (Objective 3.1)

19. Given:class Mixer {Mixer() { }Mixer(Mixer m) { m1 = m; }Mixer m1;public static void main(String[] args) {Mixer m2 = new Mixer();Mixer m3 = new Mixer(m2); m3.go();Mixer m4 = m3.m1; m4.go();Mixer m5 = m2.m1; m5.go();}void go() { System.out.print("hi "); }}

What is the result?A. hiB. hi hiC. hi hi hiD. Compilation failsE. hi, followed by an exceptionF. hi hi, followed by an exceptionAnswer:-> F is correct. The m2 object’s m1 instance variable is never initialized, so when m5 tries to use it a NullPointerException is thrown.-> A, B, C, D, and E are incorrect based on the above. (Objective 7.3)

20. Given:1. class Zippy {2. String[] x;

Page 31: SCJP(DS)

3. int[] a [] = {{1,2}, {1}};4. Object c = new long[4];5. Object[] d = x;6. }What is the result?A. Compilation succeeds.B. Compilation fails due only to an error on line 3.C. Compilation fails due only to an error on line 4.D. Compilation fails due only to an error on line 5.E. Compilation fails due to errors on lines 3 and 5.F. Compilation fails due to errors on lines 3, 4, and 5.Answer:-> A is correct, all of these array declarations are legal. Lines 4 and 5 demonstrate that arrays can be cast.->B, C, D, E, and F are incorrect because this code compiles. (Objective 1.3)

21. Given:class Fizz {int x = 5;public static void main(String[] args) {final Fizz f1 = new Fizz();Fizz f2 = new Fizz();Fizz f3 = FizzSwitch(f1,f2);System.out.println((f1 == f3) + " " + (f1.x == f3.x));}static Fizz FizzSwitch(Fizz x, Fizz y) {final Fizz z = x;z.x = 6;return z;} }What is the result?A. true true    B. false true    C. true falseD. false false  E. Compilation fails.     F. An exception is thrown at runtime.Answer:->A is correct. The references f1, z, and f3 all refer to the same instance of Fizz. The final modifier assures that a reference variable cannot be referred to a different object, but final doesn’t keep the object’s state from changing.->B, C, D, E, and F are incorrect based on the above. (Objective 7.3)

22. Given:class Knowing {static final long tooth = 343L;static long doIt(long tooth) {System.out.print(++tooth + " ");return ++tooth;}public static void main(String[] args) {System.out.print(tooth + " ");final long tooth = 340L;new Knowing().doIt(tooth);System.out.println(tooth);} }What is the result?     A. 343 340 340   B. 343 340 342    C. 343 341 342D. 343 341 340   E. 343 341 343    F. Compilation fails.G. An exception is thrown at runtime.Answer:-> D is correct. There are three different long variables named tooth. Remember that you can apply the final modifier to local variables, but in this case the 2 versions of tooth marked final are not changed. The only tooth whose value changes is the one not marked final. This program demonstrates a bad practice known as shadowing.->A, B, C, E, F, and G are incorrect based on the above. (Objective 7.3)

23. Given:1. class Bigger {

Page 32: SCJP(DS)

2. public static void main(String[] args) {3. // insert code here4. }5. }6. class Better {7. enum Faster {Higher, Longer};8. }Which, inserted independently at line 3, will compile? (Choose all that apply.)A. Faster f = Faster.Higher;B. Faster f = Better.Faster.Higher;C. Better.Faster f = Better.Faster.Higher;D. Bigger.Faster f = Bigger.Faster.Higher;E. Better.Faster f2; f2 = Better.Faster.Longer;F. Better b; b.Faster = f3; f3 = Better.Faster.Longer;

Answer:-> C and E are correct syntax for accessing an enum from another class.->A, B, D, and F are incorrect syntax.

 4. Flow Control

Q: 01 Given:10. public class Bar {11. static void foo( int... x ) {12. // insert code here13. }14. }Which two code fragments, inserted independently at line 12, will allow the class to compile? (Choosetwo.)A. foreach( x ) System.out.println(z);B. for( int z : x ) System.out.println(z);C. while( x.hasNext() ) System.out.println( x.next() );D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

Answer: B, D

Page 33: SCJP(DS)

Q: 02 Click the Task button.

Solution:int [ ] y={1,2,4,8,16,32};System.out.print("output  : ");for(int x : y ) { System.out.println(x);System.out.println("  ");

 

Q: 03 Given:25. int x = 12;26. while (x < 10) {27. x--;28. }29. System.out.print(x);What is the result?A. 0B. 10C. 12D. Line 29 will never be reached.Answer: C

Q: 04 Given:11. public static void main(String[] args) {12. Object obj = new int[] { 1, 2, 3 };13. int[] someArray = (int[])obj;14. for (int i : someArray) System.out.print(i + " ");15. }What is the result?

A. 1 2 3B. Compilation fails because of an error in line 12.C. Compilation fails because of an error in line 13.

Page 34: SCJP(DS)

D. Compilation fails because of an error in line 14.E. A ClassCastException is thrown at runtime.

Answer: A

Q: 05 Given:11. public static void main(String[] args) {12. for (int i = 0; i <= 10; i++) {13. if (i > 6) break;14. }15. System.out.println(i);16. }What is the result?A. 6B. 7C. 10D. 11E. Compilation fails.F. An exception is thrown at runtime.Answer: E

Q: 06 Given:11. public static void main(String[] args) {12. Integer i = new Integer(1) + new Integer(2);13. switch(i) {14. case 3: System.out.println("three"); break;15. default: System.out.println("other"); break;16. }17. }What is the result?A. threeB. otherC. An exception is thrown at runtime.D. Compilation fails because of an error on line 12.E. Compilation fails because of an error on line 13.F. Compilation fails because of an error on line 15.Answer: A

Q: 07 Given:10. public class ClassA {11. public void count(int i) {12. count(++i);13. }14. }And:20. ClassA a = new ClassA();21. a.count(3);Which exception or error should be thrown by the virtual machine?A. StackOverflowErrorB. NullPointerExceptionC. NumberFormatExceptionD. IllegalArgumentExceptionE. ExceptionInInitializerError

Answer: A

Q: 08 Given:35. int x = 10;36. do { 37. x--;38. } while (x < 10);How many times will line 37 be executed?A. ten times

Page 35: SCJP(DS)

B. zero timesC. one to nine timesD. more than ten timesAnswer: D

9. Given the following code:public class OrtegorumFunction {public int computeDiscontinuous(int x) {int r = 1;r += x;if ((x > 4) && (x < 10)) {r += 2 * x;} else (x <= 4) {r += 3 * x;} else {r += 4 * x;}r += 5 * x;return r;}public static void main(String [] args) {OrtegorumFunction o = new OrtegorumFunction();System.out.println("OF(11) is: " + o.computeDiscontinuous(11));} }What is the result?A. OF(11) is: 45B. OF(11) is: 56C. OF(11) is: 89D. OF(11) is: 111E. Compilation fails.F. An exception is thrown at runtime.

Answer:-> E is correct. The if statement is illegal. The if-else-else must be changed to if-elseif-else, which would result in OF(11) is: 111.-> A, B, C, D, and F are incorrect based on the above. (Objective 2.1)

10. Given:1. class Crivitch {2. public static void main(String [] args) {3. int x = 0;4. // insert code here5. do { } while (x++ < y);6. System.out.println(x);7. }8. }Which, inserted at line 4, produces the output 12?A. int y = x;B. int y = 10;C. int y = 11;D. int y = 12;E. int y = 13;F. None of the above will allow compilation to succeed.Answer:-> C is correct. x reaches the value of 11, at which point the while test fails.x is then incremented (after the comparison test!), and the println() method runs.-> A, B, D, E, and F are incorrect based on the above.

11. Given:class Swill {public static void main(String[] args) {String s = "-";switch(TimeZone.CST) {

Page 36: SCJP(DS)

case EST: s += "e";case CST: s += "c";case MST: s += "m";default: s += "X";case PST: s += "p";}System.out.println(s);}}enum TimeZone {EST, CST, MST, PST }What is the result?A. -cB. -XC. -cmD. -cmpE. -cmXpF. Compilation fails.G. An exception is thrown at runtime.Answer:-> E is correct. It’s legal to use enums in a switch, and normal switch fall-through logic applies; i.e., once a match is made the switch has been entered, and all remaining blocks will run if no break statement is encountered. Note: default doesn’t have to be last.-> A, B, C, D, and F are incorrect based on the above.(Objective 2.1)

12. Given:class Circus {public static void main(String[] args) {int x = 9;int y = 6;for(int z = 0; z < 6; z++, y--) {if(x > 2) x--;label:if(x > 5) {System.out.print(x + " ");--x;continue label;}x--;}}}What is the result?A. 8B. 8 7C. 8 7 6D. Compilation fails.E. An exception is thrown at runtime.Answer:-> D is correct. A labeled continue works only with loops. In this case, although the label islegal, label is not a label on a loop statement, it’s a label on an if statement.-> A, B, C, and E are incorrect based on the above. (Objective 2.2)

13. Given:1. class Loopy {2. public static void main(String[] args) {3. int[] x = {7,6,5,4,3,2,1};4. // insert code here5. System.out.print(y + " ");6. }7. } }Which, inserted independently at line 4, compiles? (Choose all that apply.)A. for(int y : x) {B. for(x : int y) {

Page 37: SCJP(DS)

C. int y = 0; for(y : x) {D. for(int y=0, z=0; z<x.length; z++) { y = x[z];E. for(int y=0, int z=0; z<x.length; z++) { y = x[z];F. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];Answer:-> A , D, and F are correct. A is an example of the enhanced for loop. D and F are examples of the basic for loop.-> B is incorrect because its operands are swapped. C is incorrect because the enhancedfor must declare its first operand. E is incorrect syntax to declare two variables in a for statement. (Objective 2.2)

14. Given:1. class Ring {2. final static int x2 = 7;3. final static Integer x4 = 8;4. public static void main(String[] args) {5. Integer x1 = 5;6. String s = "a";7. if(x1 < 9) s += "b";8. switch(x1) {9. case 5: s += "c";10. case x2: s += "d";11. case x4: s += "e";12. }13. System.out.println(s);14. }15. }What is the result?A. abcB. abcdeC. Compilation fails due only to an error on line 7.D. Compilation fails due only to an error on line 8.E. Compilation fails due only to an error on line 10.F. Compilation fails due only to an error on line 11.G. Compilation fails due to errors on multiple lines.Answer:-> F is correct. A switch statement requires its case expressions to be constants, and wrapper variables (even final static ones) aren’t considered constants. The rest of the code is correct.-> A, B, C, D, E, and G are incorrect based on the above. (Objective 2.1)