Java Interview Questions
Java Interview Questions
It consists of the JVM + library classes + supporting tools and is part of the
JDK. The programs are run on JRE as it loads the classes, verifies the access
to the memory, and retrieves system resources.
The other languages like C are saved in .exe format and are compiled
directly into machine-specific language according to the OS being used.
This means a code compiled on Microsoft OS cannot run on Linux OS.
Private – Access is within the current class and not outside of it.
Protected – The access within the current package unless a child class
is created outside the package. It can be accessed through a child’s
class.
Now, let’s see the benefit of making String immutable. Strings are
immutable in Java because it uses the concept of string literal or string
constant pool.
If a new String object is created with the value – “ArtOfTesting” then the
same will be placed in a Java heap memory called String pool. Now,
whenever new String literals are created with value – “ArtOfTesting” then
instead of creating multiple objects, each object will point to the same
value (i.e. “ArtOfTesting”) in the String pool.
Its real-life example would be a car. Only the steering wheel and indicators
are visible but the internal working is hidden.
The encapsulation is used to hide data as a single block with all the
variables and methods inside it. The data can be accessed through getter
and setter methods.
Class Overload{
void show(){
System.out.println("sum");
System.out.println("multiply");
t.show();
t.show(10);
Method overriding–
Creating and implementing a method with the same name in a subclass as
the parent class is called method overriding.
Methods’ name should be the same
Arguments passed should be the same(number, type, sequence)
Methods should belong to different classes
Creating IS-A relationship.
Example-
Class Example1{
void show(){
System.out.println("method1");
void show(){
System.out.println("method2");
b.show(); //method2
For example, two classes are declared, Student and Address. A student will
have an address, but it’s not true vice-versa. That means an address does
not need to have a student. This is a HAS-A relationship.
double d = (double)a;
Whereas, late binding also known as dynamic binding is when the type of
the object is identified during run time. Method overriding is a perfect
example of dynamic binding.
Checked exceptions are handled during the compile time. These include
SQL exception, IO exception, etc.
The unchecked exceptions are those which cannot be checked or handled
during compile-time and therefore throw an error during run time. These
include ArrayIndexOutOfBoundsException, NullPointerException, etc.
Throw – It is used by the user to create an exception if the code does not
run in the desired way.
Throws – When we are aware of the checked exceptions and let the caller
program know about those, the throws keyword is used before that
exception.