1. Question: Which one creates an instance of an array?

    A
    int[ ] ia = new int[15];

    B
    float fa = new float[20];

    C
    char[ ] ca = "Some String";

    D
    int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };

    Note: Option A is correct. It uses correct array declaration and correct array construction. Option B is incorrect. It generates a compiler error: incompatible types because the array variable declaration is not correct. The array construction expects a reference type, but it is supplied with a primitive type in the declaration. Option C is incorrect. It generates a compiler error: incompatible types because a string literal is not assignable to a character type variable. Option D is wrong, it generates a compiler error <identifier> expected. The compiler thinks that you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create a 3 x 3 two-dimensional array.
    1. Report
  2. Question: Which two of the following are legal declarations for nonnested classes and interfaces? 1. final abstract class Test {} 2. public static interface Test {} 3. final public class Test {} 4. protected abstract class Test {} 5. protected interface Test {} 6. abstract public class Test {}

    A
    1 and 4

    B
    2 and 5

    C
    3 and 6

    D
    4 and 6

    Note: (3), (6). Both are legal class declarations. (1) is wrong because a class cannot be abstract and final—there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.
    1. Report
  3. Question: Which of the following class level (nonlocal) variable declarations will not compile?

    A
    protected int a;

    B
    transient int b = 3;

    C
    private synchronized int e;

    D
    volatile int d;

    Note: Option C will not compile; the synchronized modifier applies only to methods. Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier.
    1. Report
  4. Question: Which two cause a compiler error? 1. float[ ] f = new float(3); 2. float f2[ ] = new float[ ]; 3. float[ ]f1 = new float[3]; 4. float f3[ ] = new float[3]; 5. float f5[ ] = {1.0f, 2.0f, 2.0f};

    A
    2, 4

    B
    3, 5

    C
    4, 5

    D
    1, 2

    Note: (1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f = new float[3]; (2) causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3]; (3), (4), and (5) compile without error.
    1. Report
  5. Question: Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

    A
    final

    B
    static

    C
    private

    D
    protected

    E
    volatile

    Note: The private access modifier limits access to members of the same class. Option A, B, D, and E are wrong because protected are the wrong access modifiers, and final, static, and volatile are modifiers but not access modifiers.
    1. Report
  6. Question: Which is a valid declaration within an interface?

    A
    public static short stop = 23;

    B
    protected short stop = 23;

    C
    transient short stop = 23;

    D
    final void madness(short stop);

    Note: (A) is valid interface declarations. (B) and (C) are incorrect because interface variables cannot be either protected or transient. (D) is incorrect because interface methods cannot be final or static.
    1. Report
  7. Question: What will be the output of the program? public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } } }

    A
    Finally

    B
    Compilation fails.

    C
    The code runs with no output.

    D
    An exception is thrown at runtime.

    Note: If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances: An exception arising in the finally block itself. The death of the thread. The use of System.exit() Turning off the power to the CPU. I suppose the last three could be classified as VM shutdown.
    1. Report
  8. Question: What will be the output of the program? try { int x = 0; int y = 5 / x; } catch (Exception e) { System.out.println("Exception"); } catch (ArithmeticException ae) { System.out.println(" Arithmetic Exception"); } System.out.println("finished");

    A
    finished

    B
    Exception

    C
    Compilation fails.

    D
    Arithmetic Exception

    Note: Compilation fails because ArithmeticException has already been caught. ArithmeticException is a subclass of java.lang.Exception, by time the ArithmeticException has been specified it has already been caught by the Exception class. If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses).
    1. Report
  9. Question: What will be the output of the program?
    public class X 
    {  
        public static void main(String [] args) 
        {
            try 
            {
                badMethod();  
                System.out.print("A"); 
            }  
            catch (Exception ex) 
            {
                System.out.print("B");  
            } 
            finally 
            {
                System.out.print("C"); 
            } 
            System.out.print("D"); 
        }  
        public static void badMethod() 
        {
            throw new Error(); /* Line 22 */
        } 
    }

    A
    ABCD

    B
    Compilation fails.

    C
    C is printed before exiting with an error message.

    D
    BC is printed before exiting with an error message.

    Note: Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.Error).
    1. Report
  10. Question: What will be the output of the program?
    public class X 
    {  
        public static void main(String [] args) 
        {
            try 
            {
                badMethod();  
                System.out.print("A");  
            } 
            catch (RuntimeException ex) /* Line 10 */
            { 
                System.out.print("B"); 
            } 
            catch (Exception ex1) 
            { 
                System.out.print("C"); 
            } 
            finally 
            {
                System.out.print("D"); 
            } 
            System.out.print("E"); 
        } 
        public static void badMethod() 
        { 
            throw new RuntimeException(); 
        } 
    }

    A
    BD

    B
    BCD

    C
    BDE

    D
    BCDE

    Note: A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.
    1. Report
Copyright © 2025. Powered by Intellect Software Ltd