i have a bunch of questions that i need answers to and understanding. i cant ful
ID: 3663873 • Letter: I
Question
i have a bunch of questions that i need answers to and understanding. i cant fully get my head wrap aroun them. Also can you provide a very brief explanation on why the answer is that! Thank you all!
1.) What output is produced by the following code fragment?
for (int val = 200; val >= 0; val -= 1)
if (val % 4 != 0)
System.out.println (val);
ANSWER CHOICES ARE
A.) all values from 200 (inclusive) to 0 (exclusive), except those that are evenly divisble by 4
B.) values from 200 (inclusive) to 0 (exclusive) that are evenly divisble by 4
C.) values from 200 (inclusive) to 0 (inclusive) that are evenly divisble by 4
D.) all values from 200 (inclusive) to 0 (inclusive), except those that are evenly divisble by 4
2.) What output is produced by the following code fragment?
for (int val = 1; val <= 99; val += 2)
System.out.println (val);
ANSWER CHOICES ARE:
A.) even numbers from 1 (inclusive) to 99 (exclusive)
B.) odd numbers from 1 (inclusive) to 99 (exclusive)
C.) even numbers from 1 (inclusive) to 99 (inclusive)
D.) odd numbers from 1 (inclusive) to 99 (inclusive)
3.How many times will the following inner loop iterate? (In other words, how many println statements will be called?)
for(int j=0; j<10; j++)
for(int k=10; k>0; k--)
System.out.println("hi");
4. What is the value of x after the switch statement executes?
int x=5;
switch (x) {
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++;
}
Explanation / Answer
1.) D - all values from 200 (inclusive) to 0 (inclusive), except those that are evenly divisble by 4
Explanation :
for (int val = 200; val >= 0; val -= 1) // for each number starting from 200 ... (keep decrementing by 1 after each iteration)
if (val % 4 != 0) // if the number is not divisible by 4
System.out.println (val); // print the number
200, 196, 192, .... ,8 ,4 ,0 are the numbers divisible by 4
The output produced is:
199
198
197
195
.
.
.
5
3
2
1
2.) D - odd numbers from 1 (inclusive) to 99 (inclusive)
Explanation :
Initially val = 1
1st Iteration : (val=1) 1 is printed ; then val = val + 2 ;
2nd Iteration : (val=3 now ) 3 is printed; then val = val + 2 ;
3rd Iteration : (val = 5 now) 5 is printed; then val = val + 2 ;
.
.
This will go on till val is less than or equal to 99
1,3,5,...99 are all odd numbers
3.) 100 times
The outer loop iterates from 0 to 9, and for each such iteration, the inner loop also iterates from 10 to 1.
Look at the following (j,k) pairs
(0,10) (0,9) .... (0,1) --> 10 times
(1,10) (1,9) .... (1,1) --> 10 times
...
(9,10) (9,9) .... (9,1) --> 10 times
4.) 10
Initially x = 5
if x = 5 ; x += 3 ; implies x = 8 now
if x = 8 ; x-- ; implies x = 7 now
if x = 7 ; x += 2 ; implies x = 9 now
if x = 9 ; x ++ ; implies x = 10 now
there is no case for x = 10 ; so the loop is broken
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.