int x = 7; if (x > 5) System.out.print("A"); System.out.print("B"); System.out.print("C");
Question 12
What is printed?
int a = 5, b = 10, c = 15; if (a < b) if (b < c) System.out.print("X"); else System.out.print("Y"); else System.out.print("Z");
Question 13
What is printed?
int x = 8; if (x > 10) System.out.print("A"); else if (x > 5) System.out.print("B"); else if (x > 3) System.out.print("C"); else System.out.print("D");
Question 14
What is the value of result?
boolean a = true, b = false, c = true; boolean result = a && b || c;
Question 15
What is printed?
int x = 5; if (x > 3 && x++ < 10) { System.out.print(x); }
Question 16
What is printed?
int x = 5; if (x < 3 && x++ < 10) { System.out.print("A"); } System.out.print(x);
Question 17
Which is equivalent to: !(a && b)?
Question 18
Which is equivalent to: !(x >= 5 || y < 3)?
Question 19
What is printed?
int score = 85; String grade; if (score >= 90) grade = "A"; else if (score >= 80) grade = "B"; else if (score >= 70) grade = "C"; else grade = "F"; System.out.println(grade);
Question 20
What is printed?
int x = 10, y = 20; boolean result = (x == 10) == (y == 20); System.out.println(result);
Question 21
What is printed?
int n = 0; if (n != 0 && 10 / n > 1) { System.out.print("A"); } else { System.out.print("B"); }
Question 22
What is the value of x after execution?
int x = 5; int y = 3; x = (y > 2) ? x * 2 : x + 2;
Iteration / Loops (Questions 23-35)
Question 23
What is printed?
for (int i = 1; i <= 5; i++) { if (i == 3) continue; System.out.print(i + " "); }
Question 24
What is printed?
for (int i = 1; i <= 5; i++) { if (i == 3) break; System.out.print(i + " "); }
Question 25
How many times does "Hello" print?
int count = 0; while (count < 10) { System.out.println("Hello"); count += 3; }
Question 26
What is the value of sum?
int sum = 0; for (int i = 1; i <= 100; i++) { if (i % 10 == 0) sum += i; }
Question 27
What is printed?
int x = 5; while (x > 0) { System.out.print(x + " "); x -= 2; }
Question 28
What is the value of total?
int total = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { total++; } }
Question 29
What is printed?
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
Question 30
What is the final value of n?
int n = 64; int count = 0; while (n > 1) { n /= 2; count++; } System.out.println(count);
Question 31
What is printed?
int i = 0; while (i < 5) { i++; if (i % 2 == 0) continue; System.out.print(i + " "); }
Question 32
What is the value of product?
int product = 1; for (int i = 1; i <= 5; i++) { product *= i; }
Question 33
How many times does the inner loop body execute?
for (int i = 0; i < 4; i++) { for (int j = i; j < 4; j++) { // body } }
Question 34
What is printed?
int x = 1; do { System.out.print(x + " "); x *= 2; } while (x < 20);
Question 35
What is the value of sum?
int sum = 0; for (int i = 10; i >= 1; i--) { if (i % 3 == 0) sum += i; }
String Methods & Manipulation (Questions 36-45)
Question 36
What is printed?
String s = "COMPUTER"; System.out.println(s.substring(3, 6));
String s = "12345"; int sum = 0; for (int i = 0; i < s.length(); i++) { sum += Integer.parseInt(s.substring(i, i + 1)); } System.out.println(sum);
Methods & Return Values (Questions 46-50)
Question 46
What is printed?
public static int mystery(int n) { if (n < 10) return n; return mystery(n / 10) + n % 10; } // In main: System.out.println(mystery(1234));
Question 47
What is printed?
public static void change(int x) { x = x * 2; } // In main: int num = 5; change(num); System.out.println(num);
Question 48
What is printed?
public static int compute(int a, int b) { if (b == 0) return a; return compute(b, a % b); } // In main: System.out.println(compute(48, 18));
Question 49
What is the return type of a method that doesn't return anything?
Question 50
What is printed?
public static int process(int n) { int result = 0; while (n > 0) { result = result * 10 + n % 10; n /= 10; } return result; } // In main: System.out.println(process(12345));
🎉 Unit 1 Complete!
Review your answers above or click Reset to practice again.
Contact form
Choosing a selection results in a full page refresh.