7.8. Sample exam questions

Q7-1.

What is the output of the following code:

class Course {
    String courseName;
    Course() {
        Course c = new Course();
        c.courseName = "Oracle";
    }
}
class EJavaGuruPrivate {
    public static void main(String args[]) {
        Course c = new Course();
        c.courseName = "Java";
        System.out.println(c.courseName);
    }
}

  1. The code will print Java.
  2. The code will print Oracle.
  3. The code will not compile.
  4. The code will throw an exception or an error at runtime.

Q7-2.

Select the correct option(s):

  1. You cannot handle runtime exceptions.
  2. You should not handle errors.
  3. If a method throws a checked exception, it must be either handled by the method or specified in its throws clause.
  4. If a method throws a runtime exception, it may include the exception in its throws clause.
  5. Runtime exceptions are checked exceptions.

Q7-3.

Examine the following code and select the correct option(s):

class EJavaGuruExcep {
    public static void main(String args[]) {
        EJavaGuruExcep var = new EJavaGuruExcep();
        var.printArrValues(args);
    }
    void printArrValues(String[] arr) {
        try {
            System.out.println(arr[0] + ":" + arr[1]);
        } catch (NullPointerException e) {
            System.out.println("NullPointerException");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException");

        }
    }
}

  1. If the class EJavaGuruExcep is executed using the following command, it prints NullPointerException:

    java EJavaGuruExcep
  2. If the class EJavaGuruExcep is executed using the following command, it prints IndexOutOfBoundsException:

    java EJavaGuruExcep
  3. If the class EJavaGuruExcep is executed using the following command, it prints ArrayIndexOutOfBoundsException:

    java EJavaGuruExcep one
  4. The code will fail to compile.

Q7-4.

What is the output of the following code?

class EJava {
    void method() {
        try {
            guru();
            return;
        } finally {
            System.out.println("finally 1");
        }
    }
    void guru() {
        System.out.println("guru");
        throw new StackOverflowError();
    }
    public static void main(String args[]) {
        EJava var = new EJava();
        var.method();
    }
}


  1. guru
    finally 1

  2. guru
    finally 1
    Exception in thread "main" java.lang.StackOverflowError

  3. guru
    Exception in thread "main" java.lang.StackOverflowError

  4. guru
  5. The code fails to compile.

Q7-5.

What is the output of the following code?

class Quest5 {
    public static void main(String args[]) {
        int arr[] = new int[5];
        arr = new int[]{1,2,3,4};

        int x = arr[1]-- + arr[0]-- /arr[0] * arr[4];
        System.out.println(x);
    }
}

  1. The code outputs a value.
  2. The code outputs a value followed by an exception.
  3. ArithmeticException
  4. NullPointerException
  5. IndexOutOfBoundsException
  6. ArrayIndexOutOfBoundsException
  7. Compilation error
  8. None of the above

Q7-6.

Which of the following methods will not compile?


  1. private void method1(String name) {
        if (name.equals("star"))
            throw new IllegalArgumentException(name);
    }

  2. private void method2(int age) {
        if (age > 30)
            throw Exception();
    }

  3. public Object method3(boolean accept) {
        if (accept)
            throw new StackOverflowError();
        else
            return new StackOverflowError();
    }

  4. protected double method4() throws Exception {
        throw new Throwable();
    }

  5. public double method5() throws Exception {
        return 0.7;
    }

Q7-7.

What is the output of the following code?

 class TryFinally {
    int tryAgain() {
        int a = 10;
        try {
            ++a;
        } finally {
            a++;
        }
        return a;
    }
    public static void main(String args[]) {
        System.out.println(new TryFinally().tryAgain());
    }
}

  1. 10
  2. 11
  3. 12
  4. Compilation error
  5. Runtime exception

Q7-8.

What is the output of the following code?

class EJavaBase {
    void myMethod() throws ExceptionInInitializerError {
        System.out.println("Base");
    }
}
class EJavaDerived extends EJavaBase {
    void myMethod() throws RuntimeException {
        System.out.println("Derived");
    }
}
class EJava3 {
    public static void main(String args[]) {
        EJavaBase obj = new EJavaDerived();
        obj.myMethod();
    }
}


  1. Base

  2. Derived

  3. Derived
    Base

  4. Base
    Derived
  5. Compilation error

Q7-9.

Which of the following statements are true?

  1. A user-defined class may not throw an IllegalStateException. It must be thrown only by Java API classes.
  2. System.out.println will throw a NullPointerException if an uninitialized instance variable of type String is passed to it to print its value.
  3. NumberFormatException is thrown by multiple methods from the Java API when invalid numbers are passed on as Strings to be converted to the specified number format.
  4. ExceptionInInitializerError may be thrown by the JVM when a static initializer in your code throws a NullPointerException.

Q7-10.

What is the output of the following code?

class EJava {
    void foo() {
        try {
            String s = null;
            System.out.println("1");
            try {
                System.out.println(s.length());
            } catch (NullPointerException e) {
                System.out.println("inner");
            }
            System.out.println("2");
        } catch (NullPointerException e) {
            System.out.println("outer");
        }
    }
    public static void main(String args[]) {
        EJava obj = new EJava();
        obj.foo();
    }
}


  1. 1
    inner
    2
    outer

  2. 1
    outer

  3. 1
    inner

  4. 1
    inner
    2

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.187