0% found this document useful (0 votes)
8 views

Java Interview QUESTIONS

The document discusses 15 Java interview questions covering topics like what Java is, classes and objects, access modifiers, abstract methods, instance variables, static methods, interfaces, inheritance relationships, multithreading, why Java is platform independent, why Java is not a pure OOP language, and the difference between heap and stack memory.

Uploaded by

Vinay Dave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Interview QUESTIONS

The document discusses 15 Java interview questions covering topics like what Java is, classes and objects, access modifiers, abstract methods, instance variables, static methods, interfaces, inheritance relationships, multithreading, why Java is platform independent, why Java is not a pure OOP language, and the difference between heap and stack memory.

Uploaded by

Vinay Dave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

.JAVA INTERVIEW QUESTIONS.

1.What is java and why it is used?


- Java is a widely-used programming language for coding web applica ons. It has been
a popular choice among developers for over two decades, with millions of Java
applica ons in use today. Java is a mul -pla orm, object-oriented, and network-
centric language that can be used as a pla orm in itself.

2. What is class?
- A class in java is a collec on of data members and member func ons. A class is a
user-de ned type that describes what a certain type of object will look like. A Class is
like an object constructor, or a "blueprint" for crea ng objects.

3. What is object?
- A Java object is a member of a Java class. It is also called an instance of a class. Java
objects have three primary characteris cs: iden ty, state, and behavior. These
characteris cs are the building blocks of any class object and set the scene for how
they are used.

4. What are the access modi ers in java and why


they are use?
- Access modi ers are keywords that can be used to control the visibility of elds,
methods, and constructors in a class. The four access modi ers in Java are public,
protected, default, and private.

Four Types of Access Modifiers


Private: We can access the private modi er only within the same class and not from
outside the class.
ti
fi
ti
fi
ti
ti
ti
fi
tf
tf
fi
ti
ti
fi
ti
ti
fi
Default: We can access the default modi er only within the same package and not
from outside the package. And also, if we do not specify any access modi er it will
automa cally consider it as default.
Protected: We can access the protected modi er within the same package and also
from outside the package with the help of the child class. If we do not make the child
class, we cannot access it from outside the package. So inheritance is a must for
accessing it from outside the package.
Public: We can access the public modi er from anywhere. We can access public
modi ers from within the class as well as from outside the class and also within the
package and outside the package.

5. What is abstraction method ?


- A method declared using the abstract keyword within an abstract class and does not
have a de ni on (implementa on) is called an abstract method. When we need just
the method declara on in a super class, it can be achieved by declaring the methods
as abstracts.

6. What is instance varible ?


- An instance variable is a variable which is declared in a class but outside of
constructors, methods, or blocks. Instance variables are created when an object is
instan ated, and are accessible to all the constructors, methods, or blocks in the class.
Access modi ers can be given to the instance variable.

7. What is static method ?


- A sta c method is a method de ned as a member of an object but is accessible
directly from an API object's constructor, rather than from an object instance created
via the constructor.
fi
ti
ti
ti
fi
fi
ti
ti
ti
fi
fi
fi
fi
fi
8. Why static method is used ?
- A sta c method has two main purposes:
For u lity or helper methods that don't require any object state. Since there is no need
to access instance variables, having sta c methods eliminates the need for the caller to
instan ate the object just to call the method.
For the state that is shared by all instances of the class, like a counter. All instance
must share the same state. Methods that merely use that state should be sta c as
well.

9. What is interface ?
- The interface in Java is a mechanism to achieve abstrac on. There can be only
abstract methods in the Java interface, not method body. It is used to achieve
abstrac on, mul ple inheritance and to achieve loose Coupling in Java.
In other words, you can say that interfaces can have abstract methods and variables. It
cannot have a method body.
Java Interface also represents the IS-A rela onship.

10.Explain is-a and has-a relation?


- Is-A rela onship depends on inheritance.It is used for code reusability in Java.When
there is an extends or implement keyword in the class declara on in Java, then the
speci c class is said to be following the Is-A rela onship.

11.What is multitreading in java?


- Mul threading in Java is a process of execu ng two or more threads simultaneously
to maximum u liza on of CPU. Mul threaded applica ons execute two or more
threads run concurrently. Hence, it is also known as Concurrency in Java.
fi
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
12.Why is Java a platform independent
language?
- Java language was developed in such a way that it does not depend on any hardware
or so ware due to the fact that the compiler compiles the code and then converts it to
pla orm-independent byte code which can be run on mul ple systems.
The only condi on to run that byte code is for the machine to have a run me
environment (JRE) installed in it.

13.Why is Java not a pure object oriented language?


- Java supports primi ve data types - byte, boolean, char, short, int, oat, long, and
double and hence it is not a pure object oriented language.

14.Di erence between Heap and Stack Memory


in java.
- Stack memory is the por on of memory that was assigned to every individual
program. And it was xed. On the other hand, Heap memory is the por on that was
not allocated to the java program but it will be available for use by the java program
when it is required, mostly during the run me of the program.

15.Can java be said to be the complete object-


oriented programming language?
- We can say that - Java is not a pure object-oriented programming language, because
it has direct access to primi ve data types. And these primi ve data types don't
directly belong to the Integer classes.
tf
ff
ft
ti
ti
fi
ti
ti
ti
ti
ti
fl
ti
ti
16.What do you mean by data encapsulation?
- Data Encapsula on is an Object-Oriented Programming concept of hiding the data
a ributes and their behaviours in a single unit.
It helps developers to follow modularity while developing so ware by ensuring that
each object is independent of other objects by having its own methods, a ributes, and
func onali es.
It is used for the security of the private proper es of an object and hence serves the
purpose of data hiding.

