Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

InterviewQuestions JavaFullStack

Basic questions for java interview.

Uploaded by

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

InterviewQuestions JavaFullStack

Basic questions for java interview.

Uploaded by

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

Contents

1. Java Basics...........................................................................................................................................3
Java Data types....................................................................................................................................4
Static....................................................................................................................................................9
Bit Manipulation................................................................................................................................12
Strings................................................................................................................................................14
Can we use a string in switch case in java?........................................................................................16
Stack , Heap and Metaspace..............................................................................................................20
How GC works in java........................................................................................................................23
Interface And Abstract.......................................................................................................................25
OOPS..................................................................................................................................................27
Overloading and Overriding...............................................................................................................30
What is Association?..............................................................................................................................35
Exception and try-catch.........................................................................................................................37
What does the keyword synchronize mean?.....................................................................................41
What is ReentrantLock? Can we call lock.lock() multiple times?.......................................................41
Deep vs. shallow copy........................................................................................................................41
Design Patterns..................................................................................................................................42
2. Collection Hierarchy..........................................................................................................................43
ArrayList.............................................................................................................................................43
List.....................................................................................................................................................46
Hashtable and HashMap....................................................................................................................48
3. Threads & Executor Service (Thread Pool Framework).....................................................................56
Questions...............................................................................................................................................56
Thread Basics.........................................................................................................................................58
ThreadPools...........................................................................................................................................61
Types of ThreadPool..............................................................................................................................62
Threads: producer and consumer problem?.....................................................................................65
Difference Between Process and Thread in Java...............................................................................65
What is the difference between concurrency and parallelism?.........................................................65
4.................................................................................................................................................................66
5. Java 8.................................................................................................................................................66
Functional Interfaces and Lambdas in Java 8.........................................................................................69
What Is Optional? How Can It Be Used?............................................................................................69
Stream API Java8...............................................................................................................................72
Tell me 5 features introduced in JDK 1.8?.........................................................................................74
Programs to check logics.......................................................................................................................76
6. Others................................................................................................................................................79
Java 19 Features....................................................................................................................................79
JDBC.......................................................................................................................................................81
7. Basic...................................................................................................................................................83
Which architectural designs are mostly used to design applications?...............................................83
Compare fail-fast and fail-safe iterators?..........................................................................................83
1. Java Basics

How much you rate yourself in Java?

Whats the feature that make java write once and use anywhere? (Platform independent)

Can we run java code without main method, justify ?

Difference between JVM and JDK?

What is the difference between JRE, JDK, JVM and JIT


JDK is a software development environment used for making applets and Java applications. The full form of JDK is Java Development Kit. Java
developers can use it on Windows, macOS, Solaris, and Linux. JDK helps them to code and run Java programs. It is possible to install more than
one JDK version on the same computer.

JRE is a piece of a software which is designed to run other software. It contains the class libraries, loader class, and JVM. In simple terms, if you
want to run Java program you need JRE. If you are not a programmer, you don't need to install JDK, but just JRE to run Java programs.
Though, all JDK versions comes bundled with Java Runtime Environment, so you do not need to download and install the JRE separately in your
PC. The full form of JRE is Java Runtime Environment.

JVM is an engine that provides a runtime environment to drive the Java Code or applications. It converts Java bytecode into machine language.
JVM is a part of Java Run Environment (JRE). It cannot be separately downloaded and installed. To install JVM, you need to install JRE. The full
form of JVM is Java Virtual Machine.
Java Data types

2 Types of data types we have, primitive and non primitive.

Why use String instead of char in Java?

What is the difference between double and float variables in Java?


Memory:In java, float takes 4 bytes in memory while Double takes 8 bytes in memory.

Suffix :By default, float numbers are double in java. In order to store them into float variable, you need to explicitly add the
suffix 'f' or 'F'.

Suppose we have a number 1.23456. Let's see what happens if we store it in the float variable num.
float num = 1.23456 // compiler error

There are two options to correct the above compiler error.


First by adding 'f' or 'F' at the end of the number

float num = 1.23456f // or


float num1 = 1.23456F

Float is single precision floating point decimal number while Double is double precision decimal number.

What is Single Precision and Double Precision?

In other words, a float can give you 6-7 digits decimal points precision.
double is a double precision floating point operation. In other words, double can give you 15-16 decimal points precision.
Can I store a double value in a long variable without casting?

How long do primitive variables exist in memory? Define the various scopes of primitive variables?
After a primitive variable is declared and initialized; how long it lives in memory is dependent on the scope of the variable.
Scope of a variable is determined based on where it is declared within a java class. Following are the various scopes of a
variable in a java program based on where they are declared.

 Class variable (Static fields)


Class variables are variables declared within the class body, outside of any methods or blocks, and declared with 'static' keyword.
Class variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains
loaded in JVM.
 Instance variable (Non-static fields)
Instance variable are variables declared within the class body, outside of any method or block, and declared without 'static'
keyword.

 Local variables
Local variables are variables declared within a method body. They live only as long as the method in which it is declared remains on
the stack.
 Block variables

Why is Java not pure Object-oriented?


Java is not considered pure Object-oriented because it supports primitive data-types such as boolean, byte, char, int, float,
double, long, short.

Explain the difference between instance variable and a class variable.


An instance variable is a variable which has one copy per object/instance. That means every object will have one copy of it.

A class variable is a variable which has one copy per class. The class variables will not have a copy in the object.

How does Java programming language pass primitive variables to methods - by value or by
reference?
In Java, primitive variables are passed to methods by value. More specifically, a copy of the primitive value is passed to the method. If the
passed value changes in the method, it does not change the original value.

Access Modifires

Non Access Modifires

Final, static and abtract.


What is implicit and explicit casting

Primitive Casting is used to convert primitive values from one data type to another. For example, an int value can be assigned to
a float data type, or a double value can be assigned to an int data type. Casting can be either implicit or explicit.

Implicit Casting: In implicit casting the conversion happens automatically, without writing specific code to do the conversion.
Implicit casting happens when you convert or assign a smaller value, like a byte, to a larger data type such as an int.

Explicit Casting: In explicit casting code has to be specifically written to perform the conversion from one primitive type to
another. Explicit casting is done by using the syntax (data_type) where data_type is the data type that the cast is being applied to.
Explicit casting happens when you convert or assign a larger value to a smaller data type.

In Java every data type requires some bytes of memory, How many bytes void data type requires?
Ans – 0

What exactly is null in memory? Or What is the null value in Java?

First of all, null is not a valid object instance, so there is no memory allocated for it. It is simply a value that indicates that the object reference is
not currently referring to an object.

What is the difference between initialization and instantiation of an object?


 Initialization is the process of the memory allocation, when a new variable is created. Variables should be explicitly given a
value, otherwise they may contain a random value that remained from the previous variable that was using the same memory
space. To avoid this problem, Java language assigns default values to data types.
 Instantiation is the process of explicitly assigning definitive value to a declared variable.
What is the difference between an Inner Class and a Sub-Class?
Inner Class is a class that is nested within another class whereas sub class is a class that extends or inherit another class.

Inner class can only be accessed using reference of outer class ( Class name , if inner class is static or object reference, if inner class is non
static ) whereas Sub class is accessed directly.

In terms of memory, inner class is stored just like another class having it's own body whereas sub class carries the body of parent class as well
as its own fields in memory.

What are the various access specifiers for Java classes?

What's the default access specifier for variables and methods of a class?

Default access specifier for variables and method is package protected i.e. variables and class is available to any other class but in
the same package, not outside the package.
What are wrapper classes in Java programaming language?
There are many cases where we cannot directly use primitive data types. For example, We cannot put primitives into Java collections since Java
collections (Lists, Sets etc.) can only store objects.

What is ternary operator? Give an example.


Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It's
denoted as?

Static
What happen if we don’t write static method?

What does the static word mean in Java?


A member of class can be accessed without instantiating an object of its class.

Can I declare class as static?


Whether static method can use nonstatic members?
What's the purpose of Static methods and static variables?
Where are static methods or static variables stored in memory?

Static methods and static variables are stored in the meta space in Java. Meta space is a region of memory that is
used to store class metadata, such as the names of classes, fields, and methods, as well as the bytecode for static
methods. Meta space is also used to store interned strings.

Prior to Java 8, static methods and static variables were stored in the permanent generation (PermGen) space.
However, PermGen was known to be difficult to tune, and it could become a bottleneck in applications that used a
lot of static data. In Java 8, PermGen was replaced with meta space, which is a more flexible and scalable memory
region.

Meta space is located in the heap memory, which means that it can grow and shrink as needed. This makes meta
space less likely to become a bottleneck than PermGen was.

It is important to note that static methods and static variables are shared by all instances of a class. This means
that there is only one copy of each static method and static variable in memory, even if there are multiple
instances of the class.

Why static methods cannot access non-static variables or methods?


A static method cannot access non-static variables or methods because static methods can be accessed without instantiating the class, so if the
class is not instantiated the variables are not initialized and thus cannot be accessed from a static method.

Can you override a private or static method in Java?


Is it Possible to override static methods in Java?

-> No, you cannot override a private method.

Overriding is based on dynamic binding at runtime, static members are bound at compile time.
Can we have static methods in an Interface?
Yes, from Java 8, an interface can also be defined.

Static methods can't be overridden in any class while any methods in an interface are by default abstract and are supposed to
be implemented in the classes being implementing the interface. So it makes no sense to have static methods in an interface in
Java.
Bit Manipulation
Logical & Bitwise Operator

