Challenge - 5 Problems
Java Print Master
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 snippet?
Consider the following Java code. What will it print to the console?
Java
public class Main { public static void main(String[] args) { System.out.print("Hello"); System.out.print(" "); System.out.print("World"); } }
Attempts:
2 left
💡 Hint
System.out.print does not add a new line after printing.
✗ Incorrect
System.out.print prints text exactly as given without adding a new line. So the three print statements print "Hello", then a space, then "World" all on the same line.
❓ Predict Output
intermediate2:00remaining
What does this Java code print?
Look at this code. What is the exact output?
Java
public class Main { public static void main(String[] args) { System.out.print(5 + 3); System.out.print("+"); System.out.print(2); } }
Attempts:
2 left
💡 Hint
Remember that + between numbers adds, but + between strings concatenates.
✗ Incorrect
5 + 3 is evaluated first as 8, then the string "+" is printed, then 2 is printed. So the output is "8+2".
❓ Predict Output
advanced2:00remaining
What is the output of this Java code with mixed print and println?
What will this code print exactly?
Java
public class Main { public static void main(String[] args) { System.out.print("Start"); System.out.println("Middle"); System.out.print("End"); } }
Attempts:
2 left
💡 Hint
System.out.println adds a new line after printing, System.out.print does not.
✗ Incorrect
The first print prints "Start" without new line, then println prints "Middle" and adds a new line, then print prints "End" on the new line. So output is "StartMiddle" then new line, then "End".
❓ Predict Output
advanced2:00remaining
What is the output of this Java code with character and string prints?
What does this code print?
Java
public class Main { public static void main(String[] args) { System.out.print('A'); System.out.print("B"); System.out.print(67); } }
Attempts:
2 left
💡 Hint
Single quotes print the character, numbers print the number itself.
✗ Incorrect
System.out.print('A') prints character A, System.out.print("B") prints string B, System.out.print(67) prints number 67 as digits. So output is "AB67".
❓ Predict Output
expert2:00remaining
What is the output of this Java code with expressions inside print?
What will this code print exactly?
Java
public class Main { public static void main(String[] args) { System.out.print("Result: " + 2 + 3); System.out.print(" and " + (2 + 3)); } }
Attempts:
2 left
💡 Hint
String concatenation happens left to right, parentheses change order of operations.
✗ Incorrect
In the first print, "Result: " + 2 + 3 is evaluated left to right as string concatenation: "Result: 2" + 3 = "Result: 23". In the second print, (2 + 3) is evaluated first as 5, so output is " and 5". Combined output is "Result: 23 and 5".