Challenge - 5 Problems
Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Java code using variables?
Consider the following Java code. What will it print when run?
Java
public class Main { public static void main(String[] args) { int apples = 5; int oranges = 3; int total = apples + oranges; System.out.println("Total fruits: " + total); } }
Attempts:
2 left
💡 Hint
Variables store values so you can use them in calculations.
✗ Incorrect
The variables apples and oranges hold numbers 5 and 3. Adding them gives 8, which is printed.
🧠 Conceptual
intermediate1:30remaining
Why do we use variables in programming?
Which of the following best explains why variables are needed in programming?
Attempts:
2 left
💡 Hint
Think about how you keep track of things in real life by giving them names.
✗ Incorrect
Variables let us store data with names so we can use and update it throughout the program.
❓ Predict Output
advanced2:00remaining
What is the output when variables are updated?
Look at this Java code. What will it print?
Java
public class Main { public static void main(String[] args) { int score = 10; score = score + 5; score = score - 3; System.out.println("Final score: " + score); } }
Attempts:
2 left
💡 Hint
Follow the changes to the variable step by step.
✗ Incorrect
Starting at 10, adding 5 makes 15, then subtracting 3 results in 12.
🔧 Debug
advanced1:30remaining
Identify the error related to variables
This Java code tries to print a variable but causes an error. What is the error?
Java
public class Main { public static void main(String[] args) { int number = 7; System.out.println(numbr); } }
Attempts:
2 left
💡 Hint
Check the spelling of the variable name used in print.
✗ Incorrect
The variable 'numbr' is misspelled; the declared variable is 'number'. This causes a compilation error.
🧠 Conceptual
expert2:00remaining
How do variables improve program flexibility?
Which statement best describes how variables help make programs flexible?
Attempts:
2 left
💡 Hint
Think about how changing a variable's value affects the program.
✗ Incorrect
Variables let programs handle different data over time, making them adaptable and reusable.