Core Java Interview Questions and Answers For Freshers
Core Java Interview Questions and Answers For Freshers
Core Java Interview Questions and Answers For Freshers and Experienced
If you are thinking to build a career in Java programming, you need to crack an interview
in which you will be asked several Core Java Interview Questions and Answers as listed
below.
#1 What is Java?
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 1/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Simple: Java is easy to learn. The syntax of Java is based on C++ which makes it
easier to write the program in it.
Object-Oriented: Java follows the object-oriented paradigm which allows us to
maintain our code as the combination of different type of objects that incorporates
both data and behavior.
Portable: Java supports the read-once-write-anywhere approach. We can execute the
Java program on every machine. Java program (.java) is converted to bytecode
(.class) which can be easily run on every machine.
Platform Independent: Java is a platform-independent programming language. It is
different from other programming languages like C and C++ which need a platform to
be executed. Java comes with its platform on which its code is executed. Java doesn’t
depend upon the operating system to be executed.
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.
Robust: Java is a strong programming language as it uses strong memory
management. The concepts like Automatic garbage collection, Exception handling,
etc. make it more robust.
Architecture Neutral: Java is architectural neutral as it is not dependent on the
architecture. In C, the size of data types may vary according to the architecture (32
bit or 64 bit) which doesn’t exist in Java.
Interpreted: Java uses the Just-in-time (JIT) interpreter along with the compiler for
the program execution.
High Performance: Java is faster than other traditional interpreted programming
languages because Java bytecode is “close” to native code. It is still a little bit slower
than a compiled language (e.g., C++).
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.
Distributed: Java is distributed because it facilitates users to create distributed
applications in Java. RMI and EJB are used for creating distributed applications. This
feature of Java makes us able to access files by calling the methods from any machine
on the internet.
Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It
means classes are loaded on demand, it also supports functions from its native
languages, i.e., C and C++.
JVM
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 2/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
JVM is an acronym for Java Virtual Machine; it is an abstract machine which provides the
runtime environment in which Java bytecode can be executed. It is a specification that
specifies 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 that is created when we run the Java class. There
are three notions of the JVM: specification, implementation, and instance.
JRE
JRE stands for Java Runtime Environment. The Java Runtime Environment is a set of
software tools that are used for developing Java applications. It is used to provide a
runtime environment, it is the implementation of JVM. It physically exists, it contains a
set of libraries + other files that JVM uses at runtime.
JDK
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 3/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Local variables are defined in the method and scope of the variables that exist inside
the method itself.
An instance variable is defined inside the class and outside the method and the scope
of the variables exists throughout the class. This is the most important Core Java
Interview Questions and Answers
#6 What is a Class?
All Java codes are defined in a Class. It has variables and methods.
Methods are the place where the exact business logic has to be done. It contains a set
of statements (or) instructions to satisfy the particular requirement.
Example:
#7 What is an Object?
An instance of a class is called an object. The object has state and behavior.
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 4/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Whenever the JVM reads the “new()” keyword then it will create an instance of that
class.
Example:
The above code creates the object for the Addition class.
Inheritance
Encapsulation
Polymorphism
Abstraction
Interface
#9 What is Inheritance?
Inheritance means one class can extend to another class. So that the codes can be
reused from one class to another class. The existing class is known as the Superclass
whereas the derived class is known as a Subclass.
Example:
Super class:
public class Manipulation(){
}
Sub class:
public class Addition extends Manipulation(){
}
Inheritance is only applicable to the public and protected members only. Private
members can’t be inherited. This is the most important Core Java Interview Questions
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 5/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
and Answers
Purpose of Encapsulation:
Example:
get A(){
}
set A(int a){
if(a>0){// Here condition is applied
………
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 6/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
}
}
For encapsulation, we need to make all the instance variables private and create a setter
and getter for those variables. Which in turn will force others to call the setters rather
than access the data directly.
A single object can refer to the super-class or sub-class depending on the reference type
which is called polymorphism.
Example:
Using the Manipulation reference type we can call the Addition class “add()” method.
This ability is known as Polymorphism. Polymorphism is applicable for overriding and
not for overloading. This is the most important Core Java Interview Questions and
Answers
Method overriding happens if the sub-class method satisfies the below conditions with
the Super-class method:
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 7/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Example:
addition.add() method calls the add() method in the Sub-class and not the parent
class. So it overrides the Super-class method and is known as Method Overriding.
Method overloading happens for different classes or within the same class.
For method overloading, sub-class method should satisfy the below conditions with the
Super-class method (or) methods in the same class itself:
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 8/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Example:
Here the add() method has different parameters in the Addition class is overloaded in
the same class as with the super-class.
Multiple inheritances cannot be achieved in java. To overcome this problem the Interface
concept is introduced.
An interface is a template which has only method declarations and not the method
implementation.
Example:
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 9/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
All the variables in the interface are internally public static final that is constants.
The class which implements the interface should provide an implementation for all the
methods declared in the interface.
We can create the Abstract class by using the “Abstract” keyword before the class name.
An abstract class can have both “Abstract” methods and “Non-abstract” methods that are
a concrete class.
Abstract method:
The method which has only the declaration and not the implementation is called the
abstract method and it has the keyword called “abstract”. Declarations ends with a
semicolon. This is the most important Core Java Interview Questions and Answers
Example:
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 10/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
The concrete Subclass which extends the Abstract class should provide the
implementation for abstract methods.
PATH is an environment variable used by the operating system to locate the executables.
That’s why when we install Java or want any executable to be found by OS, we need to
add the directory location in the PATH variable.
Classpath is specific to Java and used by java executables to locate class files. We can
provide the classpath location while running java application and it can be a directory,
ZIP files, JAR files, etc.
main() method is the entry point of any standalone java application. The syntax of main
method is public static void main(String args[]).
Java’s main method is public and static so that Java runtime can access it without
initializing the class. The input parameter is an array of String through which we can
pass runtime arguments to the java program.
Yes, we can have multiple methods with name “main” in a single class. However if we
run the class, java runtime environment will look for main method with syntax as public
static void main(String args[]). This is the most important Core Java Interview Questions
and Answers
We can’t have more than one public class in a single java source file. A single source file
can have multiple classes that are not public.
Java package is the mechanism to organize the java classes by grouping them. The
grouping logic can be based on functionality or modules based. A java class fully
classified name contains package and class name. For example, java.lang.Object is the
fully classified name of Object class that is part of java.lang package.
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 11/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
java.lang package is imported by default and we don’t need to import any class from this
package explicitly.
Java provides access control through public, private and protected access modifier
keywords. When none of these are used, it’s called default access modifier.
A java class can only have public or default access modifier
The final keyword is used with Class to make sure no other class can extend it. For
example, the String class is final and we can’t extend it.
We can use the final keyword with methods to make sure child classes can’t override it.
Java’s final keyword can be used with variables to make sure that it can be assigned only
once. However the state of the variable can be changed, for example, we can assign a
final variable to an object only once but the object variables can change later on.
static keyword can be used with class-level variables to make it global i.e all the objects
will share the same variable.
static keyword can be used with methods also. A static method can access only static
variables of class and invoke only static methods of the class.
The finally block is used with try-catch to put the code that you want to get executed
always, even if an exception is thrown by the try-catch block. finally block is mostly used
to release resources created in the try block.
finalize() is a special method in Object class that we can override in our classes. This
method gets called by the garbage collector when the object is getting garbage
collected. This method is usually overridden to release system resources when the object
is garbage collected.
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 12/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
#25 What are Loops in Java? What are three types of loops?
1) For Loops
For loops are used in java to execute statements repeatedly for a given number of times.
It is 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.
Ans: An array groups data of same primitive type and is static in nature while vectors
are dynamic in nature and can hold data of different data types.
Ans: Runnable interface is used in java for implementing multi threaded applications.
Java.Lang.Runnable interface is implemented by a class to support multi threading.
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 13/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Ans: Multi threaded applications can be developed in Java by using any of the following
two methodologies:
We can define a class inside a class and they are called nested classes. Any non-static
nested class is known as an inner class. Inner classes are associated with the object of
the class and they can access all the variables and methods of the outer class. Since
inner classes are associated with the instance, we can’t have any static variables in
them.
We can have local inner class or anonymous inner class inside a class. For more details
read java inner class.
A local inner class without a name is known as an anonymous inner class. An anonymous
class is defined and instantiated in a single statement. Anonymous inner class always
extend a class or implement an interface.
Since an anonymous class has no name, it is not possible to define a constructor for an
anonymous class. Anonymous inner classes are accessible only at the point where it is
defined.
Java Classloader is the program that loads byte code program into memory when we
want to access any class. We can create our own classloader by extending ClassLoader
class and overriding loadClass(String name) method. Learn more at java classloader.
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 14/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Bootstrap Class Loader – It loads JDK internal classes, typically loads rt.jar and other
core classes.
Extensions Class Loader – It loads classes from the JDK extensions directory, usually
$JAVA_HOME/lib/ext directory.
System Class Loader – It loads classes from the current classpath that can be set while
invoking a program using -cp or -classpath command line options.
Java ternary operator is the only conditional operator that takes three operands. It’s a
one liner replacement for if-then-else statement and used a lot in java programming. We
can use ternary operator if-else conditions or even switch conditions using nested
ternary operators. An example can be found at java ternary operator.
The super keyword can be used to access the superclass method when you have
overridden the method in the child class.
We can use the super keyword to invoke superclass constructors in child class
constructor but in this case, it should be the first statement in the constructor method.
package com.journaldev.access;
public class SuperClass {
public SuperClass(){
}
public SuperClass(int i){}
public void test(){
System.out.println("super class test method");
}
}
package com.journaldev.access;
public class ChildClass extends SuperClass {
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 15/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
We can use break statement to terminate for, while, or do-while loop. We can use a
break statement in the switch statement to exit the switch case. You can see the
example of break statement at java break. We can use a break with the label to
terminate the nested loops.
The continue statement skips the current iteration of a for, while or do-while loop. We
can use the continue statement with the label to skip the current iteration of the
outermost loop.
this keyword provides the reference to the current object and it’s mostly used to make
sure that object variables are used, not the local variables having the same name.
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
We can also use this keyword to invoke other constructors from a constructor.
public Rectangle() {
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 16/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Yes, we can have try-finally statement and hence avoiding catch block.
Garbage Collection is the process of looking at heap memory, identifying which objects
are in use and which are not, and deleting the unused objects. In Java, the process of
deallocating memory is handled automatically by the garbage collector.
An Exception is a problem that can occur during the normal flow of execution. A method
can throw an exception when something wails at runtime. If that exception couldn’t be
handled, then the execution gets terminated before it completes the task.
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 17/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
If we handled the exception, then the normal flow gets continued. Exceptions are a
subclass of java.lang.Exception.
try{
//Risky codes are surrounded by this block
}catch(Exception e){
//Exceptions are caught in catch block
}
There are two types of Exceptions. They are explained below in detail.
a) Checked Exception:
These exceptions are checked by the compiler at the time of compilation. Classes that
extend Throwable class except Runtime exception and Error are called checked
Exception.
Checked Exceptions must either declare the exception using throws keyword (or)
surrounded by appropriate try/catch.
b) Unchecked Exception:
These exceptions are not checked during the compile time by the compiler. The compiler
doesn’t force to handle these exceptions. It includes:
Arithmetic Exception
ArrayIndexOutOfBounds Exception
a) Using try/catch:
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 18/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
The risky code is surrounded by try block. If an exception occurs, then it is caught by the
catch block which is followed by the try block.
Example:
class Manipulation{
public static void main(String[] args){
add();
}
Public void add(){
try{
addition();
}catch(Exception e){
e.printStacktrace();
}
}
}
At the end of the method, we can declare the exception using throws keyword.
Example:
class Manipulation{
public static void main(String[] args){
add();
}
public void add() throws Exception{
addition();
}
}
The normal flow of the execution won’t be terminated if an exception gets handled
a) try:
When a risky code is surrounded by a try block. An exception occurring in the try block is
caught by a catch block. Try can be followed either by catch (or) finally (or) both. But
any one of the blocks is mandatory.
b) catch:
c) finally:
This is followed either by try block (or) catch block. This block gets executed regardless
of an exception. So generally clean up codes are provided here.
Join Telegram Group of Daily Jobs Updates for 2010-2023 Batch: Click Here
If You Want To Get More Daily Such Jobs Updates, Career Advice Then Join the
Telegram Group From Above Link And Never Miss Update.
How To Get a Job Easily: Professional Advice For Job Seekers: Click here
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 20/21
1/8/23, 2:51 PM Core Java Interview Questions and Answers For Freshers
Career Tips for Freshers: Top 7 Hacks To Land Your Target Job: Click here
Feel Like Demotivated? Check Out our Motivation For You: Click here
Top 5 Best Mobile Tracking App in 2021 For Mobile & PC: Click here
Cyber Tecz
Our Vision is To Provide Latest Off Campus Placement Papers, Interview Questions, Resume
Writing Tips, Exam Pattern With Syllabus, Career Advice And Many More With Latest Jobs
Information.
https://advisor.cybertecz.in/core-java-interview-questions-and-answers-for-freshers-and-experienced/ 21/21