101 Important Java Interview Questions For Freshers
101 Important Java Interview Questions For Freshers
Courses
Corporate Training
For Business
Testimonials
Resources
Contact Us
101 Important Java Interview Questions for
Freshers
Java is a platform and programming language. It tops the list of all programming languages
and offers a variety of Job options. The purpose of the understanding Java Interview
Questions for Freshers is to familiarise you with the types of questions you might be
asked during your interviews for the Java related interviews. Java Training in
Chennai at FITA Academy is the ideal setting for you to explore your profession If
you’re curious in learning the language.
When a field or method is protected, it can only be accessed within the class to which it
belongs, from subclasses, and within the same package class.
By default, only code from the same package—not code from outside the native package—
can access a class’s field or method.
The method is accessible from the same class to which it belongs via the private: field.
It has access rights to the class that contains it, which means it can access all the outer class,
there are variables and methods declared.
subclass
A subclass, also known as a derived class, inherits from a superclass. It has access to every
superclass’s protected and open fields and methods.
Loops in Java are used to repeatedly execute a statement or a block of statements. the below
given are 3 types of loops in Java:
For Loops:
For loop are used to execute statements for a specified number of times. They are
commonly used when the number of iterations is known in advance.
While Loops:
When it is necessary to execute statements repeatedly While loops are used until a specific
condition is fulfilled. This type of condition is evaluated before the execution of the
statements.
Do-While Loops:
Do-while loops are similar to while loops, but with one difference: the condition is
evaluated after the execution of the statement block. By doing this, even if the condition is
initially false, it is ensured that the assertions are carried out at least once.
When a loop is discovered, the “break” command is used to end it instantly. It completely
exits the loop, regardless of the loop condition. For example:
The “continue” statement, on the other hand, is utilised to skip the current iteration of a
loop and move on to the subsequent iteration. It allows the loop to continue its execution.
For example:
Using these statements provides control over loop execution, allowing for specific
conditions to be met for termination or skipping of iterations.
for (;;) {
// Statements to execute
// Add any loop-breaking logic
}
Additionally, the “final” keyword has other uses in Java.When a method is marked as final,
subclasses cannot override it. Final methods are resolved at compile-time, making them
faster compared to other methods.
This is an important topic which is asked in Java Interview Questions for Freshers.
On the other hand, the double variable represents a double-precision decimal number and
usually occupies 8 bytes of memory. It provides higher precision and a larger range of
values compared to float.
You can brush up on your knowledge and succeed in your interview with the help of
these Java Interview Questions for freshers.
The ternary operator is a concise way to make decisions and assign values based on
conditions in Java.
13. What is the Java basic class that all other classes are
descended from?
The Java foundation class, which all other classes are descended from is java.lang.Object.
14.What is a default case in a switch statement? Can you
provide an example?
The default case is used in a switch statement when none of the other switch conditions
match. It acts as an optional case that is executed when no other cases are satisfied.
The default case should be placed after all the other cases have been coded within the
switch statement.
The default case provides a fallback option when none of the other cases match in a switch
statement.
One of the significant benefits of using packages is the ability to control access to classes
and encapsulate code within a specific namespace. This helps prevent naming conflicts and
provides a clear boundary for accessing code from other packages.
Packages also facilitate code reuse by allowing the importation of classes and resources
from other packages. This means that once code is packaged in a particular package, it can
be easily imported into other classes and used without duplicating the code.
One of the Core Java Interview Questions that is regularly asked is this one.
Interface:
An interface can only declare public static method signatures without providing their
concrete implementation. It serves as a contract for classes that implement it, ensuring that
they define the specified methods. Interfaces can also declare constants and can be
implemented by multiple classes.
Abstract Class:
An abstract class can have members with various access specifiers (private, public, etc.)
and can include both abstract and concrete methods. It can provide partial implementation
of methods. Abstract classes are designed to be extended by subclasses, which are then
responsible for implementing any abstract methods defined by the abstract class.
For example, if a developer imports the package university.*, it will load all the classes
from the university package, but it will not load classes from its sub-packages. To access
classes from a sub-package, the developer needs to explicitly import them, like this:
import university.department.*;
This is one of the Java Basic Interview Questions to know before attending an interview.
In contrast to certain other programming languages such as C++, Java does not provide
direct support for passing arguments by reference.
One of the significant and typical Java Interview Questions is this one.
You can advance in your chosen career with the aid of these Java Questions and Answers.
However, if you want to terminate the program forcefully and avoid executing any
statements in the finally block, you can use the System.exit(0) code line at the end of the
try block. This will abruptly terminate the program and prevent the execution of subsequent
code, including the finally block. It’s important to note that using System.exit(0) should be
done with caution, as it terminates the entire Java program abruptly.
The Catch block allows for catching and handling specific exceptions, providing an
opportunity to handle exceptions gracefully or perform error logging. Code that must
always be run is specified regardless of whether an exception happens or not, in the Finally
block. It is typically used for tasks such as closing resources or releasing acquired locks.
With the aid of these Java Interview Questions for Freshers, so that you may increase
your knowledge and succeed in your interview.
For example, in the following class, the constructor is invoked twice when two objects are
created using the new keyword:
Output:
Explanation:
The main method is invoked, creating an object of the Subclass class. The displayResult
method is then called on this object. The output will first display “Displaying from
subclass” as it is defined in the Subclass class. After that, the super.displayResult() line
calls the displayResult method of the superclass Superclass, resulting in the output
“Printing from superclass” being displayed.
31. In the following example, how many string objects can be produced?
One of the Core Java Interview Questions that is regularly asked is this one.
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
// Creating a vector
Vector names = new Vector<>();
// Adding elements to the vector
names.add("John");
names.add("Alice");
names.add("Bob");
// Accessing elements from the vector
System.out.println("First name: " + names.get(0));
System.out.println("Second name: " + names.get(1));
System.out.println("Third name: " + names.get(2));
// Changing an element in the vector
names.set(1, "Eve");
// Removing an element from the vector
names.remove(0);
// Printing all elements in the vector
System.out.println("Elements in the vector: " + names);
}
}
You can better prepare for the interview with the aid of these Java Programming
Interview Questions.
38. What are the two ways that Java implements multi-
threading?
Java offers two different ways to construct multi-threaded applications:
It is one of the important Java Interview Questions that come up repeatedly during the
interview.
46. There are two distinct classes, which are Class A and
Class B. Both classes are in the same package and Can an
object of Class B access a private member of Class A?
A class’s private members cannot be accessed from outside of that class, and this includes
other classes in the same package. Therefore, an object of Class B cannot access the private
members of Class A.
47. Can two methods with the same name exist in the same
class?
Yes, Two methods with the same name but distinct parameters might exist in the same
class. The procedure that is called is determined by the parameters passed when calling the
method.
For example, in the following class, there are two print methods with the same name but
different parameters. The appropriate method will be called depending on the parameters
passed:
You can have a thorough understanding of the topic before the interview by reading
these Java Programming Interview Questions.
// Superclass
class Vehicle {
public void start() {
System.out.println("Vehicle started.");
}
}
// Subclass inheriting from Vehicle
class Car extends Vehicle {
public void accelerate() {
System.out.println("Car accelerating.");
}
}
public class Main {
public static void main(String[] args) {
// Create an object of the Car class
Car myCar = new Car();
// Access methods from both Vehicle and Car classes
myCar.start(); // Inherited from Vehicle class
myCar.accelerate(); // Specific to Car class
}
}
The most common concept in Core Java Interview Questions for Freshers is pointers in
java
In the following example, the Stone class is declared as final, which means it cannot be
subclassed:
The Protected access modifier allows access to the member variables and methods from
within the class itself, from other classes within the same package, and from subclasses of
the class, regardless of the package they are in. This provides a level of access that is more
restrictive than the default (package-private) access, but less restrictive than public access.
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Before going to any interviews, you must be familiar with these Java Basic Interview
Questions.
Here’s a sample program to illustrate the distinction between a Java Queue and Stack
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class QueueVsStackExample {
public static void main(String[] args) {
// Creating a Queue
Queue queue = new LinkedList<>();
// Adding elements to the Queue
queue.add("Element 1");
queue.add("Element 2");
queue.add("Element 3");
// Removing and printing elements from the Queue (FIFO)
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
System.out.println();
// Creating a Stack
Stack stack = new Stack<>();
// Adding elements to the Stack
stack.push("Element 1");
stack.push("Element 2");
stack.push("Element 3");
// Removing and printing elements from the Stack (LIFO)
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
}
Output
Element 1
Element 2
Element 3
Element 3
Element 2
Element 1
Here’s an example that demonstrates the usage of the Integer wrapper class:
This is a significant subject that appears in the Java Interview Questions for Freshers
New: When a thread is created but not started yet, it is in the new state.
Runnable: A thread that is ready to run or is currently running is in the runnable state.
Waiting: A thread that is stand by for another thread to release a lock or for a specific
condition to be satisfied is in the waiting state.
Timed Waiting: A thread that is waiting for a certain period of time is in the timed waiting
state.
Blocked: A blocked thread that is awaiting a monitor lock to be released is in the blocked
state.
Terminated: A thread that has completed its execution or has been terminated abruptly is
in the terminated state.
60. Can a method have a different return type but the same
method name and arguments?
In Java, method overriding requires that the method name, arguments, and return type of
the overriding method must be exactly the same as the one of the substituted approach.
Therefore, if the return type of a method is different, it cannot be considered as overriding
the method.
These Java questions and answers can help you progress in your chosen field.
The code uses the postfix++ operator,This initially returns the value before increasing it.
Therefore, the output will be 4.
It’s one of the Java Basic Interview Questions that they frequently ask candidates.
Here’s an example:
With the aid of these Java Interview Questions for Freshers, you may brush up on your
knowledge and succeed in your interview.
View these Java Programming Questions to see how well they may help you review your
knowledge.
To help you improve your knowledge, we provide you with these Java Questions and
Answers.
In the below example, the equals() given that the two string objects it returns include the
same values. However, the == operator returns false as the two string objects are
referencing different objects:
With the help of these Java Programming Interview Questions, you may more
effectively prepare for the interview.
One of the Java Basic Interview Questions to be familiar with before an interview is this
one.
79. Can we use a different return type for void in the main
method?
No, for the programme to run successfully, the main method of a Java class can only have
a void return type.
However, if you need to return a value upon the completion of the main method, you can
use System.exit(int status).
Example:
Example:
class Demo {
int i;
public Demo(int a) {
i = a;
}
public Demo(int a, int b) {
// body
}
}
void forward()
void include()
User Authentication
Cookies
URL Rewriting
By reading through these Java Programming Interview Questions before the interview, you
can have a complete understanding of the subject.
The JIT (Just-In-Time) compiler in Java is a program that converts Direct instructions are
provided to the CPU from Java bytecode. By default, When a Java method is called, In
Java, the JIT compiler is activated and launched. It “just in time” converts the bytecode for
the method into machine language. following compilation of the procedure, the JVM
directly summons the compiled code for execution, rather than interpreting it. The JIT
compiler plays a crucial role in optimizing the performance of Java applications at runtime.
class ClassName {
// member variables
// class body with methods
}
One of the Core Java Interview Questions that is frequently asked is this particular one.
Inheritance: A process where one class acquires the properties and behaviors of another
class.
Encapsulation: A mechanism for wrapping data and code together as a single unit.
Example:
Controller-Based: We can define exception handler methods within our controller classes
and annotate them with the @ExceptionHandler annotation.
Global Exception Handler: The @ControllerAdvice annotation from Spring can be used
with any class to provide a global exception handler. Exception handling is a cross-cutting
topic.
To learn more about Java Programming, visit FITA Academy for the best Java Course
in Chennai or Java Training in Bangalore.
REQUEST A BATCH
Quick Enquiry
+91
Submit
Contact Us
Chennai
93450 45466
Bangalore
93450 45466
Coimbatore
95978 88270
Online
93450 45466
Madurai
97900 94102
Pondicherry
93635 21112
For Hiring
93840 47472
hr@fita.in
Corporate Training
90036 23340
Top Courses
Interview Questions
Digital Marketing Interview QuestionsJava Interview QuestionsSelenium Interview
QuestionsHadoop Interview QuestionsPython Interview QuestionsAWS Interview
QuestionsDevOps Interview QuestionsOracle Interview QuestionsPHP Interview
QuestionsUI UX Designer Interview Questions and AnswersAngularJs Interview
Questions
Read More
Chennai
Velachery
Anna Nagar
T.Nagar
Tambaram
Thoraipakkam OMR
Porur
Bangalore
Marathahalli
Coimbatore
Saravanampatty
Singanallur
Other Locations
Madurai
Pondicherry
Contact Us
Chennai:
93450 45466
Bangalore:
93450 45466
Coimbatore:
95978 88270
Pondicherry:
93635 21112
Madurai:
97900 94102
Online:
93450 45466
For Business
o Hire From FITA
o Careers
o Business opportunities
o Corporate Training
o Become an Instructor
Testimonials
o FITA Academy Reviews
o Student Testimonials
o Success Stories
Resources
o Blog
o Interview Questions
o Tutorials
o Free Placement Session
Follow us
o
TRENDING COURSES
JAVA Training In Chennai Software Testing Training In
Chennai Selenium Training In Chennai Python Training in
Chennai Data Science Course In Chennai Digital Marketing Course In
Chennai DevOps Training In Chennai German Classes In
Chennai Ar tif ic ial Int elligenc e Cours e in Chennai AW S Tr aining in
Chennai UI UX Design course in Chennai Tally course in Chennai Full
Stack Developer course in Chennai Salesforce Training in
Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical
Hacking course in Chennai RPA Training In Chennai Cyber Security
Course in Chennai IELTS Coaching in Chennai Graphic Design Courses
in Chennai Spoken English Classes in Chennai Data Analytics Course
in Chennai
Read More