0% found this document useful (0 votes)
6 views2 pages

Java Output Prediction Exercises Guide

The document contains a series of Java output prediction exercises, each featuring code snippets and corresponding questions about their outputs. Examples include loops, conditionals, string comparisons, and user input handling. The exercises are designed to test understanding of Java programming concepts and expected outputs.

Uploaded by

kihgjy
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)
6 views2 pages

Java Output Prediction Exercises Guide

The document contains a series of Java output prediction exercises, each featuring code snippets and corresponding questions about their outputs. Examples include loops, conditionals, string comparisons, and user input handling. The exercises are designed to test understanding of Java programming concepts and expected outputs.

Uploaded by

kihgjy
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

Java Output Prediction Exercises

Example 1
int x = 5;
while (x > 0) {
[Link](x + " ");
x = x - 2;
}
■ Question: What will be the output?

Example 2
for (int i = 1; i <= 4; i++) {
[Link](i * i + " ");
}
■ Question: What does this print?

Example 3
String a = "Hello";
String b = "hello";

if ([Link](b)) {
[Link]("Equal");
} else {
[Link]("Not Equal");
}

if ([Link](b)) {
[Link]("Ignore Case Equal");
}
■ Question: What is printed?

Example 4
int a = 4, b = 7, c = 10;

if (a < b && b < c) {


[Link]("True Path");
} else if (a > c || b == 7) {
[Link]("Else If Path");
} else {
[Link]("False Path");
}
■ Question: Which branch will run?

Example 5
Scanner input = new Scanner([Link]);
int sum = 0;

for (int i = 1; i <= 3; i++) {


[Link]("Enter a number: ");
int num = [Link]();
sum = sum + num;
}
[Link]("Total = " + sum);
■ Question: If the user enters 2, 3, 4, what will the program print?

Example 6
for (int i = 10; i > 0; i = i - 3) {
[Link](i + " ");
}
■ Question: What numbers are printed?

Example 7
int count = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
count++;
}
}
[Link](count);
■ Question: How many even numbers from 1 to 5 are counted?

Example 8
int n = 3;
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
[Link](fact);
■ Question: What factorial value will this print?

Example 9
int a = 8;
int b = 12;
[Link](a > b || a % 2 == 0);
[Link](b < 10 && b % 3 == 0);
■ Question: What are the two boolean values printed?

Example 10
String word = "Java";
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i) + " ");
}
■ Question: What will be printed?

Common questions

Powered by AI

The factorial value printed will be `6`. The loop calculates 3! = 3 * 2 * 1 = 6 .

The output will be `Not Equal` followed by `Ignore Case Equal`. The `equals` method checks for exact match including case, while `equalsIgnoreCase` ignores case differences .

The loop will print `1 4 9 16 `. It calculates the square of each number from 1 to 4 and prints the results .

The first boolean value is `true` because `a % 2 == 0` is true. The second boolean value is `false` because `b < 10` is false, making the entire expression false .

The printed numbers will be `10 7 4 1 `. The loop decrements i by 3 starting from 10, printing each value until i is no longer greater than 0 .

The count will be `2`. The loop checks each number from 1 to 5, counting 2 and 4 as the even numbers .

The sequence of characters printed is `J a v a `. The for-loop iterates over each character in the string "Java" and prints it followed by a space .

The "True Path" branch will execute because both conditions `a < b` and `b < c` are true .

The program will print `Total = 9`. The loop runs three times and adds the entered numbers 2, 3, and 4 to `sum`, which results in 9 .

The code will print `5 3 1 `. This is because the loop starts with x=5 and decrements x by 2 until x is not greater than 0 .

You might also like