The Bitwise Algorithms are used to perform operations at bit-level or to manipulate bits in different ways. The bitwise operations are found to be
much faster and are some times used to improve the efficiency of a program.

For example: To check if a number is even or odd. This can be easily done by using Bitwise-AND(&) operator. If the last bit of the operator is
set than it is ODD otherwise it is EVEN. Therefore, if num & 1 not equals to zero than num is ODD otherwise it is EVEN.

Little and Big Endian Mystery

Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of
the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is
stored first.

Suppose integer is stored as 4 bytes (For those who are using DOS based compilers such as C++ 3.0 , integer is 2 bytes) then a variable x with
value 0x01234567 will be stored as following.

Memory representation of integer ox01234567 inside Big and little endian machines

What are the examples of little, big endian and bi-endian machines ?
Intel based processors are little endians. ARM processors were little endians. Current generation ARM processors are bi-endian.

Motorola 68K processors are big endians. PowerPC (by Motorola) and SPARK (by Sun) processors were big endian. Current version of these
processors are bi-endians.

Describe in how many ways can you create a singleton pattern?


There are two ways of creating a Singleton pattern.

1. Early Instantiation-It is responsible for the creation of instance at load time.

2. Lazy Instantiation-It is responsible for the creation of instance when required

What is a compile time constant in Java? What is the risk of using it?
Ans: public static final variables are also known as a compile time constant, the public is optional there. They are replaced with
actual values at compile time because compiler know their value up-front and also knows that it cannot be changed during run-
time. One of the problem with this is that if you happened to use a public static final variable from some in-house or third party
library and their value changed later than your client will still be using old value even after you deploy a new version of JARs. To
avoid that, make sure you compile your program when you upgrade dependency JAR files.

What is the difference between ++x and x++ under increment operators?
In ++x, the value of x will get incremented before it is used in the expression.

In x++, the previous value is used in the expression first, and after that x is modified.
Strings

Why is a `char[]` Preferred Over a String for Passwords?


The fundamental issue with using a string to store a password is the concept of immutability. A String object is immutable in Java. Due to this
characteristic, if you were to store a password as a string, this plain text will be accessible until Java’s Garbage Collection clears the unused
objects in memory. The immutability of strings means that the String value will not be erased or overwritten on new assignment (rather, a new
string is created). With a char[], because these are primitives types we are acting on, we could overwrite the array with any value, meaning we
can avoid having sensitive data present in memory once we are done processing it.

Additionally, strings in Java are reserved in a special area in heap memory called the String Constant Pool. When a string is declared in your
program, a String object reference is created in stack memory, and additionally, a String object with the string value is created in the heap. The
intention of this is for string reusability, but a consequence is that there is a good chance that the string will remain in memory for a prolonged
period of time. This is an even bigger security threat.

Another aspect to consider is the risk of printing the password to a logging device. Storing a password as a string does create this possibility, as
strings are easily appended to log writers or even the standard system output stream. Conversely, with an array, accidentally printing this to a
log or output stream would print the location of the array in memory.

How many objects will be created in the below statement?

String strObj1 = "Java";


String strObj2 = "Java";
String strObj3 = new String ("Java");

The answer is only 1 object is created. String strObj1 will create a new object in String constant pool whereas strObj2 will create
the reference to the String strObj1.

Is String a data type in java?


String is not a primitive data type in java. When a string is created in java, it's actually an object of Java.Lang.String class that
gets created. After creation of this string object, all built-in methods of String class can be used on the string object.

What is the difference between creating String as new() and literal?


What's the difference between "a == b" and "a.equals(b)"?
Main difference between .equals() method and == operator is that one is method and other is operator.
We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, ==
checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
If a class does not override the equals method, then by default it uses equals(Object o) method of the closest parent class that has overridden
this method. See this for detail
What is an Immutable Object? Can you write an Immutable Class?

An immutable object is an object that will not change its internal state after creation. Immutable objects are very useful in multithreaded
applications because they can be shared between threads without synchronization. To create immutable class in java, you have to do
following steps.

What are the reasons behind making strings immutable in Java?

 String Pool: Designers of Java were aware of the fact that String data type is going to be majorly used by the programmers and
developers. Thus, they wanted optimization from the beginning. They came up with the notion of using the String pool (a storage area
in Java heap) to store the String literals. They intended to decrease the temporary String object with the help of sharing. An immutable
class is needed to facilitate sharing. The sharing of the mutable structures between two unknown parties is not possible. Thus,
immutable Java String helps in executing the concept of String Pool.

 Multithreading: The safety of threads regarding the String objects is an important aspect in Java. No external synchronization is
required if the String objects are immutable. Thus, a cleaner code can be written for sharing the String objects across different threads.
The complex process of concurrency is facilitated by this method.
 Collections: In the case of Hashtables and HashMaps, keys are String objects. If the String objects are not immutable, then it can get
modified during the period when it resides in the HashMaps. Consequently, the retrieval of the desired data is not possible. Such
changing states pose a lot of risks. Therefore, it is quite safe to make the string immutable

How to check if the String is empty?

Java String class has a special method to check if the string is empty or not. isEmpty() method internally checks if
the length of the string is zero. If it is zero, that means the string is empty, and the isEmpty() method will return
true.
If the length of the string is not zero, then the isEmpty() method will return false.

Can you say String is ‘thread-safe’ in Java?


Yes, we can say that the string is thread-safe.
As we know, String is immutable in Java. That means once we created a string, we can not modify it afterword.
Hence there is no issue of multiple threads accessing a string object.

Can we use a string in switch case in java?


Yes, from Java 7 we can use String in switch case.
Below, I have given a program that shows the use of the string in switch case.

String and StringBuffer both represent String objects. Can we compare String and StringBuffer in
Java?
Although String and StringBuffer both represent String objects, we can't compare them with each other and if we try to compare them, we get
an error.

When a lot of changes are required in data, which one should be a preference to be used? String or
StringBuffer?
What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.

How would you differentiate between a String, StringBuffer, and a StringBuilder?

 Storage area: In string, the String pool serves as the storage area. For StringBuilder and StringBuffer, heap memory is the storage
area.
 Mutability: A String is immutable, whereas both the StringBuilder and StringBuffer are mutable.
 Efficiency: It is quite slow to work with a String. However, StringBuilder is the fastest in performing operations. The speed of a
StringBuffer is more than a String and less than 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 StringBuffer are used whereas a String is not used. However,
StringBuilder is suitable for an environment with a single thread, and a StringBuffer is suitable for multiple threads.
Syntax:

// String
String first = "Interview";
String second = new String("Interview");
// StringBuffer
StringBuffer third = new StringBuffer("Interview");
// StringBuilder
StringBuilder fourth = new StringBuilder("Interview");

Question: String concatinate

1. public class TestString1 {


2. public static void main(String[] args) {
3. String str = "420";
4. str += 42;
5. System.out.print(str);
6. }
7. }
What is the output?

A. 42
B. 420
C. 462
D. 42042
E. Compilation fails.

F. An exception is thrown at runtime.

Correct Answer: D
Write a java program to swap two string variables without using temp variable?

39.How does substring () inside String works?

Can we use String in the switch case? (answer)


Beginning with JDK 7, we can use a string literal/constant to control a switch statement, which is not possible in C/C++
Stack , Heap and Metaspace
In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on
Heap. In C++, when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory
to an object, we must use new(). So the object is always allocated memory on heap

Basics

Heap
The heap is the largest and most widely used part of Java’s memory model. It is responsible for storing objects created during
the execution of a Java application. When you create an object using the “new” keyword, it gets allocated on the heap

public class HelloWorld {


public static void main(String[] args) {
String greeting = new String("Hello, World!");
System.out.println(greeting);
}
}

In the example above, the “greeting” object is allocated on the heap. The heap can be further divided into two main areas: the
young generation and the old generation. The young generation is where new objects are initially created, while the old
generation contains objects that have survived multiple garbage collection cycles.

Stack
The stack is a memory area used to store local variables and method call information. Each thread in a Java application has its
own stack, which grows and shrinks as methods are called and returned. Stack memory is used to store primitive data types
(int, float, etc.) and references to objects.

public class StackExample {


public static void main(String[] args) {
int a = 10;
int b = 20;
int result = sum(a, b);
System.out.println("Sum: " + result);
}

public static int sum(int x, int y) {


int total = x + y;
return total;
}
}

In the example above, the main method calls the sum method, passing two integers as arguments. When the sum method is
called, a new stack frame is created, which stores the local variables (x, y, and total).

Metaspace

Metaspace, formerly known as PermGen (Permanent Generation), is a non-heap memory area that stores class metadata, constant
pool information, and method bytecode. It was introduced in Java 8 as a replacement for PermGen, which was removed due to
memory management issues.

Unlike the heap and stack, metaspace does not have a fixed size and can grow dynamically. However, it is still essential to
monitor its usage to avoid memory leaks and potential OutOfMemoryError exceptions.
Object space in Heap -- how much space each object consumes in the Java heap.
How objects are stored in Java?
In java, each object when created gets a memory space from a heap. When an object is destroyed by a garbage collector, the
space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.

How can we find the actual size of an object on the heap?


In java, there is no way to find out the exact size of an object on the heap.

Shallow, Retained, and Deep Object Sizes


To analyze the object sizes, we can use three different metrics: Shallow, retained, and deep sizes.

When computing the shallow size of an object, we only consider the object itself. That is, if the object has references to other
objects, we only consider the reference size to the target objects, not their actual object size. For instance:

As shown above, the shallow size of the Triple instance is only a sum of three references. We exclude the actual size of the
referred objects, namely A1, B1, and C1, from this size.

