• +91 9971497814
  • info@interviewmaterial.com

Java Interview Questions Answers

Question 1 :  What is ClassDefNotFoundException and NoClassDefFoundError and explain differences between these two?

Answer 1 : Given a class A.. A ClassNotFoundException means that the classloader cannot load class A. A ClassDefNotFoundError means that the classloader can load class A, but cannot instantiate it because it cannot load the other classes that class A depend on.

Question 2 : An application needs to load a library before it starts to run, how to code?

Answer 2 : One option is to use a static block to load a library before anything is called. For example, class Test { static { System.loadLibrary("path-to-library-file"); } .... } When you call new Test(), the static block will be called first before any initialization happens. Note that the static block position may matter.

Question 3 : ArithmeticException?

Answer 3 : The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

Question 4 : Can a for statement loop indefinitely?

Answer 4 : Yes, a for statement can loop indefinitely. For example, consider the following: for(;;) ;

Question 5 : Can a lock be acquired on a class?

Answer 5 : Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

Question 6 : Can a lock be acquired on  a class?

Answer 6 : Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

Question 7 : Can a private method of a superclass be declared within a subclass?

Answer 7 : Sure. A private field or method or inner class belongs to its declared class and hides from its subclasses. There is no way for private stuff to have a runtime overloading or overriding (polymorphism) features.

Question 8 : Can an anonymous class be declared as implementing an interface and extending a class?

Answer 8 : An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

Question 9 : Can an inner class declared inside of a method access local variables of this method?

Answer 9 : It's possible if these variables are final.

Question 10 : Can an object's finalize() method be invoked while it is reachable?

Answer 10 : An object's finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object's finalize() method may be invoked by other objects.

Java Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners