Anda di halaman 1dari 10

Core java interview questions and answers asked by the top recruiters in the world.

These core java


interview questions and answers aim to prepare you to pass any interview that deals with core java.
Those top 55 Core java interview questions are selected over the years by recruiters to
test the knowledge of the candidate in java.
We have also a core java tutorial that you can check in order to learn the basics of the
java language.

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.

It is an executable or a set of tools created by sun Microsystems that is used for


creating java application.
JDK=JRE+ java compiler (javac) + debugger + other development tools.
Please feel free to check this tutorial in order to install java development environment.

4) What is a JIT compiler?


JIT compiler stands for Just In Time Compiler.
A JIT compiler runs after the program has started and compiles the code (usually
bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a
form that's usually faster, typically the host CPU's native instruction set.
A JIT has access to dynamic runtime information whereas a standard compiler doesn't
and can make better optimizations like inlining functions that are used frequently.
This is in contrast to a traditional compiler that compiles all the code to machine
language before the program is first run (check this link for more details stackoverflow).

5) What is the extension of a source file in java?


The extension of a source file in java is .java

6) What is byte code?


The byte code is different from the machine code.
This is a binary code that the JVM can understand and interpret.
The byte code is written in the instruction set of the JVM.
The JVM is like a real computer that has a CPU having an instruction set. This instruction
set is used inside the byte code.

7) What is the extension of a compiled file in java?


The extension of a compiled file in java is .class. This file contains

8) What is a platform?
A platform can be software (Operating system, etc) or a hardware platform (hardware
architecture, CPU family, etc).

9) Why JAVA is platform independent?


The JVM is platform dependant whereas our java code is platform independent.
The Java executable file for desktop applications is an executable Jar (Java Archive).
This Jar file is an archive (a compressed file that contains .class files). You can unzip it
like any rar or zip archive.
We can break platform independency when we write a program that looks for specific OS
related files or when using JNI (Java Native Interface).

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)

What are the default values of attributes in a class?

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.

Reference type attributes take null 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)

How many public classes can be put per source file?

Only one public class per source file is allowed.


For more details, please check our java class tutorial.

15)

What are the allowed visibility modifiers for a class?

The allowed modifiers for a class are nothing or the public modifier.

16)

What is the default visibility of an attribute or a method?

When declaring an attribute in a class without specifying its visibility, it has the default
visibility. The default visibility is the package visibility.

17)

What is the this keyword in java?

This keyword contains a reference to the current class.

18)

What is a constructor?

A constructor is a method that initializes the attributes of an object when it is created.


The constructor is called when using the new keyword.

19)

What are the characteristics of a constructor?

A constructor is a method inside a java class that respects those conditions:

Has the same name as the class


Has no return type

20)

What is a default constructor?

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)

Can we overload a constructor?

Yes, a constructor can be overloaded.

23)

Can we override a constructor?

No, overriding concept is related to inheritance.


The constructor is not inherited.

24)

Can we make a constructor final?

No, a constructor cannot be final.


For more details, please check our java constructor tutorial.

25)

What means a final class?

We cannot make child classes from this class. We cannot inherit from a final class.

26)

What means a final method?

The method cannot be overridden.

27)

What is blank final variable?

The variable is constant. We cannot change its value once it is initialized.

28)

What is blank final variable?

This is a variable declared as final and not initialized when declaring it.

29)

Where a blank final variable can be initialized?

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)

Can we declare the main method as final?

31)

What is an abstract class?

Yes

It is a class that has the abstract keyword in its declaration.


This class cannot be instantiated. It is created to be inherited.
A class that has at least one abstract method is an abstract class.

32)

What is an abstract method?

It is a method that has no implementation and is marked with the abstract keyword.

33)

What is method overriding?

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)

Is it possible to override static method?

NO, we cant override static methods because they belong to the class itself and not to the
instance.

35)

Is it possible to override the overloaded method?

Yes, there is no problem.


For more details, please check our java method overriding tutorial.

36)

What is method overloading?

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)

Is it possible to overload the main method ?

Yes
For more details, please check our java method overloading tutorial.

40)

Do virtual methods exist in java?

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

Inside the same class

In the child classes: related to inheritance

Different parameters list

Same parameters list

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)

What is the purpose of inheritance in OOP?

The inheritance allows the reuse of the code of the parent class inside child classes.

44)

What is the super class of all the classes in java ?

The Object class.

45)

What is the super keyword in java?

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)

Can we use both this() and super() in a constructor?

No, because each one among the two calls needs to be the first statement.

47)

Does JAVA allow multiple inheritance?

48)

What replaces multiple inheritance in java ?

No

The use of interfaces


For more details, please check our java inheritance tutorial.
49) What we can put in an interface?
We can put inside an interface:

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

methods to be non abstract.

abstract.

abstract classes dont have this restriction.

The interface can only have final data.

Multiple inheritance is not allowed for An interface can inherit from many
interfaces
classes.
The abstract keyword must be used

51)

The abstract keyword is not used even


when declaring methods.

What is static variable?

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)

What is static method?

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)

Why main method is static?

We dont need to create an object from the principal class in order to call the main
method.

54)

What is static block?

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?

We have composition when we use an attribute of type ClassB inside a classA.


Now, you finished the top 55 Core java interview questions and answers and you
are ready for the interview. We hope that you succeed in the interview and to get hired
for your dream job.
If you have some core java interview questions that you need to answer. Please feel free
to put it as a comment.
We also advice you to check our core java tutorial to master the java language.
You can get our latest interview questions by liking our how to program Facebook page
or subscribing our mailing list.