Total Pageviews

Friday, 22 January 2016

Java – Interview Question & Answer

Q.1 Given:
1. // insert code here
2. class StatTest {
3.     public static void main(String[] args) {
4.         System.out.println(Integer.MAX_VALUE);
5.     }
6. }
Which, inserted independently at line 1, compiles? (Choose all that apply.)
A. import static java.lang;
B. import static java.lang.Integer;
C. import static java.lang.Integer.*;
D. import static java.lang.Integer.*_VALUE;
E. import static java.lang.Integer.MAX_VALUE;
F. None of the above statements are valid import syntax
Answer:
􀀂 ✓ C and E are correct syntax for static imports. Line 4 isn’t making use of static imports, so the code will also compile with none of the imports.
􀀂􀀁 A, B, D, and F are incorrect based on the above.
Q.2 Given:
import static java.lang.System.*;
class _ {
    static public void main(String… __A_V_) {
        String $ = “”;
        for(int x=0; ++x < __A_V_.length; )
            $ += __A_V_[x];
         out.println($);
    }
}
And the command line:
java _ – A .
What is the result?
A. -A
B. A.
C. -A.
D. _A.
E. _-A.
F. Compilation fails
G. An exception is thrown at runtime
Answer:
􀀂 ✓ B is correct. This question is using valid (but inappropriate and weird) identifiers, static imports, var-args in main(), and pre-incrementing logic.
􀀂􀀁 A, C, D, E, F, and G are incorrect based on the above.
Q.3 Given the default classpath:
        /foo
And this directory structure:
foo
    |
    test
         |
         xcom
                |–A.class
                |–B.java
And these two files:
     package xcom;
     public class A { }
     package xcom;
     public class B extends A { }
Which allows B.java to compile? (Choose all that apply.)
A. Set the current directory to xcom then invoke
      javac B.java
B. Set the current directory to xcom then invoke
     javac -classpath . B.java
C. Set the current directory to test then invoke
     javac -classpath . xcom/B.java
D. Set the current directory to test then invoke
     javac -classpath xcom B.java
E. Set the current directory to test then invoke
     javac -classpath xcom:. B.java
Answer:
􀀂 ✓ C is correct. In order for B.java to compile, the compiler first needs to be able to find B.java. Once it’s found B.java it needs to find A.class. Because A.class is in the xcom package the compiler won’t find A.class if it’s invoked from the xcom directory. Remember that the -classpath isn’t looking for B.java, it’s looking for whatever classes B.java needs (in this case A.class).
􀀂􀀁 A, B, and D are incorrect based on the above. E is incorrect because the compiler can’t
find B.java.
Q.4 Given two files:
     a=b.java
     c_d.class
Are in the current directory, which command-line invocation(s) could complete without error? (Choose all that apply.)
A. java -Da=b c_d
B. java -D a=b c_d
C. javac -Da=b c_d
D. javac -D a=b c_d
Answer:
􀀂 ✓ A is correct. The -D flag is NOT a compiler flag, and the name=value pair that is associated with the -D must follow the -D with no spaces.
􀀂􀀁 B, C, and D are incorrect based on the above.
Q.5 If three versions of MyClass.class exist on a file system:
      Version 1 is in /foo/bar
      Version 2 is in /foo/bar/baz
      Version 3 is in /foo/bar/baz/bing      
And the system’s classpath includes
      /foo/bar/baz
And this command line is invoked from /foo
     java -classpath /foo/bar/baz/bing:/foo/bar MyClass
