Java - Test 1
No. of Questions - 20
Time - 40 Minutes
Negative Marking- No

 

Answers

Question 1. If all three top-level elements occur in a source file, they must appear in which order?

A. Imports, package declaration, classes.
B. Classes, imports, package declarations
C. Package declaration must come first; order for imports and class definitions is not
        significant.   
D. Package declaration, imports, classes.
E. Imports must come first; order for package declaration and class definitions is not
           significant.
 

Question 2.

Consider the following application:
class Q6 {
   public static void main(String args[ ]) {
       Holder h = new Holder( );
       h.held = 100;
       h.bump(h);
       System.out.println(h.held);
     }
}

class Holder {
  public int held;
  public void bump( holder theHolder) { theHolder.held++; }
}

What value is printed out at line 6? 

A. 0
B.
C. 100 
D. 101
E. 99 

Question 3.

 What results from running the following code?
1.      public class Short {
2.          public static void main(String args[ ]) {
3.              StringBuffer s = new StringBuffer(“Hello”);
4.              if ((s.length( ) > 5) &&
5.                (s.append(“ there”).equals(“False”)))
6.                 ; / / do nothing
7.              System.out.println(“value is “ + s);
8.          }
9.      }

A.  The output: value is Hello
B.  The output: value is Hello there
C.  A compiler error at line 4 or 5
D.  No output
E. A NullPointerException
Question 4.

What results from running the following code?
1.      public class Xor {
2.           public static void main(String args[ ]) {
3.               byte b = 10; / / 00001010 binary
4.               byte c = 15; / / 00001111 binary
5.               b = (byte)(b ^ c);
6.               System.out.println(“b contains ” + b);
7.          }
8.      }

A. The output: b contains 10   
B. The output: b contains 5   
C. The output: b contains 250   
D. The output: b contains 245 
E. The output: b contains 300   

Question 5.

What results from the following fragment of code?
1.      int x = 1;
2.      String [ ] names = { “Fred”, “Jim” , “Sheila” };
3.      names [--x] += “ . ”;
4.      for (int I = 0, I < names.length; I++) {
5.         System.out.println(names[I]);
6.      }

A.  The output includes Fred. With a trailing period.
B.  The output includes Jim. With a trailing period. 
C.  The output includes Sheila. With a trailing period. 
D.  None of the outputs shows a trailing period. 
E.  An ArrayIndexOutOfBoundsException is thrown. 

Question 6.

What is the minimal modification that will make the code below compile correctly?
1.      final class Aaa
2.      {
3.            int xxx;
4.            void yyy( ) { xxx = 1; }
5.      }
6.       
7.       
8.      class Bbb extends Aaa
9.      {
10.        final Aaa finalref = new Aaa( )
11.   
12.        final void yyy( )
13.       {
14.             System.out.println(“In method yyy( )”);
15.             finalref.xxx = 12345;
16.        }
17.  }

A.  On line 1, remove the final modifier. 
B.  On line 10, remove the final modifier. 
C.  Remove line 15. 
D.  On lines 1 and 10, remove the final modifier. 
E.  The code will compile as is. No modification is needed. 

Question 7.

 Which one of the following statements is true?

A.  Transient methods may not be overridden.
B.  Transient methods must be overridden.
C.  Transient classes may not be serialized.
D.  Transient variables must be static.
E.  Transient variables are not serialized.

Question 8.

Which one statement is true about the code below?
1.      Class HasStatic
2.      {
3.             private static int x = 100;
4.       
5.             public static void main(String args[ ])
6.            {
7.                    HasStatic hs1 = new HasStatic( );
8.                    hs1.x++;
9.                    HasStatic hs2 = new HasStatic( );
10.                hs2.x++;
11.              hs1 = new HasStatic( );
12.                hs1.x++;
13.                HasStatic.x++;
14.                System.out.println(“x = ”+ x);
15.         }
16  }
 

A.  Line 8 will not compile, because it is a static reference to a private variable.
B.  Line 13 will not compile, because it is a static reference to a private variable
C.  The program compiles, and the output is x = 102. 
D.  The program compiles, and the output is x = 103.
E.  The program compiles, and the output is x = 104

Question 9. Which modifier or modifiers should be used to denote a variable that should not be written out as part of its class’ persistent state? (Choose the shortest possible answer.) 

A.     private 
B.     protected 
C.     private protected 
D.     transient 
E.     private transient 

Question 10. Which one line in the following code will not compile? 

A.  byte b = 5;
B.  char c = ‘5’;
C.  short s = 55;
D.  int i = 555;
E.  b = s; 

Question 11. Consider the following class:
1.      class Cruncher {
2.         void crunch(int i)          {System.out.println(“int    version”);}
3.         void crunch(String s) {System.out.println(“String _
   version”);}
4.       
5.         public static void main(String args[ ]) {
6.         Cruncher crun = new Cruncher( );
7.         char ch = ‘p’;
8.         crun.crunch(ch);
9.         }
10.  }
   

Which of the statements below is true? (Choose one.) 

A.  Line 3 will not compile, because void methods cannot be overridden.
B.  Line 8 will not compile, because there is no version of crunch( ) that takes a char
         argument. 
