The correct option for each question is checked in green, with an explanation below it. This quiz is now
closed — if anything here does not make sense, that is exactly what office hours and HW1 are for.
Question 1
Which of these correctly converts the String"42" into an int?
Why: A (int) cast only converts between numeric types (like double to int) - it cannot turn text into a number. charAt(0) gives you the single character '4', not the number 42. intValue() is not even a method on String (it exists on Integer/Double objects). Integer.parseInt(...) is the wrapper-class method built exactly for text-to-number conversion.
Question 2
What does Character.isDigit('7') return?
Why:isDigit answers a yes/no question - it returns a boolean, never the character itself or its numeric code. '7' is a digit character, so the answer is true.
Question 3
What is the value of Character.toUpperCase('a')?
Why:toUpperCase converts a lowercase letter to its uppercase form and returns a char: 'a' → 'A'. 'a' unchanged would mean the method did nothing; 65 is the numeric (ASCII) code for 'A', but the method returns a char, not an int; there is nothing here that fails to compile.
Question 4
For String s = "Hello";, what does s.substring(1, 3) return?
Why:substring(start, end) takes characters from index start up to but not including index end. Indexing "Hello": H=0, e=1, l=2, l=3, o=4. So substring(1, 3) grabs indices 1 and 2 only: "el". The most common mistake is treating the end index as inclusive, which gives the tempting-looking wrong answer "ell" (indices 1-3) - this was the single most common wrong answer on the quiz, so it is worth re-reading the substring examples in the Class 5 slides.
Question 5
For String s = "banana";, what does s.indexOf("na") return?
Why: Indexing "banana": b=0, a=1, n=2, a=3, n=4, a=5. The first place "na" appears is starting at index 2 (the n at position 2, a at position 3). indexOf only returns -1 when the text is not found at all - here it clearly is found, so -1 cannot be right. The usual slip is an off-by-one count landing on 3 instead of 2.
Question 6
Why prefer a StringBuilder over a String when building up text inside a loop?
Why: A String can never be changed once created, so every += inside a loop secretly builds a brand-new String and throws the old one away - wasteful if the loop runs many times. StringBuilder is the opposite: it is mutable, so append edits the same internal buffer in place. Option (b) gets this exactly backwards - it is String that is immutable, not StringBuilder - and that reversal was the most common wrong answer here, so double-check which of the two is the mutable one. The 100-characters and auto-sorting claims are not real Java behavior.
Question 7
"192.168.1.25".split(".") does not split on the periods the way you would expect. Why?
Why:split()'s argument is a regular expression, not a literal string. In regex, a bare . means “match any single character,” so it matches nearly every character in the input, not just literal periods, and you get a mess of mostly-empty pieces. To split on an actual period you must escape it: split("\\.") or split("[.]"). This exact gotcha is demonstrated in RegexDelimiters.java from Class 4.
Question 8
Which statement correctly compares String.split(...) and StringTokenizer?
Why:split() takes a regular expression and hands back the whole result at once as a String[]. StringTokenizer's classic delimiter is a set of literal characters (no regex), and you pull tokens one at a time by calling nextToken() in a loop. Option (a) swaps these two facts - which tool gives you an array all at once vs. which one you iterate - and that exact swap was the most consistently chosen wrong answer on this quiz, so it is worth memorizing which is which: split → array, all at once; StringTokenizer → one at a time.
Question 9
A CSV line is parsed with String[] f = "Ada,Lovelace,1815".split(",");. Which line correctly turns f[2] into an int birth year?
Why: Every element of f is a String (here, the text "1815"), so it must be parsed into a number with Integer.parseInt(f[2]). A plain assignment does not compile - a String is not an int. A (int) cast does not compile either - casting only converts between compatible numeric types, not text to a number. f[2].length() gives the string's length (4, for "1815"), not the year itself.
Question 10
What happens when the program calls Integer.parseInt("ninety")?
Why:parseInt only understands digit characters like "90" - it has no idea that the English word “ninety” means the same thing. Handed text it cannot parse, it throws a NumberFormatException at runtime rather than guessing, returning 0, or passing the text through unchanged. This is exactly the scenario demonstrated in ParseNumbers.java from Class 4; catching that exception with try/catch is Chapter 11 material, not required here - just recognizing that a bad token throws is the point of this question.