101 Most Important Frequently Asked Core Java Interview Questions to Ace an Interview – Part 1

In this article, we are going to list down 101 Most Important and Frequently Asked Core Java Interview Questions that will give you enough exposure and confidence to ace at Job Interview.

Java is on evolution spree. The new versions of Java are being released even before you get to know the last version. With all this, the core of java remains same and with it, one needs to be proficient at it.  We will split the questions into two parts.

Read second part – 101 Core Java Interview Questions to Ace an Interview – Part 2

Q.1: Can we write multiple public classes in one Java source file?

No. The class which has public static void main() method only that class will be public not others in one java source file.

Q.2: Why Java is called platform independent?

In other Programming Languages, compiler produce code for a particular system but Java compiler produce Bytecode(.class file) for a Java Virtual Machine. When we compile a Java program, then bytecode is generated. Bytecode is the source code that can be used to run on any platform machine (it should have JVM on it.)

e.g. Byte code generated in windows environment can also be executed in the Linux environment.

This makes java platform independent.

Q.3: What is Thread in Java and what are its advantages?

A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of execution. Threads are independent, if there occurs exception in one thread, it doesn’t affect other threads. It shares a common memory area.

Advantages

  1. It doesn’t block the user because threads are independent and you can perform multiple operations at the same time.
  2. You can perform many operations together so it saves time.
  3. Threads are independent so it doesn’t affect other threads if an exception occurs in a single thread.

Q.4: Which are the ways to create a thread?

There are two ways to create a thread:

1. By implementing Runnable interface.

2. By extending Thread class

The first approach of implementing with the interface is preferred. This approach will allow your class to extend any other required class.

Q.5: Can we start a thread twice?

No. After starting a thread, it can never be started again. If you do so, an IllegalThreadStateException is thrown. In such case, the thread will run once but for the second time, it will throw an exception.

Q.6: What is the difference between sleep() and wait() method?

wait() sleep()
wait() method releases the lock sleep() method doesn’t release the lock.
is the method of an Object class is the method of Thread class
is the non-static method is the static method
should be notified by notify() or notifyAll() methods after the specified amount of time, sleep is completed.

Q.7: What is interthread communication? How is it implemented?

Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other. Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.

It is implemented by following methods of Object class:
wait()
notify()
notifyAll()

Java provides the benefit of avoiding thread pooling using interthread communication.
All three method can be called only from within a synchronized context.

Q.8: Explain sleep () method in Java.

This method of Thread class is used to sleep a thread for the specified amount of time.

Syntax:

Q.9: Explain Thread Priorities.

Every Java thread has a priority that helps to determine the order in which threads are executed. Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10).

By default, every thread is given priority NORM_PRIORITY (a constant of 5).

Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and are very much platform dependent.

Q.10: Explain Lifecycle of a Thread.

The life cycle of the thread in java is controlled by JVM. The Java thread states are as follows:

  1. New: The thread is in a new state if you create an instance of Thread class but before the invocation of start() method.
  2. Runnable: The thread is in runnable state after an invocation of start() method, but the thread scheduler has not selected it to be the running thread.
  3. Running: The thread is in running state if the thread scheduler has selected it.
  4. Non-Runnable (Blocked): This is the state when the thread is still alive but is currently not eligible to run.
  5. Terminated: A thread is in terminated or dead state when its run() method exits.

Q.11: What is Multitasking, Multithreading, and Multiprocessing?

Multitasking: Ability to execute more than one task at the same time is known as multitasking.

Multithreading: It is a process of executing multiple threads simultaneously. Multithreading is also known as Thread-based Multitasking.

Multiprocessing: It is same as multitasking, however in multiprocessing more than one CPUs are involved. On the other hand, one CPU is involved in multitasking.

Q.12: What is the difference between wait and notify in java?

Both wait and notify methods are used in inter-thread communication. Where wait is used to pause the thread on a particular condition and notify method is used to send a notification to a waiting thread.

Q.13: What is the difference between notify and notifyAll method.

notify method wakes up only one thread waiting on the particular object and that thread starts execution. if there are multiple threads waiting for an object, this method will wake up only one thread. This choice depends on the OS implementation of thread management.

notifyAll method wakes up all the threads waiting on the object, but again the choice of threads depends on the JVM.

Q.14: Explain the final keyword in Java.

The final keyword is used to introduce immutability nature. The keyword can be used with Java variables, methods and classes.

  1. final variable: We can create a constant using the final variable, once the variable is final we can’t change its value.
  2. final method: When a method is declared as final, it can not be overridden.
  3. final class: A final class can not be extended.

