Java OCA OCP Practice Question 808

Question

What will the following code print when run?

public class Main {
   public static void main(String[] args) throws Exception {
      String[] sa = { "a", "b", "c" };
      for (String s : sa) {
         if ("b".equals(s))
            continue;
         System.out.println(s);/*from  w  w w  .jav  a  2s.c  om*/
         if ("b".equals(s))
            break;
         System.out.println(s + " again");
      }
   }
}

Select 1 option

A. a //from   w  w  w  .  j a va  2s  .c  o  m
   a again 
   c 
   c again 

B. a 
   a again 
   b 

C. a 
   a again 
   b 
   b again 

D. c 
   c again 


Correct Option is  : A

Note

To determine the output you have to run through the loop one iteration at a time in your mind:

Iteration 1: s is "a".

It is not equal to "b" so, it will print "a", and then "a again".

Iteration 2: s is "b".

It is equal to "b", so the first if will execute "continue", which mean the rest of the code in the loop will not be executed, and the next iteration will start.

The second if is not executed at all because of the continue in the first if.

Iteration 3: s is "c", both the if conditions are not satisfied.

So "c" and "c again" will be printed.




PreviousNext

Related