Which version will be used by java?
A. /foo/MyClass.class
B. /foo/bar/MyClass.class
C. /foo/bar/baz/MyClass.class
D. /foo/bar/baz/bing/MyClass.class
E. The result is not predictable.
Answer:
􀀂􀀁 D is correct. A -classpath included with a java invocation overrides a system classpath. When java is using any classpath, it reads the classpath from left to right, and uses the first match it finds.
􀀂􀀁 A, B, C, and E are incorrect based on the above.
Q.6 Given two files:
1. package pkgA;
2.     public class Foo {
3.         int a = 5;
4.         protected int b = 6;
5.     }
1. package pkgB;
2. import pkgA.*;
3. public class Fiz extends Foo {
4.     public static void main(String[] args) {
5.          Foo f = new Foo();
6.         System.out.print(” ” + f.a);
7.         System.out.print(” ” + f.b);
8.         System.out.print(” ” + new Fiz().a);
9.         System.out.println(” ” + new Fiz().b);
10.    }
11. }
What is the result? (Choose all that apply.)
A. 5 6 5 6
B. 5 6 followed by an exception
C. Compilation fails with an error on line 6
D. Compilation fails with an error on line 7
E. Compilation fails with an error on line 8
F. Compilation fails with an error on line 9
Answer:
􀀂 ✓ C, D, and E are correct. Variable a (default access) cannot be accessed from outside the package. Since variable b is protected, it can be accessed only through inheritance.
􀀂􀀁 A, B, and F are incorrect based on the above.
Q.7 Given:
3. import java.util.*;
4. public class Antique {
5.     public static void main(String[] args) {
6.         List<String> myList = new ArrayList<String>();
7.          assert (args.length > 0);
8.          System.out.println(“still static”);
9.     }
10. }
Which sets of commands (javac followed by java) will compile and run without exception  or error? (Choose all that apply.)
A. javac Antique.java
     java Antique
B. javac Antique.java
     java -ea Antique
C. javac -source 6 Antique.java
     java Antique
D. javac -source 1.4 Antique.java
     java Antique
E. javac -source 1.6 Antique.java
     java -ea Antique
Answer:
􀀂 ✓ A and C are correct. If assertions (which were first available in Java 1.4) are enabled, an
AssertionError will be thrown at line 7.
􀀂􀀁 D is incorrect because the code uses generics, and generics weren’t introduced until Java 5. B and E are incorrect based on the above.
Q.8 Given:
3. import java.util.*;
4. public class Values {
5.     public static void main(String[] args) {
6.         Properties p = System.getProperties();
7.         p.setProperty(“myProp”, “myValue”);
8.         System.out.print(p.getProperty(“cmdProp”) + ” “);
9.         System.out.print(p.getProperty(“myProp”) + ” “);
10.       System.out.print(p.getProperty(“noProp”) + ” “);
11.        p.setProperty(“cmdProp”, “newValue”);
12.       System.out.println(p.getProperty(“cmdProp”));
13.   }
14. }
And given the command line invocation:
       java -DcmdProp=cmdValue Values
What is the result?
A. null myValue null null
B. cmdValue null null cmdValue
C. cmdValue null null newValue
D. cmdValue myValue null cmdValue
E. cmdValue myValue null newValue
F. An exception is thrown at runtime
Answer:
􀀂 ✓ E is correct. System properties can be set at the command line, as indicated correctly in
the example. System properties can also be set and overridden programmatically.
􀀂􀀁 A, B, C, D, and F are incorrect based on the above.
Q.9 Given the following directory structure:
x-|
     |- FindBaz.class
     |
     |- test-|
                    |- Baz.class
                    |
                    |- myApp-|
                                          |- Baz.class
