Core Java Interview Questions

Q:  Can error occurrences be tracked in a class by extending them with Throwable?

A: Yes, we can do this but this is not a recommended procedure because while is applicable to exceptions, the errors (being serious kinds of exceptions) there is the risk that the JVM will not handle them and it will crash.

Q: Is there any type of specifier that we can use in Java for a local variable?

A: Yes, but the specifier can‘t be private, we can use only the final one. The local variables are already private themselves, so there is no need to call them private. If we do that we will receive errors.

Q: What is a private constructor and why some programmers define it as private only?

A: People do this to keep the objects number in control.

Q: Does the first reference of an object go away if we change the method of that object?

A: No, we don’t lose the reference of that object, in fact we will have two references to that same object and this is because Java makes the passing of the value.

Q: Can we kill the threads from the program all at once by a single command? If yes, how? If not, why?

A: Yes there is a method by giving the reference that null assignment. Or we can wait for the threads to terminate themselves after the execution finishes.

Q: Why do we have the need for using the same name in separate methods if anyway the overload is not helping us achieve a higher performance?

A: The reason for which similar methods may behave differently in some situations is Polymorphism implementation by the use of Overloading.

Q: Is there a separate meaning for each of these classes: StringBuffer and StringBuilder?

A: The difference is that the methods of the StringBuilder have no synchronization regarding threat protection. Also in some cases, the performance of StringBuilder which was introduced in Java with the 1.5 version exceeds that of the StringBuffer.

Q: Is there any good recommendation for the method finalize()?

A: When an object is using the system resources the usage of finalize() method is not really recommended as a solution. We have to call this method before we erase the object and the erasing is done by the use of a garbage collector but this not gives us any guarantees of success. We could replace this method with finally block, as this will be sure to execute.

Q:  Where do we use inner and nested classes and what is the difference between these two?

A: Nested classes are named those classes that exist within other classes. And the inner classes are the most important of the nested classes, they are nested classes that are non-static.

COnsider reading our Business Analyst Interview Questions post.

Q: What is the reason for the use of static keywords in a program’s main class?

A: Using static keywords in front of the main is necessary because the method doesn’t create any object, before making any object we need static calling of the method.

Q: What class is better between Abstract and Interface?

A: These two classes are used in different ways. The Abstract class is used for general things and then it is split into more particular classes. A general class like that can be for example “computers” and this is split in “desktop, laptop, notebook, tablet” etc. And the Interface class refers to a more flexible class that can support a wide range of objects.

Q: Instances and Objects are separated in Java or they are the same thing?

A: A class instance can be an object so this means that objects and instances can be that same thing.

Q: Is the checked exception any different than the unchecked exception?

A: The difference between these two is that the checked ones are mentioned by the throws clause while the unchecked are not, they are runtime exceptions.

Q: In which way are the removed objects chosen by the garbage collector?

A: The garbage collector is a function that is called automatically by the JVM and is checking for objects without reference in the heap memory. In this way the memory is freed by the garbage collector and there will not be any variable that is not reachable.

Q: How does WeakHashMap work?

A: We have to put the object as a key in the WeakHashMap and as a map value we will put extra information. In this way we will be able to find this extra information when checking the map as much as we keep the reference of the object.

Q: Why the creation of an object is impossible for an Abstract class?

A: If someone is calling a method that is abstract there is nothing to do for the JVM in this case. This means that an Abstract class has no methods implemented so even if we try to create an object for it and call a method like this, it is very possible that the program will crash. The solution is creating an instance for an the Abstract class and we can still make an object but under a subclass.

Q: What means access Specifier and Access Modifier?

A: These two terms are actually synonyms because they mean the same things: Private, Protected and Public.

Q: Why use an anonymous class and how?

A: The anonymous classes are used to make objects instantly I cases like variable initialization or as arguments in calling methods

Q: Can the garbage collector pick Methods and Classes?

A: Classes or Methods can’t be picked up by the Garbage Collector because they don’t have a physical correspondence in the memory, usually objects can be collected to let the memory free.

Q: What are static block for?

A: Static blocks are used in the time of class loading if it is required to run blocks of code.

Q: What do we use connection pooling for?

A: The purpose of use for connection pooling is to grow the performance of a program.

Leave a Reply

Your email address will not be published.