Core Java Interview Questions & Answers
Core Java Interview Questions & Answers
sh
to
👉
Git & GitHub -
hu
https://github.com/AshutoshG1?tab=repositories
As
👉
Whatsapp Group -
https://chat.whatsapp.com/Iy2Ag6hE35WENehlmNcctV
ith
👉
Telegram Group -
W
https://t.me/+3jqmwqzNp3xlMTZl
de
👉
YouTube
https://youtube.com/@codewithashutosh247?si=rXike59WnLhUEUkL
co
1
sh
to
Interview hu
As
Preparation
ith
W
de
co
2
Core java Interview Question
sh
to
Q. What is Class ?
❖Class helps us to create objects.
hu
❖It creates new when it receives requests from new keywords.
Q. What is an Object ?
As
❖Object is an instance of the class.
❖It is the real world entity that has state and behavior.
ith
3
c) It is not mandatory to initialize the static variable , if we don’t
initialize the static variable then depending upon the data type
default will get initialized.
d) We need not to create an object to access the static variable.
sh
the method.
c) These variables are also called instance variables.
d) It is not mandatory to initialize the non static variable if we don’t
to
initialize the non static variable then depending upon the data
type default value will get initialized.
hu
4) Reference variable
a) Reference variable can store the object address.
b) It can also store null value.
As
Q. What is var type ?
❖Var type is introduced in java version 10.
ith
4
https://youtube.com/@codewithashutosh247?si=rXike59WnLhUEUkL
sh
return return value
to
We can use return keyword inside We can use the “return value”
void method only. keyword only inside, not a void
hu
method & it is mandatory to be
used.
It is optional to use. Mandatory to use
As
It will return control to the method return value keyword will return
calling statement. control and value to method calling
ith
statement.
W
5
Q. What is the Default Constructor?
❖When we do not create a constructor in java then during compilation
automatically noArgs constructor with empty body gets created.
sh
provided they have different numbers of arguments or different types
of arguments.
to
Q. What is the Constructor chaining?
hu
❖When we call one constructor from another constructor . It is called
constructor chaining.
❖To call a constructor this keyword should be used. It should be the
As
first statement inside the constructor.
Q. What is Inheritance?
❖Inheritance is one the oops concept.
❖When we inherit the members of parent class to child class so that
we can reuse these members is called inheritance.
❖Java does not support multiple inheritance in class because it leads
to diamond problems.
6
Q. What are the Packages in java
sh
Q. What are the Access specifier / Access modifiers?
to
hu
Private Default protected Public
Same class yes Yes yes yes
As
Same package subclass no yes yes yes
Same package non-subclass no yes yes yes
ith
7
❖When we create more then one method with the same name in the
same class provided they have different numbers of arguments or
different types of arguments.
❖It is also called as compile time polymorphism because the method to
be called or invoked is determined at the time of compilation.
sh
implementation for a method that is already defined in its superclass.
❖It is also called as runtime polymorphism because the method to be
to
called or invoked is determined at the run time.
Q. What is Encapsulation ?
hu
❖It is used for data hiding or security
❖Wrapping of data with the methods such that the method operate on
As
that date, is called Encapsulation.
❖Here we create private variables so that direct access to that is
avoided.
ith
Q. What is Abstraction ?
de
❖For example - an ATM user can see only the services but he doesn’t
know how it is working internally.
Q. What is an Interface?
❖An interface can consist of only interface incomplete methods in it.
8
❖When a class implements an interface then that class is getting into
contract with the interface to complete all inherited methods.
❖Interfaces support multiple inheritance.
❖By default all the variables in an interface are final and static.
❖We cannot create objects of interface but reference variables of
interface can be created.
sh
❖If we make a variable final then its value cannot be changed.
❖If we make static / non static variables final then initialization is
to
mandatory.
❖If we make a method final then overridden is not allowed.
❖If we make a class final then inheritance is not allowed.
Q. Abstract Keyword
❖Abstract keyword is used to define incomplete methods in an
de
Q. Abstract Class
❖We can create both complete and incomplete methods in it.
❖To create incomplete methods in an abstract class it is mandatory to
use abstract keywords.
9
❖Abstract class does not support multiple inheritance.
❖We can create both static / non-static members in an abstract class.
❖Objects of the abstract class cannot be created , But reference
variables of the abstract class can be created.
sh
1) Functional Interface
2) Lambdas Expression
3) Default keyword
to
4) Optional class
5) Stream api
hu
Q. What is Functional Interface
❖A functional interface should consist of exactly one incomplete
As
method in it.
❖It can also contain any number of complete methods with the help of
default keywords.
ith
function.
❖It provides an implementation for the functional interface.
de
10
Q. What isStream API ?
❖Stream api are used to perform operations on collections of objects.
❖It is present in the java.util package.
❖Streams are iterated internally.
❖Stream provides some methods like map(), filter etc so that we can
sh
perform operations on objects.
to
Q. What is Exception ?
❖When a bad user input is given, the program halts abruptly . That is
hu
called an Exception.
❖We handle exceptions by using try catch blocks.
❖If any exception occurs in the try catch block , then it will create an
As
object of exception and give it to the catch block.
❖Catch blocks suppress that exception and further code will be
executed.
ith
11
Q. What is a Checked Exception ?
❖Checked exceptions also called as compile time exceptions
❖Because it occurs at the time of compilation
❖Types of checked exception
➢Class not found exception
sh
➢File not found exception
➢SQL exception
➢IO exception
to
hu
Q. What is an Unchecked Exception ?
❖Unchecked exceptions also called as run time exceptions
❖Because it occurs at the time of compilation
As
❖Types of checked exception
➢Arithmetic Exception
➢ArrayIndexOutOfBoundException
ith
➢NullPointerException
➢NumberFormatException
W
12
Q. What is Null Pointer Exception ?
❖It is a runtime exception.
❖When we want to access non-static variables using null reference
variables, then we get null pointer exceptions.
sh
❖Finally block is an extension of try catch block.
❖Any code that we write in finally block , it will 100% run.
to
Q. What is Multi catch Block
❖We can write multi catch blocks in java provided all child exception
hu
classes are written first followed by parent Exception class name.
Q. What is an Array ?
As
❖In java , an array is a data structure that allows you to store multiple
values of the same type under a single variable name.
❖Arrays are useful when you need to work with a collection of
ith
Q. What is String ?
❖ In Java, strings are immutable.
❖Once a String object is created, it cannot be modified.
de
13
Q. What is Thread in java?
❖Multitasking done at program level is called Threads.
❖The main purpose of thread is to improve the performance of the
application by reducing the execution time.
❖There are two ways to build the threads
sh
➢By using Thread class
➢By using Runnable interface
to
Q. What is Thread Synchronization ?
❖Whenever two threads perform operation on common data , the data
hu
might be corrupted
❖To resolve this problem we use Thread Synchronization.
❖To make the threads operate one after another we use synchronized
As
keywords. Wherein the thread acquired the lock can only execute the
block. Whereas other threads would be in wait status.
❖Only when the first thread releases the lock the other thread will get
ith
14
sh
to
hu
Q. What is Thread Priority ?
❖It decides which thread is going to run first and which thread is going
As
to run later.
❖If we set the priority then it is a request made to the thread scheduler
where there is no surety that it will be processed or approved.
ith
Q. What is Enum ?
❖Enum is a data type.
❖It is a collection of constants.
15
Q. What is Wrapper class?
❖In the wrapper class, the values are stored in the object.
❖The process of storing the values inside an object is called boxing or
wrapping.
❖Reading the values from the object is called unboxing.
❖The main advantage of wrapper class is easy manipulation of data.
sh
❖Finalize method is present in object class of java.
❖Garbage collection logic is implemented in the finalize method.
to
❖The finalize method is called by the garbage collector on an object
when it determines that there are no more references to the object
hu
then it cleans up those objects.
As
Q. What is File Handling?
❖In java file handling means the process of reading from and writing to
ith
files.
❖Java provides a rich set of classes and methods for file handling
➢File
W
➢FileReader
➢FileWriter
de
➢BufferedWriter
➢FileInputStream
➢FileOutputStream
co
➢BufferedReader
exists()
❖This is a non-static method present in file class.
❖It checks whether the file exists in the given path. If yes if return
boolean value true or if does not exist then it will return false.
delete()
❖This is a non-static method present in file class.
16
❖If the file is deleted then it returns boolean value true or else it will
return false.
createNewFile()
❖This is a non-static method present in file class.
❖If a file is created this method will return true or else false.
length()
❖This is a non-static method present in file class.
❖This method counts the number of characters including white spaces
sh
and returns a long value.
mkdir()
❖This is a non-static method present in file class.
to
❖This method will create a new folder if the folder does not exist . If a
folder is created it will return true or else false.
hu
list()
❖This is a non-static method present in file class.
❖This method will return all the file/ folder names in the given path as
As
String Array.
BufferedWriter()
❖BufferedWriter improves file writing performance.
ith
❖It has a readLine() method which can read the content line by line.
de
17
Q. What is Regular Expression ?
❖A regular expression in java is a sequence of characters that define a
search pattern.
❖It is used for matching , validation , and searching.
❖Regular expressions are implemented using the java.util.regex
package which provides the Pattern and Matcher class for working
with regular expressions.
sh
to
Q. What is Tokenizer ?
❖In Java , the StringTokenizer class is used to break a string into
hu
tokens.
Q. What is Cloning?
As
❖The process of creating the replicas of a particular object by copying
the content of one object completely into another object.
ith
Q. What is hashCode()?
❖It converts hexadecimal to integer format.
W
Q. What is Generics ?
❖Generics in Java are a feature that allows you to define classes,
de
18
https://youtube.com/@codewithashutosh247?si=rXike59WnLhUEUkL
👉 https://chat.whatsapp.com/Iy2Ag6hE35WENehlmNcctV
sh
to
Collection
Collection Framework
hu
As
❖Collection stores a group of objects in it.
❖In java collection is a framework which has already available logic to
deal with different data structures.
ith
W
de
co
ArrayList -
19
❖Internally it is implemented as a dynamic array.
❖Initial size of arrayList is 10.
❖When we exceed the initial size automatically arraylist size increases
by 1.5 times.
❖Array list maintains insertion order.
❖It can consist of duplicate elements.
Advantages of arrayList-
sh
❖Reading data would give us the best performance.
Disadvantages of arrayList-
to
❖Insertion of data in between of the list will result in the worst
performance.
LinkedList
hu
As
❖Linked list is the collection of dynamically allocated nodes.
❖Each node contains one value and one object address.
ith
❖If the object address is null, it is the last node of the list.
❖It has two types - singly linkedList , doubly linkedList.
❖It can consist of duplicate elements.
W
de
Set
❖It is an interface.
❖It does not maintain any insertion order.
co
Hashing
❖Hashing is a technique where we represent any entity in the form of
an integer and it is done by using hashCode() method.
❖The hashCode method is present in the object class of java.
20
Collision
❖When two values are being stored at the same index number is called
collision.
❖To resolve this problem we store the data as a list mapped to the
same index number in the hash table.
sh
HashSet
❖It uses a hashtable internally.
to
❖It uses hashing to inject the data into the database.
❖It contains only unique elements .
hu
❖It does not maintain insertion order.
❖It is not synchronized.
As
LinkedHashSet
ith
TreeSet
❖It contains unique elements only.
❖It sorts the data in ascending order.
co
21
sh
to
👉 https://chat.whatsapp.com/Iy2Ag6hE35WENehlmNcctV
HashMap
hu
As
❖HashMap uses a hashtable internally.
❖To inject the data it uses hashing technique.
❖A hashmap stores the data as (key,value) pairs.
ith
W
HashTable
❖It stores the content as (key, value) pairs.
❖Hash tables are synchronized.
de
co
https://youtube.com/@codewithashutosh247?si=rXike59WnLhUEUkL
22
sh
to
👉 hu
Git & GitHub -
As
https://github.com/AshutoshG1?tab=repositories
👉
Whatsapp Group -
ith
https://chat.whatsapp.com/Iy2Ag6hE35WENehlmNcctV
W
👉
Telegram Group -
https://t.me/+3jqmwqzNp3xlMTZl
de
👉
YouTube
co
https://youtube.com/@codewithashutosh247?si=rXike59WnLhUEUkL
23