in Object Oriented Programming recategorized by
3,615 views
3 votes
3 votes

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?

  1. 4 16
  2. 4 10 16
  3. 0 6 12 18
  4. 1 4 7 10 13 16 19
in Object Oriented Programming recategorized by
3.6k views

2 Answers

6 votes
6 votes
Best answer

Option B

4  10  16 

k =0 % 3 =0 since k is incremented by 2

 = 2%3=2 

  =4%3=1 -----------------prints 4

  = 6%3 =0

  = 8%3=2

 =10%3=1 ------------- prints 10

=12%3 =0

 .........

Therefore Output is 4  10  16 

selected by
1 vote
1 vote

Answer will be B)

Here the value printed

when ((k%3)==1)

And 4,10,16 gives those value between 1 to 20

Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true