Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ISRO | ISRO CS 2008 | Question 40

  • Last Updated : 14 Jun, 2018

Consider the following code segment

for (int k=0; k<20; k=k+2)
{
if (k % 3 == 1)
system.out.print(k+ " ")
}

What is printed as a result of executing the code segment?
(A) 4 16
(B) 4 10 16
(C) 0 6 12 18
(D) 1 4 7 10 13 16 19


Answer: (B)

Explanation: for loop starts from 0 to 20 and increments with 2 at each iteration.

k = 0 % 3 = 0 
k = 2 % 3 = 2 
k = 4 % 3 = 1 // prints 4
k = 6 % 3 = 0
k = 8 % 3 = 2
k = 10 % 3 = 1 // prints 10
k = 12 % 3 = 0
k = 14 % 3 = 2
k = 16 % 3 = 1 // prints 16
k = 18 % 3 = 0

So, Output is 4 10 16

Option (B) is correct.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!