Top Java Interview Questions and Answer You Must Prepare
Top Java Interview Questions and Answer You Must Prepare
Welcome to topInterviewsQuestion.us. In this post, We will discuss the mostly asked Java Interview
Questions and Answers which can help freshers and as well as experienced candidates.
There is the list of core java interview questions. If there is any core java interview question that has been
asked to you, kindly post it in the ask question section. We assure that you will get here the 90%
frequently asked interview questions and answers.
The answers to the core java interview questions are short and to the point. The core java interview
questions are categorized in Basics of java interview questions, Oops interview questions, String Handling
interview questions, Multi-threading interview questions, collection interview questions, JDBC interview
questions, etc.
#Note: All Java questions written below are personal experience based so Please try to cover all the
questions & get succeed in the interview.
Comparison
C++ Java
Index
Platform- C++ is platform-
Java is platform-independent.
independent dependent.
Mainly used C++ is mainly used Java is mainly used for
for for system application programming. It is
programming. widely used in window, web-
based, enterprise and mobile
applications.
Java was designed and created
C++ was designed
as an interpreter for printing
for systems and
systems but later extended as a
applications
support network computing. It
Design Goal programming. It was
was designed with a goal of
an extension of C
being easy to use and
programming
accessible to a broader
language.
audience.
C++ supports Java doesn’t support the goto
Goto
the goto statement. statement.
Java doesn’t support multiple
Multiple C++ supports inheritance through class. It can
inheritance multiple inheritance. be achieved by interfaces in
java.
C++
Operator Java doesn’t support operator
supports operator
Overloading overloading.
overloading.
C++ Java supports pointer internally.
supports pointers. However, you can’t write the
Pointers You can write pointer program in java. It means
pointer program in java has restricted pointer
C++. support in java.
C++ uses compiler Java uses compiler and
only. C++ is interpreter both. Java source
compiled and run code is converted into bytecode
using the compiler at compilation time. The
Compiler and
which converts interpreter executes this
Interpreter
source code into bytecode at runtime and
machine code so, produces output. Java is
C++ is platform interpreted that is why it is
dependent. platform independent.
Call by Value C++ supports both Java supports call by value only.
and Call by call by value and There is no call by reference in
reference call by reference. java.
C++ supports
Structure and Java doesn’t support structures
structures and
Union and unions.
unions.
C++ doesn’t have
built-in support for
Thread
threads. It relies on Java has built-in thread support.
Support
third-party libraries
for thread support.
Java supports documentation
C++ doesn’t support
Documentation comment (/** … */) to create
documentation
comment documentation for java source
comment.
code.
C++ supports virtual Java has no virtual keyword. We
keyword so that we can override all non-static
Virtual
can decide whether methods by default. In other
Keyword
or not override a words, non-static methods are
function. virtual by default.
unsigned right C++ doesn’t support Java supports unsigned right
shift >>> >>> operator. shift >>> operator that fills zero
at the top for the negative
numbers. For positive numbers,
it works same like >> operator.
Java uses a single inheritance
C++ creates a new tree always because all classes
Inheritance
inheritance tree are the child of Object class in
Tree
always. java. The object class is the root
of the inheritance tree in java.
C++ is nearer to Java is not so interactive with
Hardware
hardware. hardware.
Java is also an object-
C++ is an object-
oriented language. However,
oriented language.
everything (except fundamental
Object- However, in C
types) is an object in Java. It is a
oriented language, single
single root hierarchy as
root hierarchy is not
everything gets derived from
possible.
java.lang.Object.
2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-
classes,and from the class of same package,but not from outside.
3. Default: Method,Field,class can be accessed only from the same package and not from outside of it’s
native package.
4. Private: Method,Field can be accessed from the same class to which they belong.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public
and protected methods and fields of its super class.
Secured: Java is secured because it doesn’t use explicit pointers. Java also provides the
concept of ByteCode and Exception handling which makes it more secured.
Interpreted: Java uses the Just-in-time (JIT) interpreter along with the compiler for the
program execution.
Multithreaded: We can write Java programs that deal with many tasks at once by defining
multiple threads. The main advantage of multi-threading is that it doesn’t occupy memory
for each thread. It shares a common memory area. Threads are important for multi-media,
Web applications, etc.
1. Default constructor
2. Parameterized constructor
Ans: Looping is used in programming to execute a statement or a block of statement repeatedly. There are
three types of loops in Java:
1) For Loops
For loops are used in java to execute statements repeatedly for a given number of times. For loops are
used when number of times to execute the statements is known to programmer.
2) While Loops
While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In
while loops, condition is checked first before execution of statements.
3) Do While Loops
Do While Loop is same as While loop with only difference that condition is checked after execution of
block of statements. Hence in case of do while loop, statements are executed at least once.
Many types:
1. Class(Method) Area: Class Area stores per-class structures such as the runtime constant pool,
field, method data, and the code for methods.
2. Heap: It is the runtime data area in which the memory is allocated to the objects
3. Stack: Java Stack stores frames. It holds local variables and partial results, and plays a part in
method invocation and return. Each thread has a private JVM stack, created at the same time as the
thread. A new frame is created each time a method is invoked. A frame is destroyed when its
method invocation completes.
4. Program Counter Register: PC (program counter) register contains the address of the Java virtual
machine instruction currently being executed.
5. Native Method Stack: It contains all the native methods used in the application.
Q. Define Inheritance.
Java includes the feature of inheritance which an object-oriented programming concept. Inheritance lets a
derived class to inherit the methods of a base class.
In the below example, if rank is 1, status is assigned a value of “Done” else “Pending”.
1. i) hasNext() 1. i) hasMoreElements()
2. ii) next() 2. ii) nextElement()
3. iii) remove()
Q. What are the main differences between the Java platform and other platforms?
There are the following differences between the Java platform and other platforms.
Java is the software-based platform whereas other platforms may be the hardware platforms or
software-based platforms.
Java is executed on the top of other hardware platforms whereas other platforms can only have the
hardware components.
class Intellipaat1
{
void test()
{
system.out.println("test() method");
}
}class Intellipaat2
{
void test()
{
system.out.println("test() method");
}
}Multiple inheritance
class C extends Intellipaat1, Intellipaat2
{
………………………………………….
…………………………………………..
}
Q. Define JSON?
Expansion of JSON is “JavaScript Object Notation”, It is a much lighter and readable alternative to XML. It
is an independent and easily parse-able in all programming languages. It is primarily used for
Communicating between client – server or server -server communication. It is a much lighter and readable
alternative to XML.
Q. What is class-loader?
Class-loader is a subsystem of JVM which is used to load class files. Whenever we run the java program,
it is loaded first by the class-loader. There are three built-in class-loaders in Java.
1. Bootstrap Class Loader: This is the first class-loader which is the super class of Extension class-
loader. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang
package classes, java.net package classes, java.util package classes, java.io package classes,
java.sql package classes, etc.
2. Extension Class Loader: This is the child class loader of Bootstrap and parent class-loader of
System class loader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.
3. System/Application Class Loader: This is the child class-loader of Extension class-loader. It loads
the class files from the class-path. By default, the class path is set to the current directory. You can
change the class path using “-cp” or “-class path” switch. It is also known as Application class loader.
30Javatpoint
Javatpoint1020
Explanation :
In the first case, 10 and 20 are treated as numbers and added to be 30. Now, their sum 30 is treated as
the string and concatenated with the string Javatpoint. Therefore, the output will be 30Javatpoint.
In the second case, the string Javatpoint is concatenated with 10 to be the string Javatpoint10 which will
then be concatenated with 20 to be Javatpoint1020.
Core Java – OOPs Concepts: Initial OOPs Interview Questions
Q. What is Polymorphism?
Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using
inheritance and interface.
Object-oriented languages follow all the concepts of OOPs whereas, the object-based language
doesn’t follow all the concepts of OOPs like inheritance and polymorphism.
Object-oriented languages do not have the inbuilt objects whereas Object-based languages have the
inbuilt objects, for example, JavaScript has window object.
Examples of object-oriented programming are Java, C#, Smalltalk, etc. whereas the examples of
object-based languages are JavaScript, VBScript, etc
Q. What is multiple inheritance? Is it supported by Java?
If a child class inherits the property from multiple classes is known as multiple
inheritance. Java does not allow to extend multiple classes.
The problem with multiple inheritance is that if multiple parent classes have a
same method name, then at runtime it becomes difficult for the compiler to
decide which method to execute from the child class.
class Car {
void run()
{
System.out.println(“car is running”);
}
}
class Audi extends Car {
void run()
{
System.out.prinltn(“Audi is running safely with 100km”);
}
public static void main(String args[])
{
Car b= new Audi(); //upcasting
b.run();
}
}
Method Overloading :
In Method Overloading, Methods of the same class shares the same name but each method must
have different number of parameters or parameters having different types and order.
Method Overloading is to “add” or “extend” more to method’s behavior.
It is a compile time polymorphism.
The methods must have different signature.
It may or may not need inheritance in Method Overloading.
Let’s take a look at the example below to understand it better.
class Adder {
Static int add(int a, int b)
{
return a+b;
}
Static double add( double a, double b)
{
return a+b;
}
public static void main(String args[])
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Method Overriding:
In Method Overriding, sub class have the same method with same name and exactly the same
number and type of parameters and same return type as a super class.
Method Overriding is to “Change” existing behavior of method.
It is a run time polymorphism.
The methods must have same signature.
It always requires inheritance in Method Overriding.
class Car {
void run(){
System.out.println(“car is running”);
}
Class Audi extends Car{
void run()
{
System.out.prinltn(“Audi is running safely with 100km”);
}
public static void main( String args[])
{
Car b=new Audi();
b.run();
}
}
class Base {
private static void display() {
System.out.println("Static or class method from Base");
}
public void print() {
System.out.println("Non-static or instance method from Base");
}
class Derived extends Base {
private static void display() {
System.out.println("Static or class method from Derived");
}
public void print() {
System.out.println("Non-static or instance method from Derived");
}
public class test {
public static void main(String args[])
{
Base obj= new Derived();
obj1.display();
obj1.print();
}
}