Java OCA OCP Practice Question 3149

Question

What will the program print when compiled and run?

import java.util.ArrayList;
import java.util.Collection;

public class Main {
   public static void main(String[] args) {

      Collection<String> words = new ArrayList<String>();
      words.add("Don't");
      words.add("change");
      words.add("me!");

      System.out.println("Before: " + words);
      for (String word : words) {
         System.out.print(word.toUpperCase() + "_");
      }/*w  w w  .  j av a 2s . c  o  m*/
      System.out.println();
      System.out.println("After:  " + words);
   }
}

Select the one correct answer.

(a) Before: [Don't, change, me!]//from   w w w.ja v  a  2  s. c o  m
    DON'T_CHANGE_ME!_
    After:  [DON'T, CHANGE, ME!]

(b) Before: [Don't, change, me!]
    DON'T_CHANGE_ME!_
    After:  [Don't, change, me!]

(c) The  program  will  throw  a  java.util.ConcurrentModificationException,  when
    run.

(d) The program fails to compile.


(b)

Note

A String is immutable.

The call to the toUpperCase() method returns a new String object whose text representation is printed.

The elements of the collection remain unchanged.




PreviousNext

Related