In: Computer Science
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!!
QUESTION 1
Consider the following program segment:
int i = 2;
int n = 18;
while (i < n)
{
if ((i % 2) == 0)
i++;
else
n--;
}
System.out.println( i );
What is the value of n when the above program segment is done executing?
A. |
n is: 18. |
|
B. |
n is: 3. |
|
C. |
Infinite Loop. i will not change. |
|
D. |
No Output. |
QUESTION 2
Consider the following program segment:
int i = 2;
int n = 18;
while (i < n)
{
if ((i % 2) == 0)
i++;
else
n--;
}
System.out.println( i );
What is the output of the above program segment?
A. |
There is no output. |
|
B. |
The output is 2. |
|
C. |
The output is 18. |
|
D. |
The output is 3. |
QUESTION 3
Consider the following program segment:
int i = 1;
int n = 10;
while (i < n)
{
if ((i % 2) != 0)
i++;
}
System.out.println ( i );
And what are the values of i and n after execution?
A. |
i will be 11. n will be 10. |
|
B. |
i will be 11. n will be 1. |
|
C. |
Infinite Loop: i is stuck at 2 and n stays at 10. |
|
D. |
No Output |
QUESTION 4
Consider the following section of code:
int i = 1;
int j = 3;
while (i < 15 && j < 20)
{
i++;
j += 2;
}
System.out.println (i + j);
And what are the values of i and j after execution?
A. |
i = 10, j = 21 |
|
B. |
i = 11, j = 22 |
|
C. |
i = 15, j = 20 |
|
D. |
i = 16, j = 21 |
QUESTION 5
Consider the following section of code:
int i = 1;
int j = 3;
while (i < 15 && j < 20)
{
i++;
j += 2;
}
System.out.println (i + j);
What is the output of the above program segment?
A. |
The output will be 31. |
|
B. |
The output will be 33. |
|
C. |
The output will be 34. |
|
D. |
The output will be 35. |
QUESTION 6
Consider the following section of code:
int x = 10;
do
{
System.out.println( x );
x -= 3;
}
while (x > 0);
And what is the value of x after execution?
A. |
The value of x after loop is done = -1. |
|
B. |
The value of x after loop is done = -2. |
|
C. |
The value of x after loop is done = 1. |
|
D. |
The value of x after loop is done = 2. |
QUESTION 7
Consider the following section of code:
int x = 10;
do
{
System.out.println( x );
x -= 3;
}
while (x > 0);
What is the output of the following program segment?
A. |
-2 |
|
B. |
10 9 8 7 6 5 4 3 2 1 |
|
C. |
1 4 7 10 |
|
D. |
10 7 4 1 |
QUESTION 8
Consider the following section of code:
int i = 1;
int n = 0;
while (i <= n)
{
System.out.print( i );
i++;
}
And what are the values of i and n after execution?
A. |
i = 1 and n = 0. |
|
B. |
i = 2 and n = 0. |
|
C. |
i = 0 and n = 0. |
|
D. |
i = -1 and n = 0. |
QUESTION 9
Consider the following section of code:
int i = 1;
int n = 0;
while (i <= n)
{
System.out.print( i );
i++;
}
What is the output of the following program segment?
A. |
1, 2, 3, ... |
|
B. |
1 |
|
C. |
0 |
|
D. |
No output. |
Question 1:
For the first iteration, 2<18, and 2%2 == 0 returns true, so 'i' is incremented. After that for every iteration, i % 2 won't be equal to zero and hence 'n' wud keep on decreasing. When n=3, loop will end. So, after executing, both i and n are equal to 3.
So, (B) n is: 3 is the correct answer.
Question 2:
The code is same as question 1, so the explanation remains the same. 'i' won't increase in value after the first iteration, so it will be equal to 3 at the end.
So, (D) The output is 3 is the correct answer.
Question 3:
During the first iteration, i<n returns true so the loop starts. (1%2 != 0) returns true, so the value of i is incremented to 2. For all the next iterations, the i<n remains true, whereas ( i%2 != 0 ) returns false because i=2. Due to this, the value of i never changes and the loop goes on infintely. The value of 'n' never changes.
So, (C) Infinite Loop: i is stuck at 2 and n stays at 10 is the correct answer.
Question 4:
At start, the condition i<15 and j<20 satisfies so the loop starts. For every iteration, we increment i by 1 and j by 2.
Value after iteration1: i=2 j=5
Value after iteration2: i=3 j=7
Value after iteration3: i=4 j=9
Value after iteration4: i=5 j=11
Value after iteration5: i=6 j=13
Value after iteration6: i=7 j=15
Value after iteration7: i=8 j=17
Value after iteration8: i=9 j=19
Value after iteration9: i=10 j=21
After the 9th iteration, the condition of loop (i<15 && j<20) will return false and the loop will end. Hence, the final values are 10 and 21.
So, (A) i = 10, j = 21 is the correct answer.
Question 5:
This has the same code as in Q4. At the end i is 10 and j is 21. Their sum will be 10+21 = 31.
So, (A) The output will be 31 is the correct answer.
Question 6:
Value of x during iteration 1: 10
Value of x during iteration 2: 7
Value of x during iteration 3: 4
Value of x during iteration 4: 1
After this, this value is decremented by 3 and it becomes -2. As -2 is not greater than 0 so the while condition gets false and loop stops.
So, (B) The value of x after loop is done = -2 is the correct answer.
Question 7:
The program code is same as Q6. For every iteration, the value of x is printed, which are 10, 7, 4, and 1.
So, (D) 10
7
4
1 is the correct answer.
Question 8:
The entry condition of the loop is i<=n. This never gets true because i is 1 and n is 0. The loop never gets executed and the value of both the variables do not change.
So, (A) i = 1 and n = 0 is the correct answer.
Question 9:
The problem code is same as Q8. As discussed there, the while loop never gets executed so no print statement gets executed. The program gives no output.
So, (D) No output is the correct answer.