C.  The code will compile but will throw an exception at line 8.
D.  The code will compile and produce the following output:int version
E.  The code will compile and produce the following output:String version

Question 12.

Which of the statements below is true? (Choose one.)

A.  Object references can be converted in assignments but not in method calls.
B.  Object references can be converted in method calls but not in assignments.
C.  Object references can be converted in both method calls and assignments, but the
         rules governing these conversions are very different.
D.  Object references can be converted in both method calls and assignments, and the
         rules governing these conversions are identical.
E.  Object references can never be converted.

Question 13.

Consider the following code: 

Which line below will not compile? 

A.  Object ob = new Object( );
B.  String stringarr[ ] = new String[50];
C.  Float floater = new Float(3.14f); 
D.  floater = ob;
E.  ob = stringarr[5]; 

Question 14.

Consider the following code:

1.      outer: for (int i = 0; i < 2; i++) {
2.          for (int j = 0; j < 3; j++) {
3.              if (i = = j) {
4.                continue outer;
5.             }
6.             System.out.println(“i = “ + i + “ j = “ + j);
7.          }
8.      }

Which lines would be part of the output?

A.  i = 0 j = 0 
B.  i = 0 j = 1
C.  i = 0 j = 2 
D.  i = 1 j = 0 
E.  i = 1 j = 1 

Question 15.

What would be the output from this code fragment?
1.      int x = 0, y = 4, z = 5;
2.      If (x > 2) {
3.         If (y < 5) {
4.           System.out.println(“message one”);
5.         }
6.         else {
7.           System.out.println(“message two”);
8.         }
9.      }
10.  else if (z > 5) {
11.       System.out.println(“message three”);
12. 
13.  else {
14.       System.out.println(“message four”);
15.  }

A.  message one 
B.  message two
C.  message three
D.  message four
E.  Compile Error Occurs

Question 16.

1.      Which statement is true about the following code fragment?
1.      int j = 2;
2.      switch ( j ) {
3.           case 2:
4.               System.out.println(“value is two”);
5.           case 2 + 1:
6.               System.out.println(“value is three”);
7.               break;
8.           default:
9.               System.out.println(“value is  “ + j);
10.           break;
11. }

A.  The code is illegal because of the expression at line 5
B.  The acceptable types for the variable j, as the argument to the switch( ) construct,
        could be any of byte, short, int, or long
C.  The output would be only the text value is two.
D.  The output would be the text value is two followed by the text value is three.
E.  The output would be the text value is two, followed by the text value is three, followed
         by the text value is 2

Question 17.

1.      Which one of the following fragments shows the most appropriate way to throw an exception? Assume that any undeclared variables have been appropriately elsewhere and are in scope and have meaningful values.

A.      1. Exception e = new IOException(“File not found”);
          2. if (!f.exists( ) ) { / / f is a file object
          3.   throw e;
          4. } 

B.       1. if (!f.exists( ) ) { / / f is a File object
          2.      throw new IOException(“File” + f.getName( ) + “ not _ found”);
          3.     

C.  1.    if (!f.exists( ) ) {
         2.   throw IOException;
         3. }

D.  1. if (!f.exists( )) {
         2. throw “File not found”;
         3. }
E.    1. if (!f.exists( )) { / / f is a File object
         2.   throw new IOException( );
         3. }

Question 18.

A thread’s run( ) method includes the following lines:
1.      try {
2.         sleep(100);
3.      } catch (InterruptedException e) { }

Assuming the thread is not interrupted, which one of the following statements is correct?

A.  The code will not compile, because exceptions may not be caught in a thread’s run( )
         method.
B.  At line 2, the thread will stop running. Execution will resume in at most 100 milliseconds.
C.  At line 2, the thread will stop running. It will resume running in exactly 100 millisecond.
D.  At line 2, the thread will stop running. It will resume running some time after 100
        milliseconds have elapsed.
E.  The operating system crashes.

Question 19.

Which one statement is true about the code below?

1.      String s1 = “abc” + “def”;
2.      String s2 = newString(S1);
3.      If (s1 = = s2)
4.         System.out.println(“= = succeeded”);
5.      if (s1.equals(s2))
6.         System.out.println(“.equals( ) succeeded”);

A.  Lines 4 and 6 both execute. 
B.  Line 4 executes, and line 6 does not.
C.  Line 6 executes, and line 4 does not.
D.  Neither line 4 nor line 6 executes.
E.  No output is seen on the screen.

Question 20.

Which one statement is true about the code fragment below?

1.      String s = “abcde”;
2.      StringBuffer s1 = new StringBuffer(“abcde”);
3.      If (s.equals(s1))
4.         s1 = null;
5.      if (s1.equals(s))
6.         s = null;

A.  Compilation fails at line 1, because the String constructor must be called explicitly
B.  Compilation fails at line 3, because s and s1 have different types.
C.  Compilation succeeds. During execution, an exception is thrown at line 3.
D.  Compilation succeeds. During execution, an exception is thrown at line 5.
E.  Compilation succeeds. No exception is thrown during execution.