Unit 1 Practice Exam: Methods & Control Structures

 

Unit 1: Methods & Control Structures

50 Questions • AP Exam Difficulty

Variables • Expressions • Conditionals • Loops • Strings • Math Class

0 Correct | 0 Incorrect | 0/50 Answered
Primitive Types & Expressions (Questions 1-10)
Question 1

What is printed?

int x = 17; int y = 5; System.out.println(x / y + " " + x % y);
Question 2

What is printed?

double d = 7 / 4; System.out.println(d);
Question 3

What is printed?

int a = 5; int b = 2; double result = (double) a / b; System.out.println(result);
Question 4

What is printed?

int x = 10; x += 5; x -= 3; x *= 2; System.out.println(x);
Question 5

What is printed?

int n = (int) 9.7 + (int) 2.3; System.out.println(n);
Question 6

What is printed?

int a = 3; int b = 4; int c = a++ + ++b; System.out.println(a + " " + b + " " + c);
Question 7

What is printed?

System.out.println(2 + 3 + "hello" + 4 + 5);
Question 8

What is the value of result?

int result = Math.abs(-5) + Math.min(3, 7) - Math.max(2, 4);
Question 9

Which expression generates a random integer from 5 to 15 inclusive?

Question 10

What is the value of x?

double x = Math.pow(2, 3) + Math.sqrt(16);
Boolean Expressions & Conditionals (Questions 11-22)
Question 11

What is printed?

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

What is printed?

String s1 = "Hello"; String s2 = "Hello"; String s3 = new String("Hello"); System.out.println((s1 == s2) + " " + (s1 == s3));
Question 38

What is printed?

String s = "programming"; System.out.println(s.indexOf("mm") + " " + s.indexOf("z"));
Question 39

What is printed?

String s = "Java"; s.toUpperCase(); System.out.println(s);
Question 40

What is printed?

String s = "ABCDE"; int result = s.compareTo("ABCFG"); System.out.println(result < 0);
Question 41

What is the value of count?

String s = "Mississippi"; int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 's') { count++; } } System.out.println(count);
Question 42

What is printed?

String s = "Hello World"; System.out.println(s.substring(0, 5).toLowerCase());
Question 43

What is printed?

String s = "racecar"; String reversed = ""; for (int i = s.length() - 1; i >= 0; i--) { reversed += s.charAt(i); } System.out.println(s.equals(reversed));
Question 44

What is printed?

String s = " Hello "; System.out.println("[" + s.trim() + "]");
Question 45

What is printed?

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