And given the contents of the related .java files:
1. public class FindBaz {
2.     public static void main(String[] args) { new Baz(); }
3. }
In the test directory:
1. public class Baz {
2.     static { System.out.println(“test/Baz”); }
3. }
In the myApp directory:
1. public class Baz {
2.      static { System.out.println(“myApp/Baz”); }
3. }
If the current directory is x, which invocations will produce the output “test/Baz”? (Choose
all that apply.)
A. java FindBaz
B. java -classpath test FindBaz
C. java -classpath .:test FindBaz
D. java -classpath .:test/myApp FindBaz
E. java -classpath test:test/myApp FindBaz
F. java -classpath test:test/myApp:. FindBaz
G. java -classpath test/myApp:test:. FindBaz
Answer:
􀀂 ✓ C and F are correct. The java command must find both FindBaz and the version of Baz located in the test directory. The “.” finds FindBaz, and “test” must come before “test/myApp” or java will find the other version of Baz. Remember the real exam will default to using the Unix path separator.
􀀂􀀁 A, B, D, E, and G are incorrect based on the above
Q.10 Given the following directory structure:
test-|
          |- Test.java
          |
          |- myApp-|
                               |- Foo.java
                               |
                               |- myAppSub-|
                                                            |- Bar.java

If the current directory is test, and you create a .jar file by invoking this,
jar -cf MyJar.jar myApp
then which path names will find a file in the .jar file? (Choose all that apply.)
A. Foo.java
B. Test.java
C. myApp/Foo.java
D. myApp/Bar.java
E. META-INF/Foo.java
F. META-INF/myApp/Foo.java
G. myApp/myAppSub/Bar.java
Answer:
􀀂 ✓ C and G are correct. The files in a .jar file will exist within the same exact directory tree structure in which they existed when the .jar was created. Although a .jar file will contain a META-INF directory, none of your files will be in it. Finally, if any files exist in the directory from which the jar command was invoked, they won’t be included in the .jar file by default.
􀀂􀀁 A, B, D, E, and F are incorrect based on the above
Q.11 Given the following directory structure:
test-|
          |- GetJar.java
          |
          |- myApp-|
                                |-Foo.java
And given the contents of GetJar.java and Foo.java:
3. public class GetJar {
4.     public static void main(String[] args) {
5.         System.out.println(myApp.Foo.d);
6.     }
7. }
3. package myApp;
4. public class Foo { public static int d = 8; }
If the current directory is “test”, and myApp/Foo.class is placed in a JAR file called MyJar.jar located in test, which set(s) of commands will compile GetJar.java and produce the output 8? (Choose all that apply.)
A. javac -classpath MyJar.jar GetJar.java
         java GetJar
B. javac MyJar.jar GetJar.java
         java GetJar
C. javac -classpath MyJar.jar GetJar.java
         java -classpath MyJar.jar GetJar
D. javac MyJar.jar GetJar.java
         java -classpath MyJar.jar GetJar
Answer:
􀀂 ✓ A is correct. Given the current directory and where the necessary files are located, these
are the correct command line statements.
􀀂􀀁 B and D are wrong because javac MyJar.jar GetJar.java is incorrect syntax. C is wrong
because the -classpath MyJar.java in the java invocation does not include the test directory.
Q.12 Given the following directory structure:
x-|
     |- GoDeep.class
     |
     |- test-|
                    |- MyJar.jar
                    |
                    |- myApp-|
                                          |-Foo.java
                                          |-Foo.class
And given the contents of GoDeep.java and Foo.java:
3. public class GoDeep {
4.      public static void main(String[] args) {
5.           System.out.println(myApp.Foo.d);
6.      }
7. }
3. package myApp;
4. public class Foo { public static int d = 8; }
And MyJar.jar contains the following entry:
    myApp/Foo.class
If the current directory is x, which commands will successfully execute GoDeep.class and
produce the output 8? (Choose all that apply.)
A. java GoDeep
B. java -classpath . GoDeep
C. java -classpath test/MyJar.jar GoDeep
D. java GoDeep -classpath test/MyJar.jar
E. java GoDeep -classpath test/MyJar.jar:.
F. java -classpath .:test/MyJar.jar GoDeep
G. java -classpath test/MyJar.jar:. GoDeep
Answer:
􀀂 ✓ F and G are correct. The java command must find both GoDeep and Foo, and the -classpath option must come before the class name. Note, the current directory (.), in the classpath can be searched first or last.
􀀂􀀁 A, B, C, D, and E are incorrect based on the above.

No comments: