SlideShare a Scribd company logo
1 of 16
Exception Handling
   Duration 30 min.
Q1
What will the following code print when run?

public class Test
{
                static String j = "";
                public static void method( int i)
                {
                                   try
                                   {
                                                    if(i == 2)
                                                    {
                                                                 throw new Exception();
                                                    }
                                                     j += "1";
                                   }
                                   catch (Exception e)
                                   {
                                                    j += "2";
                                                   return;
                                   }
                                   finally
                                   {
                                                   j += "3";
                                    }
                                   j += "4";
                  }
 public static void main(String args[])
 {
  method(1);
  method(2);
  System.out.println(j);
 }
}
Select 1 correct option.
a 13432
b 13423
c 14324
d 12434
e 12342
Q2
Which digits, and in which order, will be printed when the following program is run?

public class TestClass
{
  public static void main(String args[])
  {
    int k = 0;
    try{
       int i = 5/k;
    }
    catch (ArithmeticException e){
       System.out.println("1");
    }
    catch (RuntimeException e){
       System.out.println("2");
       return ;
    }
    catch (Exception e){
       System.out.println("3");
    }
    finally{
       System.out.println("4");
    }
    System.out.println("5");
  }
}

Select 1 correct option.
a The program will print 5.
b The program will print 1 and 4, in that order.
c The program will print 1, 2 and 4, in that order.
d The program will print 1, 4 and 5, in that order.
e The program will print 1,2, 4 and 5, in that order.
Q3

What class of objects can be declared by the throws clause?


Select 3 correct options
a Exception


b Error


c Event


d Object


e RuntimeException
Q4
public class TestClass
{
   class MyException extends Exception {}
   public void myMethod() throws XXXX
   {
      throw new MyException();
   }
}
What should replace XXXX?

Select 3 correct options
a MyException

b Exception

c No throws clause is necessary

d Throwable

e RuntimeException
Q5
What will the following code print when run?

public class Test
{
                  static String s = "";
                   public static void m0(int a, int b)
                  {
                                    s +=a;
                                     m2();
                                      m1(b);
                   }
                   public static void m1(int i)
                  {
                                    s += i;
                  }
                   public static void m2()
                  {
                                    throw new NullPointerException("aa");
                  }
                   public static void m()
                  {
                                    m0(1, 2);
                                      m1(3);
                  }
 public static void main(String args[])
 {
   try
   {
                   m();
   }
   catch(Exception e){ }
   System.out.println(s);
 }
}

a   1
b   12
c   123
d   2
e   It will throw exception at runtime.
Q6
What will be the output of the following class...


class Test
{
  public static void main(String[] args)
  {
    int j = 1;
    try
    {
      int i = doIt() / (j = 2);
    } catch (Exception e)
    {
      System.out.println(" j = " + j);
    }
  }
  public static int doIt() throws Exception { throw new
Exception("FORGET IT"); }
}

Select 1 correct option.
a It will print j = 1;

b It will print j = 2;

c The value of j cannot be determined.

d It will not compile.

e None of the above.
Q7
What will be the result of compiling and running the following program ?

class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest
{
   public static void main(String[] args) throws Exception
   {
     try
     {
         m2();
     }
     finally
     {
         m3();
     }
     catch (NewException e){}
   }
   public static void m2() throws NewException { throw new NewException(); }
   public static void m3() throws AnotherException{ throw new AnotherException(); }
}

Select 1 correct option.
a It will compile but will throw AnotherException when run.
b It will compile but will throw NewException when run.
c It will compile and run without throwing any exceptions.
d It will not compile.
e None of the above.
Q8
class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExcceptionTest
{
  public static void main(String [] args) throws Exception
  {
     try
     {
        m2();
     }
     finally{ m3(); }
   }
   public static void m2() throws NewException{ throw new NewException(); }
   public static void m3() throws AnotherException{ throw new
AnotherException(); }
}

Select 1 correct option.
a It will compile but will throw AnotherException when run.
b It will compile but will throw NewException when run.
c It will compile and run without throwing any exceptions.
d It will not compile.
e None of the above.
Q9
What will be the result of attempting to compile and run the following program?
public class TestClass
{
  public static void main(String args[])
  {
    try
    {
      ArithmeticException re=null;
      throw re;
    }
    catch(Exception e)
    {
      System.out.println(e);
    }
  }
}
Select 1 Correct option

