Java OCA OCP Practice Question 3094

Question

Consider the following program:

public class Main {
   public static void main(String[] args) {
      String[] cards = { "Club", "spade", " diamond ", "hearts" };
      for (String card : cards) {
         switch (card) {
         case "Club":
            System.out.print(" club ");
            break;
         case "Spade":
            System.out.print(" spade ");
            break;
         case "diamond":
            System.out.print(" diamond ");
            break;
         case "heart":
            System.out.print(" heart ");
            break;
         default:
            System.out.print(" none ");
         }/* w w  w . j a v a 2  s  . c  o  m*/
      }
   }
}

Which one of the following options shows the output of this program?

  • a) none none none none
  • b) club none none none
  • c) club spade none none
  • d) club spade diamond none
  • e) club spade diamond heart


b)

Note

Here is the description of matches for the four enumeration values:

  • "club" matches with the case "Club".
  • For "Spade", the case "spade" does not match because of the case difference (switch case match is case sensitive).
  • does not match with "diamond" because case statements should exactly match and there are extra whitespace in the original string.
  • "hearts" does not match the string "heart".



PreviousNext

Related