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

In continuation of our previous article 101 Core Java Interview Questions to Ace the Interview, this is the second part.

Read first part – 101 Core Java Interview Questions to Ace an Interview – Part 1

Q.51: What is the purpose of throw keyword in java?

The throw keyword is used to explicitly throw an exception in java. We can throw either checked or unchecked exception. But throw keyword is generally used to throw custom or user-defined exception.

Q.52: What is the purpose of throws keyword in java?

The ‘throws’ is a keyword in Java exception handling which is used in the method signature which indicates this method might throw one of the listed type exceptions. The caller to these methods has to handle the exception using a try-catch block.

Q.53: What are assertions in Java?

Assertions are statements in java. It can be used to test your assumptions about the program. While executing assertion, it is assumed to be true. If it fails, JVM will throw an error named AssertionError. It is mainly used for testing purpose.

Q.54: Explain abstract class? 

A class declared using “abstract” keyword is called an abstract class. It can have abstract methods(only method declaration without body) as well as concrete methods (regular methods with a body).

Abstract classes can not be instantiated, and require subclasses to provide implementations for the abstract methods.

If we inherit an abstract class, then we must provide implementations to all the abstract methods in it. An abstract class can have data member, abstract method, method body, and constructor.  Abstract class provide 0 to 100% abstraction.

Q.55: Why can’t we create the object of an abstract class?

An abstract class is like a template, so you have to extend it and require subclasses to provide implementations for the abstract methods. So these classes are incomplete, they have abstract methods that have nobody.

so if Java allows you to create an object of this class then if someone calls the abstract method using that object then what would happen as there would be no actual implementation of the method to invoke. That is the reason why objects of an abstract class are not allowed.

Q.56: What is an interface.

The interface in Java is a mechanism to achieve 100 %abstraction.

In Java, Interface contains only abstract methods, the class which implements the interface they will provide a definition for abstract methods.

A class that implements an interface must implement all the methods declared in the interface. The methods must have the exact same signature as declared in the interface.

Interface fields are public, static and final by default, and methods are public. Multiple inheritances not supported in Java it is achieved through the interface. We can use interfaces in Java as a way to achieve polymorphism.

Q.57: What is a marker or tagged interface?

An interface which has no members is known as a marker or tagged interface. For example Serializable, Cloneable, Remote etc. It is used to provide some essential information to the JVM.

For example, Cloneable interface is used to mark object cloning and a Serializable interface is used to mark serialization and deserialization of an object. Marker interfaces give instructions to JVM that classes implementing them will have special behavior and must be handled with care.

Q.58: What is the difference between abstract class and interface?

abstract class interface
An abstract class provides partial abstraction Interface provides full abstraction.
It can have abstract methods as well as concrete methods. Interface contains only abstract methods(Java 8 provide default and static methods)
An abstract class can have static, final or static final variable with any access specifier.  an interface can only have public static final variable
Abstract classes can have constructors Interfaces can’t have constructors.
Abstract classes can extend other class and implement interfaces. An interface can only extend other interfaces.
A subclass can extend only one abstract class Interface implement multiple interfaces.
Abstract class can contain main() method Interface cant contain main() method.

Q.59: Which modifiers are allowed for methods in an Interface?

Only 2 modifiers are used with interface methods i.e. public and abstract

Q.60: How super keyword used with constructors?

Constructors use super keyword to invoke the super class’s constructor. If a constructor uses a super keyword, it must use it in the first line; otherwise, there will be a compile-time error.

Q.61: What is System.out.println?

  • System: System is a class in the java.lang package
  • out: The out is a static member of the System class, and is an instance of java.io.PrintStream
  • println: The println is a method of java.io.PrintStream.

Q.62: What is singleton class in java?

A singleton is a class that is instantiated exactly once in the Java Virtual Machine. Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects.

How to implement a singleton class

  1. Make Constructor private
  2. Declare a static variable object of the class
  3. Declare a static method to return the instance

