Java OCA OCP Practice Question 3265

Question

Given the following code:

package p1;
public enum Constants {
  ONE, TWO, THREE;
}
package p2;//ww  w. j  a v a 2s .c  o m
// (1) INSERT IMPORT STATEMENTS HERE.
public class Main {
  public static void main(String[] args) {
    int value = new Random().nextInt(4);
    Constants constant = null;
    switch (value) {
      case 1:
        constant = ONE;
        break;
      case 2:
        constant = TWO;
        break;
      default:
        constant = THREE;
        break;
    }
    out.println(constant);
  }
}

Which import statements, when inserted at (1), will result in a program that prints a constant of the enum type Constants, when compiled and run?.

Select the two correct answers.

(a) import java.util.*;
    import p1.Constants;
    import static p1.Constants.*;
    import static java.lang.System.out;

(b) import java.util.*;
    import static p1.Constants.*;
    import static java.lang.System.out;

(c) import java.util.*;
    import p1.Constants.*;
    import java.lang.System.out;

(d) import java.util.*;
    import p1.*;/*from   ww w . j  av a  2 s  . c  om*/
    import static p1.Constants.*;
    import static java.lang.System.*;

(e) import java.util.Random;
    import p1.*;
    import static p1.Constants.*;
    import System.out;


P:(a), (d)

Note

In the program, we need to import the types java.util.Random and the enum type p1.Constants.

We also need to statically import the constants of the enum type p1.Constants, and the static field out from the type java.lang.System.

All classes must be fully qualified in the import statements.

Only (a) and (d) fit the bill.

(b) does not import the enum type p1.Constants.

(c) does not import the enum type p1.Constants either.

In addition, it does not statically import the constants of the enum type p1.

Constants and the static field out from the type java.lang.System.

(e) does not statically import the static field out from the type java.lang.System.




PreviousNext

Related