Table of Contents

The king of programming languages is Java. It becomes the delighted to be the programming language software developers. Over a billion devices currently use Java for development is astonishing. Java’s unique architecture includes the capacity to run on any system. It has played a significant role in the creation of several applications and the development of enabling innovation. Let’s start with the blog’s primary goal: a list of more than 40+ Core Java Interview Questions and Answers for freshers and Experienced in 2024.

Core Java Interview Questions and Answers
Core Java Interview Questions and Answers

Core Java Interview Questions and Answers for Freshers

1. How does core java differ from Java?

Java is a programming language widely used in many applications, from websites to games. All android devices use Java as their core language. Internet application uses java frequently because of its safe and secure feature.

Core java is the term Sun Microsystems uses to state the basic edition of the java platform. It is not a programming language but a set of libraries. Core java is the Standard edition SE and fundamental for all java editions. It has APIs and libraries required for executing the basic principles of Java.

2. What is JVM?

JVM stands for java Virtual machine. The Java Runtime Environment contains it.

JVM converts the bytecode into machine code and provides the run time

environment.

We write a java program and save it as a .java file, and first, we compile it as

javac .java. Now the java compiler converts the source into an intermediate

file, .class file named bytecode. Next, we will run the code as Java <Classname>

Now the JVM, the Java Virtual machine, converts the bytecode into

machine-understandable code and provides us the output.

3. What is a JIT?

JIT is a Just-In-Time compiler. It is a part of JVM. At compilation or run time, it

improves the Java application’s performance.

4. What happens if I write static public void main instead of public static void main?

The program will run successfully. Because there is no specific order of specifiers in Java

5. Do the variables in Java have a default value?

When declared, a trash value adds to the variables in the C programming language. In java, on the other hand nothing adds to the variable when declared. Before we populate the variable with a value, it is empty by default.

6. Why pointer not used in Java?

The purpose of pointers is to access memory. JVM implicitly manages memory in Java.

Hence, pointers exist in Java.

7. what happens if the main method not declared static?

When the main method is not declared static, then the program compiles successfully but throws a runtime error as “NoSuchMethodError.”

8. What is a default constructor?

The default constructor provides the default values to the object, like 0, null, etc., depending on the type.

Core Java Interview Questions and Answers for Freshers on Class and object

9. What is a class in Java?

A class is a user-defined prototype for which we create objects. It is the collection of properties or methods common to all things of one type.

10. What do you mean by an object in Java?

An instance of a java class is called a java object. An object has a state and behavior. Variables store an object’s state, whereas methods show the object’s action. Objects created in Runtime for every class in Java.

Core Java Interview Questions and Answers

11. What is an instance in Java?

Even though we commonly use objects and instance terms, they occasionally confuse us. These two concepts are nearly synonymous.

When creating an object in Java, we can produce instances of any class. The object shows that the class is there, and we can use its variables and methods. As a result, sometimes we also use the term “instance” to describe it.

Employee emp= new Employee ();

Because it contains the memory address and reference of the object, emp is both an object name and an instance variable in this case. In general, we refer to objects. In specific, we refer to it as an instance.

12. What do you mean by reference in Java?

Public class Employee{
Int x=10;
Public static void main(String[] args){
Employee emp=new Employee();
}
}

Here, a new instance of an Employee class is created. Space is allocated in memory for the field “x.” When we create an object using the “new” keyword, the memory reference of that object stores in the reference variable. Therefore, we named it as a reference as it contains the actual address of the object.

Employee stud = new Employee();

The Employee object is created here, and its reference is stored in the emp variable.

13. Explain about constructor and usage of the constructor

A constructor is a special method used to initialize the object. A Constructor is invoked when the first object of a class is created. Similarly, It is also used to initialize the values of attributes of that object.

A Constructor should be created as follows.

  •  Constructor names need to match class names exactly.
  • There can be no explicit return type
  • We cannot synchronize Java constructors.
  • we can use Access specifiers in constructors.

If there is no constructor in the program, the compiler will automatically create a default constructor

Example

class Car1{  
Car1()
{
System.out.println("car is created");
}  
public static void main(String args[]){  
Car1 b=new Car1();  
}  
} 

Output

Car is created

14. Mention the types of Constructors in Java

1. Default constructor

2. Parameterized constructor

15. Can a constructor return value

Yes, a constructor can return the current instance of the class implicitly. But it

cannot return a value explicitly

16. Can you make the constructor final?

No, we cannot make the constructor final. Java constructors cannot be synchronized, inherited, static, or abstract. The constructor itself cannot be modified.

There is no need to define the constructor as final when there is no probability of modification because there is no reason to prevent change. We are already aware that the last keyword is employed to limit alteration.