Q.63: What is nested class?

A class within a class is called a nested class. The class written inside is called the nested class, and the class that holds the inner class is called the outer class.

There are 2 types of nested class.

  1. Non-static nested classes or Inner class − these are the non-static members of a class.
  2. Static nested classes − these are the static members of a class.

Q.64: What is Inner class in Java.

Java inner class also called as a nested class is a class which is declared inside the class or interface. Inner classes are used to logically group classes and interfaces in a single place so that it can be more readable and maintainable.

Additionally, it can access all the members of an outer class including private data.

There are 3 types of inner class.

  1. Member inner class: A class created within a class.
  2. Anonymous inner class: A class created for implementing interface or extending class
  3. Local inner class: a class written inside the method.

Q.65: What is local inner class.

This type of class is written inside a method and the scope of this class is limited to this method only. We must instantiate this class inside the method where the inner class is defined.

Q.66: What is a static nested class?

A static class created inside a class is called the static nested class in java which is a static member of an outer class. It cannot access non-static data members and methods of an outer class. It can be accessed by outer class name.

In this no need to create the object of Outer class because a nested class is of static type.
It can access static data members of an outer class including private.

Q.67: What is Anonymous inner class?

A class without a name is known as anonymous inner class. An anonymous class is defined and instantiated in a single statement.

Anonymous inner class created using extending a class or implementing an interface.
As an anonymous inner class has no name, it is not possible to define a constructor for an anonymous class. Anonymous inner classes are accessible only at the point where it is defined.

Q.68: What is Enum in Java?

An enum is a data type which is a collection of constants. It is added in from Java 5. The java enum constants are implicitly static and final. enum can be used in a witch, can be traversed. enum can include fields, constructors, and methods.

Q.69: What is the purpose of values() method in the Enum?

All enum types get a static values() method automatically by the Java compiler. The values() method returns an array containing all the values of the enum.

Q.70: What are java Stack and Heap memory?

STACK: The stack memory is the space for a thread of execution. This is the temporary memory where local variable values are stored when their methods are invoked. After the method is finished its execution, the memory containing those values becomes unused then it is cleared to make space for new methods.

The stack reserved in LIFO pattern, the most recently reserved block is always the next block to be freed. When a new function is invoked, a new block of memory will get created in the Stack. This new block will store the local values invoked by the function and references to objects stored in the Heap that is being used by the method.

The stack size is less than a heap space because when a method ends, all the variables created on the stack are erased to make space for future use.

HEAP: Stack is a static memory allocation but heap memory is used for dynamic memory allocation.

All Runtime classes are loaded into the Heap memory when the program is run. Heap memory is used to store objects and JRE classes in Java, all the objects are created in heap memory and it has a global access and it is referenced from anywhere from the program.

In heap memory there is no pattern is used for allocation and deallocation of blocks from a heap, any block can allocate and free at any time. It makes heap memory very complex because we cant keep track on a heap.

Q.71: Which ways are used to create string object in java?

There are two ways to create String object:

  1. By string literal: String str = ”java”;
  2. By new keyword: String str = new  String(“java”);

Q.72: What is mutable and immutable string objects?

Immutable objects are like constants which are unmodifiable. String class is used to create the immutable string.

Mutable objects are modifiable. StringBuffer and StringBuilder classes are used to create a mutable string.

Q.73: How to compare two String in Java?

equals() and equalsIgnoreCase() methods are used to compare to strings.

Q.74: Why strings have been made immutable in Java?

Immutable objects are like constants which are unmodifiable. For example

This code will print ‘java’ as an output, it will not convert str to uppercase because String class is immutable or final.

The reason is String literals are stored in String constant pool and which are shared among many applications. Therefore because of security reason strings are made in immutable in Java. We can use strings in multithreading without synchronization so strings are thread safe.

Q.75: How many objects will be created by using following code?

