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

Core Java Interview Questions & Answers

The document provides a comprehensive overview of Java concepts, including classes, objects, variables, methods, constructors, inheritance, encapsulation, abstraction, and exception handling. It also covers advanced topics such as Java 8 features, functional interfaces, lambda expressions, and thread management. Additionally, it includes links to resources like GitHub, WhatsApp, Telegram, and YouTube for further learning and community engagement.

Uploaded by

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

Core Java Interview Questions & Answers

The document provides a comprehensive overview of Java concepts, including classes, objects, variables, methods, constructors, inheritance, encapsulation, abstraction, and exception handling. It also covers advanced topics such as Java 8 features, functional interfaces, lambda expressions, and thread management. Additionally, it includes links to resources like GitHub, WhatsApp, Telegram, and YouTube for further learning and community engagement.

Uploaded by

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

Code With Ashutosh

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

Q. Types of variables in java


1)​ Local variable
W

2)​ Static variable


3)​ Non static variable
4)​ Reference variable
de

1)​ Local variable -


a)​ These variables are created inside a method and should be
co

used only within the created method.


b)​ Without initialization we cannot use local variables.
c)​ Local variable names inside different methods can be the same.

2)​ Static variable -


a)​ These variables are global variables.
b)​ These are created inside the class and outside the method.

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.

3)​ Non static variable


a)​ Non static variables are the object members.
b)​ Non-static variables are created inside the class and outside

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

❖​When we create a var type , we can store any value in it.


❖​Var type allocates the data type dynamically depending upon the
W

value store in that variable.


❖​Var is not a keyword . We can use var as a variable name.
❖​Var is not used as method arguments
de

❖​Var can be used as a local variable . It can not be used as a static or


non-static variable.
co

Q. What is Method in java?


❖​Method is a block of code.
❖​Method helps us to break the code into smaller reusable modules.
❖​When a method is void , it can’t return any value.
❖​A method will execute , when it will be called.
❖​We can pass data known as parameters into a method.

4
https://youtube.com/@codewithashutosh247?si=rXike59WnLhUEUkL

Q. Difference between return and return value ?

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

Q. What is the Constructor ?


de

❖​constructor is a special type of method that is used to initialize the


objects.
co

❖​The constructor is called when an object of a class is created .


❖​It can be used to set initial values for object attributes.
❖​There are differents types of constructor are used in java-
➢​Default constructor
➢​Parameterized constructor
➢​Copy constructor

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.

Q. What is the Constructor overloading?


❖​When we create more than one constructor in the same class

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 this keyword?


ith

❖​this keyword is a special reference variable that automatically gets


created to store an object's address.
❖​We cannot use this keyword inside a static method.
W

❖​this keyword is also used to call a constructor


❖​It should be the first statement inside the constructor
de
co

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

❖​Packages are nothing but folders created in java to store programs in


an organized manner.
❖​There are two types of packages in java
➢​User defined packages
➢​Predefined packages

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

Different package subclass no no yes yes


Different package subclass no no no yes
W
de

Q. What is polymorphism Polymorphism?


❖​It is one of the oops concepts.
❖​Polymorphism allows us to perform a single action in different ways.
co

❖​In java polymorphism is divided into two types.


➢​Method overloading or compile time polymorphism
➢​Method overriding or runtime polymorphism

Q. What is Method overloading ?

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.

Q. What isMethod overriding?


❖​Method overriding allows a subclass to provide a specific

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

❖​To operate on that variable we create publically defined getters and


setters.
W

Q. What is Abstraction ?
de

❖​Hiding the implementation details and showing only the essential


details to the user is called Abstraction.
❖​We can achieve abstraction by using interfaces and abstract classes.
co

❖​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.

Q. What is Final keyword

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. What is Function Interface


hu
As
❖​It was introduced in java 8.
❖​In the functional interface we can write only one incomplete method.
❖​But with the help of the default keyword we can write a complete
ith

method in the functional interface.


❖​Functional interface does not support multiple inheritance.
W

Q. Abstract Keyword
❖​Abstract keyword is used to define incomplete methods in an
de

interface. But it is optional to use in a functional interface.


❖​With the help of abstract keywords we can create abstract classes.
❖​With the help of abstract keywords we can create incomplete
co

methods in abstract class.

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.

Q. What is Java 8 Features

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

Q. What is Lambdas Expression


❖​Lambda expression is a concise way to represent an anonymous
W

function.
❖​It provides an implementation for the functional interface.
de

❖​It reduces the number of lines of code during development.

Q. What is the Default Keyword?


co

❖​Default keyword was introduced in java 8.


❖​Default keyword is used to create complete methods in an interface.

Q. What is Optional Class?


❖​Optional class was introduced in java 8.
❖​It is used to handle the null pointer exception.

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

❖​There are two types exception are there in java


➢​Checked Exception
➢​Unchecked Exception
W
de
co

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

Q. What is an Arithmetic Exception ?


de

❖​It is runtime exception


❖​It occurs when we perform any invalid mathematical operation.
❖​For example when we divide any number by zero then we will get a
co

number format exception.

Q. What is Number Format Exception?


❖​When an invalid string is converted into a number then we will get a
number format exception.

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.

Q. What is Finally Block?

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

elements of the same data type.


W

Q. What is String ?
❖​ In Java, strings are immutable.
❖​Once a String object is created, it cannot be modified.
de

❖​The String class is part of java.lang package and provides


methods to work with string data, such as length(), charAt(),
co

substring(), indexOf(), toUpperCase(), and


toLowerCase().
❖​ Java maintains a pool of strings to save memory.

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

the opportunity to acquire the lock and execute the block.


W

Q. What is a Thread Life Cycle?


de
co

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

❖​The ca set the minimum priority as 1 and maximum as 10 and normal


as 5.
W

Q. What isThread Pool?


de

❖​A thread pool is a collection of pre-initialized threads that are ready to


perform a task.
❖​Instead of creating a new thread for each task, the thread pool reuses
co

existing threads, which can improve the performance and resource


management.

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.

Q. What is the Finalize Method?

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

❖​BufferesWriter has a newLine() methods.


BufferedReader()
❖​BufferedWriter improves file writing performance
W

❖​It has a readLine() method which can read the content line by line.
de

Q. What is throws keyword?


co

❖​It is applied to the method.


❖​If any exception occurs in the method then the exception will be
passed on to the calling statement.

Q. What is the throw keyword?


❖​It helps us to create customized exceptions as per the requirement of
the developer.

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

interfaces, and methods with type parameters.


❖​This enables you to write more flexible and reusable code while
maintaining type safety.
co

Q. What is Serialization and Deserialization ?


❖​ In serialization we convert object state to binary and then store that
into file permanently.
❖​In de-serialization we read binary from the final and form the object
back.

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

❖​It cannot contain duplicate values.

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

❖​It maintains insertion order.


❖​It can contain only unique elements.
W
de

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

You might also like