On the contrary, the deep size of an object includes the size of all referred objects, in addition to the shallow size:

Here the deep size of the Triple instance contains three references plus the actual size of A1, B1, and C1. Therefore, deep sizes
are recursive in nature.
When the GC reclaims the memory occupied by an object, it frees a specific amount of memory. That amount is the retained
size of that object:

The retained size of the Triple instance only includes A1 and C1 in addition to the Triple instance itself. On the other hand, this
retained size doesn’t include the B1, since the Pair instance also has a reference to B1.

Sometimes these extra references are indirectly made by the JVM itself. Therefore, calculating the retained size can be a
complicated task.

To better understand the retained size, we should think in terms of the garbage collection. Collecting the Triple instance
makes the A1 and C1 unreachable, but the B1 is still reachable through another object. Depending on the situation, the retained
size can be anywhere between the shallow and deep size.

What part of memory - Stack or Heap - is cleaned in garbage collection process?


Heap

Which of the following classes will have more memory allocated?


Class A: Three methods, four variables, no object
Class B: Five methods, three variables, no object
Memory isn't allocated before creation of objects. Since for both classes, there are no objects created so no memory is
allocated on heap for any class.

There are two classes named classA and classB. Both classes are in the same package. Can a private
member of classA can be accessed by an object of classB?

Private members of a class aren't accessible outside the scope of that class and any other class even in the same package can't
access them.
How GC works in java

To understand more about GC let us talk about Java memory management.

1. Java Heap: It is used for dynamic memory allocation. It stores objects and other data structures created during program execution.
2. Stack: It is used to store local variables and method call frames. Each thread in Java has its own stack which is created when the
thread starts. All local variables inside that thread are stored in the stack. For objects created inside the stack, the actual object will be
in the heap and the local variable inside stack will store a reference for it.

There are other areas like Metaspace, registers etc which are not very relevant here.

When an object is created using new or any other object instantiation methods, memory for that object is allocated inside the heap. As the
application progresses, some of these objects become unreachable when they are not referenced anymore. If GC is not running, the entire
heap is quickly filled up and the application crashes as memory is not available.

How GC identifies dead(unreachable) objects

There is a misconception that GC tracks all dead objects, which is not true. An easier and right way is to track all the live objects ( which are
needed for program execution) and clean up everything else. Now how do we identify all the live objects?

GC roots

Consider all the objects created inside the program as a tree. If we have access to all the roots of such trees, we can easily track all the live
objects( which are reachable). In Java , the following are considered as valid GC roots.

1. Local variables: This is available via thread stack and considered live as long as the method is active
2. Active Java threads.
3. Static variable: They belong to classes and are shared across all instances. They remain GC roots as long as the class is loaded.
4. JNI references: They are created as part of a JNI call. Objects thus created are managed specially as it is not possible to know
whether it is referred by the native code or not.

How cleanup happens?

The GC is done in two steps

Marking: JVM runs the GC thread intermittently which traverses all the object trees starting from the GC roots. It marks all the objects reachable
as live.

Sweep: All the remaining heap space are marked(swept) as free for new allocations.

As you can see, to perform the marking phase, it is essential that we stop all the live application threads. Else, the object trees cannot be marked
safely. And the pause time depends on how much memory is live(not dead!!). The more live objects, the longer it takes for GC to traverse and
analyse them. And this brings us to the fundamental issue with GC — its impact on application performance.

Another issue associated with allocation/deallocation is memory fragmentation. It occurs when free memory is fragmented into smaller , non-
contiguous chunks making it impossible to allocate large objects even if memory is available. This quickly becomes a bottleneck if memory is not
compacted after each cleanup.

GC algorithms

Multiple GC algorithms and techniques are developed to reduce this performance issue and fragmentation. Let us discuss some of these.
Serial Collector

It is the simplest collector which uses a single thread to perform mark-sweep algorithm. This is a stop-the-world-approach where all the
application threads are paused while a single GC thread is running. This is suited for low concurrency applications with smaller memory
footprints.

Parallel Collector

The only difference from a serial collector is that it uses multiple GC threads. It is more suitable when multiple CPUs are available.

Concurrent Collector

The concurrent collector performs most of the activities concurrently while application threads are running. This helps with application
throughput and reduces the stop-the-world event duration, thus making the application more responsive.

Generational GC

As mentioned earlier, the GC performance depends on how many live objects are available in the heap. So in order to improve GC performance,
we need to make sure that live objects are as less as possible. This is the main reason behind dividing the heap into multiple generations. The
division allows different GC algorithms to be applied selectively based on the characteristics of objects residing there.

Young and old generation

As you know, newly created objects are more likely to be short-lived, while objects that have been around for a while are more
likely to remain live for long. Objects are usually created in young area. As most of the objects are short-lived, the GC runs
quickly and cleans up most of them. If some objects survive a few GC cycles inside the young area, they are moved to the
old area. As long as the old area is not growing big, we can avoid running GC in old area altogether.

In order to optimise this further , some JVMs divide young gen into multiple regions: Eden space and Survivor spaces(usually
two). New objects are usually created in Eden space. During GC, surviving objects from Eden space and one of the Survivor
space are copied into another survivor space, thus keeping Eden and one survivor space always free. This helps with
fragmentation issues as we are always keeping Eden and one survivor space completely empty.

1. Collecting young gen is called minor GCs. Although they have a stop-the-world stage, it is very quick resulting in
shorter pause times.
2. The old gen contains long-lived objects and hence Major GCs are very less.
3. Different GC algorithms can be applied to each gen based on size and application requirements.

It is very important that GC should be fine-tuned based on the application requirement. For example, if the young gen is too
small, it will result in a lot of objects moving to the old generation. If the young gen is too large, minor GC cycles will take
longer time to complete. This will negatively impact application response time.

One of the recent algorithms available from Java 9 is G1 GC. It provided more predictable pause time and better scalability for
applications with large heaps.

How can you minimize the need of garbage collection and make the memory use more effective?
Ans: Use object pooling and weak object references.
Interface And Abstract
Can we declare a class as Abstract without having any abstract method?

What is an abstract class? How is it different from an interface? Why would you use it?

What's the benefit of using inheritance?


Key benefit of using inheritance is reusability of code as inheritance enables sub-classes to reuse the code of its super class.
Polymorphism (Extensibility) is another great benefit which allow new functionality to be introduced without effecting existing
derived classes.
In a class implementing an interface, can we change the value of any variable defined in the
interface?
No, we can't change the value of any variable of an interface in the implementing class as all variables defined in the interface
are by default public, static and Final and final variables are like constants which can't be changed later.
OOPS

Explain Basic OOP concepts with real-world examples.


There are four basic oop concepts. They are Inheritance, Abstraction, Encapsulation and
Polymorphism.
Inheritance means inheriting parent class methods and fields in child class. Let’s consider Dog and
Animal Class. The dog is a child class of Animal class. So for example Dog inherits Animal’s eat() method.
Abstraction means hiding the implementation details. We only say what is done but hide how it is done.
For example: when we use an ATM we simply click cash withdrawal but we don’t know how exactly that
happens. It is Abstraction.
Encapsulation means data hiding. Hiding data related to a class internally. Encapsulation is achieved by
setting private attributes and providing setters and getters to access those variables. We can think of a
medical capsule as it hides internals using a wrapper. So we don’t know the ingredients of it.
Polymorphism means an object taking multiple forms. In Java, every object is polymorphic as each
object is a child of the JAVA object class. For example, Dog is a Dog as well as it is an Animal. It is
polymorphism. Parent class reference is used for child class object creation. That is also polymorphism.

Why do we need to use oop?

OOP concept is one of the oldest concepts in programming. Java and Python are object-oriented
languages. When we use oop there are many advantages. we can avoid code redundancy, manage
changes, manage complexity and code reuse support.

What are the benefits of encapsulation in programming?

Changing the class internals does not affect any code outside of the class.
Managing change is easier. Changing implementation doesn’t reflect on the clients which use them.

Which of the oops feature provides code reusablity?


Inheritance.
Polymorphism

Difference between abstraction and encapsulation?

In abstraction, implementation complexities are hidden using abstract classes and interfaces. While in encapsulation, the data is
hidden using methods of getters and setters.
Abstraction is the method of hiding unwanted implementation information. Whereas encapsulation is a method to hide the
data in a single entity or unit along with a method to protect information from outside.
What are the functional interface and marker interfaces?

An interface with only one abstract method is known as a functional interface. They can only
demonstrate one functionality at a time. A functional interface can have as many default methods as it
wants. Runnable and Comparable are examples of functional interfaces.
It’s a blank interface (no field or methods). Serializable and Cloneable are examples of marker
interfaces. All of these interfaces are completely empty. It is mainly used to say the type of an object to
the java compiler.
Overloading and Overriding

Composition over inheritance?


Composition is typically "has a" or "uses a" relationship. In the below example, the Employee class has a Person. It does not
inherit from Person but instead gets the Person object passed to it, which is why it is a "has a" Person.
Rules of Polymorphism:
Reference variable -> Own class Object

Reference variable -> Child class Object //Up casting

Reference variable CANNOT point to Parent class Object

Overloading and Overriding


Implementing OO relationships

 “is a” relationship is implemented by inheritance (extends keyword)


 “has a” relationship is implemented by providing the class with member variables. (Association)

Polymorphism in Java can be performed by two different methods i.e Method Overloading and Overriding

 Overloading is an example of compile time polymorphism. (operational / parametric)


 Overriding is an example of runtime polymorphism (inclusive)
 A method can have the same name as another method in the same class, provided it forms either a valid