Q.15: What is finally and finalize?

Finally block is used in exception handling, Java finally block is always executed whether an exception is handled or not handled. Finally block is written after try and catch block.

Finalize() is a method which is used in garbage collection. This method is a protected and non-static method present in java.lang.Object class. This method will be available for all the objects we create in java. This method is used to perform clean up operations on an object before it is removed from the memory.  One can override the finalize() method to keep particular operations they want to perform before an object is destroyed.

Q.16: What is blank final variable?

A final variable which is declared but not initialized is called as a blank final variable. It is initialized only in a constructor.

Q.17: What is static blank final variable?

A static final variable which is not initialized at the time of declaration is known as a static blank final variable. This variable can be initialized only in a static block.

Q.18: What is static variable?

  • Static variable in Java belongs to the class and not to the object, it is initialized only once at the beginning.
  • A single copy of this variable shared by all instances of the class
  • A static variable is also called as a class variable.

Q.19: What is the static method?

  • Static method in Java is a method which belongs to the class and not to the object.
  • A static method can access only static data.
  • It is a method which belongs to the class, not to the objects that’s why it is called as a class method and can be directly accessed by class name not by object.
  • A static method can access only static data and not access non-static data.
  • A static method can call only other static methods and not a non-static method from it.
  • A static method cannot use “this” or “super” keywords in any manner.

Q.20: What is a static block in java?

The static block is used to initialize the static data member like a constructor and will be executed when a class is first loaded into the JVM.

One class can have multiple Static blocks there execution happens in the same order they have been written in the class.

Q.21: What is a static class in Java?

It is also called as a static nested class. These classes cannot access non-static data members or method. It can access static data members of outer class including private data members.

Q.22: What is garbage collection in java?

Java garbage collection is the process by which Java programs perform automatic memory management. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory allocated to the program. In the end, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free the memory area.

The gc() method is used to invoke the garbage collector to perform cleanup processing. The gc() is found in System and Runtime classes.

Q.23: Is Java language is pure object-oriented language?

No. Because it uses eight primitive data types which are boolean, byte, char, int, float, double, long, short which are not objects.

Q.24: What is wrapper class?

Java wrapper classes convert the primitive data types in objects. The wrapper classes “wrap” the primitive data type into an object of that class. All the wrapper classes in Java are immutable and final.

For each primitive data type, there is a wrapper class dedicated to it.

Primitive Type Wrapper class
Boolean Boolean
Char Character
Byte Byte
Short Short
Int Integer
Long Long
Float Float
Double Double

Q.25: What is the meaning of public static void main(String args[]) in Java?

  • Public: it means that anybody can access it.
  • Static: it indicates that main is a class method which is executed without creating an object.
  • Void: it indicates the main method will not return any value
  • String args[]: the main method accept string value as a parameter

Q.26: What is JVM?

A Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run a Java program. It converts Java bytecode (.class file)  into machines language. JVM is a part of JRE(Java Run Environment). Java is platform independent but JVM is platform dependent. JVM is the engine that drives the Java code.

In other Programming Languages, compiler produce code for a particular system but Java compiler produce Bytecode(.class file) for a Java Virtual Machine.

When we compile a Java program, then bytecode is generated. Bytecode is the source code that can be used to run on any platform machine but that machine should have JVM on it.

Q.27: Can a constructor be inherited in java?

No. A Constructor cannot be inherited, but a derived class can call the base class constructor.

Q.28: What is the difference between JDK and JVM?

Java Development Kit (JDK) is for development purpose which contains all the tools like javac, java, javap etc, executable files and binaries required to compile, debug and execute a Java Program and JVM is a part of it to execute the java programs.

Q.29: Name the exception thrown when an array is accessed beyond its bounds.

ArrayIndexOutOfBoundsException

Q.30: What is the purpose of ‘this’ keyword?

  • It is used to reference instance variable of current class
  • It is used to invoke or initiate current class constructor
  • It is passed as an argument in the method call
  • It is used to pass an argument in the constructor call
  • It is used to return the current class instance

Q.31: What is a package?

A package is a group of classes, interfaces and sub packages. In java packages are used to organize classes and interfaces.

There are two types of packages in Java: built-in packages and user-defined package.

