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

Core Java Interview Question

Uploaded by

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

Core Java Interview Question

Uploaded by

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

CORE JAVA INTERVIEW QUESTION

 What is JVM JRE ,JDK


JDK is a software development kit whereas JRE is a software bundle that
allows Java program to run, whereas JVM is an environment for executingbytecode. The full
form of JDK is Java Development Kit, while the full form of JRE is Java Runtime Environment,
while the full form of JVM is Java Virtual Machine.

 What is ClassNotFoundExcpetion and NoClassDefFoundError


ClassNotFoundException is an exception that occurs when you try to load a class at run
time using Class. ... NoClassDefFoundError is an error that occurs when a particular class is
present at compile time, but was missing at run time.

 Why java is platform independent.


Java is platform-independent because it does not depend on any type of platform.
Hence, Java is platform-independent language. In Java, programs are compiled into byte
code and that byte code is platform-independent.

 OOPS concepts , Which object oriented concept is most important in


java
Encapsulation: binding data member and member function in single unit
called encapsulation
Abstraction: hiding internal information example: using abstract methods
Abstract method: method without body is called abstract method
Abstract Class: a class which is having at least one abstract method
Interface: All method of interface will be abstract
Polymorphism : it's a Greek word , Poly=many morphism : form, In java it is
applying same operation with different kind of data with the help of
method overloading and method overriding
Method overloading: compile time polymorphism
Method overriding : run time polymorpshim
Method overloading: same method signature but different parameter
Method overriding : overriding same method in child class
Inheritance : Inheritance in Java is a mechanism in which one object
acquires all the properties and behaviors of a parent object. It is an
important part of OOPs (Object Oriented programming system)
 Explain System.out.print
https://www.javatpoint.com/system-out-println-in-java

Java System.out.println() is used to print an argument that is passed to it. The


statement can be broken into 3 parts which can be understood separately as:
1. System: It is a final class defined in the java.lang package.
2. out: This is an instance of PrintStream type, which is a public and static
member field of the System class.
3. println(): As all instances of PrintStream class have a public method println(),
hence we can invoke the same on out as well. This is an upgraded version
of print(). It prints any argument passed to it and adds a new line to the
output. We can assume that System.out represents the Standard Output
Stream.

 Can we have multiple main method in one Java class


Yes, you can have as many main methods as you like. You can have main methods with
different signatures from main(String[]) which is called overloading, and the JVM will ignore
those main methods. You can have one public static void main(String[] args) method in
each class.

 Five methods of Object Class.


https://www.tutorialspoint.com/Method-of-Object-class-in-Java

 protected Object clone() - Used to create and return a copy of this object.
 boolean equals(Object obj) - Used to indicate whether some other object is "equal to"
this one.
 protected void finalize() - garbage collector calls this method on an object when it
determines that there are no more references to the object.
 Class<?> getClass() - Used to get the runtime class of this Object.
 int hashCode() - Used to get a hash code value for the object.
 void notify() - Used to wake up a single thread that is waiting on this object's monitor.
 void notifyAll() - Used to wake up all threads that are waiting on this object's monitor.
 String toString() - Used to get a string representation of the object.
 void wait() - marks the current thread to wait until another thread invokes the notify()
method or the notifyAll() method for this object
 Why Strings are immutable and how to make your own immutable
class. Example of existing immutable class in java .

Immutable objects are thread-safe so you will not have any


synchronization issues. Immutable objects are good Map keys and
Set elements, since these typically do not change
once created. Immutability makes it easier to parallelize your
program as there are no conflicts among objects

 What is String pool and what is purpose of it?


String pool is an implementation of the String Interring
Concept. String Interning is a method that stores only a copy of each
distinct string literal. The distinct values are stored in the String
pool. String pool is an example of the Flyweight Design Pattern.

 Class loader
The Java ClassLoader is a part of the Java Runtime
Environment that dynamically loads Java classes into the Java Virtual
Machine. The Java run time system does not need to know about files
and file systems because of classloaders.
Java classes aren’t loaded into memory all at once, but when required by
an application. At this point, the Java ClassLoader is called by
the JRE and these ClassLoaders load classes into memory dynamically.

 CloassNotFOundExceptin and NoClassDefintionfound Error

ClassNotFoundException is thrown when the Java Virtual Machine (JVM)


tries to load a particular class and the specified class cannot be found in
the classpath. The Java ClassNotFoundException is a checked
exception and thus, must be declared in a method or constructor's throws
clause
The NoClassDefFoundError is an error when the required class definition is
not available at runtime.

The NoClassDefFoundError is available in LinkageError. What


is LinkageError? If a class is dependent on another class and we made
changes in that class after compiling the former class, we will get
the LinkageError

 String, Immutable Class


Immutable class means that once an object is created, we cannot
change its content. In Java, all the wrapper classes (like Integer,
Boolean, Byte, Short) and String class is immutable.

 String , String Builder and String Buffer Differeces