overload or override
What is Method Overriding in Java?
Method overriding is defined as a process when the subclass or a child class has the same method as declared in the parent
class.

What is Compile-Time Polymorphism in Java?


Compile-Time polymorphism in java is also known as Static Polymorphism. In this process, the call to the method is resolved
at compile-time. Compile-Time polymorphism is achieved through Method Overloading. This type of polymorphism can also be
achieved through Operator Overloading. However, Java does not support Operator Overloading.

Method Overloading is when a class has multiple methods with the same name, but the number, types and order of parameters
and the return type of the methods are different. Java allows the user freedom to use the same name for various functions as
long as it can distinguish between them by the type and number of parameters.

Example of Compile- Time Polymorphism in java


We will do addition in java and understand the concept of compile-time polymorphism using subtract()
What is Runtime Polymorphism in Java?

Runtime polymorphism in java is also known as Dynamic Binding or Dynamic Method Dispatch. In this process, the call to
an overridden method is resolved dynamically at runtime rather than at compile-time. Runtime polymorphism is achieved
through Method Overriding.

Method Overriding is done when a child or a subclass has a method with the same name, parameters and return type as the parent
or the superclass, then that function overrides the function in the superclass. In simpler terms, if the subclass provides its
definition to a method already present in the superclass, then that function in the base class is said to be overridden.

It should be noted that runtime polymorphism can only be achieved through functions and not data members.

Overriding is done by using a reference variable of the superclass. Which method to be called is determined based on the object
which is being referred to by the reference variable. This is also known as Upcasting.

Upcasting is done when the Parent class’s reference variable refers to the object of the child class. For example:

1class A{}
2class B extends A{}
3A a=new B(); //upcasting
Examples of Runtime Polymorphism in Java

Difference between Encapsulation & Abstraction?


 Abstraction focuses on the outside view of an object (i.e. the interface)
 Encapsulation (information hiding) prevents clients from seeing it’s inside view.
 Abstraction solves the problem in the design side while Encapsulation is the Implementation.

Why would you not call abstract method in constructor?


The problem is that the class is not yet fully initialized, and when the method is called in a subclass, it may cause trouble.
What is Association?
Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many,
many-to-one, many-to-many. Composition and Aggregation are the two forms of association.

Aggregation: One Object can exist without another object. (Lose-coupling)

Composition: One Object is a part of object(Car-Engine) (Tight Coupling)

What are strong, soft and weak references in Java?

 Strong Reference: is the default Reference Object of a type/class


 Weak Reference: When an object in memory is reachable only by Weak Reference Objects, it becomes automatically
eligible for GC.
 Soft Reference: Object is basically a weak Reference Object that remains in memory a bit more: normally, it resists GC
cycle until memory is available and there is no risk of OutOfMemoryError
Question:

1 class Money {
2. private String country = "India";
3. public String getC() { return country; }
4. }
5. class Yen extends Money {
6. public String getC() { return super.country; }
7. }
8. public class Euro extends Money {
9. public String getC(int x) { return super.getC(); }
10. public static void main(String[] args) {
11. System.out.print(new Yen().getC() + " " + new Euro().getC());
12. }
13. }
What is the result?

A. India
B. null India
C. India null
D. India India
E. Compilation fails due to an error on line 6.
F. Compilation fails due to an error on line 9.
Correct Answer: E

Section: All
Explanation
Explanation/Reference:
The field Money.country is not visible
Exception and try-catch
What’s the difference between error and exceptions?
“Throwable” act as the root for Java’s error and exception hierarchy. “Error” is a critical condition that cannot be handled by the code of the
program. “Exception” is the exceptional situation that can be handled by the code of the program. Error is caused due to lack of system
resources, and an exception is caused because of your code.

Example: Error- OutOfMemory, StackOverFlow.

Exception- Checked Exceptions : NoSuchMethod, ClassNotFound. Unchecked Exceptions : NullPointer, IndexOutOfBounds.

What are checked and unchecked exceptions in java?


Checked exceptions are the exceptions which are known to compiler. These exceptions are checked at compile time only. Hence the name
checked exceptions. These exceptions are also called compile time exceptions. Because, these exceptions will be known during compile time.
For example IOException and FileNotFoundException etc

Unchecked exceptions are those exceptions which are not at all known to compiler. These exceptions occur only at run time. These
exceptions are also called as run time exceptions. All sub classes of java.lang.RunTimeException and java.lang.Error are unchecked
exceptions. For example NullPointerException, ArithmeticException etc.
Explain the user defined Exceptions?

Can we write only try block without catch and finally blocks?
Compile time error or runtime error?

Ans: No, it shows compilation error. The try block must be followed by either catch or finally block. You
can remove either catch block or finally block but not both.

Difference Between throw and throws in Java


The throw and throws are the keywords used in exception handling. The throw keyword is used to handover the instance of the exception
created by the programmer to the JVM manually. The throws keyword used to handover the responsibility of handling the exception occurred
in the method to the caller method.

What do you understand by unreachable catch block error?


What is Multi Catch block?

What happens if I write return in try block? Will finally executes? What happens if write
system.exit(), will finally block executes?
Difference between final, finalize and finally?
Which is better Null Check or try-catch
Exceptions do take extra memory, as well as time, to catch. It is ALWAYS better to test for null if it’s a possible value. Another
thing to consider it that it’s simply less code and more readable to do the null test. Usually having try/catch blocks adds
essentially no overhead to your code for the normal case, but when the exception is triggered it’s quite expensive.
Exceptions are, as their names implies, to be used only for exceptional conditions, they should never be used for ordinary control
flow.
What does the keyword synchronize mean?
 When you have two threads that are reading and writing to the same 'resource', say a variable named 'test', you need to ensure
that these threads access the variable in an atomic way. Without the synchronized keyword, your thread 1 may not see the change
thread 2 made to test.
 synchronized blocks the next thread's call to method as long as the previous thread's execution is not finished. Threads can
access this method one at a time.

What is ReentrantLock? Can we call lock.lock() multiple times?


ReentrantLock allows threads to enter into the lock on a resource more than once. When the thread first enters into the lock, a
hold count is set to one. Before unlocking the thread can re-enter into lock again and every time hold count is incremented by
one. For every unlocks request, hold count is decremented by one and when hold count is 0, the resource is unlocked.

Deep vs. shallow copy.


A shallow copy of an object copies the ‘main’ object, but doesn’t copy the inner objects. The ‘inner objects’ are shared between
the original object and its copy.

Unlike the shallow copy, a deep copy is a fully independent copy of an object. If we copied our Person object, we would copy
the entire object structure.
Design Patterns
Dependency Injection

Implementing dependency injection provides you with the following advantages:

 Reusability of code
 Ease of refactoring
 Ease of testing
2. Collection Hierarchy

What is framework in Java?


A framework is a popular and readymade architecture that contains a set of classes and interfaces.

What are the benefits of the Collection Framework in Java?

The benefits of Collection Framework in Java are:

 Java collection framework offers highly efficient and effective data structures that enhance the accuracy and speed of the program.
 The program developed with the Java collection framework is easy to maintain.
 A developer can mix classes with other types that result in increasing the reusability of code.
 The Java collection framework enables programmers to modify the primitive collection types the way they like.

Explain the basic interfaces of the Java collections framework


Java collection framework is a root of the collection hierarchy. It represents a group of objects as its elements. The Java
programming language does not provide a direct implementation of such interface.
Set: Set is a collection having no duplicate elements. It uses hashtable for storing elements.
List: List is an ordered collection that can contain duplicate elements. It enables developers to access any elements from its
inbox. The list is like an array having a dynamic length.
MAP: It is an object which maps keys to values. It cannot contain duplicate keys. Each key can be mapped to at least one value.
ArrayList

Difference between Array and ArrayList in Java with Example

What are 2 Ways to Remove Elements/Objects From ArrayList in Java


There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using
Iterator.
Another gotcha can have occurred due to autoboxing. If you look closely that two remove methods, remove(int index)
and remove(Object obj) are indistinguishable if you are trying to remove from an ArrayList of Integers.

Suppose you have three objects in ArrayList i.e. [1,2,3] and you want to remove the second object, which is 2. You may call
remove(2), which is actually a call to remove(Object) if consider autoboxing, but will be interpreted as a call to remove
3rd element, by interpreting as remove(index).

You can ConcurrentModificationException, due to call to remove() method from ArrayList. This is easy in
simple examples like this, but in a real project, it can be really tough. Now, to fix this exception, just replace the call of
numbers.remove() to itr.remove(), this will remove the current object you are Iterating
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(100);
numbers.add(200);
numbers.add(300);
numbers.add(400);

for (Integer num: numbers


){
if(num == 2){
numbers.remove(num);
}
}

Iterator<Integer> itr = numbers.iterator();


while (itr.hasNext()){
Integer num = itr.next();
if(num == 200){
numbers.remove(num);
}
}

The difference between List, Set, Map, and Queue in Java?

Explain various types of queues in Java


There are three types of queues in Java:
 Priority queue: It is a special type of Queue wherein elements are sorted as per their natural ordering or custom
comparator.
 Circular Queue: It is a type of Queue in which user operations are performed based on the FIFO method. The last
element is connected to the first position in order to make a circle.
 Double-ended Queue: A double-ended queue is an abstract data type that generalizes a queue. The elements in this
queue can be added or removed from either head or tail.
Explain deque Interface
Java.util.Deque is Java, an interface that extends Queue interface. It gives support for the insertion and deletion of elements at
both the end. This Queue is also called a double-ended queue.