Only one string object will be created. The str1 creates a new string object in the heap memory area and a reference to this string will be added to the String constant pool. In str2, the same reference stored in the string constant pool will be assigned to str2.

Q.76: How many objects will be created by using following code?

Two string objects will be created by the above code and stored in heap.

Q.77: What does String intern() method do?

It is the process of creating a string object in String Constant Pool which will be an accurate copy of string object in heap memory. Intern refers to a string object in the String Constant Pool. Intern() method is present in java.lang.String class.

This method first checks whether there is an existence of an object with the same content in the String Constant Pool. If object not presents in the pool, it will create an object with the same content in the string constant pool and returns the reference of that object. If object present in the pool then it returns the reference of that object without creating a new object.

Q.78: What is String Constant Pool?

String constant pool presents inside a section of memory is called the heap. This is a part of memory that is used for run-time operations, working with classes and objects. When you declare a new string in Java, following things happen
We create a new string variable called str and give it a value.

String str1= “Hello Nita”;

Not only will Java create the variable str1, it will allocate space in the memory for the literal value ‘Hello Nita.’ This area in memory is called the string constant pool. It is like a pool of string values that are available to other parts of the program.

Now, if you created another variable, say str2, and also gave it a value of ‘Hello Nita ‘ Java simply re-uses the value that’s already in the pool. 

If we create a new instance of the String class, the constant pool works differently. Now create another variable, str3, and also give it the same literal value. This time, however, we will create a new instance of the String class:

String str3= new String (“Hello Nita”);

When this code is processed, now Java will react differently. Instead of re-using the same literal again, it will create a NEW value in memory. In this case, it does NOT create it in the string constant pool, but in the memory heap.

Q.79: What is the similarity and difference between String and StringBuffer class and StringBuilder?

String StringBuffer StringBuilder
String is immutable


StringBuffer is mutable StringBuilder is mutable
The object created as a

A string is stored in the

Constant String Pool


Is stored in HEAP Is stored in HEAP
Thread safe (String cannot be used by two threads simultaneously.) Thread safe(String cannot be used by two threads simultaneously.) Not thread safe(String can be used by two threads simultaneously.)
Fast Slow Fast


Q.80: Why is char array better than String for storing password?

Strings are immutable so there is no way contents of Strings can be changed because any try to change string will produce new String, while if you char array you can still set all his element as blank or zero.

Since Strings are immutable in Java if you store the password as plain text it will be available in memory until Garbage collector clears it and since String is used in String pool for reusability there is pretty high chance that it will remain in memory for a long duration, which poses a security threat. Since the one who accesses memory dump can find the password in clear text and that’s the reason you should always use an encrypted password than plain text.

So Storing password in character array clearly moderates security risk of stealing a password.

Q.81: How many objects will be created for the following code and where they will be stored?

Two objects will be created and they will be stored in the heap memory.

Q.82: Is string thread-safe in Java?

Yes. As strings are immutable therefore we cant change its value in the program. Because of this, it is threadsafe and it can be shared between multiple threads.

Q.83: What is the purpose of trim() method?

The Java string trim() method eliminates leading and trailing spaces. The Unicode value of space character is ‘\u0020’. The trim() method checks this Unicode value before and after the string if it exists then removes the spaces and returns the string with leading and trailing whitespace omitted.

Q.84: What is JDK, JRE, JVM?

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.

JDK:  JDK is called as Java Development Kit which has a physical existence. It is a kit inside which resides the JRE along with developing tools within it like javac, javap, java, javadoc etc. It is mostly used by the programmers and developers.

JRE: it is called as Java Runtime Environment, it is used as a package that gives an environment to run the Java program on your machine. It combines the JVM, platform core classes and supporting libraries. JRE is part of the Java Development Kit (JDK) but can be downloaded separately.

Q.85: What is the Difference between ArrayList and Vector