17. can you overload the constructor

Yes, a constructor can be overloaded. Constructors are like methods without return types. A constructor can be overloaded by the different parameter lists. Different types of the parameter and the number of parameters

18. Differentiate constructor and method in java

Constructors Methods
A Constructor’s name and class name. should be sameA Method’s name can be anything.
A Constructor is called implicitlyA Method is called explicitly
when an object is created, constructor is invoked.A Method is called through method calls.
A Constructor doesn’t have a return type.A Method has a return type.

19. What is a copy constructor?

Typically, we write a parameterized constructor that accepts the values for all instance variables and initializes them with the supplied values

int name;
int age;
public Student(String name, int age){
this.name = name;
   this.age = age;
}

But with a copy constructor, an object of the current class is taken as an argument, and the instance variables’ values are initialized with those of the obtained object.

public Student(Student std){
   this.name = std.name;
   this.age = std.age;
}

If you create an object and call the copy constructor with object it as a parameter, You will receive a copy of the object that is previously created,.

Student std = new Student("nameValue", ageValue);
Student copyOfStd = new Student(std);

Core Java Interview Questions and Answers for Freshers on Method Overloading

20. Explain the “method overloading.”

If a class has many methods having the same name but different in arguments and return type is refered to as Method Overloading.

Types of Overloading

1. Method having a different number of arguments

class Addition{  
static int add(int a,int b)
{
return a+b;
}  
static int add(int a,int b,int c)
{
return a+b+c;
}  
}  
class TestingOverloading1{  
public static void main(String[] args){  
System.out.println(Addition.add(11,11));  
System.out.println(Addition.add(11,11,11));  
}}  

2. Method has different types of arguments

class Addition{  
static int add(int a, int b)
{
return a+b;
}  
static double add(double a, double b)
{
return a+b;
}  
}  
class TestingOverloading2{  
public static void main(String[] args){  
System.out.println(Addition.add(11,11));  
System.out.println(Addition.add(12.3,12.6));  
}}  

core java interview questions and answers for experienced on “this” and “super” keyword

21. What is the purpose of the “this” keyword?

 “this” keyword in Java is used to refer to the current class variables, methods, and objects. 

Let’s see each with an example

1. To refer current class variable

In this case the variable has the same name as the parameters of constructors. “this” is used to avoid the naming conflict between class attributes and parameters

class Employee{  
int empid;  
String name;  
float salary;  
Employee(int empid,String name,float salary){  
this.empid=empid;  
this.name=name;  
this.salary=salary;  
}  
void display(){System.out.println(empid+" "+name+" "+salary);}  
} 
class TestThis2{  
public static void main(String args[]){  
Employee e1=new Employee (111, "Ankit",5000f);  
Employee e2=new Employee (112, "summit",6000f);  
e1.display();  
e2.display();  
}}  

Output

111 Ankit 5000.0

112 summit 6000.0

2. To refer current class method

 “This” keyword is used to call a current class method

class Test {
void display()
{
this.show();
System.out.println("Inside display function");
}
void show() {
System.out.println("Inside show function");
}
public static void main(String args[]) {
Test t1 = new Test();
t1.display();
}
}

Output

Inside show function

Inside display function

3. To refer current class constructor.

The current class constructor can be called using the this() function. It allows for the constructor to be reused. In other words, constructor chaining is accomplished.

class A1{  
A1(){System.out.println("hello a1");}  
A1(int x){  
this();  
System.out.println(x);  
}  
}  
class Test{  
public static void main(String args[]){  
A1 a=new A1(10);  
}}  

Output 

Hello a1

10

22. What is the purpose of the super keyword?

Parent class object is referred to by the super keyword.”Super” is used in 3 ways

  1. While referring to the parent class variable, “super” is used.
  2. super can be used to call the immediate parent class method.
  3. super() can be used to call the immediate parent class constructor

core java interview questions and answers for experienced on the “final” keyword

23. For what the final keyword is used

final keyword is used to restrict the modification. The final keyword applies to variable, method, and classes.

 Example for “final” variable

 A variable cannot be changed if the variable is a final variable.

	class Car{  
            	final int speed=90;  
            	void run(){ 
            	speed=400; 
	 	} 
	 	public static void main(String args[]){  
	 	Car obj=new Car();  
            	obj.run();  
            	}  
            	}

Output            
Compile Time Error

 Example of “final” method

Methods cannot be overloaded if it is a final method.

class Car{  
final void run(){System.out.println("running");}  
} 
class Honda extends Car{  
void run(){System.out.println("running safely with 100kmph");}  
public static void main(String args[]){  
Honda honda= new Honda();  
honda.run();  
} }  

