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); } }
- The code will print Java.
- The code will print Oracle.
- The code will not compile.
- The code will throw an exception or an error at runtime.
Select the correct option(s):
- You cannot handle runtime exceptions.
- You should not handle errors.
- If a method throws a checked exception, it must be either handled by the method or specified in its throws clause.
- If a method throws a runtime exception, it may include the exception in its throws clause.
- Runtime exceptions are checked exceptions.
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"); } } }
- If the class EJavaGuruExcep is executed using the following command, it prints NullPointerException:
java EJavaGuruExcep- If the class EJavaGuruExcep is executed using the following command, it prints IndexOutOfBoundsException:
java EJavaGuruExcep- If the class EJavaGuruExcep is executed using the following command, it prints ArrayIndexOutOfBoundsException:
java EJavaGuruExcep one- The code will fail to compile.
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(); } }
guru finally 1
guru finally 1 Exception in thread "main" java.lang.StackOverflowError
guru Exception in thread "main" java.lang.StackOverflowError
guru- The code fails to compile.
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); } }
- The code outputs a value.
- The code outputs a value followed by an exception.
- ArithmeticException
- NullPointerException
- IndexOutOfBoundsException
- ArrayIndexOutOfBoundsException
- Compilation error
- None of the above
Which of the following methods will not compile?
private void method1(String name) { if (name.equals("star")) throw new IllegalArgumentException(name); }
private void method2(int age) { if (age > 30) throw Exception(); }
public Object method3(boolean accept) { if (accept) throw new StackOverflowError(); else return new StackOverflowError(); }
protected double method4() throws Exception { throw new Throwable(); }
public double method5() throws Exception { return 0.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()); } }
- 10
- 11
- 12
- Compilation error
- Runtime exception
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(); } }
Base
Derived
Derived Base
Base Derived- Compilation error
Which of the following statements are true?
- A user-defined class may not throw an IllegalStateException. It must be thrown only by Java API classes.
- System.out.println will throw a NullPointerException if an uninitialized instance variable of type String is passed to it to print its value.
- 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.
- ExceptionInInitializerError may be thrown by the JVM when a static initializer in your code throws a NullPointerException.
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 inner 2 outer
1 outer
1 inner
1 inner 2
3.144.97.187