Core Java interview questions

Core Java interview questions Download Core Java interview questions PDF

Core Java interview questions

In order to load the Java Classes into a virtual environment, Classloader are used. A class loader will load key classes such as java.lang.object and several other codes into the memory. Usually, these classes are loaded only if a demand occurs. Since Java Runtime Environment includes loaders, they need not know about file and file systems. Also, the loaders are responsible for converting the named class into an equivalent binary form.

After creating the object JVM internally calls the toString method to generate the indirect address of the object. To string method is present inside object class, which is defined as non-static. So that object class makes it as a superclass for every class in java.

It is not possible in java to write the definition of a function inside another function. It is like we are creating confusion for the compiler in making a decision for which function definition part to terminate and which is not. It is not recommended and not professional. For this purpose, we can use Lambda expression.

Note: Lambda expression is introduced in Java 8 which can be created without belonging to any class.

Deadlock is a situation that occurs when the same resource is required by two processes. This situation occurs in multithreading when one thread holds a resource which is required by the second thread and second thread holds a resource which is required by the first thread. To identify the deadlock, we can use the jConsole/VisualVM, it will show exactly which threads are getting locked and on which object.

Thread scheduler maintains threading states by using primitive and time slicing scheduling. It schedules the tasks by setting priority and time. Only one thread can run at a time in a single process by thread scheduler. When the highest priority is set to a process, the first process at highest priority gets starts before coming into dead or waiting state, after that the next process in the waiting state comes and make it task done and so on. This is how the thread scheduler schedules the task.

The purpose of using multithreading is to make multiple lightweight processes running simultaneously because threads have a plus point, they are lightweight process. Multiple processes not only reserve the memory space but also make the increase the work complication as well. To eliminate the memory space issue and complications we prefer multithreading because they used a shared memory area which saves memory and performs better memory management.

The process of the connecting function call with function body is called binding. When this binding is done at compile time, it is called early binding. Early binding is static binding it is done when the type of an object is determined at compile time. On the hand, late binding is done at runtime. Late binding is dynamic binding because it is done at runtime by the compiler itself.

The purpose of the final keyword is to make things unchanged in application lifeline. We can make a class, method or variable unchanged by writing "final" keyword before the method, class, and variable. When the final is written before the variable, it remains unchanged (you can’t change it). A function which use final keyword cannot be overridden. On the other hand, if we make a class final we cannot extend it. When we have to make these unchanged conditions, we prefer to use the final keyword.

When we have to store a collection of objects which is unordered we prefer the Set interface. The other main reason for using Set is we can ignore duplicate entities and keep our data unique. For example, if we have a Set containing different entities "John", "Smith", "Smith", "John", "Alisha". It will display "John" and "Smith" only one time in output by ignoring the duplicated entities. On the other hand, List is and ordered the collection of objects. So, when we have to keep our entities in ordered form while not caring about the duplication we will prefer List.

We do expectation handling to handle the errors or problems that arise due to the syntax error or logical error. If these errors are not handled properly then it disturbs the flow of the application and performs abnormal behavior like stops the, these errors need to be handled carefully for smooth and error-free flow.

There are 2 types of exceptions.

  1. Checked Exception
  2. Unchecked Exception

Java use garbage collection which is performed automatically in Java to free the memory space from unused objects automatically. It is used to identify and dispose those objects that are no longer needed for the application. In other languages like C and C++ you must perform garbage collection by yourself to manage the memory, it means Java provides good memory management.

It is important to understand the difference between JDK, JRE, and JVM in Java.

JVM (Java Virtual Machine):  

Java virtual machine is actually an abstract machine which provides a runtime environment in which the java bytecode gets executed. A JVM performs some main tasks such as- loading, verifying and executing the code. It also provides a runtime environment.

JRE (Java Runtime Environment):

The Java runtime environment is used to provide a runtime environment. It is implemented by JVM. It consists of certain libraries which are used by JVM at runtime.

JDK (Java Development Kit):

The Java development kit consists of JRE and all the development tools which are necessary for compiling a program.

Access specifiers in java are the keywords which define the access scope of the function. It can be used before a class or a method. There are basically 4 types of access specifiers in java: –
  • Public: – class, methods, and fields are accessible from anywhere.
  • Protected: – methods and fields are accessed from the same class to which they belong. Even from the subclass and class from the same package. Any outside class cannot access the methods and fields.
  • Default: – only from the same package, the methods and fields be accessed and not from the outside package.
  • Private: – the methods and fields can be only accessed from the same class to which they belong.
If we want to execute a statement or a block of statements repeatedly in java, then loops are used. And such process is known as looping. There are basically 3 types of looping. They are: –
  • “For loop”: – for executing a statement or a set of statements for a given number of times, for loops are used. It is used when the programmer knows the number of times a statement is to be executed.
  • “While loop”: – in this, a particular statement or set of statements are executed until a particular condition is fulfilled. In this, before the execution of statements, the condition is checked. It is known as an entry controlled loop.
  • “Do While loop”: – there is only one difference between the while and do while loop and that is- in do while loop, the condition is checked after the execution of the block of statements. It is known as an exit controlled loop.