Difference Between poll() and remove() method of Queue Interface in Java


The poll() method returns an object of type Element, which represents a queue element. The remove() method throws an exception if the
queue is empty, and it returns nothing otherwise.
The remove() method can throw an InterruptedException if it is interrupted while waiting for a lock on this queue’s monitor or performing
some other blocking operation (such as dequeuing or popping elements).
Feature poll() Method remove() Method
The next element in the queue, or null if the queue is The next element in the queue, or throws an exception if the queue is
Returns
empty. empty.
Blocks Blocks until an element becomes available for removal. Does not block.
Time Complexity O(1) O(n), where n is the number of elements in the queue.
Exceptions Throws InterruptedException and ExecutionException Throws NoSuchElementException

Explain the difference between Queue and Deque.


Queue Deque
It is called a single-ended Queue It is called a double-ended Queue
Elements in the Queue are added from either end can be added
Elements in the Queue are added or removed from one end
and removed from the both end
It is less versatile. It is more versatile.

What is the peek() of the Queue interface?


Peek () is a method of queue interface. It retrieves all the elements but does not remove the queue
head. In case if the Queue is empty, then this method will return null.

List

Is there a way to increase the size of an array after its declaration?


Arrays are static and once we have specified its size, we can't change it. If we want to use such collections where we may require a change of
size (no of items), we should prefer vector over array.

Why do we need to use iterator on ArrayList in Java?


As you have stated iterator is used when you want to remove stuff whilst you iterate over the array contents. If you don't use an iterator but
simply have a for loop and inside it use the remove method you will get exceptions because the contents of the array changes while you iterate
through. e.g: you might think array size is 10 at the start of the for loop but it wont be the case once you remove stuff.. so when u reach the last
loops probably there will be IndexOutofBoundsException etc.

What are the differences between a LinkedList vs ArrayList?

 two different implementations of the List interface


 LinkedList implements it with a doubly-linked list.
 ArrayList implements it with a dynamically re-sizing array.
 LinkedList is better for working with stacks mostly, or when working with buffers.
 ArrayList is best for working with indexes.

What is default size of ArrayList and HashMap in Java? (answer)


As of Java 7 now, default size of ArrayList is 10 and default capacity of HashMap is 16, it must be power of 2. Here is code snippet from ArrayList
and HashMap class :

Mention the implementing List and Set interface


Class implementing List interface: 1) ArrayList, 2) Vector, and 3) LinkedList.
Class implementing Set interface: 1) HashSet, and 2) TreeSet.

Differentiate between ArrayList and LinkedList


The difference between ArrayList and LinkedList is:
ArrayList LinkedList
It uses a dynamic array. It uses a doubly-linked list.
ArrayList is not preferable for manipulation. LinkedList is preferable for manipulation.
ArrayList provides random access. LinkedList does not provide random access.
LinkedList stores object as well as address object; hence, it takes more
ArrayList s stores only objects hence it takes less overhead of memory
overhead of memory.
Hashtable and HashMap
What is the difference between Hashtable and HashMap? (answer)
There are many differences between these two classes, some of them are following:
a) Hashtable is a legacy class and present from JDK 1, HashMap was added later.
b) Hashtable is synchronized and slower but HashMap is not synchronized and faster.
c) Hashtable doesn't allow null keys but HashMap allows one null key.
HashMap HashTable
HashMap is not synchronized thereby making it better for non- HashTable is synchronized and hence it is suitable for
threaded applications. threaded applications.
Allows only one null key but any number of null in the values. This does not allow null in both keys or values.
Supports order of insertion by making use of its subclass Order of insertion is not guaranteed in HashTable.
LinkedHashMap.

Hashmap Hashtable
It is not synchronized. It is synchronized.
HashMap allows one key as a null value. HashTable does not allow null values.
Either Iterator or Enumerator is used for traversing a
Iterator is used to traverse HashMap.
HashTable.
It can be used for both HashTable, HashMap and is fail-fast. It can be used with HashTable and is fail-safe.
HashMap perform faster than the HashTable. Hashtable is not much faster as compared to HashMap.
What difference between Hashmap vs concurrent hashmap and the follow-up question on how they
work internally etc?

Internal Working of HashMap?


Few important points about HashMap :

HashMap uses its static inner class Node<K,V> for storing the entries in the map.

HashMap allows at most one null key and multiple null values.

The HashMap class does not preserve the order of insertion of entries into the map.

HashMap has multiple buckets or bins which contain a head reference to a singly linked list. That means there would be as many linked lists
as there are buckets. Initially, it has a bucket size of 16 which grows to 32 when the number of entries on the map crosses the 75%. (That means
after inserting in 12 buckets bucket size becomes 32)

HashMap is almost similar to Hashtable except that it’s unsynchronized and allows at max one null key and multiple null values.

HashMap uses hashCode() and equals() methods on keys for the get and put operations. So HashMap key objects should provide a good
implementation of these methods.

That’s why the Wrapper classes like Integer and String classes are a good choice for keys for HashMap as they are immutable and their object
state won’t change over the course of the execution of the program.

Note: Java HashMap is not thread-safe and hence it should not be used in multithreaded application. For the multi-threaded application, we
should use ConcurrentHashMap class.

What is Hashing?
It is the process of converting an object into an integer value. The integer value helps in indexing and faster searches. Same input have same
output value.

What is HashMap

HashMap is a part of the Java collection framework. It uses a technique called Hashing. It implements the map interface. It stores the data in the
pair of Key and Value. 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. There are four fields in HashMap.
Before understanding the internal working of HashMap, you must be aware of hashCode() and equals() method.

 equals(): It checks the equality of two objects. It compares the Key, whether they are equal or not. It is a method of
the Object class. It can be overridden. If you override the equals() method, then it is mandatory to override the
hashCode() method.
 hashCode(): This is the method of the object class. It returns the memory reference of the object in integer form. The
value received from the method is used as the bucket number. The bucket number is the address of the element inside
the map. Hash code of null Key is 0.
 Buckets: Array of the node is called buckets. Each node has a data structure like a LinkedList. More than one node can
share the same bucket. It may be different in capacity.

Insert Key, Value pair in HashMap

We use put() method to insert the Key and Value pair in the HashMap. The default size of HashMap is 16 (0 to 15).

Explain map. entry In Map


Map.entry is a Java interface of java.util. It has a nested interface in Map. This interface must be qualified by the name of class
or interface, which it is a member. Therefore, it is qualified as a Map. Entry. It represents a key and value pair that can forms
element of a Map.
This method returns a view of the collection. For example, consider cityMap as a map. The developer can use entrySet() to get
the set view of map having an element Map.Entry. Programmer can also use getKey() and getValue() of the Map.Entry to get
the pair of key and value of the map.
How to iterate map?
Iterate a hashmap using Java 8, in how many ways we can do it

The developer cannot directly iterate map, but, this interface has two methods that gives view set of map. These methods are:
 Set<Map.Entry<K, V>>entrySet(): It is a method that returns a set having the entries mention in the map. These
entries are generally objected, which has type Map. Entry.
 Set<K>keySet(): This Java method returns a set that having the map key.

1. Using forEach and a lambda expression:

Map<KeyType, ValueType> map = new HashMap<>();


// Add some key-value pairs to the map
map.forEach((key, value) -> {
// Perform operations on each key-value pair
System.out.println("Key: " + key + ", Value: " + value);
});

2. Using the entrySet() method and a forEach loop:

Map<KeyType, ValueType> map = new HashMap<>();


// Add key-value pairs to the map
for (Map.Entry<KeyType, ValueType> entry : map.entrySet()) {
KeyType key = entry.getKey();
ValueType value = entry.getValue();

// Perform operations on each key-value pair


System.out.println("Key: " + key + ", Value: " + value);
}

3. Using the keySet() method and a forEach loop:

Map<KeyType, ValueType> map = new HashMap<>();


// Add key-value pairs to the map
for (KeyType key : map.keySet()) {
ValueType value = map.get(key);

// Perform operations on each key-value pair


System.out.println("Key: " + key + ", Value: " + value);
}

What is IdentityHashMap?
IdentityHashMap is a class that implements Serializable, Clonable interfaces, Map, and extends AbstractMap class. It is designed
for the case wherein there is a need of reference-equality semantics.

What is WeakHashMap?
WeakHashMap is an implementation of the Java Map. It is used to store weak references to its keys. Sorting using this Map
allows a key-value pair is collected as garbage. Its key is not referenced outside WeakHashMap.
What will happen if you try to store a key that is already present in HashMap? If you store an existing key
in the HashMap, then it will override the old value with the new value, and put() will return the old value. There will not be
any exception or error.

Is HashMap thread-safe in Java?


No, HashMap is not a thread-safe in Java. You should not share a HashMap with multiple threads if one or more thread is
modifying the HashMap e.g., inserting or removing a map. Though, you can easily share a read-only HashMap.

List various classes available in sets


Various classes available in sets are: HashSet, TreeSetand, and LinkedHashSet.

Explain Linked HashSet


Java LinkedHashSet class is a Linked list and Hash table implementation of the Set interface. It contains unique elements same
as a HashSet. Linked HashSet in Java also provides optional set operations that can maintain the order of insertion.

-----------------------------------------------------------------------------------------------------------------------------------------------------
Question: Given:
HashMap props = new HashMap();
props.put("key45", "some value");
props.put("key12", "some other value");
props.put("key39", "yet another value");
Set s = props.keySet();
//insert code here for sorting

What, inserted at line 39, will sort the keys in the props HashMap?

