Similar documents
More on Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

More on Exception Handling

Fundamentals of Object Oriented Programming

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Introduction to Software Design

Exception Handling in Java

Object Oriented Programming

CSC 1214: Object-Oriented Programming

Java Errors and Exceptions. Because Murphy s Law never fails

BBM 102 Introduction to Programming II Spring 2017

Unit 5 - Exception Handling & Multithreaded

COE318 Lecture Notes Week 10 (Nov 7, 2011)

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Full file at Chapter 2 - Inheritance and Exception Handling

Tutorial 8 Date: 15/04/2014

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

Recitation 3. 2D Arrays, Exceptions

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Exception-Handling Overview

National University. Faculty of Computer Since and Technology Object Oriented Programming

For more details on SUN Certifications, visit

Data Structures. 02 Exception Handling

Download link: Java Exception Handling

UNIT - V. Inheritance Interfaces and inner classes Exception handling Threads Streams and I/O

Class, Variable, Constructor, Object, Method Questions

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

COMP1008 Exceptions. Runtime Error

Exceptions. CSC207 Winter 2017

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

1. Find the output of following java program. class MainClass { public static void main (String arg[])

C16b: Exception Handling

Exception Handling CSCI 201 Principles of Software Development

Programming II (CS300)

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Exceptions - Example. Exceptions - Example

Chapter 13 Exception Handling

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Exceptions. Examples of code which shows the syntax and all that

Sri Vidya College of Engineering & Technology Question Bank

Exceptions: When something goes wrong. Image from Wikipedia

Types, Values and Variables (Chapter 4, JLS)

Object Oriented Programming Exception Handling

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Operators and Expressions

Programming - 2. Common Errors

Programming II (CS300)

Exceptions. What exceptional things might our programs run in to?

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Exceptions Handling Errors using Exceptions

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Introduction to Java. Handout-3a. cs402 - Spring

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Internal Classes and Exceptions

Std 12 Lesson-10 Exception Handling in Java ( 1

Exception Examples. All examples written by Edith Hemaspaandra and modified by Jessica Bayliss

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Rules and syntax for inheritance. The boring stuff

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Program Correctness and Efficiency. Chapter 2

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

ECE 122. Engineering Problem Solving with Java

17. Handling Runtime Problems

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Java Exceptions Version June 2009

Object-Oriented Design. March 2005 Object Oriented Design 1

Chapter 12 Exception Handling

Programming overview

Exception Handling. --After creating exception object,method handover that object to the JVM.

Module 4 - 异常和断言 一 选择题 :

CS 61B Discussion 5: Inheritance II Fall 2014

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Index COPYRIGHTED MATERIAL

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

ITI Introduction to Computing II

Lecture 19 Programming Exceptions CSE11 Fall 2013

Unit 4. Exception handling mechanism. new look try/catch mechanism in Java Enumeration in Java 5 - usage.

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

CSCI 261 Computer Science II


CS159. Nathan Sprague

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Constructor in Java. What is a Constructor? Rules for create a Java Constructor

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

ASSERTIONS AND LOGGING

Transcription:

Exceptions Questions https://www.journaldev.com/2167/java-exception-interview-questionsand-answers https://www.baeldung.com/java-exceptions-interview-questions https://javaconceptoftheday.com/java-exception-handling-interviewquestions-and-answers/ https://www.geeksforgeeks.org/java-exception-handling-question-1/ https://www.javamadesoeasy.com/2015/05/exceptions-top-60- interview-questions_16.html https://www.journaldev.com/2167/java-exception-interview-questionsand-answers 1. What is Exception in Java? 2. What are the Exception Handling Keywords in Java? 3. Explain Java Exception Hierarchy? 4. What are important methods of Java Exception Class? 5. Explain Java 7 ARM Feature and multi-catch block? 6. What is difference between Checked and Unchecked Exception in Java? 7. What is difference between throw and throws keyword in Java? 8. How to write custom exception in Java? 9. What is OutOfMemoryError in Java? 10. What are different scenarios causing Exception in thread main? 11. What is difference between final, finally and finalize in Java? 12. What happens when exception is thrown by main method? 13. Can we have an empty catch block? 14. Provide some Java Exception Handling Best Practices? 15. What is the problem with below programs and how do we fix it? https://www.baeldung.com/java-exceptions-interview-questions What is an exception? What is the purpose of the throw and throws keywords?

How can you handle an exception? How can you catch multiple exceptions? What is the difference between a checked and an unchecked exception? What is the difference between an exception and error? What exception will be thrown executing the following code block? Integer[][] ints = 1, 2, 3, null, 7, 8, 9 ; System.out.println("value = " + ints[1][1].intvalue()); What is exception chaining? What is a stacktrace and how does it relate to an exception? Why would you want to subclass an exception? What are some advantages of exceptions? Can you throw any exception inside a lambda expression s body? What are the rules we need to follow when overriding a method that throws an exception? Will the following code compile?

void dosomething() //... throw new RuntimeException(new Exception("Chained Exception")); Is there any way of throwing a checked exception from a method that does not have a throws clause? https://javaconceptoftheday.com/java-exception-handling-interviewquestions-and-answers/ What is an exception? How the exceptions are handled in java? OR Explain exception handling mechanism in java? What is the difference between error and exception in java? Can we keep other statements in between, catch and finally blocks? Can we write only block without catch and finally blocks? There are three statements in a block statement1, statement2 and statement3. After that there is a catch block to catch the exceptions occurred in the block. Assume that exception has occurred in statement2. Does statement3 get executed or not? What is unreachable catch block error? Explain the hierarchy of exceptions in java? What are run time exceptions in java. Give example? What is OutOfMemoryError in java? What are checked and unchecked exceptions in java? What is the difference between ClassNotFoundException and NoClassDefFoundError in java Can we keep the statements after finally block If the control is returning from the finally block itself? Does finally block get executed If either or catch blocks are returning the control? Can we throw an exception manually? If yes, how? What is Re-throwing an exception in java? What is the use of throws keyword in java? What is the difference between final, finally and finalize in java? How do you create customized exceptions in java? What is ClassCastException in java? What is the difference between throw, throws and throwable in java? What is StackOverflowError in java? Can we override a super class method which is throwing an unchecked exception with checked exception in the sub class? What are chained exceptions in java? Which class is the super class for all types of errors and exceptions in java? What are the legal combinations of, catch and finally blocks? What is the use of printstacktrace() method? Give some examples to checked exceptions? Give some examples to unchecked exceptions?

https://www.geeksforgeeks.org/java-exception-handling-question-1/ Predict the output of following Java program class Main public static void main(string args[]) throw 10; catch(int e) System.out.println("Got the Exception " + e); (A) Got the Exception 10 (B) Got the Exception 0 (C) Compiler Error class Test extends Exception class Main public static void main(string args[]) throw new Test(); catch(test t) System.out.println("Got the Test Exception"); finally System.out.println("Inside finally block "); (A) Got the Test Exception Inside finally block (B) Got the Test Exception (C) Inside finally block (D) Compiler Error Output of following Java program?

class Main public static void main(string args[]) int x = 0; int y = 10; int z = y/x; (A) Compiler Error (B) Compiles and runs fine (C) Compiles fine but throws ArithmeticException exception class Base extends Exception class Derived extends Base public class Main public static void main(string args[]) // some other stuff // Some monitored code throw new Derived(); catch(base b) System.out.println("Caught base class exception"); catch(derived d) System.out.println("Caught derived class exception"); (A) Caught base class exception (B) Caught derived class exception (C) Compiler Error because derived is not throwable (D) Compiler Error because base class exception is caught before derived class class Test public static void main (String[] args) int a = 0; System.out.println ("a = " + a + "\n"); int b = 20 / a; System.out.println ("b = " + b); catch(arithmeticexception e) System.out.println ("Divide by zero error"); finally System.out.println ("inside the finally block");

(A) Compile error (B) Divide by zero error (C) a = 0 Divide by zero error inside the finally block (D) a = 0 (E) inside the finally block class Test public static void main(string[] args) int a[]= 1, 2, 3, 4; for (int i = 1; i <= 4; i++) System.out.println ("a[" + i + "]=" + a[i] + "\n"); catch (Exception e) System.out.println ("error = " + e); catch (ArrayIndexOutOfBoundsException e) System.out.println ("ArrayIndexOutOfBoundsException"); (A) (B) (C) (D) (E) Compiler error Run time error ArrayIndexOutOfBoundsException Error Code is printed Array is printed Predict the output of the following program. class Test String str = "a"; void A()

str +="b"; B(); catch (Exception e) str += "c"; void B() throws Exception str += "d"; C(); catch(exception e) throw new Exception(); finally str += "e"; str += "f"; void C() throws Exception throw new Exception(); void display() System.out.println(str); public static void main(string[] args) Test object = new Test(); object.a(); object.display(); (A) (B) (C) Abdef Abdec abdefc Predict the output of the following program.

class Test int count = 0; void A() throws Exception count++; count++; count++; throw new Exception(); catch(exception ex) count++; throw new Exception(); catch(exception ex) count++; catch(exception ex) count++; void display() System.out.println(count); public static void main(string[] args) throws Exception Test obj = new Test(); obj.a(); obj.display(); (A) 4 (B) 5 (C) 6 (D) Compilation error

https://www.javamadesoeasy.com/2015/05/exceptions-top-60- interview-questions_16.html What is exception in java? Explain exception hierarchy in java?

What are differences between checked and unchecked exceptions in java? What are 5 exception handling keywords in java? Explain what is Error in java? What are differences between Exception and Error in java? Explain throw keyword in java? Explain throws keyword in java? What is difference between throw and throws in java? How to create user defined checked and unchecked Exception in java? How to use -catch-finally in java? Can we use,catch or finally block alone in java? Is it allowed to use multiple catch block in java? What is Automatic resource management in java 7? Explain -with-resource in java? Now, question comes why we need not to close file when we are using Try-with-resources in java? Explain finally keyword in java? Is it allowed to use nested -catch in java?

Discuss which checked and unchecked exception can be thrown/declared by subclass method while overriding superclass method in java? What will happen when catch and finally block both return value, also when and finally both return value in java? What is exception propagation in java?

Can a catch or finally block throw exception in java? Why shouldn t you use Exception for catching all exceptions in java? What is Difference between multiple catch block and multi catch syntax? Can a method be overloaded on basis of exceptions in java? Mention few exception handling best practices in java? Difference between Final, Finally and Finalize in java? What are the differences between between ClassNotFoundException and NoClassDefFoundError in java? What are the most important frequently occurring Exception and Errors which you faced in java?