Image Study the d, s, w, and. metacharacters

Image regex provides for quantifiers which allow you to specify concepts like: "look for one or more digits in a row."

Image Study the ?, *, and + greedy quantifiers.

Image Remember that metacharacters and Strings don't mix well unless you remember to "escape" them properly. For instance String s = "\d";

Image The Pattern and Matcher classes have Java's most powerful regex capabilities.

Image You should understand the Pattern compile() method and the Matcher matches(), pattern(), find(), start(), and group() methods

Image You WON'T need to understand Matcher's replacement-oriented methods.

Image You can use java.util.Scanner to do simple regex searches, but it is primarily intended for tokenizing.

Image Tokenizing is the process of splitting delimited data into small pieces.

Image In tokenizing, the data you want is called tokens, and the strings that separate the tokens are called delimiters.

Image Tokenizing can be done with the Scanner class, or with String.split().

Image Delimiters are single characters like commas, or complex regex expressions.

Image The Scanner class allows you to tokenize data from within a loop, which allows you to stop whenever you want to.

Image The Scanner class allows you to tokenize Strings or streams or files.

Image The String.split() method tokenizes the entire source data all at once, so large amounts of data can be quite slow to process.

Image New to Java 5 are two methods used to format data for output. These methods are format() and printf(). These methods are found in the PrintStream class, an instance of which is the out in System.out.

Image The format() and printf() methods have identical functionality.

Image Formatting data with printf() (or format()) is accomplished using formatting strings that are associated with primitive or string arguments.

Image The format() method allows you to mix literals in with your format strings.

Image The format string values you should know are

