SCJP Parcial 2013

download SCJP Parcial 2013

If you can't read please download the document

Transcript of SCJP Parcial 2013

  • Indicar en la ltima pgina TODAS las respuestas correctas. Puede haber preguntas con respuestas mltiples.

    Nombre:

    [1]. Given:

    1. enum Towns1{NY, LA, SF} 2. public class DeclareEnum { 3. enum Towns2{NY, LA, SF}; 4. public static void main(String [] args) { 5. enum Towns3{NY, LA, SF}; 6. } 7. }

    What is the result? A. The code compiles. B. Compilation fails due to an error on line 1. C. Compilation fails due to an error on line 3. D. Compilation fails due to an error on line 5. E. Compilation fails due to errors on lines 1 and 5. F. Compilation fails due to errors on lines 3 and 5. [2]. Given:

    1. public interface Word { boolean isSpelled(String w); } 2. abstract class Verb1 implements Word { boolean isSpelled() { } } 3. abstract class Verb2 implements Word { boolean isSpelled() { return true; } } 4. abstract class Verb3 implements Word { 5. boolean isSpelled(String w) { return true; } 6. }

    What is the result? A. Compilation succeeds. B. Compilation fails due only to an error on line 2. C. Compilation fails due only to an error on line 3. D. Compilation fails due only to an error on line 4. E. Compilation fails due only to errors on lines 2 and 3. F. Compilation fails due only to errors on lines 2 and 4. G. Compilation fails due only to errors on lines 3 and 4.

    Sun Certified Programmer for the Java Platform Assessment Test Exam 310-065 SCJP 1.6 - 2013

  • [3]. Given:

    1. class Banana { 2. int x = 1; 3. public static void main(String [] args) { 4. int x = 2; 5. Banana b = new Banana(); 6. b.go(); 7. } 8. { x += x; } 9. void go() { 10. ++x; 11. System.out.println(x); 12. } 13. }

    What is the result? A. 1 B. 2 C. 3 D. 5 E. Compilation fails. [4]. Given:

    1. public class Spock { 2. public static void main(String[] args) { 3. Long tail = 2000L; 4. Long distance = 1999L; 5. Long story = 1000L; 6. if((tail > distance)^((story * 2) == tail)) 7. System.out.print(1); 9. if((distance + 1 != tail) ^((story * 2) == distance)) 10. System.out.print(2); 11. } 12.}

    What is the result? A. 1 B. 2 C. 12 D. Compilation fails E. No output is produced F. An exception is thrown at runtime

  • [5]. A class games.cards.Poker is correctly defined in the jar file Poker.jar. A user want to execute the main method of Poker on a UNIX system using the command: java games.card.Poker what allows the user to do this?

    A. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java

    B. put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/*.jar

    C. Put Poker.jar in directory /stuff/java, and set the CLASSPATH to include /stuff/java/Poker.jar

    D. Put Poker.jar in directory /stuff/java/games/card, and set the CLASSPATH to include /stuff/java

    E. Put Poker.jar in directory /stuff/java/games/card, and set the CLASSPATH to include /stuff/java/*.jar

    F. Put Poker.jar in directory /stuff/java/games/card, and set the CLASSPATH to include /stuff/java/Poker.jar

    [6]. Given:

    4. public class FreeRange { 5. public static void main(String[] args) { 6. int x = 7, y = 8; 7. if(x < y) 8. if(x+2 > y) 9. if(y < x) ; 10. else if(!false) 11. System.out.print("inner "); 12. else if(true) 13. System.out.print("middle "); 14. } 15. }

    What is the result? A. inner B. middle C. inner middle D. middle inner E. Compilation fails F. An exception is thrown at runtime

  • [7] Given three different source files:

    1. package com.sun2; 2. public enum Seasons {SUMMER, FALL, WINTER, SPRING }

    And:

    1. import com.sun2.Seasons; 2. class Enum3a { 3. Seasons s = Seasons.FALL; 4. }

    And:

    1. import com.sun2.*; 2. class Enum3b { 3. Seasons s = Seasons.FALL; 4. }

    Which is true? A Only the first file will compile. B Only the first and second files will compile. C Only the first and third files will compile. D Only the second and third files will compile. E All three files will compile. [8] Given:

    3. class Bottle { } 4. public class Recycle { 5. public static void main(String[] args) { 6. Bottle b1 = new Bottle(); 7. Bottle b2 = b1; 8. b1 = new Bottle(); 9. Bottle b3 = b2; 10. b2 = null; 11. b1 = b3; 12. // do stuff 13. } 14. }

    What is true about objects being eligible for the garbage collector (GC), when line 12 is reached during execution? A No Bottle objects are eligible for GC. B Only the first Bottle object created is eligible for GC. C Only the second Bottle object created is eligible for GC. D The first two Bottle objects created are eligible for GC. E It is impossible to determine which objects are eligible for the GC.

  • [9] Given:

    1. class CopyArray { 2. public static void main(String [] args) { 3. int[] x = {1, 2 ,3}; 4. // insert code here 5. } 6. }

    Which two, inserted independently at line 4, will compile? (Choose two.)

    A. int[] y1 = x;

    B. int[] y2; y2 = x;

    C. int[] y3 = x.copy();

    D. int[] y4; for(int z : x) { y4[z] = x[z]; }

    [10] Given:

    1. class HorseRadish { 2. // insert code here 3. protected HorseRadish(int x) { 4. System.out.println("bok choy"); 5. } 6. } 7. class Wasabi extends HorseRadish { 8. public static void main(String [] args) { 9. Wasabi w = new Wasabi(); 10. } 11. }

    Which two, inserted independently at line 2, will allow the code to compile and produce the output "bok choy"? (Choose two.) A. // just a comment B. protected HorseRadish() { } C. protected HorseRadish() { this(42);} D. protected HorseRadish() { new HorseRadish(42);}

  • [11] Given: - list is a reference to a valid collection - getCollection() returns a reference to a valid collection Which two are valid? (Choose two.) A. for(Object o ; list) B. for(Object o : list.iterator()) C. for(Object o : getCollection()) D. for(Iterator i ; list.iterator() ; i.hasNext() ) E. for(Iterator i = list.iterator(); i.hasNext(); ) [12] Given:

    1. class MoreAsserts { 2. static int x = 5; 3. public static void main(String [] args) { 4. assert(doStuff(42)); 5. if(x < 40) ; 6. else assert(false); 7. } 8. public static boolean doStuff(int arg) { 9. assert(arg < x++); 10. return false; 11. } 12. }

    Which is true? A. None of the assert statements are appropriate. B. The assert statement on line 4 is appropriate. C. The assert statement on line 6 is appropriate. D. The assert statement on line 9 is appropriate. E. All three of the assert statements are appropriate

  • [13] Given:

    . class Mutate { 2. public static void main(String [] args) { 3. StringBuilder s = new StringBuilder("012345678 "); 4. if (s.length() == 10) 5. s.insert(10, "abcdef"); 6. s.delete(3,8); 7. System.out.println(s.indexOf("c")); 8. } 9. }

    What is the result?

    A. -1 B. 5 C. 6 D. 7 E. Compilation fails F. An exception is thrown at runtime

    [14]Given that c is a reference to a valid java.io.Console object and the following code snippet:

    12. char[] pw = c.readPassword("%s", "pw: "); 13. for(char ch : pw) 14. System.out.print(ch);

    And when prompted the user keys "bob" What is displayed on the terminal?

    A. pw: B. pw:

    bob C. pw: bob

    bob D. An exception is thrown

  • [15] Given the following valid code fragment from classes in the java.util package:

    11. Date d = new Date(1700000000000L); 12. System.out.println(d.toString()); // output line 1 13. Calendar c1 = Calendar.getInstance(); 14. c1.setTime(d); 15. Calendar c2 = Calendar.getInstance(); 16. c2.setTime(d); 17. c1.add(Calendar.MONTH, 10); 18. c2.roll(Calendar.MONTH, 10); 19. System.out.println(c1.getTime().toString()); 20. System.out.println(c2.getTime().toString());

    If the first line of output is Tue Nov 14 15:13:20 MST 2023, what is the result?

    A. Thu Sep 14 15:13:20 MDT 2023 Thu Sep 14 15:13:20 MDT 2023

    B. Thu Sep 14 15:13:20 MDT 2023 Sat Sep 14 15:13:20 MDT 2024

    C. Sat Sep 14 15:13:20 MDT 2024 Thu Sep 14 15:13:20 MDT 2023

    D. Sat Sep 14 15:13:20 MDT 2024 Sat Sep 14 15:13:20 MDT 2024

    [16] Given:

    1. class StringSplit { 2. public static void main(String [] args) { 3. 4. String s = "x1234 y56 z7 a"; 5. String [] sa = s.split("\\d"); 6. int count = 0; 7. for( String x : sa) 8. count++; 9. System.out.println("total: " + count); 10. } 11. }

    What is the result?

    A. total: 3 B. total: 4 C. total: 7 D. total: 8 E. Compilation fails F. An exception is thrown at runtime

  • [17] Which three statements are true? (choose three): A. A final method in class X can be abstract if and only if X is abstract B. A protected method in class X can be overridden by any subclass of X C. A private static method can be called only within other static methods in class X D. A non-static public final method in class X can be overridden in any subclass of X E. A public static method in class X can be called by a subclass of X without explicitly referencing the class X F. A method with the same signature as a private final method in class X can be implemented in a subclass of X [18] Given:

    1. import java.io.*; 2. 3. public class MyReader { 4. BufferedReader in; 5. 6. public MyReader(File file) throws IOException { 7. FileReader fr = new FileReader(file); 8. in = new BufferedReader(fr); 9. } 10. 11. public void go() throws IOException { 12. String s = null; 13. while((s = in.readLine()) != null) { 14. System.out.print(s); 15. } 16. } 17. 18. public static void main(String [] args) { 19. try { 20. File file = new File(data.txt); 21. new MyReader(file).go(); 22. }catch(IOException e) { 23. e.printStackTrace(); 24. } 25. } 26. }

  • What is the output of the MyReader program if the fi le data.txt is in the same directory as MyReader.class and contains the following contents: H E L L O A. The characters HELLO with each character on a separate line. B. The characters HELLO on the same line. C. The character H . D. An IOException is thrown on line 13. E. The code does not compile. [19] Which two statements comparing java.lang.StringBuilder to java.lang.StringBuffer are true? (Choose two.)

    A. Both classes have a lastIndexOf method. B. Both classes' key methods are synchronized. C. Only StringBuffer has a lastIndexOf method. D. Only StringBuilder has a lastIndexOf method. E. Only StringBuffer's key methods are synchronized. F. Only StringBuilder's key methods are synchronized.

    [20] Which three are true about the java.util.Scanner class compared to the String class's split() method? (Choose three.)

    A. Both are used primarily to tokenize source data. B. Only the Scanner class can use a custom delimiter. C. Only the Scanner class can use a stream as a source. D. The Scanner class allows you to exit before the entire source has been read. E. The split() method allows you to exit before the entire source has been read.

  • 1 A B C D E F G H

    2 A B C D E F G H

    3 A B C D E F G H

    4 A B C D E F G H

    5 A B C D E F G H

    6 A B C D E F G H

    7 A B C D E F G H

    8 A B C D E F G H

    9 A B C D E F G H

    10 A B C D E F G H

    11 A B C D E F G H

    12 A B C D E F G H

    13 A B C D E F G H

    14 A B C D E F G H

    15 A B C D E F G H

    16 A B C D E F G H

    17 A B C D E F G H

    18 A B C D E F G H

    19 A B C D E F G H

    20 A B C D E F G H

  • 1 A B C D E F G H

    2 A B C D E F G H

    3 A B C D E F G H

    4 A B C D E F G H

    5 A B C D E F G H

    6 A B C D E F G H

    7 A B C D E F G H

    8 A B C D E F G H

    9 A B C D E F G H

    10 A B C D E F G H

    11 A B C D E F G H

    12 A B C D E F G H

    13 A B C D E F G H

    14 A B C D E F G H

    15 A B C D E F G H

    16 A B C D E F G H

    17 A B C D E F G H

    18 A B C D E F G H

    19 A B C D E F G H

    20 A B C D E F G H

  • 1. Option D is correct. An enum may NOT be declared in a method. 2. Option F is correct. Verb1's isSpelled() method is missing a return statement, and

    Verb3 fails attempting to assign weaker access privileges to isSpelled().. 3. Option C is correct. The variable x on line 2 is not the same x as on line 4. The

    initialization block runs after the new Banana object is created. 4. E. ^is XOR 11 0 5. C 6. A. Option A is correct. Although it's bad practice, it's legal to nest if statements like

    this without using curly b races as long as no block is longer than one statement. 7. E. Option E is correct. An enum can be imported, and the first file correctly declares

    an enum. 8. Option C is correct. When line 12 is reached both b1 and b3 refer to the first Bottle

    object, and b2 is null. There is no live reference to the second Bottle object.

    9. AB - Option C is incorrect because arrays do not have a copy method. Option D is incorrect because y4 was NOT initialized.

    10. CD - Options C and D are correct. B is incorrect because no output would be produced.

    11. CE - Options C and E demonstrate the correct syntax to iterate through a collection. 12. C - Option C is correct because the assert on line 4 can cause a state change, and

    the assert on line 9 checks the argument of a public method. 13. D - Option D is correct. String indexes are 0-based, and in delete the end is

    exclusive. 14. B - Option B is correct. By default the readPassword() method does not echo the

    user's input. 15. C - Option C is correct. The add() method adds time increments and increments

    days, and months and years at the same time. The roll() method adds time increments without incrementing the larger time elements.

    16. D - Option D is correct. The \d means that every digit is a terminator, which creates 7 entries, and then the end of the String creates the eighth terminator.

    17. Options B, E and F are correct. 18. Option B is correct. The code compiles and runs fi ne, so D and E are incorrect. The

    File object represents the fi lename data.txt . The constructor of MyReader chains a FileReader and BufferedReader to data.txt . The go method reads in the contents of data.txt one line at a time and prints each character without a linefeed, so the output is HELLO and the answer is B.

    19. Options A and E are correct. You don't have to memorize all of these classes' methods, but you have to know that they share the same methods.

    20. Options A, C, and D are correct. The split() method tokenizes the entire source in a single atomic operation.