CSC 240 — Quiz 1 Practice Examples

Ungraded warm-up — not the real quiz

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);

Answer: C, S, C are letters (3); the space is neither; 2,0,2,6 are digits (4). Output: 3 4.

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);

Answer: f = {"Hopper","94","89"}, so quiz=94, exam=89. Watch the integer division: (94+89)/2 = 183/2 = 91 (not 91.5). Output: Hopper average: 91.

← Back to CSC 240