Output
Compile Time Error

  Example for “final” class

You cannot extend a class if it is a final class

final class Car{}  
class Honda extends Car{ 
void run(){System.out.println("running safely with 100kmph");}  
public static void main(String args[]){  
Honda honda= new Honda();  
honda.run();  
}
} 

Output        
Compile time error

core Java interview questions and answers for experienced on “static” keyword

24. Define the keyword “static .”

Static refers to a constant value common for all objects. “static” applies to variables and methods. It manages the memory.

Static Variable

Memory is allocated to the variable every time the object creates. But if we declare a variable static, the memory is allotted only once when the class loads.

Example of Static Variable

class Employee{  
  	 int empid;  
String name;  
   	Static String companyname="XYZ";  
}  

Static Method

  1. A static method is an attribute of the class, not the class’s object.
  2. It is possible to call a static method by its class name, not by creating an object
  3. A static method can access and control static data members’ values.

Example of static method

class Employee{  
    	 int empid;  
 String name;  
 static String companyname= "XYZ";  
   	 static void change(){  
     	 companyname= "ABC";  
     	}  
     	Employee(int r, String n){  
     	empid= r;  
     	name = n;  
     	}  
void display(){System.out.println(empid+" "+name+" "+companyname);}  
}  
public class TestingStaticMethod{  
    	public static void main(String args[]){  
    	Employee.change();  
    	Employee e1 = new Employee(111,"Kavin");  
Employee e2 = new Employee (222, "Arya");  
    	Employee e3 = new Employee (333, "Sam");  
    	e1.display();  
    	e2.display();  
    	e3.display();  
    	}  
}  

Output

111 Kavin ABC
222 Arya ABC
333 Sam ABC

25. Can we overload a static method?

Case1 Yes, It is possible to overload static methods with the same method name but different arguments by number or type.
Case2 No, we cannot overload static methods if they differ only by static keyword.

core java interview questions and answers for experienced on Inheritance

26. what is an inheritance in Java

Acquiring the properties of one class for another class for reusability is known as inheritance.

27. what are the three types of inheritance

1. single Inheritance
2. Multi-level inheritance
3. Hierarchical inheritance

28. What is Multi-level inheritance in Java

Creating a new class from an existing class, then keeping the newly created class as a parent, creating another subclass, and so on. This kind of chain of inheritance is called Multi-level inheritance.

Example

class Bird{  
void eat(){System.out.println("eating...");}  
}  
class Peacock extends Bird{  
void dance(){System.out.println("Dancing...");}  
}  
class BabyPeacock extends Peacock{  
void weep(){System.out.println("weeping...");}  
}  
class TestInheritance2{  
public static void main(String args[]){  
BabyPeacock d=new BabyPeacock();  
d.weep();  
d.dance();  
d.eat();  
}}  

Output

Weeping…dancing …Eating…

29. Why are multiple inheritances not supported in Java

Multiple inheritance is a subclass having two parent classes which are not supported in Java, because it leads to confusion when two parent classes have the same method name.

Core Java Interview Questions and Answers

core java interview questions and answers for experienced on Array

30. What is an array in Java?

An array is a finite collection of similar datatype arranged in an adjacent manner.

Syntax 1:
Datatype arrayname[]= new datatype[size];
Syntax 2:
Datatype arrayname[];
Arrayname = new datatype[size];
Syntax 3: 
Datatype arrayname[]={value1,value2,value3,...};

31. What are the types of an array in Java?

  1. Single-Dimensional Array.
  2. Multi-Dimensional Array.

32 What is the access modifier in Java?

There are four types of access modifiers:

  1. Private: The variables, methods, and classes which are declared as private cannot be accessed from outside the class.
  2. Default: It will be the default if you do not specify any access specifier. We can access it within the package, not from outside the package.
  3. Protected: If a protected modifier is used. You can have access within the package and outside the package by the child class.

Public: This specifier means access is everywhere. It can be accessed in and out of the class, in and out of the package.

core java interview questions and answers for experienced on Loops

33. Explain for loop

When you want to repeat a section of the program many times until a condition is true, then for loop is used.

Syntax:
for(initialization; condition; increment/decrement){    
// Program Statements
}    
Example
for(int i=0;i<5;i++)
{
System.out.println(i);
}

34. Explain While loop

Until the provided Boolean condition is true, a section of the program is repeated again using the while loop in Java. when the Boolean condition changes to false, the loop ends.

Syntax:
while (condition){    
//code to be executed   
Increment/decrement statement  
}    
Example 
public class WhileExample {  
public static void main(String[] args) {  
int i=1;
while(i<=5){  
System.out.println(i);  
 i++;  
 }  
}  
}  

Output
1
2
3
4
5