a. The code will fail to compile, since RuntimeException cannot be caught by catching an
Exception.
b. The program will fail to compile, since re is null.
c. The program will compile without error and will print java.lang.RuntimeException when run.
d. The program will compile without error and will print java.lang.NullPointerException when
run.
e. The program will compile without error and will run and print 'null'.
Q10
Given the following program, which of these statements are true?
public class FinallyTest
{
  public static void main(String args[])
  {
    try
    {
        if (args.length == 0) return;
        else throw new Exception("Some Exception");
    }
    catch(Exception e)
    {
        System.out.println("Exception in Main");
    }
    finally
    {
        System.out.println("The end");
    }
  }
}
Select 2 correct options
a If run with no arguments, the program will only print 'The end'.
b If run with one argument, the program will only print 'The end'.
c If run with one argument, the program will print 'Exception in Main' and 'The end'.
d If run with one argument, the program will only print 'Exception in Main'.
e Only one of the above is correct.
Q11
class E1 extends Exception{ }
class E2 extends E1 { }
class Test
{
            public static void main(String[] args)
            {
                        try{
                                  throw new E2();
                        }
                        catch(E1 e){
                                  System.out.println("E1");
                        }
                        catch(Exception e){
                                  System.out.println("E");
                        }
                        finally{
                                  System.out.println("Finally");
                        }
            }
}
Select 1 correct option.
a It will not compile.
b It will print E1 and Finally.
c It will print E1, E and Finally.
d It will print E and Finally.
e It will print Finally.
Q12

What will be the output of the following program?
class TestClass
{
            public static void main(String[] args) throws Exception
            {
                         try{
                                    amethod();
                                    System.out.println("try");
                         }
                         catch(Exception e){
                                    System.out.println("catch");
                         }
                         finally    {
                                    System.out.println("finally");
                         }
                         System.out.println("out");
            }
            public static void amethod(){ }
}


Select 1 correct option.
a try finally
b try finally out
c try out
d catch finally out
e It will not compile because amethod() does not throw any exception.
Q13
class MyException extends Throwable{}
class MyException1 extends MyException{}
class MyException2 extends MyException{}
class MyException3 extends MyException2{}
public class ExceptionTest
{
                void myMethod() throws MyException
                {
                                  throw new MyException3();
                }
                public static void main(String[] args)
                {
                                  ExceptionTest et = new ExceptionTest();
                                  try
                                  {
                                                   et.myMethod();
                                  }
                                  catch(MyException me)
                                  {
                                                   System.out.println("MyException thrown");
                                  }
                                  catch(MyException3 me3)
                                  {
                                                   System.out.println("MyException3 thrown");
                                  }
                                  finally
                                  {
                                                   System.out.println(" Done");
                                  }
                }
}

Select 1 correct option.
a MyException thrown
b MyException3 thrown
c MyException thrown Done
d MyException3 thrown Done
e It fails to compile
Q14

What letters, and in what order, will be printed when the following program
is compiled and run?
public class FinallyTest
{
  public static void main(String args[]) throws Exception
  {
     try
     {
        m1();
        System.out.println("A");
     }
     finally
     {
        System.out.println("B");
     }
     System.out.println("C");
  }
  public static void m1() throws Exception { throw new Exception(); }
}
Select 1 correct option.
a It will print C and B, in that order.
b It will print A and B, in that order.
c It will print B and throw Exception.
d It will print A, B and C in that order.
e Compile time error.
Q15

What is wrong with the following code written in a single file named TestClass.java?

class SomeThrowable extends Throwable { }
class MyThrowable extends SomeThrowable { }
public class TestClass
{
  public static void main(String args[]) throws SomeThrowable
  {
    try{
       m1();
    }catch(SomeThrowable e){
       throw e;
    }finally{
       System.out.println("Done");
    }
  }
  public static void m1() throws MyThrowable
  {
    throw new MyThrowable();
  }
}

Select 2 correct options
a The main declares that it throws SomeThrowable but throws MyThrowable.
b You cannot have more than 2 classes in one file.
c The catch block in the main method must declare that it catches MyThrowable rather than
SomeThrowable
d There is nothing wrong with the code.
e 'Done' will be printed.

More Related Content

What's hot

Test string and array
Test string and arrayTest string and array
Test string and arrayNabeel Ahmed
 
Import java
Import javaImport java
Import javaheni2121
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphismUsama Malik
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Nishan Barot
 
SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8Chaitanya Ganoo
 
Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Intel® Software
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
Data structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU ExamsData structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU ExamsiCreateWorld
 
Fnt software solutions placement paper
Fnt software solutions placement paperFnt software solutions placement paper
Fnt software solutions placement paperfntsofttech
 
Java concurrency begining
Java concurrency   beginingJava concurrency   begining
Java concurrency beginingmaksym220889
 

What's hot (20)

Test string and array
Test string and arrayTest string and array
Test string and array
 
Import java
Import javaImport java
Import java
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
Kotlin decompiled
Kotlin decompiledKotlin decompiled
Kotlin decompiled
 
