Java Interview Questions For Freshers

Java Interview Questions For Freshers

Java Online Training from India1.What is the difference between an Interface and an Abstract class?
ANSWER: Abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

2.What are the principle concepts of OOPS?
ANSWER: There are four principle concepts upon which object oriented design and programming rest. They are:
1. Abstraction
2. Polymorphism
3. Inheritance
4. Encapsulation
(i.e. easily remembered as A-PIE).

3.What is immutable object? Can you write immutable object?
ANSWER: Immutable classes are Java classes whose objects can not be modified once created. Any modification in Immutable object result in new object. For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent sub class from overriding methods in Java which can compromise Immutability. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.

4.How you can force the garbage collection?
ANSWER: Garbage collection automatic process and can’t be forced. You could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

Garbage collection is one of the most important feature of Java, Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program can’t directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists.

5.How to create a immutable object in Java? Does all property of immutable object needs to be final?
ANSWER: To create a object immutable You need to make the class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor. Also its NOT necessary to have all the properties final since you can achieve same functionality by making member as non final but private and not modifying them except in constructor.

6.What does synchronized means in Hashtable context?
ANSWER: Synchronized means only one thread can modify a hash table at one point of time. Any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

7.What is the purpose of garbage collection in Java, and when is it used?
ANSWER: The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

8.What is Encapsulation?
ANSWER: Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.

9.How does substring () inside String works?
ANSWER: Another good Java interview question, I think answer is not sufficient but here it is “Substring creates new object out of source string by taking a portion of original string”. see my post How SubString works in Java for detailed answer of this Java question.

10.What’s the difference between constructors and normal methods?
ANSWER: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.

For Java Online Training please visit