Interview Questions On Java
Interview Questions On Java
1. What is Java?
Java is a platform-independent high-level programming language. It is platform-independent because its byte codes
can run on any system regardless of its operating system.
Platform independent
High performance
Multi-threaded
Inheritance
Encapsulation
Polymorphism
Abstraction
Interface
Encapsulation is a concept in Object-Oriented Programming for combining properties and methods in a single unit.
Encapsulation helps developers follow a modular approach for software development because each object has its
own set of methods and variables and serves its functions independent of other objects. In addition to that,
encapsulation serves data hiding purposes.
5. What is polymorphism?
Polymorphism is one interface with many implementations. This characteristic allows you to assign a different
meaning or usage to something in different contexts. For example, you can use polymorphisms to enable more
than one form for entities, such as variables, functions, or objects.
An interface as it relates to Java is a blueprint of a class or a collection of abstract methods and static
constants.
Each method is public and abstract, but it does not contain any constructor.
The two types of constructors in Java are the Default Constructor and the Parameterized Constructor.
Default Constructor
o Main purpose is to initialize the instance variables with the default values
Parameterized Constructor
It is an abstract machine that provides a run-time environment that allows programmers to execute Java
bytecode.
JRE refers to a runtime environment that allows programmers to execute Java bytecode.
13. In Java, what are the differences between heap and stack memory?
Memory
Access
Memory Management
Memory management for heap stems from the generation associated with each object.
Lifetime
Stack exists until the end of the execution of the thread.
Heap memory lives from the start till the end of application execution.
Usage
Stack memory only contains local primitive and reference variables to objects in heap space.
Whenever you create an object, it is always stored away in the heap space.
A JIT compiler runs after the program is executed and compiles the code into a faster form, hosting the CPU’s
native instructing set.
JIT can access dynamic runtime information, and a standard compiler does not. Therefore, JIT can better optimize
frequently used inlining functions.
An inner class is a class that is nested within another class. An Inner class has access rights for the class that is
nesting it, and it can access all variables and methods defined in the outer class.
A subclass is a class that inherits from another class called the superclass. Subclass can access all public and
protected methods and fields of its superclass.
In Java, packages are the collection of related classes and interfaces which bundle together.
Packages in Java allow developers to modularize the code and optimize its reuse easily. In addition, developers can
use other classes to import and reuse the code within the packages.
Packages can also contain hidden classes that are not visible to the outer classes and are only used within
the package.
Packages create a standardized hierarchical structure, making it easier to locate related classes.
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. Methods contain a set of statements or
instructions that satisfy specified requirements.
A singleton class in Java can have only one instance. Therefore, all its methods and variables belong to this
instance. The singleton class concept is useful when the developer needs to limit the number of objects for a class.
The sole purpose of using Constructors in Java is to create an instance of a class. Creating an object of a class will
invoke them. Some key features of Java constructors include:
If a class already defines a constructor with arguments, you can no longer use a default no-argument
constructor — you have to write one.
They do not return a value, and you do not have to specify the keyword void.
If you do not create a constructor for the class, Java helps you by using a so-called default no-argument
constructor.
Constructor overloading indicates passing different numbers and types of variables as arguments, all of which are
private variables of the class.
A static variable is associated with a class and not objects of that class.
30. What are Java data types, and how are they grouped?
In Java, a variable must be a specified data type such as an integer, floating-point number, character Boolean, or
string. The two groups of data types are:
Primitive data types, which include byte, short, int, long, float, double, boolean, and char
31. How do you define primitive data types and describe each by size and description?
float is 4 bytes in size. It stores fractional numbers and is sufficient for storing 6 to 7 decimal digits.
double is 8 bytes in size. It stores fractional numbers and is sufficient for storing 15 decimal digits.
Unboxing is the automatic transformation of wrapper types into their primitive equivalent.
Every primitive data type has a class dedicated to it, known as wrapper classes.
We call them wrapper classes because they “wrap” the primitive data type into an object of that class.
Wrapper classes convert the Java primitives into reference types (objects).
34. In Java, what are the differences between methods and constructors?
Methods Constructors
Used to represent the behavior of an object. Used to initialize the state of an object.
You cannot override a private or static method in Java. You cannot override a private method in subclass because
it’s not accessible there.
Method hiding is an alternative to overriding a private or static method, which occurs when you hide the superclass
method. You create a similar method with the same return type and same method arguments in child class. For
example, you can create another private method with the same name in the child class.
Equals() method
o Used for checking the equality of two objects defined by business logic.
o A binary operator provided by Java programming language and used to compare primitives and
objects.
o public boolean equals (object o) is the method provided by the Object class.
o Default uses == operator to compare two objects. For example, you can override a method like
string class. equals() method is used to compare the values of two objects.
38. Can you write multiple catch blocks under a single try block?
Yes, you can have multiple catch blocks under a single try block. Your approach should be from specific to general,
public class Example {
try {
a[10]= 10/0;
catch(ArithmeticException e)
catch(ArrayIndexOutOfBoundsException e)
catch(Exception e)
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. The scope of the variables exists
throughout the class.
41. How do you use final keywords and final variables in Java?
When Java programmers use final keywords with a variable of primitive data types, they cannot change the
variable’s value.
When you use final with non-primitive variables, you cannot change the members of the referred object.
Inheritance in Java is the concept where the properties of one class can be inherited by the other. It helps to reuse
the code and establish a relationship between different classes.
Parent class
Child class
Single inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
In Java, exceptions are objects. When you throw an exception, you throw an object. However, you can’t throw just
any object as an exception — only those objects whose classes descend from throwable. Throwable serves as the
base class for an entire family of classes, declared in java.lang, that your program can instantiate and throw.
46. What are the differences between unchecked exception, checked exception, and errors?
An Unchecked exception inherits from RuntimeException (which extends from exception). The JVM treats
RuntimeException differently as there is no requirement for the application code to deal with them
explicitly.
A checked exception inherits from the exception class. The client code has to handle the checked
exceptions either in a try-catch clause or has to be thrown for the super-class to catch the same. A checked
exception thrown by a lower class (sub-class) enforces a contract on the invoking class (super-class) to
catch or throw it.
Errors (members of the error family) usually appear for more serious problems, such as OutOfMemoryError
(OOM), that may not be so easy to handle.
48. What are the types of loops in Java, and how are they used?
For loops are used in Java to execute statements repeatedly for a given number of times. For loops are
used when the programmer knows the number of times to execute the statements.
The while loop is useful when certain statements need to execute repeatedly until it fulfills a condition. In
while loops, it checks the condition before the execution of statements.
The do while loop is the same as the while loop, except that it checks the condition after the execution of
block of statements. Also, do while loop statements execute at least once.
An infinite loop runs without any condition and runs infinitely. You can break an infinite loop by defining any
breaking logic in the body of the statement blocks.
for (;;)
// Statements to execute
51. What is the difference between the continue and break statement?
Break and continue are two important keywords used in loops. When using a break keyword in a loop, the loop
breaks instantly. The current iteration breaks when using the continue keyword, and the loop continues with the
next iteration.
53. In Java, what are public static void main string args?
Public static void main string args, also known as public static void main(String[] args), means:
Public is an access modifier used to specify who can access this method. Also, this method is accessible by
any class.
Static is a keyword in java that identifies when it is class-based. main() is made static in Java to access it
without creating the instance of a class. If main is not made static, the compiler will throw an error as
main() is called by the JVM before creating any objects. It can only invoke static methods directly via the
class.
Void is the return type of the method that defines the method. That method does not return a value.
Main is the name of the method searched by JVM as a starting point for an application (with a particular
signature only). It is also the method where the main execution occurs.
54. In Java, what’s the purpose of static methods and static variables?
Developers use a static keyword to make a method or variable shared for all objects when there is a requirement to
share a method or a variable between multiple objects of a class. This is used instead of creating separate copies
for each object.
55. How do you use, call, and access a static method in Java?
You must use the static keyword before the method name.
56. How do you use, call, and access a non-static method in Java?
You do not need to use the static keyword before the method name.
Non-static methods can access any static method or static variables without creating an instance of the
class.
57. In Java, what are this() and super(), and where are you required to use them?
In Java, super() and this() are special keywords used to call the constructor. When using this() and super(), they
must be the first line of a block.
Used to:
Used to:
In programs involving more complicated cases, complex scenarios require calling several methods for which
switch solves this problem.
In Java scenarios that yield a high number of iterations, the switch is typically faster than using if….else
statements.
In a switch statement, the default case executes when no other switch condition matches. Because the default case
is optional, you can only declare it after coding all other switch cases.