Study the d, s, w, and. metacharacters
regex provides for quantifiers which allow you to specify concepts like: "look for one or more digits in a row."
Study the ?, *, and + greedy quantifiers.
Remember that metacharacters and Strings don't mix well unless you remember to "escape" them properly. For instance String s = "\d";
The Pattern and Matcher classes have Java's most powerful regex capabilities.
You should understand the Pattern compile() method and the Matcher matches(), pattern(), find(), start(), and group() methods
You WON'T need to understand Matcher's replacement-oriented methods.
You can use java.util.Scanner to do simple regex searches, but it is primarily intended for tokenizing.
Tokenizing is the process of splitting delimited data into small pieces.
In tokenizing, the data you want is called tokens, and the strings that separate the tokens are called delimiters.
Tokenizing can be done with the Scanner class, or with String.split().
Delimiters are single characters like commas, or complex regex expressions.
The Scanner class allows you to tokenize data from within a loop, which allows you to stop whenever you want to.
The Scanner class allows you to tokenize Strings or streams or files.
The String.split() method tokenizes the entire source data all at once, so large amounts of data can be quite slow to process.
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.
The format() and printf() methods have identical functionality.
Formatting data with printf() (or format()) is accomplished using formatting strings that are associated with primitive or string arguments.
The format() method allows you to mix literals in with your format strings.
The format string values you should know are
Flags: -, +, 0, ",", and (
Conversions: b, c, d, f, and s
If your conversion character doesn't match your argument type, an exception will be thrown
1. Given:
And the command line:
What is the result?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails
2. Given:
A. pc
B. pcc
C. pcp
D. pcpc
E. Compilation fails
F. An exception is thrown at runtime
3. Given:
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:
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:
Fragments:
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:
Fragments:
7. Given:
and that the invocation
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.)
8. Given:
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:
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:
And given that myfile.txt contains the following two lines of data:
What is the result?
A. ab
B. abcd
C. ab
cd
D. a
b
c
d
E. Compilation fails
11. Given:
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:
Instances of which class(es) can be serialized? (Choose all that apply.)
A. Car
B. Ford
C. Dodge
D. Wheels
E. Vehicle
13. Given:
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:
And given the command line invocation:
What is the result?
A. 0
B. 3
C. 4
D. 8
E. 9
F. Compilation fails
15. Given:
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
1. Given:
And the command line:
What is the result?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails
Answer: |
|
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. |
|
A, B, C, D, F, and G are incorrect based on the above. (Objective 3.5) |
2. Given:
What is the result?
A. pc
B. pcc
C. pcp
D. pcpc
E. Compilation fails
F. An exception is thrown at runtime
Answer: |
|
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. A, B, D, E, and F are incorrect based on the above. (Objective 3.3) |
3. Given:
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: |
|
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. |
|
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:
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: |
|
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. |
|
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:
Fragments:
Answer: |
|
|
|
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:
Fragments:
7. Given:
and that the invocation
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.)
Answer: |
|
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. |
|
C and D are incorrect based on the above. (Objective 3.2) |
8. Given:
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: |
|
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. |
|
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:
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: |
|
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. |
|
A, B, C, E, and G are incorrect based on the above. (Objective 3.1) |
10. Given:
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: |
|
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. |
|
A, B, C, and D are incorrect based on the above. (Objective 3.2) |
11. Given:
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: |
|
D is correct. The readPassword() method returns a char[]. If a char[] were used, answer B would be correct. |
|
A, B, C, and E are incorrect based on the above. (Objective 3.2) |
12. Given:
Instances of which class(es) can be serialized? (Choose all that apply.)
A. Car
B. Ford
C. Dodge
D. Wheels
E. Vehicle
Answer: |
|
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. |
|
C, D, and E are incorrect based on the above. (Objective 3.3) |
13. Given:
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: |
|
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. |
|
A, B, C, and E are incorrect based on the above. (Objective 3.4) |
14. Given:
And given the command line invocation:
What is the result?
A. 0
B. 3
C. 4
D. 8
E. 9
F. Compilation fails
Answer: |
|
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. |
|
A, C, D, E, and F are incorrect based on the above. (Objective 3.5) |
15. Given:
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: |
|
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. |
|
A, B, C, D, and E are incorrect based on the above. (Objective 3.5) |