Loop Practice Java
Loop Practice Java
7. What are the values of i and j after the following code snippet is run?
int i = 10;
int j = 20;
int count = 0;
while (count < 5)
{
i = 2*i;
i = i + 1;
j = j - 1;
count++;
}
System.out.println("i = " + i + ", j = " + j);
8. What is the output of the following code snippet?
double r = 1;
double b = 2;
int i = 16;
while (i > 0)
{
if (i % 2 == 0) // i is even
{
b = b * b;
i = i / 2;
}
else
{
r = r * b;
i = i - 1;
}
}
System.out.println("r = " + r);
9. What for loop can be used in the indicated area so the code will print:
****
***
**
*
10.The following loop does not work as intended. What error exists?
for (int i = 0; i < 10; i++);
{
System.out.println("Loop Execution");
}
11.What is the output of the following code snippet?
int f1 = 0;
int f2 = 1;
int fRes;
System.out.print(f1 + " ");
System.out.print(f2 + " ");
for (int i = 1; i < 10; i++)
{
fRes = f1 + f2;
System.out.print(fRes + " ");
f1 = f2;
f2 = fRes;
}
System.out.println();