Oracle Java Interview Questions & Answers Set-4

«« Previous
Next »»

61) Is it possible to instantiate the abstract class?
Ans: No, abstract class can never be instantiated.


62) What is interface?
Ans: Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.


63) Can you declare an interface method static?
Ans: No, because methods of an interface is abstract by default, and static and abstract keywords can't be used together.


64) Can an Interface be final?
Ans: No, because its implementation is provided by another class.


65) What is marker interface?
Ans: An interface that have no data member and method is known as a marker interface.For example Serializable, Cloneable etc.


66) What is difference between abstract class and interface?
Ans: 


Abstract class
Interface
1)An abstract class can have method body (non-abstract methods). Interface have only abstract methods.
2)An abstract class can have instance variables. An interface cannot have instance variables.
3)An abstract class can have constructor. Interface cannot have constructor.
4)An abstract class can have static methods. Interface cannot have static methods.
5)You can extends one abstract class. You can implement multiple interfaces.


67) Can we define private and protected modifiers for variables in interfaces?
Ans: No, they are implicitly public.


68) When can an object reference be cast to an interface reference?
Ans: An object reference can be cast to an interface reference when the object implements the referenced interface.


Core Java - OOPs Concepts : Package Interview Questions



69) What is package?
Ans: A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.


70) Do I need to import java.lang package any time? Why ?
Ans: No. It is by default loaded internally by the JVM.


71) Can I import same package/class twice? Will the JVM load the package twice at runtime?
Ans: One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.


72) What is static import ?
Ans: By static import, we can access the static members of a class directly, there is no to qualify it with the class name.


Core Java : Exception Handling Interview Questions



73) What is Exception Handling?
Ans: Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.


74) What is difference between Checked Exception and Unchecked Exception?
Ans: 
1)Checked Exception

The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.

2)Unchecked Exception

The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.


75) What is the base class for Error and Exception?
Ans: Throwable.


76) Is it necessary that each try block must be followed by a catch block?
Ans: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.


77) What is finally block?
Ans: finally block is a block that is always executed.


78) Can finally block be used without catch?
Ans: Yes, by try block. finally must be followed by either try or catch.more details...


79) Is there any case when finally will not be executed?
Ans: finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).


80) What is difference between throw and throws?
Ans: 
throw keyword
throws keyword
1)throw is used to explicitly throw an exception. Interface have only abstract methods.
2)checked exceptions can not be propagated with throw only. checked exception can be propagated with throws.
3)throw is followed by an instance. throws is followed by class.
4)throw is used within the method. throws is used with the method signature.
5)You can extends one abstract class. You can declare multiple exception e.g. public void method()throws IOException,SQLException.

«« Previous
Next »»