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 classFirstJavaProgram {
public static voidmain(String[] args) {
System.out.println("CS 203 First JAVA program !!! ");
}
}
Question 2 Numbers.java
What value is printed by this program?
public classNumbers {
public static voidmain(String[] args) {
intx = 100;
intsum = 0;
for (inti = 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 (inti = 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?
intfirst = 0;
intsecond = 1;
for (inti = 1; i <= n; i++) {
System.out.print(first + " ");
intnext = 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)?
BossCharacter has getType() (inherited, no args) and getType(boolean detailed) (its own). What does boss.getType() output?
// Inherited from GameCharacter:publicStringgetType() { return"Game Character"; }
// Defined in BossCharacter (different signature — has a parameter):publicStringgetType(booleandetailed) { ... }
Question 16 GameTester.java
What does boss.getType(true) return?
publicStringgetType(booleandetailed) {
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)?
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 intfindLargestPrime(intmaxNum) {
for (inti = maxNum; i > 1; i--) {
booleanisPrime = true;
for (intj = 2; j < i; j++) {
if (i % j == 0) { isPrime = false; break; }
}
if (isPrime) { return i; }
}
return0;
}
// Checks 20(no), 19(yes!) → returns 19