Sail E0 Webinar

MCQs

Total Questions : 23 | Page 2 of 3 pages
Question 11. 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().
  1.    2 and 3
  2.    2 and 4
  3.    3 and 4
  4.    1 and 2
  5.    1 and 4
 Discuss Question
Answer: Option C. -> 3 and 4
Question 12. 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?
  1.    ArrayIndexOutOfBoundsException
  2.    NumberFormatException
  3.    None of these
  4.    IllegalStateException
  5.    ClassCastException
 Discuss Question
Answer: Option B. -> NumberFormatException
Question 13. 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); }}
  1.    Error occured
  2.    Compilation Error
  3.    3.5
  4.    None of these
  5.    3
 Discuss Question
Answer: Option E. -> 3
Question 14. 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. "); }}
  1.    There is a NullPointerException. There is an Exception.
  2.    There is a NullPointerException.
  3.    There is a NullPointerException. Everything went fine.
  4.    This code will not compile, because in Java there are no pointers.
 Discuss Question
Answer: Option C. -> There is a NullPointerException. Everything went fine.
Question 15. 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(); }}
  1.    Compilation Error
  2.    Two
  3.    One
  4.    None of these
  5.    One Two
 Discuss Question
Answer: Option E. -> One Two
Question 16. Try{ File f = new File("a.txt");}catch(Exception e){}catch(IOException io){}Is this code create new file name a.txt ?
  1.    Compilation Error
  2.    false
  3.    None of these
  4.    true
 Discuss Question
Answer: Option A. -> Compilation Error
Question 17. 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(); }}
  1.    printName()
  2.    Compilation succeed but no output
  3.    None of these
  4.    Value-A
  5.    Compilation fails-Exception NullPointerException is not compatible with throws clause in
  6.    Name-B
 Discuss Question
Answer: Option F. -> Name-B
Question 18. 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); }}
  1.    Compile with error
  2.    None of these
  3.    Prints out: Exception
  4.    Prints out: Finally
  5.    Prints out: Exception Finally
 Discuss Question
Answer: Option D. -> Prints out: Finally
Question 19. Predict the output:public class Test{ public static void main(String args[]){ try{ String arr[] = new String[10]; arr = null; arr[0] = "one"; System.out.print(arr[0]); }catch(Exception ex){ System.out.print("exception"); }catch(NullPointerException nex){ System.out.print("null pointer exception"); } }}
  1.    "one" is printed.
  2.    "null pointer exception" is printed.
  3.    "exception" is printed.
  4.    None of these
  5.    Compilation fails saying NullPointerException has already been caught.
 Discuss Question
Answer: Option E. -> Compilation fails saying NullPointerException has already been caught.
Question 20. 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.
  1.    B and C
  2.    A and D
  3.    A and C
  4.    A and B
  5.    B and D
 Discuss Question
Answer: Option D. -> A and B

Latest Videos

Latest Test Papers