CS203 – Lab Code Output Analysis

Practice Test 2 • Professor Mahmut Unan • UAB CS

35 Questions • "What does this code output?" • Grounded in Labs 1–6 & Homework 1
Trace through your actual lab submissions to predict outputs and understand behavior
Section 1: Basic Java Programs Lab 1
FirstJavaProgram.java, Numbers.java, Fibonacci.java — println, for loops, variables
Question 1 FirstJavaProgram.java
What is the output of this program?
public class FirstJavaProgram { public static void main(String[] args) { System.out.println("CS 203 First JAVA program !!! "); } }
Question 2 Numbers.java
What value is printed by this program?
public class Numbers { public static void main(String[] args) { int x = 100; int sum = 0; for (int i = 0; i <= x; i++) { sum = sum + i; } System.out.println(sum); } }
Question 3 Numbers.java
In Numbers.java, if we changed the loop condition to i < x (strictly less than), what would be printed?
for (int i = 0; i < x; i++) { // changed from i <= x sum = sum + i; }
Question 4 Fibonacci.java
If the user enters 5 for n, what is printed?
int first = 0; int second = 1; for (int i = 1; i <= n; i++) { System.out.print(first + " "); int next = first + second; first = second; second = next; }
Question 5 Fibonacci.java
In Fibonacci.java, what is the difference between System.out.print() and System.out.println()?
Question 6 Fibonacci.java
In Fibonacci.java, what is the purpose of Scanner scanner = new Scanner(System.in)?
Section 2: Classes, Constructors & Getters/Setters Lab 3
Book.java, BookTester.java — Object creation, accessors/mutators, state changes
Question 7 Book.java
In the Book class, all member variables are declared as private. Why?
public class Book { private String name; private int page; private boolean stock; // ... }
Question 8 BookTester.java
What does the following line output?
Book myBook = new Book("The Great Gatsby", 180, true); System.out.println("Book Name: " + myBook.getName());
Question 9 BookTester.java
After running these lines, what does myBook.getPage() return?
Book myBook = new Book("The Great Gatsby", 180, true); myBook.setPage(200);
Question 10 BookTester.java
What is the full console output of BookTester's "Updated Values" section?
myBook.setPage(200); myBook.setStock(false); System.out.println("--- Updated Values ---"); System.out.println("Book Name: " + myBook.getName()); System.out.println("Page Count: " + myBook.getPage()); System.out.println("In Stock: " + myBook.getStock());
Question 11 Book.java
In Book.java, is setPage(int p) an accessor or a mutator method?
public void setPage(int p) { page = p; }
Section 3: Inheritance & Overriding Lab 4
GameCharacter.java, BossCharacter.java, GameTester.java — extends, super(), @Override, method overloading
Question 12 GameCharacter.java
What does attack() return in the GameCharacter class?
public int attack() { return 10; }
Question 13 BossCharacter.java
BossCharacter overrides attack(). What does boss.attack() return?
@Override public int attack() { return super.attack() + 20; }
Question 14 GameTester.java
What is the output of this line?
BossCharacter boss = new BossCharacter("Dragon Lord", 500); System.out.println("Attack Damage: " + boss.attack());
Question 15 GameTester.java
BossCharacter has getType() (inherited, no args) and getType(boolean detailed) (its own). What does boss.getType() output?
// Inherited from GameCharacter: public String getType() { return "Game Character"; } // Defined in BossCharacter (different signature — has a parameter): public String getType(boolean detailed) { ... }
Question 16 GameTester.java
What does boss.getType(true) return?
public String getType(boolean detailed) { if (detailed == true) { return "Boss Character with High Damage"; } return "Boss Character"; }
Question 17 GameTester.java
What does boss.getType(false) return?
Question 18 BossCharacter.java
The BossCharacter has getType() (no args, inherited) and getType(boolean) (its own). This is an example of:
Question 19 BossCharacter.java
Why does BossCharacter's constructor call super(name, health)?
public BossCharacter(String name, int health) { super(name, health); }
Question 20 GameCharacter.java
Can BossCharacter directly access name or health from GameCharacter?
// In GameCharacter: private String name; private int health;
Section 4: Inheritance & Method Calls Lab 6
Calculator.java, ScientificCalculator.java, CalculatorTester.java — super, inherited methods, division
Question 21 Calculator.java
Given Calculator calc = new Calculator(10, 3), what does calc.add() return?
public int add() { return this.num1 + this.num2; }
Question 22 Calculator.java
Given Calculator calc = new Calculator(10, 3), what does calc.divide() return?
public double divide() { if (this.num2 == 0) { return 0.0; } return (double) this.num1 / this.num2; }
Question 23 Calculator.java
What does calc.divide() return if num2 is 0?
Question 24 Calculator.java
Why does the divide() method cast this.num1 to (double)?
return (double) this.num1 / this.num2;
Question 25 ScientificCalculator.java
ScientificCalculator extends Calculator. Can a ScientificCalculator object call add()?
public class ScientificCalculator extends Calculator { public ScientificCalculator(int a, int b) { super(a, b); } // only defines square_root() and exponentiation() }
Question 26 ScientificCalculator.java
Given ScientificCalculator sc = new ScientificCalculator(9, 2), what does sc.square_root() return?
public double square_root() { return Math.sqrt(get_first_number()); }
Question 27 ScientificCalculator.java
Given ScientificCalculator sc = new ScientificCalculator(9, 2), what does sc.exponentiation() return?
public double exponentiation() { return Math.pow(get_first_number(), get_second_number()); }
Question 28 ScientificCalculator.java
In square_root(), why does ScientificCalculator call get_first_number() instead of directly accessing num1?
Question 29 Calculator.java
After running this code, what does calc.get_first_number() return?
Calculator calc = new Calculator(10, 5); calc.set_first_number(25);
Section 5: Loop & Logic Tracing Homework 1
hw1_strachan.java — trace for loops, while loops, conditionals, and string processing
Question 30 hw1_strachan.java — numSteps
Trace through numSteps(14). What is returned?
public static int numSteps(int n) { int steps = 0; while (n > 0) { if (n % 2 == 0) { n = n / 2; } else { n = n - 1; } steps++; } return steps; } // n: 14→7→6→3→2→1→0 steps: 6
Question 31 hw1_strachan.java — countVowelsConsonants
What does countVowelsConsonants("hello") print?
public static void countVowelsConsonants(String s) { int v = 0; int c = 0; s = s.toLowerCase(); for (int i = 0; i < s.length(); i++) { char letter = s.charAt(i); if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') { v++; } else if (letter >= 'a' && letter <= 'z') { c++; } } System.out.println(v + "," + c); } // "hello" → h(c), e(v), l(c), l(c), o(v)
Question 32 hw1_strachan.java — countVowelsConsonants
What does countVowelsConsonants("java") print?
Question 33 hw1_strachan.java — blazerNumber
The blazerNumber method sums divisors of n (less than n). It returns true if the sum does NOT equal n, false if it does. What does blazerNumber(6) return?
// Divisors of 6 less than 6: 1, 2, 3 // Sum = 1 + 2 + 3 = 6 // sum == n → return false (it IS a "perfect number")
Question 34 hw1_strachan.java — findLargestPrime
What does findLargestPrime(20) return?
public static int findLargestPrime(int maxNum) { for (int i = maxNum; i > 1; i--) { boolean isPrime = true; for (int j = 2; j < i; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { return i; } } return 0; } // Checks 20(no), 19(yes!) → returns 19
Question 35 hw1_strachan.java — countVowelsConsonants
The countVowelsConsonants method calls s.toLowerCase() before processing. Why?

Your Results