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

Java Interview Questions For 2 Years Experienced

This document contains interview questions and answers related to Java for candidates with 2 years of experience. It includes questions about overriding and overloading methods, static methods, private methods, the Object class, HashMap vs HashSet, serialization, exceptions, ArrayList vs LinkedList, and differences between wait and sleep.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
508 views

Java Interview Questions For 2 Years Experienced

This document contains interview questions and answers related to Java for candidates with 2 years of experience. It includes questions about overriding and overloading methods, static methods, private methods, the Object class, HashMap vs HashSet, serialization, exceptions, ArrayList vs LinkedList, and differences between wait and sleep.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA INTERVIEW QUESTIONS FOR 2 YEARS EXPERIENCED

Can we override static method in java?


No, you cannot override static method in java. You can only hide them.

• Static methods are those which belong to the class.They do not belong
to the object and in overriding, object decides which method is to be
called.

• Method overriding occurs dynamically(run time) that means which


method is to be executed decided at run time according to object used
for calling while static methods are looked up statically(compile time).

Can you overload main method in java?

Yes, we can overload main method in java but when you run your program,
JVM will search for public static void main(String[] args) and execute that
method.

Can we override private methods in java?


You cannot override private methods in java as it is visible to that class only.

What is the base class for all the classes?


java.lang.Object is base class for the objects.
Can you list down some of important method from object
class?

Important methods of object classes are:

• hashcode : It returns hash value of the object


• equals : It compares the object references
• wait : It causes current thread to wait until notify or notifyAll is not called
• notify : Wakes up single thread which is waiting for lock
• notifyAll: Wakes up all threads which is waiting for lock
• toString : Provides String representation of the object
• clone : This method is used to clone the object
• finalize: This method is called when object is being garbage collected.

Which two methods should you override while putting


the custom object as Key in HashMap?
You need to override hashcode and equals method in custom class while
putting objects of custom class in HashMap.

What is the difference between HashMap and HashSet


in java?

HashMap vs HashSet:
Parameter HashMap HashSet

Interface This is core difference HashSet implement Set


among them.HashMap interface
implements Map
interface
Method for storing data It stores data in a form It uses add(value)
of key->value pair.So it method for storing data
uses put(key,value)
method for storing data

Duplicates HashMap allows HashSet does not allow


duplicate value but not duplicate values.
duplicate keys

Performance It is faster than hashset It is slower than


as values are stored HashMap
with unique keys

HashCode Calculation In hash map hashcode In this,hashcode is


value is calculated using calculated on the basis
key object of value
object.Hashcode can be
same for two value
object so we have to
implement equals()
method.If equals()
method return false then
two objects are different.

Can we have abstract class without having any


abstract method in it?
Yes, you can have abstract class without having any abstract method.

Have you heard about transient variable? When will


you use it?
Transient variables are used Serialization. If you don’t want to make variable
serializable, you can make it transient variable.
Can you call start method twice in java?
No, you cannot call Start method twice. If you try to start thread again , it will
throw Illegal Thread State Exception

Do you know how to make a class immutable? Can


you provide steps for it?
• Make your class final

• Declare all instance variable with private and final

• Say no to setter methods

• Initialize all variables in constructor


• Perform cloning of mutable objects while returning from getter method

Can we have static method in the interface?


Yes, we can have static method in the interface from Java 8.

Can you declare constructor final?


No, You can not declare constructor final.

What is the difference between StringBuffer and


StringBuilder?
Parameter StringBuffer StringBuilder

Thread-safe StringBuffer is thread StringBuilder is not


safe. Two threads can thread safe, so two
not call methods of threads can call
StringBuffer methods of StringBuilder
simultaneously. simultaneously.
Performance It is less performance It is more performance
efficient as it is thread- efficient as it is not
safe thread-safe.

What is Java ClassPath?


ClassPath is environment variable which java virtual machine (JVM) uses to
locate all classes which is used by the program.
For example: jre/lib/rt.jar has all java classes and you also need to include jar
files or class file which is being used by program.

You have a list of Custom objects? How can you sort


them?
You need to use Comparable or Comparator interface to sort list of custom
objects.

What is volatile in java?


If you mark any variable volatile then this variable will be read from main
memory rather than CPU cache so each thread will have updated value in the
variable.

What are two different ways to call garbage collector?


System.gc() OR Runtime.getRuntime().gc().

What is marker interface in java? Can you provide


some examples of marker interface?
Marker interfaces are those interfaces which do not have any method in it.
Examples of marker interfaces are : Serializable and Cloneable.

How many objects will be created below:


String str1= new String("John");
String str2= new String("John");

Three objects will be created here, two in heap memory and one in String
constant pool.

Can you differentiate between Checked Exception and


Unchecked exception?
Checked exceptions are those exceptions which are checked at compile. If
you do not handle them , you will get compilation error.

Unchecked exceptions are those exceptions which are not checked at compile
time. Java won’t complain if you do not handle the exception.

What is the difference between ArrayList and


LinkedList? How will you decide which one you need
to use?

ArrayList

• ArrayList is implementation of list interface.


• ArrayList is not synchonized(so not thread safe)
• ArrayList is implemented using array as internal data structure.It can be
dynamically resized .
LinkedList

• LinkedList is implementation of list and deque interface.


• LinkedList is not synchronized
• LinkedList is implemented using doubly linked list as internal data
structure.

ArrayList vs LinkedList:
Parameter ArrayList LinkedList

Internal data structure It uses dynamic array to It uses doubly Linked


store elements internally List to store elements
internally

Manipulation If We need to insert or If We need to insert or


delete element in delete element in
ArrayList, it may take LinkedList, it will take
O(n), as it internally O(1), as it internally
uses array and we may uses doubly LinkedList
have to shift elements in
case of insertion or
deletion

Search Search is faster in Search is slower in


ArrayList as uses array LinkedList as uses
internally which is index doubly Linked List
based. So here time internally So here time
complexity is O(1) complexity is O(n)

Interfaces ArrayList implements LinkedList implements


List interface only, So it List,Deque interfaces, so it can
be used as List,Stack or Queue
can be used as List only
What is difference between wait and sleep in java?

sleep

• It causes current executing thread to sleep for specific amount of time.


• Its accuracy depends on system timers and schedulers.
• It keeps the monitors it has acquired, so if it is called from synchronized
context, no other thread can enter that block or method.
• If we call interrupt() method , it will wake up the sleeping thread.

wait

It causes current thread to wait until either another thread invokes the notify()
method or the notifyAll() method for this object

It must be called from synchronized context i.e. from block or method.It means
before wait() method is called,current thread must have lock on that object.

It releases lock on the object on which it is called and added to wait list, so
another thread can acquire lock on the object.

You might also like