The String class is an immutable class
whereas StringBuffer and StringBuilder classes are mutable.
... StringBuffer is synchronized i.e. thread safe. It means two
threads can't call the methods
of StringBuffer simultaneously. StringBuilder is non-synchronized
i.e. not thread safe

 How Many type of Memory Area ?


The memory in the JVM divided into 5 different parts:
Class(Method) Area
Heap
Stack
Program Counter Register
Native Method Stack

 DeepCopy and ShallowCopy


A shallow copy is a copy of the reference pointer to the object,
whereas a deep copy is a copy of the object itself. In Java, objects
are kept in the background, what you normally interact with when
dealing with the objects is the pointers. The variable names point to
the memory space of the object.
Whenever you try to create a copy of an object, in the deep copy all
fields of the original objects are copied exactly, in addition to this, if it
contains any objects as fields then copy of those is also created
(using the clone() method).

 Collection Hashset and hashMap


HashSet is implementation of Set Interface which does not
allow duplicate value. The main thing is, objects that are stored
in HashSet must override equals() for check for equality and
hashCode() methods for no duplicate value are stored in our
set.
HashMap is an implementation of Map Interface, which map a
key to value. Duplicate keys are not allowed in a map.Basically
Map Interface has two implementation classes HashMap
and TreeMap the main difference is TreeMap maintains order
of the objects but HashMap will not.HashMap allows null values
and null keys.
Both HashSet and HashMap are not synchronized.

 Design Pattern and Singelton


Definition:
The singleton pattern is a design pattern that restricts the
instantiation of a class to one object.
Let’s see various design options for implementing such a class.
If you have a good handle on static class variables and access
modifiers this should not be a difficult task.

The singleton pattern is one of the simplest design patterns.


Sometimes we need to have only one instance of our class for
example a single DB connection shared by multiple objects as
creating a separate DB connection for every object may be
costly.
 How to break SIngelton Design pattern.
Reflection: Reflection can be caused to destroy singleton property
of singleton class, as shown in following example: ...
Serialization:- Serialization can also cause breakage
of singleton property of singleton classes. ...
Cloning: Cloning is a concept to create duplicate objects.

 Concurrent modification exception

The ConcurrentModificationException occurs when an object is


tried to be modified concurrently when it is not permissible.
This exception usually comes when one is working
with Java Collection classes. For Example - It is not permissible for a
thread to modify a Collection when some other thread is iterating
over it.

 Static Keyword
The static keyword in Java is used for memory management mainly.
We can apply static keyword with variables, methods, blocks and
nested classes. The static keyword belongs to the class than an
instance of the class. ... Variable (also known as a class variable)

 What is Exception and Error , Checked and Unchecked Exception


Checked Exception:

They are programmatically recoverable problems which are caused


by unexpected conditions outside the control of the code (e.g.
database down, file I/O error, wrong input, etc). Example:
IOException , SQLException ,

An unchecked exception is the one which occurs at the time of


execution. These are also called as Runtime Exceptions. These
include programming bugs, such as logic errors or improper use of an
API. Runtime exceptions are ignored at the time of compilation.
 Exception Hierarchy
The class at the top of the exception class hierarchy is the
Throwable class, which is a direct subclass of the Object class.
Throwable has two direct subclasses - Exception and
Error. Exceptions are further subdivided into checked (compile-time)
and unchecked (run-time) exceptions. ...

 How To create Custom Exception


Create a new class whose name should end with Exception like
ClassNameException. ...
Make the class extends one of the exceptions which are subtypes of
the java. ...
Create a constructor with a String parameter which is the detail
message of the exception.

 Comparable and comparator


Comparable is meant for objects with natural ordering which means
the object itself must know how it is to be ordered. For example Roll
Numbers of students. ... Logically, Comparable interface compares
“this” reference with the object specified and Comparator in
Java compares two different class objects provided.

 Hashset internal working and HashMap internal Working


HashMap contains an array of the nodes, and the node is
represented as a class. It uses an array and LinkedList data
structure internally for storing Key and Value. ... Before
understanding the internal working of HashMap, you must be aware
of hashCode() and equals() method. equals(): It checks the equality
of two objects.

HashSet is created, it internally implements a HashMap. An


element can be inserted into the HashSet using the 'add' function.
This internally calls the 'put' function since a HashMap would have
been internally created. Hence, Set takes in unique values with the
help of HashMap.
 Equals and Hashcode , what is Hashcode collision ?
HashCode collisions
Whenever two different objects have the same hash code, we call
this a collision. A collision is nothing critical, it just means that there is
more than one object in a single bucket, so a HashMap lookup has to
look again to find the right object.

hashCode() does not return the object's reference, but a hash of the
object, computed in some way. equals(obj2) is true then obj1.
hasCode() must be true to be a valid implementation.
Reason: hashCode just returns int value for an Object, even
two different objects can have same hashCode integer.

You might also like