Java practical
Java practicalJava practical
Java practical
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
Java programs
Java programsJava programs
Java programs
 
Mutation @ Spotify
Mutation @ Spotify Mutation @ Spotify
Mutation @ Spotify
 
Java programs
Java programsJava programs
Java programs
 
SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8SoCal Code Camp 2015: An introduction to Java 8
SoCal Code Camp 2015: An introduction to Java 8
 
Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*
 
7
77
7
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
14 thread
14 thread14 thread
14 thread
 
Data structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU ExamsData structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU Exams
 
Fnt software solutions placement paper
Fnt software solutions placement paperFnt software solutions placement paper
Fnt software solutions placement paper
 
Unit testing concurrent code
Unit testing concurrent codeUnit testing concurrent code
Unit testing concurrent code
 
Java concurrency begining
Java concurrency   beginingJava concurrency   begining
Java concurrency begining
 

Similar to Exception handling

4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладкаDEVTYPE
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentrohitgudasi18
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - iiRavindra Rathore
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginnersishan0019
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressionsLogan Chien
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...MaruMengesha
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة السادسة
شرح مقرر البرمجة 2   لغة جافا - الوحدة السادسةشرح مقرر البرمجة 2   لغة جافا - الوحدة السادسة
شرح مقرر البرمجة 2 لغة جافا - الوحدة السادسةجامعة القدس المفتوحة
 

Similar to Exception handling (20)

4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
 
STS4022 Exceptional_Handling
STS4022  Exceptional_HandlingSTS4022  Exceptional_Handling
STS4022 Exceptional_Handling
 
sample_midterm.pdf
sample_midterm.pdfsample_midterm.pdf
sample_midterm.pdf
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Awt
AwtAwt
Awt
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Java generics
Java genericsJava generics
Java generics
 
Exception handling
Exception handlingException handling
Exception handling
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - ii
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
 
Lab4
Lab4Lab4
Lab4
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Chapter v(error)
Chapter v(error)Chapter v(error)
Chapter v(error)
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة السادسة
شرح مقرر البرمجة 2   لغة جافا - الوحدة السادسةشرح مقرر البرمجة 2   لغة جافا - الوحدة السادسة
شرح مقرر البرمجة 2 لغة جافا - الوحدة السادسة
 

Recently uploaded

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Recently uploaded (20)

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 

