1. What is the output of the following Java code? int num = 10; boolean found =
ID: 3575965 • Letter: 1
Question
1. What is the output of the following Java code?
int num = 10;
boolean found = false;
do
{
System.out.print(num + " ");
if (num <= 2)
found = true;
else
num = num – 4;
}
while (num > 0 && !found);
System.out.println();
2. What is the output of the following Java code?
int x = 1;
int count;
for (count = 0; count <= 8; count++)
x = x * count;
System.out.println(x);
3. What is the output of the following Java code?
int j;
for (j = 10; j <= 10; j++)
System.out.print(j + " ");
System.out.println(j);
4. What is the output of the following Java code?
int count = 10;
int num = 30;
while (count > 1)
{
num = num + 2;
count--;
}
System.out.println(num + " " + count);
5. What is the value of counter after the following statements execute?
counter = 1;
while (counter <= 64)
counter = 2 * counter;
Explanation / Answer
1.) 10 6 2
Explanation:
num found
10 flase
6 false
2 true
After this the condition, while (num > 0 && !found) would fail, And hence the output.
2.) 0
Explanation:
In the loop, we are repeatedly multiplying with 0, and hence 0 will be printed as value of x.
3.) 10 11
Explanation:
The loop will be executed only once, when it will output 10. Loop will terminate when j=11 after first iteration.
So the value of j is 11 after loop is terminated, and hence 11 is printed.
4.) 48 1
Explanation:
count num
10 30
9 32
8 34
7 36
.
.
.
2 46
1 48
Now condition would evaluate to false, as count is no longer greater than 1. So 1 and 48 are final values for num and count.
5)128
The value of counter would increment as 1, 2, 4, 16, 64, 128 ...
And the condition would be evaluated to false when counter=128.
So after the above statements, counter would be 128.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.