Image Flags: -, +, 0, ",", and (

Image Conversions: b, c, d, f, and s

Image If your conversion character doesn't match your argument type, an exception will be thrown

SELF TEST

1. Given:

Image

 

And the command line:

image

 

What is the result?

A. 234

B. 334

C. 2334

D. 0123456

E. 01234456

F. 12334567

G. Compilation fails

2. Given:

Image

 

What is the result?

A. pc

B. pcc

C. pcp

D. pcpc

E. Compilation fails

F. An exception is thrown at runtime

3. Given:

Image

 

Which of the following will be included in the output String s? (Choose all that apply.)

A. .e1

B. .e2

C. =s

D. fly

E. None of the above

F. Compilation fails

G. An exception is thrown at runtime

4. Given:

Image

 

What is the result? (Choose all that apply.)

A. exc

B. done

C. Compilation fails

D. Exactly one object is serialized

E. Exactly two objects are serialized

5. Using the fewest fragments possible (and filling the fewest slots possible), complete the code below so that the class builds a directory named "dir3" and creates a file named "file3" inside "dir3'. Note you can use each fragment either zero or one times

Code:

Image

 

Fragments:

Image

 

6. Given that 1119280000000L is roughly the number of milliseconds from Jan 1, 1970, to June 20, 2005, and that you want to print that date in German, using the LONG style such that "June" will be displayed as "Juni', complete the code using the fragments below. Note: you can use each fragment either zero or one times, and you might not need to fill all of the slots

Code:

Image

 

Fragments:

Image

 

7. Given:

Image

 

and that the invocation

Image

 

is issued from a directory that has two subdirectories, " dir1" and " dir2", and that " dir1" has a file " file1.txt" and " dir2" has a file " file2.txt", and the output is " false true" ; which set(s) of code fragments must be inserted? (Choose all that apply.)

Image

 

8. Given:

Image

 

Which are true? (Choose all that apply.)

A. Compilation fails

B. The output is 10 0 9

C. The output is 10 0 10

D. The output is 10 7 9

E. The output is 10 7 10

F. In order to alter the standard deserialization process you would implement the readObject() method in method in SpecialSerial

G. In order to alter the standard deserialization process you would implement the defaultReadObject() method in SpecialSerial

9. Given:

Image

 

Which are true? (Choose all that apply.)

A. Compilation fails

B. The first line of output is abc abc true

C. The first line of output is abc abc false

D. The first line of output is abcd abc false

E. The second line of output is abcd abc false

F. The second line of output is abcd abcd true

G. The second line of output is abcd abcd false

10. Given:

Image

 

And given that myfile.txt contains the following two lines of data:

image

 

What is the result?

A. ab

B. abcd

C. ab
cd

D. a
b
c
d

E. Compilation fails

11. Given:

Image

 

If line 6 creates a valid Console object, and if the user enters fred as a username and 1234 as a password, what is the result? (Choose all that apply.)

A. username:
password:

B. username: fred
password:

C. username: fred
password: 1234

D. Compilation fails

E. An exception is thrown at runtime

12. Given:

Image

 

Instances of which class(es) can be serialized? (Choose all that apply.)

A. Car

B. Ford

C. Dodge

D. Wheels

E. Vehicle

13. Given:

Image

 

Which are true? (Choose all that apply.)

A. The output is 987.12345 987.12345

B. The output is 987.12346 987.12345

C. The output is 987.12345 987.123456

D. The output is 987.12346 987.123456

E. The try/catch block is unnecessary

F. The code compiles and runs without exception

G. The invocation of parse() must be placed within a try/catch block

14. Given:

Image

 

And given the command line invocation:

Image

 

What is the result?

A. 0

B. 3

C. 4

D. 8

E. 9

F. Compilation fails

15. Given:

Image

 

What is the result?

A. 1 2

B. 1 2 3 45 6

C. 1 2 3 4 5 6

D. 1 2 a 3 45 6

E. Compilation fails

F. 1 2 followed by an exception

SELF TEST ANSWERS

1. Given:

Image

 

And the command line:

Image

 

What is the result?

A. 234

B. 334

C. 2334

D. 0123456

E. 01234456

F. 12334567

G. Compilation fails

Answer:

 

Image E is correct. The d is looking for digits. The * is a quantifier that looks for 0 to many occurrences of the pattern that precedes it. Because we specified *, the group() method returns empty Strings until consecutive digits are found, so the only time group() returns a value is when it returns 34 when the matcher finds digits starting in position 2. The start() method returns the starting position of the previous match because, again, we said find 0 to many occurrences.

 

Image A, B, C, D, F, and G are incorrect based on the above. (Objective 3.5)

 

2. Given:

Image

 

What is the result?

A. pc

B. pcc

C. pcp

D. pcpc

E. Compilation fails

F. An exception is thrown at runtime

Answer:

 

Image C is correct. It's okay for a class to implement Serializable even if its superclass doesn't. However, when you deserialize such an object, the non-serializable superclass must run its constructor. Remember, constructors don't run on deserialized classes that implement Serializable.

Image A, B, D, E, and F are incorrect based on the above. (Objective 3.3)

 

3. Given:

Image

 

Which of the following will be included in the output String s? (Choose all that apply.)

A. .e1

B. .e2

C. =s

D. fly

E. None of the above

F. Compilation fails

G. An exception is thrown at runtime

Answer:

 

Image B, C, and D are correct. Remember, that the equals() method for the integer wrappers will only return true if the two primitive types and the two values are equal. With C, it's okay to unbox and use ==. For D, it's okay to create a wrapper object with an expression, and unbox it for comparison with a primitive.

 

Image A, E, F, and G are incorrect based on the above. (Remember that A is using the equals() method to try to compare two different types.) (Objective 3.1)

 

4. Given:

Image

 

What is the result? (Choose all that apply.)

A. exc

B. done

C. Compilation fails

D. Exactly one object is serialized

E. Exactly two objects are serialized

Answer:

 

Image A is correct. An instance of type Computer Has-a Keyboard. Because Keyboard doesn't implement Serializable, any attempt to serialize an instance of Computer will cause an exception to be thrown.

 

Image B, C, D, and E are incorrect based on the above. If Keyboard did implement Serializable then two objects would have been serialized. (Objective 3.3)

 

5. Using the fewest fragments possible (and filling the fewest slots possible), complete the code below so that the class builds a directory named " dir3" and creates a file named " file3" inside " dir3". Note you can use each fragment either zero or one times.

Code:

Image

 

Fragments:

Image

 

Answer:

 

Image

 

 

Notes: The new File statements don't make actual files or directories, just objects. You need the mkdir() and createNewFile() methods to actually create the directory and the file. (Objective 3.2)

 

6. Given that 1119280000000L is roughly the number of milliseconds from Jan. 1, 1970, to June 20, 2005, and that you want to print that date in German, using the LONG style such that "June" will be displayed as "Juni', complete the code using the fragments below. Note: you can use each fragment either zero or one times, and you might not need to fill all of the slots.

Code:

Image

 

Fragments:

Image

 

Answer:

 

Image

 

 

Notes: Remember that you must build DateFormat objects using static methods. Also remember that you must specify a Locale for a DateFormat object at the time of instantiation. The getInstance() method does not take a Locale. (Objective 3.4)

 

7. Given:

Image

 

and that the invocation

Image

 

is issued from a directory that has two subdirectories, "dir1" and "dir2", and that "dir1" has a file "file1.txt" and "dir2" has a file "file2.txt', and the output is "false true', which set(s) of code fragments must be inserted? (Choose all that apply.)

Image

 

Image

 

Answer:

 

Image A and B are correct. Because you are invoking the program from the directory whose direct subdirectories are to be searched, you don't start your path with a File.separator character. The exists() method tests for either files or directories; the isFile() method tests only for files. Since we're looking for a file, both methods work.

 

Image C and D are incorrect based on the above. (Objective 3.2)

 

8. Given:

Image

 

Which are true? (Choose all that apply.)

A. Compilation fails

B. The output is 10 0 9

C. The output is 10 0 10

D. The output is 10 7 9

E. The output is 10 7 10

F. In order to alter the standard deserialization process you would implement the readObject() method in method in SpecialSerial

G. In order to alter the standard deserialization process you would implement the defaultReadObject() method in SpecialSerial

Answer:

 

Image C and F are correct. C is correct because static and transient variables are not serialized when an object is serialized. F is a valid statement.

 

Image A, B, D, and E are incorrect based on the above. G is incorrect because you don't implement the defaultReadObject() method, you call it from within the method, you call it from within the readObject() method, along with any custom read operations your class needs. (Objective 3.3)

 

9. Given:

Image

 

Which are true? (Choose all that apply.)

A. Compilation fails

B. The first line of output is abc abc true

C. The first line of output is abc abc false

D. The first line of output is abcd abc false

E. The second line of output is abcd abc false

F. The second line of output is abcd abcd true

G. The second line of output is abcd abcd false

Answer:

 

Image D and F are correct. While String objects are immutable, references to Strings are mutable. The code s1 += "d" ; creates a new String object. StringBuffer objects are mutable, so the append() is changing the single StringBuffer object to which both StringBuffer references refer.

 

Image A, B, C, E, and G are incorrect based on the above. (Objective 3.1)

 

10. Given:

Image

 

And given that myfile.txt contains the following two lines of data:

ab
cd

What is the result?

A. ab

B. abcd

C. ab
cd

D. a
b
c
d

E. Compilation fails

Answer:

 

Image E is correct. You need to call flush() only when you're writing data. Readers don't have flush() methods. If not for the call to flush(), answer C would be correct.

 

Image A, B, C, and D are incorrect based on the above. (Objective 3.2)

 

11. Given:

Image

 

If line 6 creates a valid Console object, and if the user enters fred as a username and 1234 as a password, what is the result? (Choose all that apply.)

A. username:
password:

B. username: fred
password:

C. username: fred
password: 1234

D. Compilation fails

E. An exception is thrown at runtime

Answer:

 

Image D is correct. The readPassword() method returns a char[]. If a char[] were used, answer B would be correct.

 

Image A, B, C, and E are incorrect based on the above. (Objective 3.2)

 

12. Given:

Image

 

Instances of which class(es) can be serialized? (Choose all that apply.)

A. Car

B. Ford

C. Dodge

D. Wheels

E. Vehicle

Answer:

 

Image A and B are correct. Dodge instances cannot be serialized because they "have" an instance of Wheels, which is not serializable. Vehicle instances cannot be serialized even though the subclass Car can be.

 

Image C, D, and E are incorrect based on the above. (Objective 3.3)

 

13. Given:

Image

 

Which are true? (Choose all that apply.)

A. The output is 987.12345 987.12345

B. The output is 987.12346 987.12345

C. The output is 987.12345 987.123456

D. The output is 987.12346 987.123456

E. The try/catch block is unnecessary

F. The code compiles and runs without exception

G. The invocation of parse() must be placed within a try/catch block

Answer:

 

Image D, F, and G are correct. The setMaximumFractionDigits() applies to the formatting but not the parsing. The try/catch block is placed appropriately. This one might scare you into thinking that you'll need to memorize more than you really do. If you can remember that you're formatting the number and parsing the string you should be fine for the exam.

 

Image A, B, C, and E are incorrect based on the above. (Objective 3.4)

14. Given:

Image

 

And given the command line invocation:

Image

 

What is the result?

A. 0

B. 3

C. 4

D. 8

E. 9

F. Compilation fails

Answer:

 

Image B is correct. The "d" metacharacter looks for digits, and the + quantifier says look for "one or more" occurrences. The find() method will find three sets of one or more consecutive digits: 2, 4, and 67.

 

Image A, C, D, E, and F are incorrect based on the above. (Objective 3.5)

 

15. Given:

Image

 

What is the result?

A. 1 2

B. 1 2 3 45 6

C. 1 2 3 4 5 6

D. 1 2 a 3 45 6

E. Compilation fails

F. 1 2 followed by an exception

Answer:

 

Image F is correct. The nextXxx() methods are typically invoked after a call to a hasNextXxx(), which determines whether the next token is of the correct type.

 

Image A, B, C, D, and E are incorrect based on the above. (Objective 3.5)

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset