Java OCA OCP Practice Question 3242

Question

Which one of the following programs compiles without any errors and prints "hello world" in console?.

a)      import static java.lang.System.out.println;
    class Main {/*  w  w  w . j a  v  a  2 s.  c  o  m*/
        public static void main(String []args) {
                println("hello world");
        }
    }

b)      import static java.lang.System.out;
    class Main {
        public static void main(String []args) {
                out.println("hello world");
        }
    }

c)      import static java.lang.System.out.*;
    class Main {
        public static void main(String []args) {
                out.println("hello world");
        }
    }

d)      import static java.lang.System.out.*;

    class Main {
        public static void main(String []args) {
                println("hello world");
        }
    }


b)

Note

The member out is a static member in the System class; you can statically import it and call println method on it.

Note that println is a non-static member.

Also, the statement import static java.lang.System.out.*; will result in a compiler error since out is not a class (but a static member of type PrintStream).




PreviousNext

Related