ExamVeda
Login
Home
41
Given the following piece of code:
class SalaryCalculationException extends Exception{}
class Person{
        public void calculateSalary() throws SalaryCalculationException{
                //...
                throw new SalaryCalculationException();
                //...
        }
}
class Company{
        public void paySalaries(){
                new Person().calculateSalary();
        }
}

Which of the following statements is correct?
1. This code will compile without any problems.
2. This code will compile if in method paySalaries() we return a boolean in stead of void.
3. This code will compile if we add a try-catch block in paySalaries().
4. This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries().
Discuss
Answer & Solution
Answer: Option D
Solution:
1. This code will compile without any problems.
This statement is incorrect because the code includes the declaration of a checked exception (SalaryCalculationException), but it is not handled or propagated properly. So, it will not compile without addressing this issue.

2. This code will compile if in method paySalaries() we return a boolean instead of void.
This statement is incorrect because the issue with the compilation lies in the exception handling, not the return type of the paySalaries() method. Changing the return type to boolean will not resolve the compilation problem.

3. This code will compile if we add a try-catch block in paySalaries().
This statement is correct. By adding a try-catch block in the paySalaries() method, we can catch the SalaryCalculationException thrown by the calculateSalary() method. This way, the exception is properly handled, and the code will compile successfully.

4. This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries().
This statement is correct. By adding the throws SalaryCalculationException clause to the paySalaries() method signature, we indicate that this method may throw the SalaryCalculationException. This way, the exception is propagated to the caller, and the code will compile successfully.

Therefore, the correct statement is D. 3 and 4.
42
What will be the output of the following piece of code:
class Person{
    public void talk() {}
}
public class Test{
    public static void main(String args[]){
        Person p = null;
        try{
                p.talk();
        }
        catch(NullPointerException e){
                System.out.print("There is a NullPointerException. ");
        }
        catch(Exception e){
                System.out.print("There is an Exception. ");
        }
        System.out.print("Everything went fine. ");
    }
}
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
43
Determine output of the following program code?
public class Test{
      public static void main(String args[]){
            int i;
            try{
                  i = calculate();
                  System.out.println(i);
            }catch(Exception e){
                  System.out.println("Error occured");
            }
      }

      static int calculate(){
            return (7/2);
      }
}
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
44
public class Test{
      public static void main(String args[]){
            try{
                  int a = Integer.parseInt("four");
            }
      }
}

Which exception could be handled by the catch block for above?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
45
What will be the output?
class MyClass{
      public String test(){
            try{
                  System.out.print("One");
                  return "";
            }
            finally{
                  System.out.print("Two");
            }
      }
}

public class Test{
      public static void main(String args[]){
            MyClass m =  new MyClass();
            m.test();
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Finally block will execute irrespective of return statement in try block.

46
What will be the result after compiling this code?
class SuperClass{
      public int doIt(String str, Integer... data)throws Exception{
            String signature = "(String, Integer[])";
            System.out.println(str + " " + signature);
            return 1;
      }
}

public class Test extends SuperClass{
      public int doIt(String str, Integer... data){
            String signature = "(String, Integer[])";
            System.out.println("Overridden: " + str + " " +signature);
            return 0;
      }

      public static void main(String... args){
            SuperClass sb = new Test();
            sb.doIt("hello", 3);
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Exceptions must be caught or declared to be thrown.

47
try{
      File f = new File("a.txt");
}catch(Exception e){
}catch(IOException io){
}

Is this code create new file name a.txt ?
Discuss
Answer & Solution
Answer: Option C
Solution:

IOException is unreachable to compiler because all exception is going to catch by Exception block.

48
What is the output for the below code?
import java.io.FileNotFoundException;
class A{
      public void printName() throws FileNotFoundException{
            System.out.println("Value-A");
      }
}
class B extends A{
      public void printName() throws NullPointerException{
            System.out.println("Name-B");
      }
}
public class Test{
      public static void main (String[] args) throws Exception{
            A a = new B();
            a.printName();
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

The overriding method can throw any unchecked (runtime) exception, regardless of exception thrown by overridden method. NullPointerException is RuntimeException so compiler will not complain.

49
What will be the result of executing the following code?
public class Test{
      public void divide(int a, int b){
            try{
	          int c = a / b;
   	    }catch(Exception e){
	          System.out.print("Exception ");
  	    }finally{
	          System.out.println("Finally");
      }

      public static void main(String args[]){
            Test t = new Test();
            t.divide(0,3);
      }
}
Discuss
Answer & Solution
Answer: Option D
Solution:

Finally blocks are always executed whether exception occurs or not.
0/3 = 0 Does not throws exception.

50
Which of the below statement is/are true about Error?
A. An Error is a subclass of Throwable.
B. An Error is a subclass of Exception.
C. Error indicates serious problems that a reasonable application should not try to catch.
D. An Error is a subclass of IOException.
Discuss
Answer & Solution
Answer: Option C
Solution:
Statement A: This is true. In Java, Error is indeed a direct subclass of Throwable, which is the superclass of all errors and exceptions in the Java language.

Statement B: This is false. Error is not a subclass of Exception. Both Error and Exception are direct subclasses of Throwable but are distinct from each other.

Statement C: This is true. Errors generally indicate serious problems that a reasonable application should not try to catch. These are conditions that are usually outside the control of the program and require intervention at a higher level (such as the runtime environment). Examples include virtual machine errors, out-of-memory errors, etc.

Statement D: This is false. Error is not a subclass of IOException. IOException is a subclass of Exception, specifically used for handling I/O-related issues.

Correct Statements: A and C:
Option C is correct because it includes the accurate statements about Error in Java.
Example for better understanding: In Java, Error and Exception classes are used for different purposes. Errors indicate problems that are typically not expected to be caught by applications, such as out-of-memory errors, while exceptions are conditions that applications might want to catch and handle, such as file not found exceptions.