A. Arrays.sort(s);
B. s = new TreeSet(s);
C. Collections.sort(s);
D. s = new SortedSet(s);

Correct Answer: B
Since Java 8, we can use the Stream API and lambda expressions to sort the map. All we need is to call
the sorted method over the map’s stream pipeline.
map.entrySet()
.stream()
.sorted(Map.Entry.<String, Employee>comparingByKey())
.forEach(System.out::println);
-----------------------------------------------------------------------------------------------------------------------------------------------------

What are the differences between a SparseArray and Hashmap?

 Sparse arrays can be used to replace hash maps when the key is an Integer or a Long (HashMap <Integer, V>).
 is made to be memory efficient than using the regular HashMap
 It is generally slower than a traditional HashMap
What is the difference between HashSet, HashMap and Hashtable? How do they behave in a multi-
threaded environment?

Hashtable is basically a data structure to retain values of key-value pair.

 It does not allow null for both key and value. It will throw NullPointerException.
 Hashtable does not maintain insertion order. The order is defined by the Hash function. So only use this if you do not
need data in order.
 It is synchronized. It is slow. Only one thread can access in one time.
 HashTable rea thread safe.
 HashTable uses Enumerator to iterate through elements.

Like Hashtable, HashMap also accepts key value pair.

 It allows null for both key and value.


 HashMap does not maintain insertion order. The order is defined by the Hash function.
 It is not synchronized. It will have better performance.
 HashMap are not thread safe, but you can use Collections.synchronizedMap(new HashMap<K,V>())

HashSet does not allow duplicate values.

 It provides add method rather put method.


 You also use its contain method to check whether the object is already available in HashSet. HashSet can be used
where you want to maintain a unique list.

Are you aware of HashMap, ConcurrentHashMap, SynchronizedMap? Which one is faster?

1. Declare the class as final so it can’t be extended.


2. Make all fields private so that direct access is not allowed.
3. Don’t provide setter methods for variables
4. Make all mutable fields final so that its value can be assigned only once.
5. Initialize all the fields via a constructor performing deep copy.
6. Perform cloning of objects in the getter methods to return a copy rather than returning the actual object
reference.

Difference between fail-fast and fail-safe Iterators?


 Fail-fast Iterators throws ConcurrentModificationException when one Thread is iterating over collection object and other
thread structurally modify Collection either by adding, removing or modifying objects on underlying collection. They are called
fail-fast because they try to immediately throw Exception when they encounter failure.

 On the other hand fail-safe Iterators works on copy of collection instead of original collection.

Failfast Failsafe
It does not allow collection modification while iterating. It allows collection modification while iterating.
It can throw ConcurrentModificationException It can't throw any exception.
It uses the original collection to traverse the elements. It uses an original collection copy to traverse the elements.
There is no requirement of extra memory. There is a requirement of extra memory.

What are the methods to make collection thread-safe?

The methods to make collection thread safe are:

 Collections.synchronizedList(list);
 Collections.synchronizedMap(map);
 Collections.synchronizedSet(set);

What is a good way to sort the Collection objects in Java?


A good way to sort Java collection objects is using Comparable and Comparator interfaces. A developer can use
Collections.sort(), the elements are sorted based on the order mention in compareTo().
When a developer uses Collections, sort (Comparator), it sorts the objects depend on compare() of the Comparator interface.

What do you understand by BlockingQueue?


The Java BlockingQueue interface, java.util.concurrent.BlockingQueue, represents a queue which is thread safe
to put elements into, and take elements out of from. In other words, multiple threads can be inserting and taking elements
concurrently from a Java BlockingQueue, without any concurrency issues arising.

Explain diamond operator


Diamond operator enables the compiler to collect the type arguments of generic class. In Java SE, developer can substitute the
parameterized constructor with an empty parameter sets (<>) known as diamond operator.

Explain Big-O notation with an example


The Big-O notation depicts the performance of an algorithm as the number of elements in ArrayList. A developer can use Big-O
notation to choose the collection implementation. It is based on performance, time, and memory.
For example, ArrayList get(index i) is a method to perform a constant-time operation. It does not depend on the total number
of elements available in the list. Therefore, the performance in Big-O notation is O(1).

What is difference between Synchronized Collection and Concurrent Collection?


(Lock Striping)

Is it possible for two unequal objects to have the same hashcode?


Yes
What is a.hashCode() used for? How is it related to a.equals(b)?

12.Difference between poll() and remove() method?

Ans Both poll() and remove() take out the object from the Queue but if poll() fails then it returns null but
if remove fails it throws Exception.

What is a couple of ways that you could sort a collection? (answer)


Ans:You can either use the Sorted collection like TreeSet or TreeMap or you can sort using the ordered collection like a list and using
Collections.sort() method.

What is the difference between Comparator and Comparable in Java? (answer)


Ans:The Comparable interface is used to define the natural order of object while Comparator is used to define custom order. Comparable can
be always one, but we can have multiple comparators to define customized order for objects.
3. Threads & Executor Service (Thread Pool Framework)

Questions
What’s the difference between class lock and object lock?
Class Lock: In java, each and every class has a unique lock usually referred to as a class level lock. These locks are achieved using the keyword
‘static synchronized’ and can be used to make static data thread-safe. It is generally used when one wants to prevent multiple threads from
entering a synchronized block.

Example:

public class ClassLevelLockExample


{
public void classLevelLockMethod()
{
synchronized (ClassLevelLockExample.class)
{
//DO your stuff here
}
}
}

Object Lock: In java, each and every object has a unique lock usually referred to as an object-level lock. These locks are achieved using the
keyword ‘synchronized’ and can be used to protect non-static data. It is generally used when one wants to synchronize a non-static method or
block so that only the thread will be able to execute the code block on a given instance of the class.

Example:

public class ObjectLevelLockExample


{
public void objectLevelLockMethod()
{
synchronized (this)
{
//DO your stuff here
}
}
}

What are the wait() and sleep() methods?


wait(): As the name suggests, it is a non-static method that causes the current thread to wait and go to sleep until
some other threads call the notify () or notifyAll() method for the object’s monitor (lock). It simply releases the lock
and is mostly used for inter-thread communication. It is defined in the object class, and should only be called from
a synchronized context.
Example:

synchronized(monitor)

monitor.wait(); Here Lock Is Released by Current Thread

}
sleep(): As the name suggests, it is a static method that pauses or stops the execution of the current thread for
some specified period. It doesn’t release the lock while waiting and is mostly used to introduce pause on
execution. It is defined in thread class, and no need to call from a synchronized context.

Example:
synchronized(monitor)

Thread.sleep(1000); Here Lock Is Held by The Current Thread

//after 1000 milliseconds, the current thread will wake up, or after we call that is interrupt() method

What is Runnable and Callable Interface? Write the difference between them.

Both the interfaces are generally used to encapsulate tasks that are needed to be executed by another thread. But there are
some differences between them as given below:

Running Interface: This interface is basically available in Java right from the beginning. It is simply used to execute code on a
concurrent thread.
The Runnable interface is a functional interface and has a single run() method that doesn’t accept any parameters or return any
value

Callable Interface: This interface is basically a new one that was introduced as a part of the concurrency package. It addresses
the limitation of runnable interfaces along with some major changes like generics, enum, static imports, variable argument
method, etc. It uses generics to define the return type of object.
The Callable interface is a generic interface containing a single call() method that returns a generic value V:

public interface Runnable


{
public abstract void run();
}
public interface Callable<V>
{
V call() throws Exception;
}

Can you start a thread twice?


No, it's not at all possible to restart a thread once a thread gets started and completes its execution. Thread only runs once and
if you try to run it for a second time, then it will throw a runtime exception i.e., java.lang.IllegalThreadStateException.

What is CyclicBarrier and CountDownLatch?


CyclicBarrier and CountDownLatch, both are required for managing multithreaded programming. But there is some difference between them as
given below:
CyclicBarrier: It is a tool to synchronize threads processing using some algorithm. It enables a set of threads to wait for each other till they
reach a common execution point or common barrier points, and then let them further continue execution. One can reuse the same
CyclicBarrier even if the barrier is broken by setting it.
CountDownLatch: It is a tool that enables main threads to wait until mandatory operations are performed and completed by other threads. In
simple words, it makes sure that a thread waits until the execution in another thread completes before it starts its execution. One cannot reuse
the same CountDownLatch once the count reaches 0.

What is Thread Scheduler and Time Slicing?


Thread Scheduler: It is a component of JVM that is used to decide which thread will execute next if multiple threads are waiting
to get the chance of execution. By looking at the priority assigned to each thread that is READY, the thread scheduler selects the
next run to execute. To schedule the threads, it mainly uses two mechanisms: Preemptive Scheduling and Time slicing
scheduling.

Time Slicing: It is especially used to divide CPU time and allocate them to active threads. In this, each thread will get a
predefined slice of time to execute. When the time expires, a particular thread has to wait till other threads get their chances to
use their time in a round-robin fashion. Every running thread will get executed for a fixed time period.

How is the safety of a thread achieved?

 Immutable objects are by default thread-safe because there state can not be modified once created. Since String is
immutable in Java, its inherently thread-safe.
 Read only or final variables in Java are also thread-safe in Java.
 Locking is one way of achieving thread-safety in Java.
 Static variables if not synchronized properly becomes major cause of thread-safety issues.
 Example of thread-safe class in Java: Vector, Hashtable, ConcurrentHashMap, String etc.
 Atomic operations in Java are thread-safe e.g. reading a 32 bit int from memory because its an atomic operation it can't