Only by importing the particular package in a program, one can use all the classes and its methods. Example of built-in packages :

  1. java.io; this package is a group of all the classes used for input and output.
  2. java.net: it is a group of all the classes used for client-server programming

 Q.32: Which are the access modifiers in Java?

  1. Public: this modifier can be used with class and class members, it indicates it can be accessed from anywhere.
  2. Private: it can be used with class members. It provides restricted access to the members. If Class members are private it will be accessed only within the same class. Generally, the class members are kept as private.
  3. Protected: it can be used with class members. If a class member is protected it will be accessible only to the classes in the same package and to the subclasses. A protected modifier is used when there is a need that class variables are accessible only to the subclasses.
  4. Default: if no modifier is used then it is a default one. Default access modifier provides access to the classes in the same package. The rules are same for class and class members.

Q.33: What is the root class for all the classes in Java.

java.lang.Object and no need to extend it.

Q.34: The main method can be overloaded?

Yes. In one class if we have more than one methods with name “main”, and when the program will be executed the java runtime environment will check for the main method with the following syntax

Q.35: Can we declare a class as static?

No. The only inner class can be declared as static its called as a static nested class.

Q.36: What is method overriding?

Method overriding used in inheritance. When subclass declares a method that has the same signature as a method declared by one of its superclasses. So the subclass can define different behavior than its superclass.

Note:

  • A final method cannot be overridden.
  • We can not override a static method.

Q.37: What is method overloading?

When more than one method in the same class have the same name but with the arguments, then it is called as method overloading.

Q.38: Is it possible to override the main method?

No. Main is a static method, we cant override a static method.

Q.39: How to prevent a method from being overridden?

To prevent a specific method from being overridden in a subclass, make that method as final in the superclass.

Q.40: Which are the java exceptional handling keywords?

There are 5 keywords used in java exception handling.

  1. try
  2. catch
  3. finally
  4. throw
  5. throws

Q.41: Can we use multiple catch block with a single try.

Yes. But all catch blocks must be ordered from most specific to most general.

Q.42: What are the types of Exceptions in Java?

Checked exceptions: Checked exceptions are checked at compile-time.

A checked exception is some subclass of Exception, except class RuntimeException and its subclasses. Each method must either handle all checked exceptions by supplying a catch clause or list of each unhandled checked exception as a thrown exception. Example: IOException, SQLException  etc

Unchecked exceptions: are checked at runtime.

All Exceptions that extend the RuntimeException class are unchecked exceptions. Class Error and its subclasses also are unchecked.

Example: ArrayIndexOutOfBoundsException , ArithmeticException, NullPointerException etc.

Q.43: Can we write try block without catch block?

No, It will give a compilation error.

The try block must be followed by either catch or finally block. At least one you should write after try either catch block or finally.

Q.44: Can we write down a code between try and catch blocks?

NO. It is not allowed to write any code between try and catch block. Catch block should immediately come after the try block.

Q.45: What is the purpose of finally block in java?

Finally block is used in exception handling, The block is used for cleaning up of resources such as closing connections, sockets etc.

In Java finally block is always executed whether an exception is handled or not handled.

The finally block will not be executed if program exits in an abnormal way.

Q.46: Can we have multiple finally block after try.

No. For each try block, there can be more than one catch blocks, but only one finally block.

Q.47: Can we write down a code between try and finally blocks?

NO. We shouldn’t write any code between try and finally block. Finally block should immediately start after catch block.If there is no catch block it should immediately come after the try block.

Q.48: List down checked Exceptions?

  1. IOException,
  2. SQlException,
  3. FileNotFoundException,
  4. InvocationTargetException,
  5. CloneNotSupportedException
  6. ClassNotFoundException
  7. InstantiationException

Q.49: List down unchecked Exceptions?

  1. Arithmetic Exception
  2. ArrayIndexOutOfBoundsException
  3. ClassCastException
  4. IndexOutOfBoundException
  5. NullPointerException
  6. NumberFormatException
  7. StringIndexOutOfBounds
  8. UnsupportedOperationException

 Q.50: Explain a situation where finally block will not be executed?

Finally block will not be executed whenever JVM shutdowns. If we use system.exit(0) in try statement in that case finally block if present will not be executed.

Next set of questions will be covered in our next article. Please comment if you need any more information.

6 Comments
  1. Dharmendra kumar Jat
    December 10, 2021 | Reply
  2. Jayashri M
    August 2, 2019 | Reply
  3. Naveen
    April 2, 2019 | Reply
    • Pavan
      April 5, 2019 | Reply
  4. Sudharani
    February 9, 2019 | Reply
    • Pavan
      February 9, 2019 | Reply

Leave a Reply to Sudharani Cancel reply

Your email address will not be published. Required fields are marked *