Studydroid is shutting down on January 1st, 2019

Bookmark and Share

Front Back
DateFormat df = new DateFormat(); Compiles? If not how would you make it compile?
Fails to compile. DateFormat is abstract. Need to call one of the following static methods: getInstance(); - unlike getDateInstance() includes time as well - is the default and uses a SHORT formatter for both date and time - a SHORT time formatter means that there will be no milliseconds getDateInstance(); getDateInstance(style); -where style is DateFormat.SHORT DateFormat.MEDIUM DateFormat.LONG DateFormat.FULL getDateInstance(field locale);
Methods of a DateFormat object?
getInstance() getDateInstance() getDateInstance(SHORT/MEDIUM/LONG/FULL) getDateInstance(SHORT/MEDIUM/LONG/FULL Locale) format(Date d) parse(String dateAsString) -might throw a ParseException!
How does Date connect Calendar and DateFormat?
DateFormat method format() does not take a Calendar object. It only takes a Date or in some cases a Number object. So if you convert your Calendar to a Date then you can use DateFormat.
What happens when you call Calendar's toString() method?
You get lots of crud! Better to output as a Date (getTime() will output Date object) and either use Date's toString() or use Date as argument of DateFormat's format() method.
When you send a bad string to a DateFormat object's parse() method what happens?
a CHECKED exception gets thrown (meaning you MUST do a parse inside a try block or declare that the current method throws the exception) This is what might be thrown: ParseException
Calendar c = Calendar.getInstance(); Date d = c.getTime(); System.out.println(d); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); String s = df.format(d); d = df.parse(s); System.out.println(d); Compile? If so will the printed outputs match?
This will NOT compile if if the DateFormat parse() method call is not inside a try statement or part of a method that declares it throws the checked ParseException. If the above condition is met then NO the 2 strings printed will not be the same (probably ;P ). When the Date is formatted with the DateFormat.SHORT field the formatted string doesn't include any time information. When that string get's parsed back into a Date object it now is missing any time info. So unless this code is run EXACTLY at midnight the 2 strings output will be different.
If the Locale class is abstract how do you instantiate a Locale object?
The Locale class IS NOT abstract (so you directly instantiate) new Locale(String language); new Locale(String language String country); ISO 639 = language codes ISO ? = country codes (you don't need to know any codes for the exam)
What happens when you try to format with a locale language or country that's not supported on the local machine? Compiles? Runs?
Yes and yes. But the output will be question marks. I.E. ?????? ?? ?????? ????
When you want to change locales in a DateFormat or NumberFormat object what method to you call on the formatting object?
None. You must create a new formatting object if you want to change locales. Once a formatting object is built with a Locale object that Locale object is immutably set.
What are the primary methods you'll call on a locale object?
Locale l1 = new Locale(String language String country); l1.getDisplayCountry(); or l1.getDisplayCountry(Locale l2) // The actual instace of the locale will be displayed in default locale format if no arg version called and will display in the locale format of the argument if a Locale argument is provided. l1.getDisplayLanguage() l1.getDisplayLanguage(Locale l2)
How do you get an instance of the NumberFormat class?
NumberFormat.getInstance(); NumberFormat.getInstance(Locale l); NumberFormat.getNumberInstance(); NumberFormat.getNumberInstance(Locale l); NumberFormat.getCurrencyInstance(); NumberFormat.getCurrencyInstance(Locale l);
What are the important methods of the NumberFormat class?
NumberFormat nf = NumberFormat.getInstance(); nf.getMaximumFractionDigits() nf.setMaximumFractionDigits(int i) // fractions higher than max will be ROUNDED (not truncated) nf.setParseIntegerOnly(boolean b) - if set to true then any decimals in parse string when parse() converts a string to a primitive number nf.parse(String s) - throws ParseException nf.format(primitive number)
What package is regex found in?
How do you set up a pattern in Java's regex?
Pattern p = Pattern.compile(String s); NOTE: You don't directly instantiate a pattern object! Instead you call the static method compile(String s) which outputs a Pattern object
How do you build an object that looks for matches?
Pattern p = Pattern.compile(String lookForThis); Matcher m = p.matcher(String lookInHere);
How might you iterate through a source string to find all the matches of certain pattern?
Pattern p = Pattern.compile(""\\d*""); Matcher m = p.matcher(""123abc456def""); boolean b; System.out.println(p.pattern()); while (b = m.find()) { System.out.println(m.start() + "": "" +; }
In regex what do the following quantifiers mean? + ? *
+ = 1 or more occurrences ? = zero or 1 * = 0 or more
What are the 3 behavioral qualities of quantifiers in regex?
greedy (default) - eats entire source string and works backwards reluctant (add a ?) - eats as little of string as it can and works forward possessive (add a +) - eats entire string like greedy but it does not work backwards. EXAMPLES: Current REGEX is: .*foo // greedy quantifier Current INPUT is: xfooxxxxxxfoo I found the text ""xfooxxxxxxfoo"" starting at index 0 and ending at index 13. Current REGEX is: .*?foo // reluctant quantifier Current INPUT is: xfooxxxxxxfoo I found the text ""xfoo"" starting at index 0 and ending at index 4. I found the text ""xxxxxxfoo"" starting at index 4 and ending at index 13. Current REGEX is: .*+foo // possessive quantifier Current INPUT is: xfooxxxxxxfoo No match found.
String ps = ""\d\d""; String source = ""123cat346""; Pattern p = Pattern.compile(ps); Matcher m = p.matcher(source); boolean b; while (b = m.find()) { System.out.println(m.start()); } What prints?
Compiler error. Compiler will read \d as an illegal escape. String literal needs to be \\d\\d
Pattern p = Pattern.compile(""\\d*""); Matcher m = p.matcher(""123abc456def""); boolean b; System.out.println(p.pattern()); while (b = m.find()) { System.out.println(m.start() + "": "" +; } What prints?
\d* 0: 123 3: 4: 5: 6: 456 9: 10: 11: 12: NOTE: The star (""*"") is greedy AND it will match 0 characters. At the 4th index... it first eats entire string and works backwards. When it finally works back to the ""a"" and that doesn't match it DOES match on the empty string. If you changed the * to a + you'd get what you might expect from this: \d+ 0: 123 6: 456
What package is Scanner found in?
What does Scanner do?
It scans a File InputStream Readable ReadableByteChannel or String. Often used for tokenizing it's source.
How do you use Scanner for searching?
Scanner s = new Scanner(; String token = """"; do { token = s.findInLine(""a""); } while (token != null);
Name 2 classes you can use for tokenizing?
String and Scanner
What's special about the String classes split method?
It takes String that is a regex expression.
What's the drawback of tokenizing with String's split(String regexPattern)?
It tokenizes it's target all at once. You can't do stuff after finding each token.
What is Scanner's default delimiter?
List the Scanner methods you need to know():
findInLine(String regexPattern); hasNext() - see if there is another token present (often use this in while loop along with next() - outputs boolean value next() - outputs the next token hasNextInt() hasNextBoolean() hasNextFloat() etc. - is there another token of this particular type? nextInt() nextBoolean() nextFloat() etc.. - get next token a particular type useDelimiter(String regexPattern) NOTE: there is NO hasNextXxx() nextXxx() for type char.
What is the most important thing to remember about the relationship between method format() and printf()?
They are aliases of each other!
What class are methods format() an printf() a part of?
When embedding argument-specfic formatting data in a string what character must that data begin with?
% (percent sign)
What is the structure of a format string?
%[arg_index$][flags][width][.precision]conversion char
Is the argument index in a printf() formatting string zero based
What are the possible flags that can be put in a formatting string?
- left justify + include sign (+ or -) 0 padd this arg with zeros use locale specific separators ( enclose neg #s in paras
What is the width declartion in a formatting string?
The minimum number of charaters to print for the argument
What is the precision declartion in a formatting string?
When using floats indicate number of digits to print after decimal point
What are the conversion character codes in a formatting string?
b boolean c char s string (NOT short) d integer (NOT decimal) f float
System.out.format(""%d"" 12.3); Compiles?
Yes but you get a runtime error! 12.3 is more specific than integer (d)
What is notable about an transient object ivar that has been instantiated to some object by it's constructor then serialized then deserialized?
It's set to null... UNLESS 1) A custom writeObject(ObjectOutputStream os) and custom readObject(ObjectInputStream) have been added to the class and recreate the object or.... 2) The ivar is part of an ancestor of the actual serialized object at which point it will get recreated when the ancestor's constructor is called.
Class A implements Serializable {} Class B extends A{} Class C extends B implements Serializable {} When serializing an object of Class C will the ivars of class A be restored via serializiation or will they be initialized by the A() constructor?
They'll be initialized by the constructor. The key point is that at the very first ancestor that is NOT serializable all ancestors above it will have their constructors called - regardless of whether they're marked Serializable or not. NOTE: Think about the constructor chaining that occurs when an object is instantiated.
Are static variables serialized?
What characters does the \w metacharacter represent?
\w stands for Word characters. They include: letters numbers underscore
What class is the split() method associated with? What does split() return?
The String class. It returns an array of Strings. The argument to the string() method is a regex expression that serves to define the delimiter. The tokens are then put in the array.
What is the difference between a token and a delimiter?
Delimiters are seperators and the stuff between the separators are the tokens.
BufferedWriter bf = new BufferedWriter(""temp.txt""); Compiles?
No. A BufferedWriter is a decorator class that can only be instantiated by wrapping another Writer object. It cannot take a string and turn that directly into a BufferredWriter. Here is what would work: try { FileWriter fw = new FileWriter(""temp.txt""); BufferedWriter bf = new BufferedWriter(fw); } catch (IOException e) {}
Currency c = new Currency(); Compiles? Runs?
It doesn't compile. Currency does not have a public constructor. Currency objects are created using static mehtod (getInstance). Currency is a singleton.
NumberFormat nf = NumberFormat.getCurrencyInstance(); nf.parse(""1234""); Compiles? What kind of object does parse function produce?
It does not compile. The parse method can throw a CHECKED exception ParseException and needs to be wrapped accordingly. If wrapped so and the string is a valid number then the parse function outputs an object of class Number (NOT class String).
System.out.format(""%b"" 123); What prints?
true the boolean conversion char ""b"" returns true for any non null or non-boolean argument.
System.out.format(""%f"" 123); What prints?
Runtime error. int literals are not automatically promoted to floats.
String a = new String(""hello""); String b = a.toString(); System.out.println(a == b); What prints?
True. toString() called on a String object returns the calling object.
|< < Previous x of y cards Next >|