interleave with other thread.
 local variables are also thread-safe because each thread has there own copy and using local variables is good way to
writing thread-safe code in Java.
 In order to avoid thread-safety issue minimize sharing of objects between multiple thread.
 Volatile keyword in Java can also be used to instruct thread not to cache variables and read from main memory and can
also instruct JVM not to reorder or optimize code from threading perspective.

Virtual Threads and Platform threads.

Thread Basics

Which one is better to implement Runnable or to extends Thread?


implements Runnable is better than extends Thread. Using extends Thread we can not inherit any other class since Java does
not allow multiple inheritance of classes

How threads are implemented in Java?


Threads are a lightweight process. There are two ways in java to implement threads.

By extending the Thread class and overriding


By implementing Runnable interface.

Any class whose instances are meant to be run by a thread should implement the Runnable interface or thread class. There is
just one abstract method named to run in a runnable interface. It is used to act as a thread. We can’t extend any other class
when we extend the Thread class, even if we need to. We may save space for our class when we implement a Runnable
interface for threads. So that we can extend any other class in the future or right now.

Difference between "implements Runnable" and "extends Thread" in Java


1. Inheritance Option: The limitation with "extends Thread" approach is that if you extend Thread, you can not extend
anything else . Java does not support multiple inheritance. In reality , you do not need Thread class behavior , because in order
to use a thread you need to instantiate one anyway.On the other hand, Implementing the Runnable interface gives you the
choice to extend any class you like , but still define behavior that will be run by separate thread.

2. Reusability : In "implements Runnable" , we are creating a different Runnable class for a specific behavior job (if the work
you want to be done is job). It gives us the freedom to reuse the specific behavior job whenever required. "extends Thread"
contains both thread and job specific behavior code. Hence once thread completes execution , it can not be restart again.

3. Object Oriented Design: Implementing Runnable should be preferred . It does not specializing or modifying the thread
behavior . You are giving thread something to run. We conclude that Composition is the better way. Composition means two
objects A and B satisfies has-a relationship.
"extends Thread" is not a good Object Oriented practice.

4. Loosely-coupled : "implements Runnable" makes the code loosely-coupled and easier to read .
Because the code is split into two classes . Thread class for the thread specific code and your Runnable implementation class for
your job that should be run by a thread code. "extends Thread" makes the code tightly coupled . Single class contains the
thread code as well as the job that needs to be done by the thread.

5. Functions overhead : "extends Thread" means inheriting all the functions of the Thread class which we may do not need .
job can be done easily by Runnable without the Thread class functions overhead.

In multi-threading how can we ensure that a resource isn't used by multiple threads
simultaneously?

In multi-threading, access to the resources which are shared among multiple threads can be controlled by using the concept of
synchronization. Using synchronized keyword, we can ensure that only one thread can use shared resource at a time and others
can get control of the resource only once it has become free from the other one using it.

Describe different states of a thread.


A thread in Java can be in either of the following states:
Ready: When a thread is created, it's in Ready state.
Running: A thread currently being executed is in running state.
Waiting: A thread waiting for another thread to free certain resources is in waiting state.
Dead: A thread which has gone dead after execution is in dead state.

Can a dead thread be started again?


In java, a thread which is in dead state can't be started again. There is no way to restart a dead thread.
ThreadPools
A thread pool reuses previously created threads to execute current tasks and offers a solution to the
problem of thread cycle overhead and resource thrashing.

Thread pool is a single FIFO task queue with a group of worker threads. The producers (E.g. the UI thread) sends tasks to the
task queue. Whenever any worker threads in the thread pool become available, they remove the tasks from the front of the
queue and start running them.

Comparing with starting a random number of individual worker threads, using a thread pool prevent the overhead of killing
and recreating threads every time a worker thread is needed. It also gives you fine control over the number of threads and
their lifecycle. E.g. ThreadPoolExecutor allows you to specify how many core threads, how many max threads the pool should
create and the keep alive time for the idle threads.

What is a ThreadPool? And is it more effective than using several separate Threads?

ThreadPool consists of a task queue and a group of worker threads, which allows it to run multiple parallel instances of a task.

Here, you’re assessing the app developer’s understanding of how multithreading has the potential to improve an app’s performance, but also how
it can negatively impact performance when used incorrectly.

Using ThreadPool is more efficient than having multiple operations waiting to run on a single thread, but it also helps you avoid the considerable
overhead of creating and destroying a thread every time you require a worker thread.

What is ThreadPool in Java?


A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and
resource thrashing. Since the thread is already existing when the request arrives, the delay introduced by thread creation is eliminated, making
the application more responsive.

Java provides the Executor framework which is centered around the Executor interface, its sub-interface –ExecutorService and the class-
ThreadPoolExecutor, which implements both of these interfaces. By using the executor, one only has to implement the Runnable objects and
send them to the executor to execute.

One java thread corresponds to 1 system thread

Creating thread is expansive operation. Threadpool uses blocking queue (thread safe operation).
Types of ThreadPool

1.FixedThreadPool

2 CachedThreadPool: No control for no. threads created. It check for any thread free, if not create new
thread.
3. ScheduledThreadPool:
4. SingleThreadedPool: To ensure to run task in sequentially.
Threads: producer and consumer problem?
In computing, the producer–consumer problem (also known as the bounded-buffer problem) is a classic
example of a multi-process synchronization problem. The problem describes two processes, the
producer and the consumer, which share a common, fixed-size buffer used as a queue.

 The producer’s job is to generate data, put it into the buffer, and start again.
 At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one
piece at a time.

Difference Between Process and Thread in Java


Java provides the concept of multitasking, which allows more than two processes to run concurrently, and allows more than two threads to run
concurrently. The main difference between process and thread is that a process is a program in execution whereas, a thread is part of that
running process.

Process and thread share a relationship where a process provides an environment for the execution of the thread. A process can contain
multiple threads.

 Each process has its own address space whereas, the threads of the same process share the address space as that of the process.
 In process based multitasking, more than two processes can run at the same time whereas, in thread-based multitasking, more than
two thread can run at the same time.
 Inter-process communication between two processes is costlier than inter-thread communication.
 Context switching between two processes is expensive and limited as compared to context switching between two threads.
 A process is also called the heavyweight task whereas, the thread is called lightweight task.
 Multitasking over a process is not under the control of Java whereas, the Multitasking over multithreading is under the control of
Java.
 Components contained by a process its own address space, global variables, signal handlers, open files, child processes, accounting
information. On the other hand, a thread contains its own register, state, stack, program counter.

Check Android P

What is the difference between concurrency and parallelism?


Concurrency is when two or more tasks can start, run, and complete in overlapping time periods. It
doesn't necessarily mean they'll ever both be running at the same instant. For example, multitasking on
a single-core machine.

Parallelism is when tasks literally run at the same time, e.g., on a multicore processor.

 Concurrency: A condition that exists when at least two threads are making progress. A
more generalized form of parallelism that can include time-slicing as a form of virtual
parallelism.
 Parallelism: A condition that arises when at least two threads are executing
simultaneously.
4.

18.The difference between Serializable and Externalizable in Java?

20.What is blank final variable?

Ans:A final variable, not initalized at the time of declaration, is known as blank final variable

22. Why Should an Object Used As the Key should be Immutable?

23.What’s wrong with using HashMaps in a multi-threaded environment? When does a get() method go
into an infinite loop?

24.Can you write critical section code for a singleton?

5. Java 8

Imperative Vs Declarative programming

28.Difference between Vector and ArrayList?


ArrayList Vector
1) ArrayList is not synchronized. Vector is synchronized.
2) ArrayList increments 50% of current
Vector increments 100% means doubles the array size if total
array size if number of element
number of element exceeds than its capacity.
exceeds from its capacity.
3) ArrayList is not a legacy class, it is
Vector is a legacy class.
introduced in JDK 1.2.
Vector is slow because it is synchronized i.e. in multithreading
4) ArrayList is fast because it is non-
environment, it will hold the other threads in runnable or non-
synchronized.
runnable state until current thread releases the lock of object.
5) ArrayList uses Iterator interface to Vector uses Enumeration interface to traverse the elements. But
traverse the elements. it can use Iterator also.

31. What is Marker Interface?

Ans:Marker interface in Java is interfaces with no field or methods.

32.Can an inner class be built in an Interface?

33.Explain the usage of the keyword transient?

Ans:Transient keyword indicates that the value of this member variable does not have to be serialized
with the object.

34. Why would you use a synchronized block vs. synchronized method?

36.There are two classes: A and B. The class B need to inform a class A when some important event has
happened. What Java technique would you use to implement it?
Functional Interfaces and Lambdas in Java 8
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit.
From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface
can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional
interfaces.

There are a lot of functional interfaces in the java.util.function package. The more common ones include, but are not limited to:

 Function – it takes one argument and returns a result


 Consumer – it takes one argument and returns no result (represents a side effect)
 Supplier – it takes no arguments and returns a result
 Predicate – it takes one argument and returns a boolean
 BiFunction – it takes two arguments and returns a result
 BinaryOperator – it is similar to a BiFunction, taking two arguments and returning a result. The two arguments and the
result are all of the same types.
 UnaryOperator – it is similar to a Function, taking a single argument and returning a result of the same type

@FunctionalInterface Annotation
@FunctionalInterface annotation is used to ensure that the functional interface can’t have more than one abstract method. In
case more than one abstract methods are present, the compiler flags an ‘Unexpected @FunctionalInterface annotation’
message. However, it is not mandatory to use this annotation.

Java Anonymous inner class