1.  Synchronization and Thread-Safe
Vector is synchronized while ArrayList is not synchronized. Synchronization and thread safe means at a time only one thread can access the code.In Vector class all the methods are synchronized.That’s why the Vector object is already synchronized when it is created.

2.  Performance
Vector is slow as it is threadsafe. In comparison, ArrayList is fast as it is non-synchronized. Thus in ArrayList two or more threads can access the code at the same time, while Vector is limited to one thread at a time.

3. Automatic Increase in Capacity
A Vector defaults to doubling the size of its array. While when you insert an element into the ArrayList, it increases its Array size by 50 %. By default, ArrayList size is 10. It checks whether it reaches the last element then it will create the new array, copy the new data of the last array to new array, then an old array is a garbage collected by the Java Virtual Machine (JVM).

4. Set Increment Size
ArrayList does not define the increment size. Vector defines the increment size.

5. Enumerator

Other than Hashtable, Vector is the only other class which uses both Enumeration and Iterator.While ArrayList can only use Iterator for traversing an ArrayList.

Q.86: What is different between Iterator and ListIterator?

  • An iterator can traverse in one direction ie.forward direction only whereas ListIterator can be used to traverse in both the directions ie. Forward and backward direction.
  • We cannot add an element to the collection while traversing it using Iterator, it throws Exception when you try to do it. But we can add an element at any point in time while traversing a list using ListIterator.
  • An iterator is used for traversing List and Set both and ListIterator used to traverse List
  • There are 3 Methods in Iterator: hasNext(), next(), remove().

There are following Methods in ListIterator:  add(E e), hasNext(), hasPrevious(), next(),  nextIndex(), previous(), previousIndex(), remove(), set(E e)

  • We cannot replace the existing element value when using Iterator but By using set(E e) method of ListIterator we can replace the last element returned by next() or previous() methods.

Q.87: What is the difference between Iterator and Enumeration?

The Iterator has remove() method while Enumeration doesn’t. Hence using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration can only traverse the objects and fetch it.

Q.88: What is the difference between Array and ArrayList

Array ArrayList
Array declaration

int arr[] = new int[10]

ArrayList declaration

ArrayList<Type> arr = new ArrayList<Type>();

Array is a fixed length data structure.We cannot change length of array once created in Java.


ArrayList is a variable length Collection class. Length changes in ArrayList.
Array store both primitive data and objects. ArrayList only store objects not primitive data.
Arrays can be multi-dimensional. It cant be multi-dimensional.
Elements are added using assignment operator. Elements are added using add() method.
The size of an array is checked using length attribute. The size of an ArrayList can be checked using size() method.
Arrays can be iterated through a for loop. In ArrayList iterators are used to iterate through their elements.

Q.89: What is the Difference between ArrayList and LinkedList?

ArrayList LinkedList
ArrayList internally uses dynamic array to store the elements. LinkedList internally uses doubly linked list to store the elements.
Insertions in ArrayList are slow.
because in worst case if an array is full, there is an extra cost of resizing array and copying elements to the new array, which makes runtime of add operation in ArrayList O(n), in the best case it is O(1).
Insertions in LinkedList are generally fast as compare to ArrayList. It takes O(1) operation for insertion.
 In ArrayList, we need to write our own code to iterate over the ArrayList in reverse direction. In LinkedList, it can be iterated in reverse direction using descendingIterator()
ArrayList requires less memory it maintains indexes and element data. LinkedList requires more memory than ArrayList because of every node in LinkedList stores reference of previous and next elements.

Q.90: What is the difference between HashMap and Hashtable?

HashMap Hashtable
HashMap is not synchronized.

It is not threadsafe and can’t be shared between many threads without proper synchronization code.

Hashtable is synchronized.

It is thread-safe and can be used in multithreaded environment.

