DOC

java

By Albert Payne,2014-06-27 10:57
10 views 0
java

151

    Given that the current directory is empty, and that the user has read and write privileges to the current

    directory, and the following:

     1. import java.io.*;

     2. public class Maker{

     3. public static void main(String args){

     4. File dir = new File("dir");

     5. File f = new File(dir, "f");

     6. }

     7. }

    Which statement is true?

    A. Compilation fails.

    B. Nothing is added to the file system.

    C. Only a new file is created on the file system.

    D. Only a new directory is created on the file system.

    E. Both a new file and a new directory are created on the file system.

152

    Which three statements concerning the use of the java.io.Serializable interface are true? (Choose three.)

    A. Objects from classes that use aggregation cannot be serialized.

    B. An object serialized on one JVM can be successfully deserialized on a different JVM. C. The values in fields with the volatile modifier will NOT survive serialization and deserialization. D. The values in fields with the transient modifier will NOT survive serialization and deserialization.

    E. It is legal to serialize an object of a type that has a supertype that does NOT implement

    java.io.Serializable.

153

    Chain these constructors to create objects to read from a file named "in" and to write to a file named "out".

    reader = Place here Place here "in" ));

    writer = Place here Place here Place here "out" )));

     Constructors

    new FileReader( new PrintReader( new BufferedReader(

     new BufferedWriter( new FileWriter( new PrintWriter(

    154

    Given that c is a reference to a valid java.io.Console object, which two code fragments read a line of text

    from the console? (Choose two.)

    A. String s = c.readLine();

B. char[] c = c.readLine();

    C. String s = c.readConsole();

    D. char[] c = c.readConsole();

    E. String s = c.readLine("%s", "name "); F. char[] c = c.readLine("%s", "name ");

157

    Given that c is a reference to a valid java.io.Console object, and:

     11. String pw = c.readPassword("%s", "pw: ");

     12. System.out.println("got " + pw);

     13. String name = c.readLine("%s", "name: ");

     14. System.out.println(" got", name); If the user types fido when prompted for a password, and then responds bob when

    prompted for a name, what

    is the result?

    A. pw:

     got fido

     name: bob

     got bob

    B. pw: fido

     got fido

     name: bob

     got bob

    C. pw:

     got fido

     name: bob got bob

    D. pw: fido

     got lido

     name: bob got bob

    E. Compilation fails.

    F. An exception is thrown at runtime.

158

    Given a vaid DateFormat object named df, and

     16. Date d = new Date(0L);

     17. String ds = "December 15, 2004";

     18. //insert code here

    What updates d's value with the date represented by ds?

    A. 18. d = df.parse(ds);

    B. 18. d = df.getDate(ds);

    C. 18. try{

     19. d = df.parse(ds);

     20. }catch(ParseException e){}

    D. 18. try{

     19. d = df.getDate(ds);

     20. }catch(ParseException e){}

159

    Given:

     11. double input = 314159.26;

     12. NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);

     13. String b;

     14. //insert code here

    Which code, inserted at line 14, sets the value of b to 314.159,26?

    A. b = nf.parse(input);

    B. b = nf.format(input);

    C. b = nf.equals(input);

    D. b = nf.parseObject(input);

160

    Given:

     22. StringBuilder sb1 = new StringBuilder("123");

     23. String s1 = "123";

     24. //insert code here

     25. System.out.println(sb1 + " " + s1); Which code fragment, inserted at line 24, outputs "123abc 123abc"?

    A. sb1.append("abc"); s1.append("abc"); B. sb1.append("abc"); s1.concat("abc"); C. sb1.concat("abc"); s1.append("abc"); D. sb1.concat("abc"); s1.concat("abc"); E. sb1.append("abc"); s1 = s1.concat("abc"); F. sb1.concat("abc"); s1 = s1.concat("abc"); G. sb1.append("abc"); s1 = s1 + s1.concat("abc"); H. sb1.concat("abc"); s1 = s1 + s1.concat("abc");

161

    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.

162

    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. three

    B. other

    C. 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.

163

    Given:

     1. public class TestString3{

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

     3. //insert code here

     5. System.out.println(s);

     6. }

     7. }

    Which two code fragments, inserted independently at line 3, generate the output 4247?

    (choose two.)

    A. String s = "123456789";

     s = (s - "123").replace(1, 3, "24") - "89"; B. StringBuffer s = new StringBuffer("123456789");

     s.delete(0, 3).replace(1, 3, "24").delete(4, 6); C. StringBuffer s = new StringBuffer("123456789");

     s.substring(3, 6).delete(1, 3).insert(1, "24"); D. StringBuilder s = new StringBuilder("123456789");

     s.substring(3, 6).delete(1, 2).insert(1, "24"); E. StringBuilder s = new StringBuilder("123456789");

     s.delete(0, 3).delete(1, 3).delete(2, 5).insert(1, "24");

164

    Given:

     1. d is a valid, non-null Date object

     2. df is a valid, non-null DateFormat object set to the current locale

    What outputs the current locale's country name and the appropriate version of d's date?

    A. Locale loc = Locale.getLocale();

     System.out.println(loc.getDisplayCountry()

     + " " + df.format(d));

    B. Locale loc = Locale.getDefault();

     System.out.println(loc.getDisplayCountry()

     + " " + df.format(d));

    C. Locale loc = Locale.getLocale();

     System.out.println(loc.getDisplayCountry()

     + " " + df.setDateFormat(d));

    D. Locale loc = Locale.getDefault();

     System.out.println(loc.getDisplayCountry()

     + " " + df.setDateFormat(d));

165

    Given:

     5. import java.util.Date;

     6. import java.text.DateFormat;

     21. DateFormat df;

     22. Date date = new Date();

     23. //insert code here

     24. String s = df.format(date);

    Which code fragment, inserted at line 23, allows the code to compile?

    A. df = new DateFormat();

    B. df = Date.getFormat();

    C. df = date.getFormat();

    D. df = DateFormat.getFormat();

    E. df = DateFormat.getInstance();

166

    Given:

     1. public class BuildStuff{

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

     3. Boolean test = new Boolean(true);

     4. Integer x = 343;

     5. Integer y = new BuildStuff().go(test, x);

     6. System.out.println(y);

     7. }

     8. int go(Boolean b, int i){

     9. if(b) return (i/7);

     10. return (i/49);

     11. }

     12. }

    What is the result?

    A. 7

    B. 49

    C. 343

    D. Compilation fails.

    E. An exception is thrown at runtime.

167

    Given:

     11. String test = "Test A. Test B. Test C.";

     12. //insert code here

     13. String[] result = test.split(regex); Which regular expression, inserted at line 12, correctly splits test into "Test A",

    "Test B", and "Test C"?

    A. String regex = "";

    B. String regex = " ";

    C. String regex = ".*";

    D. String regex = "\\s";

    E. String regex = "\\.\\s*";

    F. String regex = "\\w[\.] +";

168

    Given:

     11. public void testIfA(){

     12. if(testIfB("True")){

     13. System.out.println("True");

     14. }else{

     15. System.out.println("Not true");

     16. }

     17. }

     18. public Boolean testIfB(String str){

     19. return Boolean.valueOf(str);

     20. }

    What is the result when method testIfA is invoked? A. True

    B. Not true

    C. An exception is thrown at runtime. D. Compilation fails because of an error at line 12.

    E. Compilation fails because of an error at line 19.

169

    Given:

     12. NumberFormat nf = NumberFormat.getInstance();

     13. nf.setMaximumFractionDigits(4);

     14. nf.setMinimumFractionDigits(2);

     15. String a = nf.format(3.1415926);

     16. String b = nf.format(2);

    Which two statements are true about The result if the default locale is Locale.US? (Choose

    two.)

    A. The value of b is 2.

    B. The value of a is 3.14.

    C. The value of b is 2.00.

    D. The value of a is 3.141.

    E. The value of a is 3.1415.

    F. The value of a is 3.1416.

    G. The value of b is 2.0000.

170

    Given:

     12. String csv = "Sue,5,true,3";

     13. Scanner scanner = new Scanner(csv);

     14. scanner.useDelimiter(",");

     15. int age = scanner.nextInt(), What is the result?

    A. Compilation fails.

    B. After line 15, the value of age is 5. C. After line 15, the value of age is 3. D. An exception is thrown at runtime.

171

    Given:

     11. String test = "a1b2c3";

     12. String[] tokens = test.split("\\d");

     13. for(String s : tokens) System.out.print(s + " ");

    What is the result?

    A. a b c

    B. 1 2 3

    C. a1b2c3

    D. a1 b2 c3

    E. Compilation fails.

    F. The code runs with no output. G. An exception is thrown at runtime.

172

    Given:

     33. Date d = new Date(0);

     34. String ds = "December 15, 2004";

     35. //insert code here

     36. try{

     37. d = df.parse(ds);

     38. }

     39. catch(ParseException e){

     40. System.out.println("Unable to parse " + ds);

     41. }

     42. //insert code here too

    What creates the appropriate DateFormat object and adds a day to the Date object?

    A. 35. DateFormat df = DateFormat.getDateFormat();

     42. d.setTime((60 * 60 * 24) + d.getTime(); B. 35. DateFormat df = DateFormat.getDateInstance();

     42. d.setTime((1000 * 60 * 60 * 24) + d.getTime()); C. 35. DateFormat df = DateFormat.getDateFormat();

     42. d.setLocalTime((1000 * 60 * 60 * 24) + d.getLocalTime()); D. 35. DateFormat df = DateFormat.getDateInstance();

     42. d.setLocalTime((60 * 60 * 24) + d.getLocalTime());

173

    Given:

     11. public class Yikes{

     12.

     13. public static void go(Long n){System.out.print("Long ");}

     14. public static void go(Short n){System. outprint("Short ");}

     15. public static void go(int n){System.out.print("int ");}

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

     17. short y = 6;

     18. long z = 7;

     19. go(y);

     20. go(z);

     21. }

     22. }

    What is the result?

    A. int Long

    B. Short Long

    C. Compilation fails.

    D. An exception is thrown at runtime.

174

    Given:

     12. Date date = new Date();

     13. df.setLocale(Locale.ITALY);

     14. String s = df.format(date);

    The variable df is an object of type DateFormat that has been initialized in line 11.

    What is the result if this code is run on December 14, 2000? A. The value of s is 14-dic-2000.

    B. The value of s is Dec 14, 2000.

    C. An exception is thrown at runtime.

    D. Compilation fails because of an error in line 13.

175

    Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder

    object? (Choose

    two.)

    A. When using versions of Java technology earlier than 5.0. B. When sharing a StringBuffer among multiple threads. C. When using the java.io class StringBufferInputStream. D. When you plan to reuse the StringBuffer to build more than one string.

    E. Enitiation of separate design processes to the separation of users

177

    Given:

     11. String test = "This is a test";

     12. String[] tokens = test.split("\s");

     13. System.out.println(tokens.length);

    What is the result?

    A. 0

    B. 1

    C. 4

    D. Compilation fails.

    E. An exception is thrown at runtime.

178

    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;

185

    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.

187

    Given:

     1. public class Score implements Comparable{

     2. private int wins, losses;

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

     public int getWins(){return wins;}

     4.

     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 */}

Report this document

For any questions or suggestions please email
cust-service@docsford.com