Quiz 1 — practice examples
These two questions are not on the real quiz and are not graded — they are here so you can see the format and difficulty of Quiz 1 before you take it. The real quiz has 10 different multiple-choice questions covering the same Chapter 9 topics (wrapper classes, the Character class, String methods, StringBuilder, and tokenizing). If these two feel comfortable, you are in good shape.
Two worked examples (not graded)
Example A. What does this code print?
String s = "CSC 2026";
int letters = 0, digits = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetter(c)) letters++;
else if (Character.isDigit(c)) digits++;
}
System.out.println(letters + " " + digits);
Example B. What does this code print?
String record = "Hopper,94,89";
String[] f = record.split(",");
int quiz = Integer.parseInt(f[1]);
int exam = Integer.parseInt(f[2]);
System.out.println(f[0] + " average: " + (quiz + exam) / 2);