0% found this document useful (0 votes)
7 views3 pages

Java Loops Conditionals Worksheet

The document contains a series of Java code snippets designed to test the understanding of loops, conditionals, and expressions. Each question requires predicting the output of the code, covering various concepts such as if-else statements, for and while loops, switch cases, and arithmetic operations. The questions range from simple comparisons to nested loops and calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Java Loops Conditionals Worksheet

The document contains a series of Java code snippets designed to test the understanding of loops, conditionals, and expressions. Each question requires predicting the output of the code, covering various concepts such as if-else statements, for and while loops, switch cases, and arithmetic operations. The questions range from simple comparisons to nested loops and calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Worksheet: Predict the Output (Loops, Conditionals, Expressions)

Instructions: Predict the output of the following Java code snippets. Focus on how loops, if-else

statements, and expressions are evaluated.

Question 1:
int a = 10, b = 20;
if (a < b)
System.out.println("A is smaller");
else
System.out.println("B is smaller");

Question 2:
int a = 15;
if (a % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");

Question 3:
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}

Question 4:
int i = 0;
while (i < 3) {
System.out.println("Hello");
i++;
}

Question 5:
int i = 5;
do {
System.out.println(i);
i--;
} while (i > 0);

Question 6:
int x = 10;
if (x > 5 && x < 15)
System.out.println("x is in range");
Question 7:
int num = 7;
switch(num) {
case 5:
System.out.println("Five");
break;
case 7:
System.out.println("Seven");
break;
default:
System.out.println("Default");
}

Question 8:
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
System.out.println("Sum: " + sum);

Question 9:
int a = 4, b = 6;
System.out.println((a > b) ? a : b);

Question 10:
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= i) {
System.out.print("* ");
j++;
}
System.out.println();
i++;
}

Question 11:
for (int i = 5; i > 0; i--) {
if (i % 2 == 0)
continue;
System.out.print(i + " ");
}
Question 12:
int count = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 2; j++) {
count++;
}
}
System.out.println("Count: " + count);

Question 13:
int x = 3;
switch (x + 1) {
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
default:
System.out.println("Default");
}

Question 14:
int i = 1;
int sum = 0;
while (i <= 5) {
sum += i * i;
i++;
}
System.out.println("Sum of squares: " + sum);

You might also like