Core Java Interview Questions and Answers
1) What is Java?
Java is a simple, high-level, secure, platform-independent, multithreaded, and object oriented programming language. It was developed by James
Gosling in June 1991. It can also be known as the platform as it provides its own runtime environment as JRE and API.
2) What is the di erence between JDK, JRE, and JVM?
JVM
JVM is an acronym for Java Virtual Machine; it is an abstract machine which provides the runtime environment in which Java byte code can be
executed. It is a speci cation which speci es the working of Java Virtual Machine. Its implementation has been provided by Oracle and other
companies. Its implementation is known as JRE.
JVMs are available for many hardware and software platforms (so JVM is platform dependent). It is a runtime instance which is created when we run
the Java class. There are three notions of the JVM: speci cation, implementation, and instance.
JRE
JRE stands for Java Runtime Environment. It is the implementation of JVM. The Java Runtime Environment is a set of software tools which are used for
developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of
libraries + other les that JVM uses at runtime.
JDK
The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java
Runtime Environment (JRE), an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools
needed in Java development.
Q3. What is the di erence between Encapsulation and Abstraction?
Ans. 1.Abstraction solves the problem at design level while encapsulation solves the problem at implementation level
2.Abstraction is used for hiding the unwanted data and giving relevant data. while Encapsulation means hiding the code and data into a single unit to
protect the data from outside world.
3. Abstraction lets you focus on what the object does instead of how it does it while Encapsulation means hiding the internal details or mechanics of
how an object does something.
4.For example: Outer Look of a Television, like it has a display screen and channel buttons to change channel it explains Abstraction but Inner
Implementation detail of a Television how CRT and Display Screen are connect with each other using di erent circuits , it explains Encapsulation.
Q4. What is Polymorphism in Java ?
Ans. Polymorphism means the condition of occurring in several di erent forms.
Polymorphism in Java is achieved in two manners
1. Static polymorphism is the polymorphic resolution identi ed at compile time and is achieved through function overloading whereas
2. Dynamic polymorphism is the polymorphic resolution identi ed at runtime and is achieved through method overriding.
, Update Your Skills form Our Experts: Core Java Online Training
Q5. What is a nal method ?
Ans. It’s a method which cannot be overridden. Compiler throws an error if we try to override a method which has been declared nal in the parent
class.
Q6. What is the di erence between StringBu er and String class ?
Ans. A string bu er implements a mutable sequence of characters. A string bu er is like a String, but can be modi ed. At any point in time it contains
some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
The String class represents character strings. All string literals in Java programs, such as “abc” are constant and implemented as instances of this class;
their values cannot be changed after they are created.
Q7. What are Default Methods?
Ans. With Java 8, We can provide method de nitions in the Interfaces that gets carried down the classes implementing that interface in case they are
not overridden by the Class. Keyword “default” is used to mark the default method.
Q8. What is garbage collection ?
Ans. The garbage collection is a facility wherein a program runs on the Java Virtual Machine which gets rid of objects, which are not being used by a
Java application anymore. It is a form of automatic memory management and recollection.
Update Your Skills form Our Experts: Core Java Online Training
Q9. What is abstract class or abstract method?
Ans.We cannot create instance for an abstract class. We can create instance for its subclass only. By specifying abstract keyword just before class, we
can make a class as abstract class.
public abstract class MyAbstractClass{
}
Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its
subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:
public abstract int getLength();
Q10. When to use LinkedList or ArrayList?
Ans. Accessing elements are faster with ArrayList, because it is index based. But accessing is di cult with LinkedList. It is slow access. This is to access
any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know
the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations
ArrayList need to adjust the indexes according to deletion or insertion if you are performing on middle indexes. Means, an ArrayList having 10
elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Q11. Can we override static method?
Ans.We cannot override static methods. Static methods are belongs to class, not belongs
to object. Inheritance will not be applicable for class members
Q12. What is the di erence between super() and this()?
, Ans.super() is used to call super class constructor, whereas this() used to call constructors in the same class.
Q13. What is the purpose of garbage collection?
Ans.The garbage collection process is to identify the objects which are no longer referenced or needed by a program so that their resources can be
reclaimed and reused. These identi ed objects will be discarded.
Q14. Explain public static void main(String args[])
Ans.Here public is an access modi er, which means that this method is accessible by any class.
static – static keyword tells that this method can be accessed without creating the instance of the class.
void – this main method returns no value.
main – It is the name of the method.
String args[] – The args is an array of String type. This contains the command line arguments that we can pass while running the program.
Q15. What is the base class of all classes?
Ans.java.lang.Object is the base class (super class) of all classes in java.
Update Your Skills form Our Experts: Core Java Online Training
Q16. What is a path and classPath in Java?
Ans.Path speci es the location of .exe les. Classpath speci es the location of bytecode (.class les).
Q17. What is Type casting in Java?
Ans.When we assign a value of one data type to the di erent data type then these two data types may not be compatible and needs a conversion. If
the data types are compatible (for example assigning int value to long) then java does automatic conversion and does not require casting. However if
the data types are not compatible then they need to be casted for conversion.
For example:
//here in the brackets we have mentioned long keyword, this is casting
double num = 10001.99;
long num2 = (long)num;
Q18. What is an Array?
Ans.An array is a collection (group) of xed number of items. Array is a homogeneous data structure which means we can store multiple values of
same type in an array but it can’t contain multiple values of di erent types. For example an array of int type can only hold integer values.
Q19. Four main principles of OOPS Concepts?
Ans.
Inheritance
Polymorphism
Data Encapsulation
Abstraction