Java OCA OCP Practice Question 3280

Question

Consider the following program and choose the appropriate option:

import java.util.*;

public class Main {
     public static void main(String []args) {
             Map<String, int> map =
                     new HashMap<int, String>();            //#1
             Map<String, String> map2 =
                     new HashMap<String, String>();         //#2
             Map<String, String> map3 = new HashMap<>();    //#3
             Map<> map4 = new HashMap<String, String>();    //#4
     }/* w w  w  .  j  av a  2 s .  com*/
}
  • a) Statement #1 and #2 will compile successfully.
  • b) Statement #2 and #3 will compile successfully.
  • c) Statement #3 and #4 will compile successfully.
  • d) Statement #4 and #1 will compile successfully.


b)

Note

Due to the diamond syntax, it is optional to specify template types in the right hand side of an object creation statement.

Hence, statement #3 is right.

Statement #2 is correct since HashMap is a Map.

Therefore, option b) is correct.

In statement #1, the order of arguments of the declared type is different from the order of arguments in the initialized type.

In statement #4, the diamond syntax is used in the declaration of the type and so is incorrect (the correct way is to use the diamond operator in the initialization type).




PreviousNext

Related