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

Java Test 1 Prep Read Code

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

swarit22agrawal
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)
5 views2 pages

Java Test 1 Prep Read Code

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

swarit22agrawal
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/ 2

Java Output Prediction Exercises

Example 1
int x = 5;
while (x > 0) {
System.out.print(x + " ");
x = x - 2;
}
■ Question: What will be the output?

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

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

if (a.equals(b)) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}

if (a.equalsIgnoreCase(b)) {
System.out.println("Ignore Case Equal");
}
■ Question: What is printed?

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

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


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

Example 5
Scanner input = new Scanner(System.in);
int sum = 0;

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


System.out.print("Enter a number: ");
int num = input.nextInt();
sum = sum + num;
}
System.out.println("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) {
System.out.print(i + " ");
}
■ Question: What numbers are printed?

Example 7
int count = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
count++;
}
}
System.out.println(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;
}
System.out.println(fact);
■ Question: What factorial value will this print?

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

Example 10
String word = "Java";
for (int i = 0; i < word.length(); i++) {
System.out.print(word.charAt(i) + " ");
}
■ Question: What will be printed?

You might also like