35. Explain Do while loop in detail

A portion of the program is repeatedly iterated using the Java do-while loop until the desired condition is met. You can use a do-while loop when you want the statement to run once, even if the condition fails.

Syntax:
do{    
//code to be executed / loop body  
//update statement   
}while (condition);    
Example
public class DoWhileExample {    
public static void main(String[] args) {    
 int i=1;    
 do{    
 System.out.println(i);    
 i++;    
 }while(i<=10);    
}    
}    

core java interview questions and answers for experienced on Exception Handling

36. What is Exception Handling in Java?

The exception handling feature is one of Java’s robust mechanisms for handling runtime faults and preserving the application’s regular flow.
Try: This block reserves the collection of sentences or lines of code that call for exception tracking.
Catch: This block catches all the exceptions available in this block.
Finally: This block executes every time the program runs. Even there is no exception available in try block.
Example

public class ExceptionExample{  
public static void main(String args[]){  
try{  
 //code that may raise exception  
int exp=100/0;  
}catch(ArithmeticException e){System.out.println(e);}  
//rest code of the program  
System.out.println("rest of the code...");  
}  
}  

Output

Exception in thread main java.lang.ArithmeticException:/ by zero

rest of the code…

37. Why is Java’s exception handling necessary?

The program will crash if a try-and-catch block is not there and an exception occurs. An efficient program operation without program termination ensures via exception handling.

38. List the various exception kinds in Java.

There are often two types of exceptions in Java, depending on how the JVM handles them:

Checked: This occurred throughout the compilation. Here, the compiler examines if the exception handles and throws an error accordingly.

Unchecked: Take place while the program is running. We cannot discover them during compilation.

However, based on their definition, there are two additional exceptions: built-in and user-defined expectations.

39. Can we use try instead of finally and catch blocks?

No, doing so will result in an error in compilation. finally block and catch block cannot be removed either one can be removed.

core java interview questions and answers for experienced on Threads

40. What is a thread?

The thread is a separate execution path. It is a method for utilizing all of the CPUs on a computer. You can accelerate tasks that are CPU-bound by using several threads. For example, if one thread takes 100 milliseconds to accomplish a job, you can employ ten threads to minimize that operation into ten milliseconds. One of the key selling features of Java is its superb multithreading capabilities at the language level.

41. In Java, what distinguishes a thread from a process?

The thread is a subset of Process. we can have multiple threads in one process. Though two processes execute on different memory areas, all threads share the same memory space.

42.What are the various thread states? What is the thread lifecycle?

A Thread can be in any one of the states at a time.
New
Runnable
Blocked
Waiting
Timed Waiting
Terminated

Practice the Core Java Interview Questions and Answers with Example Programs. It will be quite helpful for your interview. I hope your interview goes well.

Recommended Courses:

1.Java Programming Course for Beginners
2.JAVA Foundation with DS & Algo Combo Certification Course
3.Certified Java Full Stack Developer Course in Online
4.Certified Application Security Engineer (CASE) JAVA Course

Related Posts

The following posts contain questions similar to core java interview questions and answers.

1.Top 30 Must-Know Java Interview Questions

2.Top 50+ OOPs Interview Questions and Answers in 2023

3.The Top 40 Coding Interview Questions

FAQs

Q.1 Why is Java a platform-independent language?

Because its byte codes can execute on any system, regardless of its underlying operating system, Java is known as a platform independent language.

Q.2 Why is Java not entirely object-oriented?

Java is not entirely object-oriented since it uses eight primitive data types that are not objects, such as Boolean, byte, char, int, float, double, long, and short.

Q.3 Benefits and Drawbacks of Java careers’

Benefits – decent packaging ,Increasing demand, Freelance options.
Drawbacks – High competition, long hours at work, Stressful work.

Q.4 What exactly the java developers do?

Java programmers are primarily responsible for designing, creating, and managing the Java code before it is executed on all platforms that support Java. The position is more expensive in larger organizations.

E&ICT IIT Guwahati Best Data Science Program

Ranks Amongst Top #5 Upskilling Courses of all time in 2021 by India Today

View Course

Join the Discussion

Interested in Henry Harvin Blog?
Get Course Membership Worth Rs 6000/-
For Free

Our Career Advisor will give you a call shortly

Someone from India

Just purchased a course

1 minutes ago

Noida Address:

Henry Harvin House, B-12, Sector 6, Noida, Uttar Pradesh 201301

FREE 15min Course Guidance Session:

Henry Harvin Student's Reviews
Henry Harvin Reviews on Trustpilot | Henry Harvin Reviews on Ambitionbox |
Henry Harvin Reviews on Glassdoor| Henry Harvin Reviews on Coursereport