Question : You have been given following code snippet. Based on the below code, select the correct option which can have overridden method in child class.
Correct Answer : Get Lastest Questions and Answer : Explanation: As we know we can have more specific exception child class. Hence, option A is correct. Because FileAlreadyExistsException is a child class of FileSystemException. So that your derived class can have more specific throws clause.
Question : You have been given following code. When you run it what is the expected output.
package com.hadoopexam; class Welcome { public static void call() { try { throw new ArrayIndexOutOfBoundsException(); } catch (ArrayIndexOutOfBoundsException boundException) { RuntimeException runtimeException = new RuntimeException(boundException); runtimeException.initCause(boundException); throw runtimeException; } }
Initializes the cause of this throwable to the specified value. (The cause is the throwable that caused this throwable to get thrown.) This method can be called at most once. It is generally called from within the constructor, or immediately after creating the throwable. If this throwable was created with Throwable(Throwable) or Throwable(String, Throwable), this method cannot be called even once. An example of using this method on a legacy throwable type without other support for setting the cause is: try { lowLevelOp(); } catch (LowLevelException le) { throw (HighLevelException) new HighLevelException().initCause(le); // Legacy constructor } Parameters: cause the cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.) Returns: a reference to this Throwable instance. Throws: IllegalArgumentException - if cause is this throwable. (A throwable cannot be its own cause.) IllegalStateException - if this throwable was created with Throwable(Throwable) or Throwable(String, Throwable), or this method has already been called on this throwable. In the expression new RuntimeException(boundException)the exception object boundException is already chained to the RuntimeException object. The method initCause() cannot be called on an exception object that already has an exception object chained during the constructor call. Hence, the call re.initCause(oob); results in initCause() throwing an IllegalStateException.
Question : You have been given below exception, select the correct behavior package com.hadoopexam;
class Welcome { public static void call() { try { throw new IndexOutOfBoundsException(); } catch (IndexOutOfBoundsException ioob) { throw new Exception(ioob); } }
public static void main(String[] args) { try { call(); } catch (Exception e) { System.out.println(e.getCause()); } } } 1. Code will give compile time error.
Correct Answer : Get Lastest Questions and Answer : Explanation: As you know, when you throw a checked exception, It should be added in the throws clause or you have to catch it again. Here throw new Exception(ioob) is a checked exception hence, you need to have it in throws clause of call method. public static void call() throws Exception
4. It will print "Velocity with new speed 1.4kmph"
5. It will give NullPointerException Correct Answer : Exp : It is a Question of mixing Inner classes as well access outer class variable. As we know distance variable is accessible from inner class. While creating constructor of Welcome class , we assigned distance=100. In Inner class we are acceding defined value of timeTravel=60. Hence, value= 100/60 , as we know both are int value hence result would be int value. Which is 1. So it will print Velocity with new speed 1kmph
Question : You have been given below code. Please select the correct options, which applies to this code.
package com.hadoopexam;
class Parent { public void printValue() { System.out.println("It is a Parent Class"); } }
abstract class Child extends Parent { // n1 public static void main(String[] args) { Parent obj = new Parent(); obj.printValue (); // n2 } } 1. You cannot have an abstract class which can extend non abstract class. Hence, it will give compile time error.
2. Parent class printValue() method is not accessible from a child class.
3. Program will compile successfully and also run perfectly with "It is a Parent Class".
4. There will be a RuntimeError. As child class is an abstract class.