Skip to content

Java Basics Quiz

Test your Java skills! This quiz checks if you’re ready for FRC programming.

Each question shows the answer immediately so you can learn as you go.


What does this code print?

int score = 10;
score = score + 5;
System.out.println(score);
Show Answer

Answer: 15

Why?

  • First line: score is set to 10.
  • Second line: score + 5 = 15, then score = 15
  • Third line: Prints 15.

Think of it like:

  • You have a box labeled “score”.
  • Put 10 in the box.
  • Add 5 more to the box.
  • Now the box has 15.

What does this code print?

String name = "FRC";
String greeting = "Hello " + name;
System.out.println(greeting);
Show Answer

Answer: Hello FRC

Why?

  • + joins strings together (called concatenation)
  • "Hello " + "FRC" = "Hello FRC"

Think of it like:

  • Glue two pieces of paper together.
  • One says “Hello ”, the other says “FRC”.
  • After gluing: “Hello FRC”.

What does this code print?

int speed = 7;
if (speed > 5) {
System.out.println("Fast!");
} else {
System.out.println("Slow");
}
Show Answer

Answer: Fast!

Why?

  • speed is 7
  • 7 > 5 is true.
  • Run the code inside if
  • Skip the code inside else

Think of it like:

  • “Is the speed greater than 5?”.
  • Yes! Print “Fast!”.
  • No need to check the “else” part.

What does this code print?

int score = 85;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("C");
}
Show Answer

Answer: B

Why?

  • score is 85
  • 85 >= 90? No, skip first block.
  • 85 >= 80? Yes! Print “B”.
  • Skip the else block.

Think of it like:

  • Check conditions in order.
  • Stop at the first one that’s true.
  • Don’t check the rest.

How many times does this print?

for (int i = 0; i < 3; i++) {
System.out.println("Hello");
}
Show Answer

Answer: 3 times

Why?

  • i = 0: Print (i is 0, 0 < 3)
  • i = 1: Print (i is 1, 1 < 3)
  • i = 2: Print (i is 2, 2 < 3)
  • i = 3: Stop (i is 3, 3 < 3 is false)

Think of it like:

  • Count: 0, 1, 2.
  • That’s 3 numbers.
  • Print 3 times

What does this code print?

int count = 3;
while (count > 0) {
System.out.println(count);
count = count - 1;
}
Show Answer

Answer:

3
2
1

Why?

  • count = 3: 3 > 0? Yes, print 3, then count becomes 2.
  • count = 2: 2 > 0? Yes, print 2, then count becomes 1.
  • count = 1: 1 > 0? Yes, print 1, then count becomes 0.
  • count = 0: 0 > 0? No, stop.

Think of it like:

  • Keep going while the condition is true.
  • Stop when it becomes false.

What does this code print?

public int add(int a, int b) {
return a + b;
}
// Somewhere else:
int result = add(3, 4);
System.out.println(result);
Show Answer

Answer: 7

Why?

  • Call add(3, 4)
  • a = 3, b = 4
  • return 3 + 4 = 7.
  • result = 7
  • Print 7

Think of it like:

  • A method is a recipe.
  • Give it ingredients (3 and 4)
  • It cooks them together (adds them)
  • Gives you the result (7)

What does this code print?

boolean hasPermission = true;
boolean isAdmin = false;
if (hasPermission && isAdmin) {
System.out.println("Access granted");
} else {
System.out.println("Access denied");
}
Show Answer

Answer: Access denied

Why?

  • hasPermission is true.
  • isAdmin is false.
  • true && false = false.
  • Run the else block.

Think of it like:

  • && means “both must be true”.
  • One is false, so the whole thing is false.
  • Like saying “I have a ticket AND I’m VIP”.
  • If you don’t have VIP, you can’t get in.

What does this code print?

int[] scores = {10, 20, 30};
System.out.println(scores[1]);
Show Answer

Answer: 20

Why?

  • Arrays start at index 0.
  • scores[0] = 10.
  • scores[1] = 20.
  • scores[2] = 30.

