Term
In general, How are regex expressions processed? |
|
Definition
In general from left to right, and once a source's character has been used in a match, it cannot be reused.
pg. 490 |
|
|
Term
What are the three metacharacter types we can use to build regex patterns.
Define each one. |
|
Definition
\d - a digit \s - a whitespace character \w - a word character (letters, digits, or "_")
pg. 491 |
|
|
Term
Define how each of the following patterns work, and what would m.start() return after m.find()
[abc] [a-f] [a-fA-F] .*[abc] |
|
Definition
character 'a' or 'b' or 'c' character 'a' or 'b' or 'c' or 'd' or 'f' character 'a' or 'b' or 'c' or 'd' or 'f' or 'A' or 'B' or 'C' or 'D' or 'F' any number of characters followed by an 'a', or 'b' or 'c'
pg391-396 |
|
|
Term
How is a pattern and matcher created? |
|
Definition
Pattern p = Pattern.compile("pattern expression"); Matcher m = p.matcher("source"); |
|
|
Term
What does the following do, what is the output?
import java.util.regex.*; Class Regit { public static void main (String... args) { String inputString = "abaaaba"; Pattern pattern = Pattern.compile(".*ab"); Matcher bacon = pattern.matcher(inputString); while (bacon.find()) { if(bacon.start() == (inputString.length() - bacon.group().length())){ System.out.println("found the bacon!"); } } } |
|
Definition
Checks to see if a match is found at the end of the specified inputString and will print "found the bacon!" if found.
There will be no output when the above code is run.
m.start() returns the starting index bacon.group().length() returns the length of the pattern found.
if the inputString was "abaaabab" then when the 3rd match is found, m.start would return 6 the size of the input would be 8 and the size of the mathcing pattern would be 2.
So 6 == 8-2
if the inputString was "abaaab" then when the 2nd match is found, m.start would return 2 the size of the input would be 6 and the size of the mathcing pattern would be 4.
So 2 == 6-4
pg 489 to 499 |
|
|
Term
True/False -
If a superclass is Serializable, then according to normal Java interface rules, all subclasses of that class automatically implement Serializable implicitly. |
|
Definition
|
|
Term
import java.io.*; class MainClass { public static void main(String[] args) {
Apple a = new Apple("red", 10, 20); System.out.println("before: " + a.color + " " + a.height + " " + a.weight);
try { FileOutputStream fs = new FileOutputStream("Serialized.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(a) os.close(); } catch (Exception e) {e.printStackTrace(); } try { FileInputStream fis = new FileInputStream("Serialized.ser"); ObjectInputStream ois = new ObjectInputStream(fis); a = (Apple) ois.readObject(); ois.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("after: " + a.color + " " + a.height + " " + a.weight); } }
class Apple extends Fruit implements Serializable { String color; transient int height Apple (String c, int h, int w) { color = c; height = h; weight = w; } }
class Fruit { int weight = 25; }
Choose all that will be outputted: a) before: red 10 20 b) before: red 0 20 c) before: d) before: red 10 25 e) after: red 10 20 f) after: red 0 25 g) after: h) after: red 10 25 |
|
Definition
|
|
Term
True / False
Static variables cannot be serialized. |
|
Definition
|
|
Term
The following code will compile:
import java.util.*; import java.text.*; class TestCalendar{ public static void main(String[] args) { Date now = new Date(); //(1) Locale locPT = new Locale("it"); //(2) Calendar c = new Calendar(); //(3) NumberFormat nf = NumberFormat.getInstance(loc); //(4) DateFormat dfUSfull = DateFormat.getDateInstance( DateFormat.FULL); //(5) } }
Choose the following that are true: a) Line 1 will compile b) Line 2 will compile c) Line 3 will compile d) Line 4 will compile e) Line 5 will compile |
|
Definition
a, b, d, e.
"c" is false because it needs to be declared like the following: Calendar c = Calendar.getInstance(); (pg477) |
|
|
Term
What is the correct invocation to retrieve a reference to the physical device on which the JVM is running? |
|
Definition
Console c = System.console(); (not Console c = new Console();) p. 457/458 |
|
|
Term
What do the Console.readLine and Console.readPassword methods return? |
|
Definition
Console.readLine() - a String Console.readPassword() - a char array p. 457 |
|
|
Term
What are the key interface and methods involved in serialization? |
|
Definition
1. The class (or a parent class) must implement Serializable 2. Invoke ObjectOutputStream.writeObject() on the object to serialize 3. Invoke ObjectOutputStream.readObject() on the object to deserialize p. 460 |
|
|
Term
What is passed to the ObjectOutputStream and ObjectInputStream constructors? |
|
Definition
FileOutputStream and FileInputStream respectively. p. 460 |
|
|
Term
What error occurs when attempting to serialize an object that has a member which is not serializable? |
|
Definition
RuntimeException similar to: java.io.NotSerializableException: problemMemberVariable p. 463 |
|
|
Term
What is one option for dealing with member variable(s) that, for one reason or another, cannot be serialized? |
|
Definition
Mark the variable transient. p. 465 |
|
|
Term
What are the set of private methods that will be invoked during serialization and deserialization, if present? |
|
Definition
private void writeObject (ObjectOutputStream os) throws IOException private void readObject (ObjectInputStream is) throws IOException, ClassNotFoundException p. 466, 467 |
|
|
Term
What methods can be invoked during the special callback contract the serialization systems offers you, to tell the JVM to perform the normal serialization or deserialization process for an object? |
|
Definition
os.defaultWriteObject //invoked using the ObjectOutputStream (os) argument passed into the private void writeObject method.
is.defaultReadObject (invoked using the ObjectInputStream (is) argument passed into the private void readObject method.
p. 466/467 |
|
|
Term
T/F: Calls to defaultWriteObject and defaultReadObject are order dependent |
|
Definition
T: If you write out additional data (e.g. using ObjectOtuputStream.writeInt) AFTER calling objectOutputStream.defaultWriteObject(), then the call to read in the additional data (e.g. via objectIntputStream.readInt()) must occur AFTER the call to objectInputStream.defaultReadObject() (and vice versa). p. 467 |
|
|
Term
What is the result: String s = "I am"; s.concat(" a String"); System.out.println(s); |
|
Definition
I am
the result of s.concat is never assigned to a new reference. pg 430 |
|
|
Term
What is the output of:
Calendar c = new Calendar(); Date d = c.getTime(); DateFormat df = DateFormat.getInstance(DateFormat.SHORT); System.out.println(df.format(c)); |
|
Definition
Compilation error because Calendar is abstract. |
|
|
Term
Given a Calendar object, how do you add 1 hour to it?
Calendar c = Calendar.getInstance(); // What goes here? |
|
Definition
|
|
Term
What are 4 built-in DateFormat enums? |
|
Definition
Dateformat.SHORT, MEDIUM, LONG, and FULL. |
|
|
Term
What are the arguments for the Locale constructor?
Locale l = new Locale(arg1, arg2); |
|
Definition
A:
Locale(String language, String country) |
|
|
Term
What function is used to convert a String to a Number?
NumberFormat nf = NumberFormat.getInstance(); Number n = nf.?????("1234.5");
|
|
Definition
|
|
Term
Consider the following: File file = new File("foo");
is a file created? |
|
Definition
|
|
Term
Consider the following: File file = new File("foo"); PrintWriter pw = new PrintWriter(file);
is a file created? |
|
Definition
|
|
Term
calling delete() on a directory reference variable will delete the directory and its contents. True or False? |
|
Definition
|
|
Term
Given the following code: import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternMatching { public static void main(String[] args) { Pattern p = new Pattern(args[0]); Matcher m = p.matcher(args[1]); while(m.find()){ System.out.println(m.start() + " " + m.group()); } } } And a command line invocation of: % java PatternMatching "\d\w" "ab4*56_7ab" What is the result: a) The code will not compile b) The code will compile but will throw an error at runtime c) 2 4* 4 56 7 7a d) 4 56 7 7a |
|
Definition
a. There are no constructors in the Pattern API. If the instantiation of the Pattern variable was changed to: Pattern p = Pattern.compile(args[0]); then the code would compile and the answer would be d. (p. 499) |
|
|
Term
What do the find(), start() and group() methods of the Matcher class do? |
|
Definition
The find method turns on the reg ex engine and does some searching. It returns true if a match is found, and remembers the start position of the match. The starting position of the match can be obtained by calling start(). The matched data can be obtained via the group() method. (p. 499) |
|
|
Term
When can zero-length matches be returned by the Matcher.find() method? |
|
Definition
If the regex is a greedy quantifier ? or *, zero-length matches can occur: a) after the last character of the source data, or the beginning of source data b) In between characters after a match has been found c) At the beginning of zero-length source data (p. 500) |
|
|
Term
How can one use the java.util.Scanner class to search data? |
|
Definition
Instantiate a Scanner instance, passing in the data to be searched, and then call its findInLine() method, passing in the data to search for: Scanner s = new Scanner("search me"); String token = s.findInLine("a"); //put the above in a loop for searching the entire string. (p. 501) |
|
|
Term
What argument is passed to the String.split() method, and what does the method return? |
|
Definition
One passes a regex expression to the method, and the method returns an array of Strings containing the tokens produced by the split process. String[] tokens = "StringToSplit".split("regEx");
(p. 502/503) |
|
|
Term
How can a metacharacter (e.g. \d, \w, etc) be used appropriately in a string? |
|
Definition
Use a second backslash to escape the sequence (e.g. "\\d") (p. 503) |
|
|
Term
How do you create a String that uses a double quote or a backslash? |
|
Definition
Escape the character within the string: System.out.println("\" \\"); prints: " \ (p. 504) |
|
|
Term
Which of the following statements about the java.util.Scanner class are true? a) Scanners can be constructed using files, streams, or Strings as a source. b) The default Scanner delimiter is a comma c) Tokens can be converted into their primitive types automatically d) Scanner has findNext, findNextInt, findNextBoolean, etc methods to assist in looping e) All of the above |
|
Definition
a and c For b, the default delimiter of the Scanner is whitespace. d is incorrect because the methods are hasNext, hasNextInt, hasNextBoolean, etc. (The Scanner class has nextXxx() methods for every primitive type except char). Regarding c, Scanner can convert tokens into their primitive types automatically by invoking the nextInt, nextBoolean, etc methods of the class. (p. 504/505) |
|
|
Term
T/F: The format() and printf() methods of java.io.PrintStream behave exactly the same way. |
|
Definition
|
|
Term
What is the syntax for the printf statement and its format string? |
|
Definition
printf statement: printf("format string", argument(s)); format string: %[arg_index$][flags][width][.precision]conversion_char (p. 506/507) |
|
|
Term
What is the output of the following code?
String x = "01234567";
System.out.println(x.length() );
|
|
Definition
|
|
Term
What is the output of the follwoing code?
String x = "test";
System.out.println( x.length);
|
|
Definition
Will give a comiler error p437 |
|
|
Term
According to Java which is better to use StringBuffer vs. StringBuilder?
|
|
Definition
Sun recommends that you use StringBuilder whenever possible because it will run faster (but it is not thread safe). p439 |
|
|
Term
What is the output for the following code:
String x = "abc";
String y = x.concat("def").toUpperCase().replace('C','x');
System.out.println ("y = " + y);
|
|
Definition
|
|
Term
What is the use of class BufferedReader?
|
|
Definition
The calss is used to make lower-level Reader classes like FileReader more efficient and easier to use. p443 |
|
|
Term
Define the meaning of the following flags used in a format string: a) "-" b) "+" c) "0" d) "," e) "(" |
|
Definition
a) "-" Left justify the argument b) "+" Include a sign (+ or -) with the argument c) "0" Pad the argument with zeros d) "," Use locale-specific grouping separators (e.g. the comma in 123,456) e) "(" Enclose negative numbers in parentheses (p. 507) |
|
|
Term
How are the optional width and precision elements of the format string used? |
|
Definition
width indicates the minimum numbers of characters to print precision indicates the number of digits to print after the decimal point (p. 507) |
|
|
Term
List the five options available for the conversion characters of the format string |
|
Definition
b boolean c char d integer f floating point s string (p. 508) |
|
|
Term
What is the output of the following code:
int i1 = -123; int i2 = 12345; System.out.printf("a) *%1$(d* \n", i1); System.out.printf("b) *%0,7d* \n", i2); System.out.printf("c) *%+-7d* \n", i2); System.out.printf("d) *%2$b + %1$5d* \n", i1, false); |
|
Definition
a) *(123)* b) *012,345* c) *+12345 * d) *false + -123*
(p. 508) |
|
|
Term
What is the output of the following code?: double pi = 3.14; System.out.printf("%.7d", pi); |
|
Definition
java.util.IllegalFormatPrecisionException: 7 The given conversion character, d, does not support a precision. Removing the precision flag (.7) will still result in a java.util.IllegalFormatConversionException: d != java.lang.Double error, because the conversion character 'd' means integer, NOT double. Changing the line to System.out.printf("%.7f", pi); outputs the following: 3.1400000 (p. 508) |
|
|