What does this loop print?
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
1
2
3
Q: What does int x = 5 + 3 * 2; set x to?
11
System.out.println("Hello, world")
missing a semicolon
System.out.println("Hello, world");
Q:
What does this print?
What does this print?
5
What's wrong with this loop?
int x = 0;
while (x < 5)
System.out.println(x);
x++;
Only the first line is inside the loop. Fix it with curly braces:
while (x < 5) {
System.out.println(x);
x++;
}
Q: What does System.out.println(10 % 4); print?
2
Q:
What is printed by this?
int a = 5;
double b = a;
System.out.println(b);
5.0
int x = 5;
if (x > 0 && x < 10) {
System.out.println("x is between 1 and 9");
}
else{
System.out.println("Bye");
}
x is between 1 and 9
Q:
What is the output?
String a = "Java";
String b = "Java";
System.out.println(a == b);
true
Fix the bug in this for loop so it prints only even numbers from 2 to 10.
for (int i = 2; i <= 10; i++)
if (i % 2 = 0)
System.out.println(i);
Use == instead of = for comparison:
What will this print?
int x = 8;
if (x % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
Even
String s = "Hello";
s = s + " World";
System.out.println(s.length());
11
How do you set up a while loop?
while (condition is true) {
do this;
}
What’s wrong here?
String name = "Alice";
if (name = "Alice") {
System.out.println("Welcome!");
}
Use .equals() instead of =
What will this code print?
int i = 5;
while (i >= 0) {
System.out.print(i + " ");
i--;
}
5 4 3 2 1 0
int num1=7;
int num2=2;
What does System.out.println(num1 / num2); print in Java?
3 (integer division — decimal is dropped)
if x == 10 {
System.out.println("Ten!");
}
missing parenthesis around x==10
Q:
Whats wrong with this
String 2ndPlayer;
No — variable names cannot start with a digit
What does this print?
String text = "Computer";
System.out.println(text.substring(0, 4));
Comp
int x = 3;
while (x < 10) {
System.out.println("x: " + x);
x += 2;
}
x: 3
x: 5
x: 7
x: 9
: What does this print?
int x = 3;
int y = 4;
if (x * y > 10) {
System.out.println(x + y);
} else {
System.out.println(x - y);
}
7
(Because 3 * 4 = 12, which is greater than 10 → prints x + y → 3 + 4)
scannner scanner = new Scanner(System.in)
System.out.print("Enter your age: ")
int age = input.nextLine()
if (age >= 18) {
System.out.println("You're an adult!")
}
1. second scanner should be capital
2.semicolon
3.nextInt
4.semicolon
int x = 0;
while (x < 4) {
System.out.println(x);
x--;
}
infinitely
1000-Point Question – What Does This Print?
javaCopyEdit
Programming