Think of it like:

  • Mailboxes on a street.
  • Address 0 has mail (10)
  • Address 1 has mail (20)
  • Address 2 has mail (30)
  • You check address 1: you get 20.

What does this code print?

public class Robot {
public String name;
public Robot(String robotName) {
name = robotName;
}
}
Robot myRobot = new Robot("MARS");
System.out.println(myRobot.name);
Show Answer

Answer: MARS

Why?

  • Create a new Robot with name “MARS”.
  • Constructor sets name = "MARS"
  • Access myRobot.name
  • Print “MARS”

Think of it like:

  • A class is a blueprint for a house.
  • An object is the actual house built from the blueprint.
  • You can build many houses from one blueprint.
  • Each house can have different features.

What happens with this code?

String name = null;
System.out.println(name.length());
Show Answer

Answer: NullPointerException (crashes!)

Why?

  • name is null (nothing)
  • Can’t call methods on nothing.
  • Java throws an error.

How to fix:

String name = null;
if (name != null) {
System.out.println(name.length());
}

Think of it like:

  • Trying to open a door that doesn’t exist.
  • You can’t do it!
  • Check if the door exists first.

What does this code print?

int count = 5;
count++;
System.out.println(count);
Show Answer

Answer: 6

Why?

  • count++ means “add 1 to count”.
  • Same as count = count + 1
  • 5 + 1 = 6

Think of it like:

  • A shortcut for adding 1.
  • programmers are lazy!
  • ++ is shorter than + 1

What does this code print?

int x = 10 % 3;
System.out.println(x);
Show Answer

Answer: 1

Why?

  • % is the modulo operator (remainder)
  • 10 ÷ 3 = 3 with remainder 1.
  • So 10 % 3 = 1

Think of it like:

  • You have 10 cookies.
  • Divide into groups of 3.
  • You get 3 groups (9 cookies)
  • 1 cookie left over.
  • That’s the remainder!

What does this code print?

String a = new String("hello");
String b = new String("hello");
if (a == b) {
System.out.println("Same");
} else {
System.out.println("Different");
}
Show Answer

Answer: Different

Why?

  • == checks if they’re the SAME object.
  • .equals() checks if they have the SAME VALUE.
  • a and b are different objects with same value.

How to fix:

if (a.equals(b)) {
System.out.println("Same"); // This prints!
}

Think of it like:

  • Two different cookies.
  • Both are chocolate chip.
  • They’re not the SAME cookie.
  • But they’re the same TYPE of cookie.

What does this code print?

int x = 5;
if (true) {
int x = 10; // Line A
System.out.println(x); // Line B
}
System.out.println(x); // Line C
Show Answer

Answer: This code doesn’t compile!

Why?

  • Can’t declare x twice in the same scope.
  • The outer x is still visible inside the if.
  • Java doesn’t know which x you mean.

Fix:

int x = 5;
if (true) {
x = 10; // Just assign, don't redeclare
}
System.out.println(x); // Prints 10

Think of it like:

  • You can’t have two people with the same name in the same room.
  • But you can have different people with same name in different rooms.

Count your correct answers:

  • 13-15 correct: 🌟 Amazing! You’re ready for advanced FRC programming!
  • 10-12 correct: 👍 Great job! You understand most Java basics.
  • 7-9 correct: 👌 Good start! Review the questions you missed.
  • Below 7: 📚 Keep learning! Try the Beginner Learning Path.

If you got 13-15 correct:

If you got 7-12 correct:

If you got below 7:


Write your own code:

  1. Make a calculator that adds two numbers.
  2. Write a program that counts to 10.
  3. Create a method that finds the larger of two numbers.
  4. Build an array of your favorite foods.

Test yourself:

// Challenge 1: Make this print "Hello World" 5 times
// Your code here
// Challenge 2: Make a method that returns true if a number is even
// Your code here
// Challenge 3: Find the largest number in this array: {5, 2, 9, 1, 7}
// Your code here

Remember: Programming is a skill. Keep practicing and you’ll get better!

Stuck on a question? Ask for help - we’re here to help you learn!