A class that have no name is known as anonymous inner class in java. It should be used if you have to override method of class
or interface. Java Anonymous inner class can be created by two ways:

Class (may be abstract or concrete).

Interface

What Is Optional? How Can It Be Used?


Optional is a new class in Java 8 that encapsulates an optional value, i.e. a value that is either there or not. It's a wrapper
around an object, and we can think of it as a container of zero or one element.

Optional has a special Optional.empty() value instead of wrapped null. Thus it can be used instead of a nullable value to get rid
of NullPointerException in many cases.

String[] words = new String[10];


Optional<String> checkNull
= Optional.ofNullable(words[5]);
if (checkNull.isPresent()) {
String word = words[5].toLowerCase();
System.out.print(word);
}
else
System.out.println("word is null");
The Web and blogosphere are full of claims that the Optional class solves the problem of null pointer exceptions. This is not
true. Changing your code to use the Optional class has these effects:

 It transforms a NullPointerException into a NoSuchElementException, which still crashes your program.


 It creates new problems that were not a danger before.
 It clutters your code.
 It adds both space and time overheads.

Optional introduces overhead

Optional introduces space overhead: an Optional is a separate object that consumes extra
memory.

Optional introduces time overhead: Its data must be accessed via an extra indirection, and
calling methods on it is more expensive than Java's efficient test for null.

Optional introduces coding overhead: You have to deal with its incompatibility with existing
interfaces that use null, with the fact that it is not serializable, etc.

How to create Optional Object in Java 8

There are many ways to create an object of the Optional class in Java, you can create an empty Optional by using the static
method Optional.empty() as shown below:

Optional<Person> p = Optional.empty();
This Optional is empty, if you want to create an Optional with a non-null value then you can write the following code:

Person p = new Person();


Optional<Person> op = Optional.of(p);

n case the Person object is null, the resulting Optional object would be empty, but it won't throw the NullPointerException.

You can also create an instance of the Optional class by using the static factory method Optional.of() in Java 8 as shown
in the following example:

Address home = new Address(block, city, state, country, pincode);


Optional<Address> = Optional.of(home);
This code will return an Optional of Address object but value i.e. home must be not-null. In case the object is null then
Optional.of() method will throw NullPointerException.
What Is a Default Method and When Do We Use It?
A default method is a method with an implementation, which can be found in an interface. We can use a default method to add
a new functionality to an interface, while maintaining backward compatibility with classes that are already implementing the
interface:

public interface Vehicle {


public void move();
default void hoot() {
System.out.println("peep!");
}
}

What Is a Lambda Expression and What Is It Used for?


A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to
methods, but they do not need a name and they can be implemented right in the body of a method.

Lambda expressions are usually passed as parameters to a function

In very simple terms, a lambda expression is a function that we can reference and pass around as an object. Moreover, lambda
expressions introduce functional style processing in Java, and facilitate the writing of compact and easy-to-read code.

As a result, lambda expressions are a natural replacement for anonymous classes such as method arguments. One of their
main uses is to define inline implementations of functional interfaces.

Explain the Syntax and Characteristics of a Lambda Expression


A lambda expression consists of two parts, the parameter part and the expressions part separated by a forward arrow:

params -> expressions

(parameter1, parameter2) -> { code block }

Any lambda expression has the following characteristics:

Optional type declaration – when declaring the parameters on the left-hand side of the lambda, we don't need to declare
their types as the compiler can infer them from their values. So int param -> … and param ->… are all valid

Optional parentheses – when only a single parameter is declared, we don't need to place it in parentheses. This means
param -> … and (param) -> … are all valid, but when more than one parameter is declared, parentheses are required

Optional curly braces – when the expressions part only has a single statement, there is no need for curly braces. This means
that param – > statement and param – > {statement;} are all valid, but curly braces are required when there is more than one
statement

Optional return statement – when the expression returns a value and it is wrapped inside curly braces, then we don't need a
return statement. That means (a, b) – > {return a+b;} and (a, b) – > {a+b;} are both valid
Stream API Java8

Once you consume stream you can’t reuse stream.

Java Stream Operations

forEach

map
Double the value of arrayList

How may streams in the above code. 2


Does it consuming memory? By making 2 streams.
collect

filter

findFirst

toArray

flatMap

peek

Method Types and Pipelines

Intermediate

terminal operations

Comparison Based Stream Operations


sorted
min and max
distinct
allMatch, anyMatch, and noneMatch

Java Stream Specializations

Creation

Short Circut operations

Tell me 5 features introduced in JDK 1.8?


-Lambda expression, which allows you pass an anonymous function as object.
-Stream API, take advantage of multiple cores of modern CPU and allows you to write succinct code.
-Date and Time API, finally you have a solid and easy to use date and time library right into JDK
-Extension methods, now you can have static and default method into your interface
-Repeated annotation, allows you apply the same annotation multiple times on a type
Programs to check logics

How do you remove spaces from a string in Java?


String removeWhiteSpaces(String input) {
StringBuilder output = new StringBuilder();

char[] charArray = input.toCharArray();

for (char c : charArray) {


if (!Character.isWhitespace(c))
output.append(c);
}
return output.toString();
}

1.Write a program in Java which Counts total number of Characters, Words and Lines

2.Write a program to sort an array and remove duplicates?

3.Write a program to print Fibonacci series?

4.Write a program to reverse a string using recursive algorithm.


How do you reverse a given string in place?
How to Find Duplicate Characters in String?
6. Others

Java 19 Features

Java Architect Interview

What is Open-API and swagger ? Are they same or different ?

How will you implement interceptors and filters while using spring boot ?

What are the core classes to implement the Spring Security ? Is this any how different while using with
spring MVC or with spring boot ? Or

Is all Maven/Gradle dependency needs to add while using spring boot or is there any dedicate starter for
Spring Security?

What is service mash ?

Name two service discovery which you have implemented in your spring boot microservice application ?
Is there any configuration needs to be added to your application or in K8s cluster related to this ?

Name Load balancer which you are using in your application and what all steps need to follow while
configure it with Kubernetes or any of cloud ?

How will scale your single microservice or multiple microservices ? is application.yaml or


application.properties enough or you have to tell any thing to your cloud environment ?

What is the difference b/w Joint Point and Point Cuts in Spring AOP.
What is cloud formation template ?

OR

Have you ever get a chance to create cloud formation template ?

What is use of Spring Batch , have you ever implemented the same , if yes kindly tell me the steps ?

What are the basic commands to create a docker image and where are you storing your docker images ?

What CICD tools your are using in your project for Continuous building and continuous deployment ?

Different type of inheritance in Hibernate ?

How many tables will create in many to many relations ?

What is the use of sleuth in spring boot microservice application ? As Spring boot 3.x no more
supporting sleuth ,please name the alternative for same.

What is Micrometer Tracing in spring boot 3 ?

What are ways of communication between microservices ?

What type of injection use by @Autowired ?

Why constructor injection is a recommended approach over setter base i?


Please describe the workflow of kafka ?

Sum of n numbers e.g 1,2,3... 10

Find missing number from arraylist.

JDBC
Steps to Connect Java Application with Database

1. Step 1 – Import the Packages.


2. Step 2 – Load the drivers using the forName() method.
3. Step 3 – Register the drivers using DriverManager.
4. Step 4 – Establish a connection using the Connection class object.
5. Step 5 – Create a statement.
6. Step 6 – Execute the query.
7. Basic

What’s your weakness and power in Android development?


What project management tools have you used before?
Are you familiar with Agile, SCRUM, Sprint ...?

Which architectural designs are mostly used to design applications?


In software design, we use the following architectural design patterns:

Model View Controller

Master-Slave Pattern

Layered Pattern

Model View Presenter

Monolithic Architecture

Event-Driven Architecture Pattern

Compare fail-fast and fail-safe iterators?


Basis of
Fail Fast Iterator Fail Safe Iterator
Comparison

Operates It operates directly on the collection itself. It operates on a cloned copy of the collection.

It throws a ConcurrentModificationException in modifying the object during


Exception It does not throw Exception.
the iteration process.

A copy or clone object is created during the


Clone Object No clone object is created during the iteration process.
iteration process.

Memory
It requires low memory during the process. It requires more memory during the process.
utilization

It allows modification during the iteration


Modification It does not allow modification during iteration.
process.

Performance It is fast. It is slightly slower than Fail Fast.

CopyOnWriteArrayList, ConcurrentHashMap,
Examples HashMap, ArrayList, Vector, HashSet, etc.
etc.

Whats the outpout of this program


It has ConcurrentModification Error. Iterator not allowed to modified the source during the iteration.
For the first iteration it run, but on second iteration it throw exception on it.next() , checkForComodification().
Exceptions are getting at time of iteration are Fail Fast iterator.

Bad way, as it costly, CopyOnWriteArrayList has no checkForComodification() and creates new array list, i.e. changing data soure.
What is double brace initialization in Java and where it is used?
In Java, double brace initialization is a combination of two separate processes. The two consecutive curly braces {{ involved in it.

The first curly brace represents the creation of an anonymous inner class. Remember that the second curly brace will not be considered in such
a case. It is just like creating an anonymous inner class.

The second curly brace represents an initialization block that we have seen in it as a class for initialization. When we use the initialization block
for an anonymous inner class it becomes Java double brace initialization. The inner class has a reference to the enclosing outer class. We can
use the reference by using this pointer.

It is used to initialize collections because it is easier to initialize a constant collection using double brace initialization. The example of double
brace initialization is as follows:

Why should you avoid to run non-UI code on the main thread?

You might also like