Saraswat World

What is the use of finally block in java?

What is the use of finally block in java?

JAVA INTERVIEW QUESTIONS

Finally block in java-Finally block is used for cleaning up of resources such as closing connections, sockets etc. if try blockexecutes with no exceptions then finally is called after try block without executing catch block. If there isexception thrown in try block finally block executes immediately after catch block.If an exception is thrown, finally block […]

Can we have multiple catch block for a try block in Java?

Can we have multiple catch block for a try block in Java?

JAVA INTERVIEW QUESTIONS

In some cases our code may throw more than one exception. In such case we can specify two or morecatch clauses, each catch handling different type of exception. When an exception is thrown jvm checkseach catch statement in order and the first one which matches the type of exception is execution andremaining catch blocks are […]

Can we have try block without catch block in Java?

Can we have try block without catch block in Java?

JAVA INTERVIEW QUESTIONS

Each try block requires atleast one catch block or finally block. A try block without catch or finally, will result in compiler error. We can skip either of catch or finally block but not both.

What are try and catch keywords in java?

What are try and catch keywords in java?

JAVA INTERVIEW QUESTIONS

try and catch keywords in java- In try block we define all exception causing code. In java try and catch forms a unit. A catch block catches the exception thrown by preceding try block. Catch block cannot catch an exception thrown by another try block. If there is no exception causing code in our program […]

List out five keywords related to Exception handling in Java ?

List out five keywords related to Exception handling in Java ?

JAVA INTERVIEW QUESTIONS

List of five keywords related to Exception handling in Java1) Try2) Catch3) throw4) throws5) finally

In how many ways we can do exception handling in java?

In how many ways we can do exception handling in java?

JAVA INTERVIEW QUESTIONS

We can handle exceptions in either of the two ways :1) By specifying try catch block where we can catch the exception.2) Declaring a method with throws clause .

What are advantages of Exception handling in java?

What are advantages of Exception handling in java?

JAVA INTERVIEW QUESTIONS

1) Separating normal code from exception handling code to avoid abnormal termination of program.2) Categorizing in to different types of Exceptions so that rather than handling all exceptions withException root class we can handle with specific exceptions. It is recommended to handle exceptions withspecific Exception instead of handling with Exception root class.3) Call stack mechanism […]

What is an error in Java?

What is an error in Java?

JAVA INTERVIEW QUESTIONS

Error is the subclass of Throwable class in java. When errors are caused by our program we call that as Exception, but some times exceptions are caused due to some environment issues such as running out of memory. In such cases we canโ€™t handle the exceptions. Exceptions which cannot be recovered are called as errors […]

What is Exception handling in java?

What is Exception handling in java?

JAVA INTERVIEW QUESTIONS

Exception handling is a mechanism what to do when some abnormal situation arises in program. When an exception is raised in program it leads to termination of program when it is not handled properly. The significance of exception handling comes here in order not to terminate a program abruptly and to continue with the rest […]

State some situations where exceptions may arise in java?

State some situations where exceptions may arise in java?

JAVA INTERVIEW QUESTIONS

1) Accessing an element that does not exist in array.2) Invalid conversion of number to string and string to number.(NumberFormatException)3) Invalid casting of class(Class cast Exception)4) Trying to create object for interface or abstract class(Instantiation Exception)

What is an exception in java?

What is an exception in java?

JAVA INTERVIEW QUESTIONS

In java exception is an object. Exceptions are created when an abnormal situations arise in our program. Exceptions can be created by JVM or by our application code. All Exception classes are defined in java.lang. In otherwords we can say Exception as run time error.

What are abstract methods in java?

What are abstract methods in java?

JAVA INTERVIEW QUESTIONS

An abstract method is the method which doesn’t have any body. Abstract method is declared with keyword abstract and semicolon in place of method body. Signature : public abstract void <method_name>(); Ex : public abstract void getDetails(); It is the responsibility of subclass to provide implementation to abstract method defined in abstract class.

Can we create constructor in abstract class in Java?

Can we create constructor in abstract class in Java?

JAVA INTERVIEW QUESTIONS

We can create constructor in abstract class , it doesn’t give any compilation error. But when we cannot instantiate class there is no use in creating a constructor for abstract class.

Explain about abstract classes in java?

Explain about abstract classes in java?

JAVA INTERVIEW QUESTIONS

Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such case we declare a class as abstract. To make a class abstract we use key word abstract. Any class that contains one […]

What is final access modifier in java?

What is final access modifier in java?

JAVA INTERVIEW QUESTIONS

final access modifier can be used for class, method and variables. The main advantage of final access modifier is security no one can modify our classes, variables and methods. The main disadvantage of final access modifier is we cannot implement oops concepts in java. Ex : Inheritance, polymorphism. final class : A final class cannot […]

Explain what access modifiers can be used for variables in Java?

Explain what access modifiers can be used for variables in Java?

JAVA INTERVIEW QUESTIONS

We can use all access modifiers public, private, protected and default for variables.public : When a variables is declared as public it can be accessed1) In the same class2) In the same package subclass3) In the same package non subclass4) In the different package subclass5) In the different package non subclass. default : When a […]

Explain what access modifiers can be used for methods in Java?

Explain what access modifiers can be used for methods in Java?

JAVA INTERVIEW QUESTIONS

We can use all access modifiers public, private, protected and default for methods.public : When a method is declared as public it can be accessed1) In the same class2) In the same package subclass3) In the same package non subclass4) In the different package subclass5) In the different package non subclass. default : When a […]

What access modifiers can be used for class in Java?

What access modifiers can be used for class in Java?

JAVA INTERVIEW QUESTIONS

We can use only two access modifiers for class public and default.public: A class with public modifier can be visible1) In the same class2) In the same package subclass3) In the same package nonsubclass4) In the different package subclass5) In the different package non subclass.default : A class with default modifier can be accesed1) In […]

What is the difference between access specifiers and access modifiers in java?

What is the difference between access specifiers and access modifiers in java?

JAVA INTERVIEW QUESTIONS

In C++ we have access specifiers as public, private, protected and default and access modifiers as static, final. But there is no such division of access specifiers and access modifiers in java. In Java we have access modifiers and non access modifiers. Access Modifiers : public, private, protected, default Non Access Modifiers : abstract, final, […]

What are access modifiers in java?

What are access modifiers in java?

JAVA INTERVIEW QUESTIONS

The important feature of encapsulation is access control. By preventing access control we can misuse of class, methods and members. A class, method or variable can be accessed is determined by the access modifier. There are three types of access modifiers in java. public, private, protected. If no access modifier is specified then it has […]