Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Ques 266. When is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in Java?

Ans. Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.

Is it helpful? Add Comment View Comments
Ques 267. What is reason of NoClassDefFoundError in Java?
Ans. NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time. for example if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError. Itís important to understand that this is different than ClassNotFoundException which comes while trying to load a class at run-time only and name was provided during runtime not on compile time. Most of the java developer mingle this two Error and gets confused.
Is it helpful? Add Comment View Comments
Ques 268. How to resolve NoClassDefFoundError?
Ans. Obvious reason of NoClassDefFoundError is that a particular class is not available in Classpath, so we need to add that into Classpath or we need to check why itís not available in Classpath if we are expecting it to be. There could be multiple reasons like:

1) Class is not available in Java Classpath.
2) You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.
3) Any startup script is overriding Classpath environment variable.
Is it helpful? Add Comment View Comments
Ques 269. Difference between ClassNotFoundException and NoClassDefFoundError in Java?
Ans. Many a times we confused ourselves with ClassNotFoundException and NoClassDefFoundError, though both of them related to Java Classpath they are completely different to each other. ClassNotFoundException comes when JVM tries to load a class at runtime dynamically means you give the name of class at runtime and then JVM tries to load it and if that class is not found in classpath it throws ClassNotFoundException. While in case of NoClassDefFoundError the problematic class was present during Compile time and that's why program was successfully compile but not available during runtime by any reason. NoClassDefFoundError is easier to solve than ClassNotFoundException in my opinion because here we know that Class was present during build time.
Let me know how exactly you are facing NoClassDefFoundError and I will guide you how to troubleshoot it, if you are facing with something new way than I listed above we will probably document if for benefit of others and again donít afraid with Exception in thread "main" java.lang.NoClassDefFoundError
Is it helpful? Add Comment View Comments
Ques 270. What is java.lang.OutOfMemoryError in Java?
Ans. OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and JVM throws java.lang.OutOfMemoryError when it ran out of memory in heap. OutOfMemoryError in Java can come any time in heap mostly while you try to create an object and there is not enough space in heap to allocate that object. javavdoc of OutOfMemoryError is not very informative about this though.
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2018 WithoutBook