Exception handling

  • 1. Exception Handling Duration 30 min.
  • 2. Q1 What will the following code print when run? public class Test { static String j = ""; public static void method( int i) { try { if(i == 2) { throw new Exception(); } j += "1"; } catch (Exception e) { j += "2"; return; } finally { j += "3"; } j += "4"; } public static void main(String args[]) { method(1); method(2); System.out.println(j); } } Select 1 correct option. a 13432 b 13423 c 14324 d 12434 e 12342
  • 3. Q2 Which digits, and in which order, will be printed when the following program is run? public class TestClass { public static void main(String args[]) { int k = 0; try{ int i = 5/k; } catch (ArithmeticException e){ System.out.println("1"); } catch (RuntimeException e){ System.out.println("2"); return ; } catch (Exception e){ System.out.println("3"); } finally{ System.out.println("4"); } System.out.println("5"); } } Select 1 correct option. a The program will print 5. b The program will print 1 and 4, in that order. c The program will print 1, 2 and 4, in that order. d The program will print 1, 4 and 5, in that order. e The program will print 1,2, 4 and 5, in that order.
  • 4. Q3 What class of objects can be declared by the throws clause? Select 3 correct options a Exception b Error c Event d Object e RuntimeException
  • 5. Q4 public class TestClass { class MyException extends Exception {} public void myMethod() throws XXXX { throw new MyException(); } } What should replace XXXX? Select 3 correct options a MyException b Exception c No throws clause is necessary d Throwable e RuntimeException
  • 6. Q5 What will the following code print when run? public class Test { static String s = ""; public static void m0(int a, int b) { s +=a; m2(); m1(b); } public static void m1(int i) { s += i; } public static void m2() { throw new NullPointerException("aa"); } public static void m() { m0(1, 2); m1(3); } public static void main(String args[]) { try { m(); } catch(Exception e){ } System.out.println(s); } } a 1 b 12 c 123 d 2 e It will throw exception at runtime.
  • 7. Q6 What will be the output of the following class... class Test { public static void main(String[] args) { int j = 1; try { int i = doIt() / (j = 2); } catch (Exception e) { System.out.println(" j = " + j); } } public static int doIt() throws Exception { throw new Exception("FORGET IT"); } } Select 1 correct option. a It will print j = 1; b It will print j = 2; c The value of j cannot be determined. d It will not compile. e None of the above.
  • 8. Q7 What will be the result of compiling and running the following program ? class NewException extends Exception {} class AnotherException extends Exception {} public class ExceptionTest { public static void main(String[] args) throws Exception { try { m2(); } finally { m3(); } catch (NewException e){} } public static void m2() throws NewException { throw new NewException(); } public static void m3() throws AnotherException{ throw new AnotherException(); } } Select 1 correct option. a It will compile but will throw AnotherException when run. b It will compile but will throw NewException when run. c It will compile and run without throwing any exceptions. d It will not compile. e None of the above.
  • 9. Q8 class NewException extends Exception {} class AnotherException extends Exception {} public class ExcceptionTest { public static void main(String [] args) throws Exception { try { m2(); } finally{ m3(); } } public static void m2() throws NewException{ throw new NewException(); } public static void m3() throws AnotherException{ throw new AnotherException(); } } Select 1 correct option. a It will compile but will throw AnotherException when run. b It will compile but will throw NewException when run. c It will compile and run without throwing any exceptions. d It will not compile. e None of the above.
  • 10. Q9 What will be the result of attempting to compile and run the following program? public class TestClass { public static void main(String args[]) { try { ArithmeticException re=null; throw re; } catch(Exception e) { System.out.println(e); } } } Select 1 Correct option a. The code will fail to compile, since RuntimeException cannot be caught by catching an Exception. b. The program will fail to compile, since re is null. c. The program will compile without error and will print java.lang.RuntimeException when run. d. The program will compile without error and will print java.lang.NullPointerException when run. e. The program will compile without error and will run and print 'null'.
  • 11. Q10 Given the following program, which of these statements are true? public class FinallyTest { public static void main(String args[]) { try { if (args.length == 0) return; else throw new Exception("Some Exception"); } catch(Exception e) { System.out.println("Exception in Main"); } finally { System.out.println("The end"); } } } Select 2 correct options a If run with no arguments, the program will only print 'The end'. b If run with one argument, the program will only print 'The end'. c If run with one argument, the program will print 'Exception in Main' and 'The end'. d If run with one argument, the program will only print 'Exception in Main'. e Only one of the above is correct.
  • 12. Q11 class E1 extends Exception{ } class E2 extends E1 { } class Test { public static void main(String[] args) { try{ throw new E2(); } catch(E1 e){ System.out.println("E1"); } catch(Exception e){ System.out.println("E"); } finally{ System.out.println("Finally"); } } } Select 1 correct option. a It will not compile. b It will print E1 and Finally. c It will print E1, E and Finally. d It will print E and Finally. e It will print Finally.
  • 13. Q12 What will be the output of the following program? class TestClass { public static void main(String[] args) throws Exception { try{ amethod(); System.out.println("try"); } catch(Exception e){ System.out.println("catch"); } finally { System.out.println("finally"); } System.out.println("out"); } public static void amethod(){ } } Select 1 correct option. a try finally b try finally out c try out d catch finally out e It will not compile because amethod() does not throw any exception.
  • 14. Q13 class MyException extends Throwable{} class MyException1 extends MyException{} class MyException2 extends MyException{} class MyException3 extends MyException2{} public class ExceptionTest { void myMethod() throws MyException { throw new MyException3(); } public static void main(String[] args) { ExceptionTest et = new ExceptionTest(); try { et.myMethod(); } catch(MyException me) { System.out.println("MyException thrown"); } catch(MyException3 me3) { System.out.println("MyException3 thrown"); } finally { System.out.println(" Done"); } } } Select 1 correct option. a MyException thrown b MyException3 thrown c MyException thrown Done d MyException3 thrown Done e It fails to compile
  • 15. Q14 What letters, and in what order, will be printed when the following program is compiled and run? public class FinallyTest { public static void main(String args[]) throws Exception { try { m1(); System.out.println("A"); } finally { System.out.println("B"); } System.out.println("C"); } public static void m1() throws Exception { throw new Exception(); } } Select 1 correct option. a It will print C and B, in that order. b It will print A and B, in that order. c It will print B and throw Exception. d It will print A, B and C in that order. e Compile time error.
  • 16. Q15 What is wrong with the following code written in a single file named TestClass.java? class SomeThrowable extends Throwable { } class MyThrowable extends SomeThrowable { } public class TestClass { public static void main(String args[]) throws SomeThrowable { try{ m1(); }catch(SomeThrowable e){ throw e; }finally{ System.out.println("Done"); } } public static void m1() throws MyThrowable { throw new MyThrowable(); } } Select 2 correct options a The main declares that it throws SomeThrowable but throws MyThrowable. b You cannot have more than 2 classes in one file. c The catch block in the main method must declare that it catches MyThrowable rather than SomeThrowable d There is nothing wrong with the code. e 'Done' will be printed.