17.The di erence between equals() method and


equality operator (==) in Java?
- equals() method is used for checking the equality of contents between two objects as
per the speci ed business logic.
== operator is used for comparing addresses (or references), i.e checks if both the
objects are poin ng to the same memory loca on.

18.Can the main method be Overloaded?


- Yes, It is possible to overload the main method. We can create as many overloaded
main methods we want. However, JVM has a prede ned calling method that JVM will
only call the main method with the de ni on of -
public sta c void main(string[] args)

19.Overloading and Overriding in java?


- Overloading is same class same method name di erent paramters.
Overriding is di erent Class same method name and same parameters.
tt
ti
ti
ti
ff
fi
ff
ti
ti
fi
ti
ti
ti
ff
fi
ft
tt
20.Explain the use of nal keyword in variable,
method and class.
- In Java, the nal keyword is used as de ning something as constant / nal and
represents the non-access modi er.
nal variable:
When a variable is declared as nal in Java, the value can’t be modi ed once it has
been assigned.
If any value has not been assigned to that variable, then it can be assigned only by the
constructor of the class.
nal method:
A method declared as nal cannot be overridden by its children's classes.
A constructor cannot be marked as nal because whenever a class is inherited, the
constructors are not inherited.
nal class:
No classes can be inherited from the class declared as nal. But that nal class can
extend other classes for its usage.

21.Explain nal, nally and nalize keywords.


- Final: If any restric on is required for classes, variables, or methods, the nal
keyword comes in handy. Inheritance of a nal class and overriding of a nal method is
restricted by the use of the nal keyword. The variable value becomes xed a er
incorpora ng the nal keyword.
Finally: It is the block used in execp on handling. all the codes wri en inside nally
block gets executed irrespec ve of handling of excep ons.
Finalize: Prior to the garbage collec on of an object, the nalize method is called so
that the clean-up ac vity is implemented.
fi
fi
fi
ti
fi
fi
fi
ti
ti
fi
fi
fi
ti
fi
fi
fi
ti
ti
fi
fi
fi
fi
ti
fi
fi
tt
fi
fi
fi
fi
fi
fi
ft
fi
22.When can you use super keyword?
- Following are the cases when this keyword can be used:
Accessing data members of parent class when the member names of the class and its
child subclasses are same.
To call the default and parameterized constructor of the parent class inside the child
class.
Accessing the parent class methods when the child classes have overridden them.

23.Can the static methods be overloaded?


- Yes! There can be two or more sta c methods in a class with the same name but
di ering input parameters.

24.Why is the main method static in Java?


- The main method is always sta c because sta c members are those methods that
belong to the classes, not to an individual object. So if the main method will not be
sta c then for every object, It is available. And that is not acceptable by JVM. JVM calls
the main method based on the class name itself. Not by crea ng the object.
Because there must be only 1 main method in the java program as the execu on starts
from the main method. So for this reason the main method is sta c.

25.Can the static methods be overridden?


- No! Declara on of sta c methods having the same signature can be done in the
subclass but run me polymorphism can not take place in such cases.
ff
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
26.What is the main objective of garbage
collection?
- The main objec ve of this process is to free up the memory space occupied by the
unnecessary and unreachable objects during the Java program execu on by dele ng
those unreachable objects.

27.What is a ClassLoader?
- Java Classloader is the program that belongs to JRE (Java Run me Environment). The
task of ClassLoader is to load the required classes and interfaces to the JVM when
required.

28.What part of memory - Stack or Heap - is


cleaned in garbage collection process?
- Heap.

29.What is a singleton class in Java?


- Singleton classes are those classes, whose objects are created only once. And with
only that object the class members can be accessed.

How would you di eren ate between a String, StringBu er, and a StringBuilder?
- Storage area: In string, the String pool serves as the storage area. For StringBuilder
and StringBu er, heap memory is the storage area.
Mutability: A String is immutable, whereas both the StringBuilder and StringBu er are
mutable.
E ciency: It is quite slow to work with a String. However, StringBuilder is the fastest in
performing opera ons. The speed of a StringBu er is more than a String and less than
ffi
ff
ti
ti
ff
ti
ff
ff
ti
ti
ff
ti
a StringBuilder. (For example appending a character is fastest in StringBuilder and very
slow in String because a new memory is required for the new String with appended
character.)
Thread-safe: In the case of a threaded environment, StringBuilder and StringBu er are
used whereas a String is not used. However, StringBuilder is suitable for an
environment with a single thread, and a StringBu er is suitable for mul ple threads.

30.What is the di erence between the ‘throw’


and ‘throws’ keyword in java?
- The ‘throw’ keyword is used to manually throw the excep on to the calling method.
And the ‘throws’ keyword is used in the func on de ni on to inform the calling
method that this method throws the excep on. So if you are calling, then you have to
handle the excep on.

31.Is it mandatory for a catch block to be


followed after a try block?
- No, it is not necessary for a catch block to be present a er a try block. - A try block
should be followed either by a catch block or by a nally block. If the excep ons
likelihood is more, then they should be declared using the throws clause of the
method.

32.Oop's concepts
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstrac on
ti
ti
ff
ti
ti
ff
fi
fi
ti
ft
ti
ti
ti
ff
6. Encapsula on

7. Coupling
8. Cohesion
9. Associa on
10.Aggrega on
11.Composi on
ti
ti
ti
ti

You might also like