Core Java Interview Questions and Answers
Core Java Interview Questions and Answers
1) What is JVM?
JVM stands for Java Virtual Machine. It simulates a real computer and provides the
runtime environment for running java applications.
In the first step, the java source code is converted to a byte code (binary code that can
be understood by the JVM : instruction set of the JVM) by the java compiler (javac).
This byte code is converted by the JVM into a machine code (binary). Then, the machine
code is run by the computer that has the JRE installed.
Actually, JVM interprets the byte code and runs the java program.
It uses the class libraries, and other files provided in JRE in order to accomplish this task.
2) What is JRE ?
JRE is an acronym of Java Runtime Environment.
Java Runtime Environment is an executable file that includes JVM, class libraries (util,
math,etc), and other files.
JRE doesnt include any development tool like compiler, debugger, etc.
JRE = JVM + Java standard classes (math, lang, util, etc) + runtime libraries
JREs are available for download in Oracle website. There are many versions for each
hardware configuration (32 bits/ 64 bits) or operating system (windows, linux, Mac OS,
etc).
3) What is JDK ?
JDK stands for Java Development Kit.
8) What is a platform?
A platform can be software (Operating system, etc) or a hardware platform (hardware
architecture, CPU family, etc).
10)
What is a classloader?
The class loader is a piece of software packaged in the JRE. Its role is to dynamically load
Java classes into the JVM.
JVM must at least include one class loader which is the primordial (or bootstrap) class
loader.
The classes are loaded in java when needed. The first class loaded is the one that has a
static main method.
11)
What is classpath?
The Java Virtual Machine must know where to find the projects compiled classes.
It is not appropriate that the JVM looks through every folder on your machine in order to
find your compiled classes.
So, we have to provide the JVM with the directories to use to look up for our compiled
classes.
This is done by putting those directories in the classpath.
So, the classpath contains the paths used by the JVM (classloader) in order to find our
compiled classes or the libraries used.
12)
When accessed before their initialization, the attributes of a class take a default value
based on the type of each attribute:
Numeric types attributes (int, float, double, etc) take zero as a default value.
13)
What are the default values of local variables declared
inside methods?
Those variables have whatever value when accessed before their initialization. We cant
predict their values before their initialization.
14)
15)
The allowed modifiers for a class are nothing or the public modifier.
16)
When declaring an attribute in a class without specifying its visibility, it has the default
visibility. The default visibility is the package visibility.
17)
18)
What is a constructor?
19)
20)
The default constructor is the constructor that has no arguments. This constructor is
automatically generated by the compiler if we dont write it explicitly.
When overloading constructor, the compiler will not auto-generate the default
constructor for us. So, we should write the default constructor by ourselves.
This can cause many problems when using the well known frameworks like hibernate,
spring, etc
21) Do child classes inherit the constructor of parent classes?
No, the constructor is not inherited. We can call the constructor of the parent class
explicitly using the super keyword.
22)
23)
24)
25)
We cannot make child classes from this class. We cannot inherit from a final class.
26)
27)
28)
This is a variable declared as final and not initialized when declaring it.
29)
The blank final variable should be initialized in the constructor of the class if it is not
static.
If it is static, we should initialize it in a static block.
If we dont respect these rules, we will get an exception.
For more details, please check our java variables for beginners tutorial.
30)
31)
Yes
32)
It is a method that has no implementation and is marked with the abstract keyword.
33)
Method overriding is used when creating a method that is provided in the parent class in
a child class. This concept is tightly related to inheritance.
34)
NO, we cant override static methods because they belong to the class itself and not to the
instance.
35)
36)
Method overriding is used inside a single class. It is used when creating, in the same
class, many methods that have the same name but differ by the list of arguments.
37)
How method overloading enhances the readability of a
program?
Method overloading enhances the readability of a program because we arent forced to
create method of different names that make the same thing.
38)
Is it possible to overload methods by changing the return
type?
No, keeping the same list of parameters and changing only the return type will cause a
compiler error.
39)
Yes
For more details, please check our java method overloading tutorial.
40)
For those who are familiar with C++, virtual methods are used to make dynamic binding.
They are related to the polymorphism concept.
In JAVA, all the methods are virtuals.
41)
What is difference between method Overriding and method
Overloading ?
Method Overloading
Method Overriding
42)
What is inheritance?
A class B inherits from class A. The class B inherits some attributes and method from the
class A under certain conditions (public and protected members are inherited).
In this case, class A is called the parent class (super class) and class B is called the child
class.
So, the child class can use the inherited members without having to copy/paste them in
the code of the class.
43)
The inheritance allows the reuse of the code of the parent class inside child classes.
44)
45)
The super keyword is used inside the child class to call a method present in the parent
class explicitly using the dot notation.
We can call a constructor present in the parent class using the super keyword.
46)
No, because each one among the two calls needs to be the first statement.
47)
48)
No
final variables
abstract methods: we can omit the abstract keyword
50)
What is the difference between abstract classes and
interfaces ?
Abstract class
abstract classes has abstract methods and
implemented methods. So, the child class
needs only to implement the abstract
interface
The implementing class must implement
all the methods of the interface or it will be
abstract.
Multiple inheritance is not allowed for An interface can inherit from many
interfaces
classes.
The abstract keyword must be used
51)
A static variable belongs to the class itself whereas instance variable belongs to an
instance created from this class.
The static variable is shared between all the instances of a class. It is accessed using the
class name and the dot notation.
Example: ClassName.staticVariableName
52)
A static method belongs to the class itself whereas instance method belongs to an
instance created from this class.
The static method can be called without creating an instance of the class. It is accessed
using the class name and the dot notation.
Example: ClassName.staticMethodName(params)
We cant use not static attributes inside a static method and vice versa.
53)
We dont need to create an object from the principal class in order to call the main
method.
54)
It is a bloc of code (instructions present between curl braces) marked with the static
keyword.
It is used in a class to initialize static attributes in the class loading phase.
55)
What is composition?