HashMap allows one null key and multiple null values. Hashtable doesn’t allow any null key or value.
HashMap is a new class introduced in JDK 1.2. Hashtable is a legacy class.
HashMap is fast. Hashtable is slow.
We have to use following method to make HashMap as synchronized
Map m = Collections.synchronizedMap(hashMap);
Hashtable is internally synchronized and can’t be unsynchronized.
HashMap is traversed by Iterator. Hashtable is traversed by Enumerator and Iterator.
Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast.

Q.91: What is the Difference between HashSet and TreeSet?

HashSet TreeSet
HashSet does not maintain any order of elements TreeSet elements are by default sorted in ascending order.
HashSet can store one null value. TreeSet will not allow null object otherwise it will throw Null Pointer Exception.
HashSet uses equals() method for comparison.  TreeSet uses compareTo() method for maintaining ordering.
It is internally implemented using HashMap It is internally implemented using TreeMap
It is faster It is slow.

Q.92: What is the difference between fail-fast and fail-safe iterator?

  1. The fail-safe iterator will not get affected by any modification in the collection. Fail-fast iterators throw ConcurrentModificationException if there is a structural modification of the collection.
  2. Fail Safe Iterator makes a copy of the internal data structure (object array) and iterates over the copied data structure. Any structural modification done to the iterator affects the copied data structure original one remains same. Hence , no ConcurrentModificationException throws by the fail-safe iterator.
  3. In fail-fast iterator, there is no Overhead of maintaining the copied data structure. They use the original collection to traverse over the elements of the collection.
  4. Fail-safe iterator requires extra memory (because it makes a copy of internal data structure)
  5. Fail-fast requires less memory.

Q.93: Which collection classes are thread-safe?

Vector, Hashtable, Properties, and Stack are synchronized classes or called as thread safe and that’s why it can be used in the multi-threaded environment.

Q.94: What are the basic interfaces of Java Collections Framework?

Java Collections Framework provides a set of interfaces and classes that support operations on a collection. The most basic interfaces  in the Java Collections Framework are:

Collection: It represents a group of objects known as its elements.

Set: It is a collection that cannot contain duplicate elements.

List:  It is an ordered collection and can contain duplicate elements.

Map:  It is an object that maps keys to values and cannot contain duplicate keys

Q.95: Which are the classes implementing List and Set interface?

List interface classes: ArrayList, Vector, LinkedList,

Set interface classes: HashSet, TreeSet

Q.96: What is the difference between Collection and Collections?

A collection is an interface while Collections is class, both are present in java.util package and part of java collections framework.

 Q.97: What is the difference between List and Set?

Set does not contains duplicate elements while List can contain duplicate elements.
Set is unordered while List is ordered, it maintains the order in which the objects are added.

 Q.98: What is the Difference between Comparable and Comparator?

Comparable Comparator
Comparable is present in java.lang package Comparator is present in java.util package.
 In the Comparable interface, actual class is modified. In Comparator actual class is not modified.
In Comparable we can sort the collection on the basis of a single element such as eid or name or salary etc. In Comparator, we can sort the collection on the basis of many elements such as  (id, name) etc.
Uses  compareTo() method to sort elements. Uses compare() method to sort elements.

 Q.99: What is the difference between JAVA and C++?

  1. Java is platform independent and C++ is platform dependent.
  2. Garbage collection is present in java but it is not available in C++
  3. Java doesn’t supports multiple inheritances and it is supported by C++
  4. Java supports multithreading and C++ does not support multithreading.
  5. There is no pointer concept in Java and it is present in C++
  6. Global variables are not present in java but are present in C++.

Q.100: Where to initialize the blank final variable?

It can be initialized in the constructor if it is non-static. If it is static it must be initialized in a static block.

Q.101: What is Runtime Polymorphism in Java?

Runtime polymorphism is also called as Dynamic Method Dispatch it is a process in which a call to an overridden method is resolved at runtime.

In runtime polymorphism, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.





Thus we have completed 101 Core Java Interview Questions listing. We are sure that these questions will help you understand the core of Java.

Add a Comment

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