General
Java and Object Oriented Programming:
- History of
Java?
- What is Java?
- What is JVM?
- Why is Java
called the Platform Independent Programming Language?
- What is the
difference between JDK, JRE and JVM?
- What does the
static keyword mean?
- Can you
override private or static method in Java?
- Can you access
the non-static variable in static context?
- What are the
Data Types supported by Java?
- What are the
concepts of OOPs?
- What is the
unique feature of Java?
- What is an
instance and a local variable?
- What are
functional or SAM interfaces?
- Difference
between == and equals() method in java?
- What is a
Lambda expression and its syntax?
- What is the
difference between Collection and Streams in Java?
- What are the
advantages of design patterns in Java?
- Why String is
called an immutable object?
- What is an Optional
class in Java?
- What is
Autoboxing and Unboxing?
- What is
Function Overriding and Overloading in Java?
- Does Java
support multiple inheritance?
- What is the
difference between an Interface and an Abstract class?
- What are pass
by reference and pass by value?
- What is the
purpose of a Volatile Variable?
- What is the
purpose of a Transient variable?
- What is Local
Variable and Instance Variable?
- What are the
different access modifiers available in Java?
- What are
wrapper classes?
- What is singleton class and how can we make a class singleton?
- What is the
difference between processes and threads?
- Explain
different ways of creating a thread. Which one would you prefer and why?
- What is the
difference between a method and block that are synchronized?
- What are the
differences between wait and sleep method in Java?
1)
History of Java?
- James Gosling
and Sun Microsystems invented the Java programming language in 1991.
- He had a single
motto while creating the language: “Write once; run anywhere.”
- The language
was initially called Oak because of the oak tree outside Gosling’s office.
- Later, the name
changed to Green, then to Java Coffee, a type of coffee from Indonesia,
and eventually shortened to Java in 1995.
- Gosling
designed Java with a C/C++-style syntax that system and application
programmers would find familiar.
- Sun
Microsystems released the first public implementation as Java 1.0 in 1996.
- It promised
write once, run anywhere (WORA) functionality, providing no-cost run-times
on popular platforms.
2) What
is Java?
- Java is a
widely-used programming language for coding web applications.
- It has been a
popular choice among developers for over two decades, with millions of
Java applications in use today.
- Java is a
multi-platform, object-oriented, and network-centric language that can be
used as a platform in itself.
- It is a fast,
secure, reliable programming language for coding everything from mobile
apps and enterprise software to big data applications and server-side
technologies.
- Java is free to
download for personal and development use.
- It is used for:
- Mobile
applications (specially Android apps)
- Desktop
applications
- Web
applications
- Web servers
and application servers
- Games
- Database connection
- And much, much
more!
3) What
is JVM?
- The JVM (Java
Virtual Machine) is only one part of Java software that is involved in
running an application.
- It acts as a
run-time engine to run Java applications. JVM is the one that actually
calls the main method present in a Java code. It is a part of JRE(Java
Runtime Environment).
- Java
applications are called WORA (Write Once Run Anywhere). This means a
programmer can develop Java code on one system and can expect it to run on
any other Java-enabled system without any adjustment. This is all possible
because of JVM.
4) Why
is Java called the Platform Independent Programming Language?
- Java is
platform-independent means the Java code that has been compiled generates
compiled code or the byte code, and this byte code can run on all of the
operating systems provided they have JVM installed in it.
- This means you
can write a Java program on one platform (such as Windows) and then run it
on a different platform (such as macOS or Linux) without making any
changes to the code.
- The JVM acts as
an interpreter for the Java bytecode, translating it into instructions
that the host machine can understand and execute. This means that the same
Java program can run on any device that has a JVM, making it a truly
“write once, run anywhere” language.
5) What
is JDK, JRE and JVM?
- JDK is for
development purpose whereas JRE is for running the java programs.
- JDK and JRE
both contain JVM so that we can run our java program.
- JVM is the
heart of java programming language and provides platform independence.
6) What
does the static keyword mean?
- The static
keyword is mainly used for memory management in Java.
- The static
keyword can be used with variables, methods, code blocks, and nested
classes.
- In Java, it is
used to share the same variable or method of a given class.
- If we want to
access class members without creating an instance of the class, we need to
declare the class members static.
- Static methods
are also called class methods. It is because a static method belongs to
the class rather than the object of a class.
7) Can
you override private or static method in Java?
- No, we cannot
override private or static methods in Java.
- Private
methods are not visible to any other class in Java.
- Static methods
cannot be overridden in Java. Because static methods are associated with
the class itself, but not with the instance of a class. Therefore, when a
subclass inherits a static method from its parent class, it cannot modify
the behavior of the static method in any way.
8) Can
you access the non-static variable in static context?
- Non-static
members cannot be accessed from a static context.
- Non-static
variables and methods depend on class instances, as they store and
manipulate data specific to individual objects.
- There has to
be a class instance that references these members.
9) What
are the Data Types supported by Java?
- There are two
types of data types in Java:
- Primitive data
types: The primitive data types include boolean, char, byte, short, int,
long, float, and double.
- Non-primitive
data types: The non-primitive data types include Classes, Interfaces, and
Arrays.
- Example:
- Primitive
data types:
- byte: Byte
data type is an 8-bit signed two's complement integer.
- short: Short
data type is a 16-bit signed two's complement integer.
- int: Int
data type is a 32-bit signed two's complement integer.
- long: Long
data type is a 64-bit signed two's complement integer.
- float: Float
data type is a single-precision 32-bit IEEE 754 floating point.
- double:
double data type is a double-precision 64-bit IEEE 754 floating point.
- boolean:
boolean data type represents one bit of information.
- char: char
data type is a single 16-bit Unicode character.
10) What
are the concepts of OOPs?
- Object-Oriented
Programming System (OOPs) is a paradigm that provides many concepts such
as:
- Inheritance
- Data binding
- Polymorphism
- Encapsulation
- Abstraction
- Message
Passing
11) What
is the unique feature of Java?
- The main unique
feature of Java is its platform independence.
- Unlike many
other programming languages like C and C++, when Java is compiled, it is
not compiled into platform-specific machine code, but rather into
platform-independent byte code.
- This byte code
is distributed over the web and interpreted by the JVM (Java Virtual
Machine) on whichever platform it is being run.
- Java is
guaranteed to be Write Once, Run Anywhere.
12) What
is an instance and a local variable?
- Instance
variables: Instance variables are variables that are declared in a class
but outside a method, constructor, or any block. They are created when an
object of the class is instantiated and destroyed when the object is
destroyed.
- Local
variables: Local variables are variables that are declared inside a
method, constructor, or any block. They are created when the method,
constructor, or block is entered and destroyed when it is exited.
13) What
are functional or SAM interfaces?
- A functional
interface is an interface that has just one abstract method, and thus
represents a single function contract. It is also called a SAM (Single
Abstract Method) interface.
- Examples:
- Runnable: The
Runnable interface is a SAM interface with a single method void run().
- Callable: The
Callable interface is a SAM interface with a single method V call().
14)
Difference between == and equals() method in java?
- == operator: In
Java, the == operator compares the memory reference (or address) of two
objects, not the contents of the objects. If two object references point
to the same memory address, then the == operator will return true.
- equals()
method: The equals() method is defined in the Object class and is used to
compare the actual contents of the objects.
- For example,
two String objects may have the same value, but different memory
references. In this case, the == operator would return false, but the
equals() method would return true.
15) What
is a Lambda expression and its syntax?
- Lambda
expression: 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.
- Syntax:
(argument-list) -> {body}
- Example:
- (int a, int b)
-> { return a + b; }
16) What
is the difference between Collection and Streams in Java?
- Collection:
- Collections
are in-memory data structures that hold all the values the data structure
currently contains.
- Collections
store data for processing.
- Examples of Collection
are List, Set, Map, etc.
- Stream:
- Streams are a
new abstraction introduced in Java 8 that represents a sequence of
elements from a source that supports aggregate operations.
- Streams
process data on demand.
- Streams do not
store data; they perform a series of computational steps on the data.
17) What
are the advantages of design patterns in Java?
- Advantages:
- They are
reusable in multiple projects.
- They provide
the solutions to the commonly occurring problems in software design.
- They lead to
more flexible, scalable, and robust code.
- They improve
the readability of the code.
- They help in
faster software development.
18) Why
String is called an immutable object?
- String is
called an immutable object because its value cannot be changed once it has
been created.
- Example: If we
create a String with a value of "Hello", then that value cannot
be changed. Any operation that appears to modify the value will actually
create a new String object with the new value, and leave the original
String unchanged.
- Reasons for
immutability:
- Security:
String is widely used as a parameter for many Java classes, e.g., for
opening network connections, loading classes, and so on. If the string
is mutable, it could cause serious security threats.
- Synchronization:
The immutability of strings makes them inherently thread-safe. Multiple
threads can access a single "immutable" string instance
without any extra synchronization.
19) What
is an Optional class in Java?
- Optional: The
Optional class in Java is a container object that may or may not contain a
non-null value.
- Optional is a
public final class and is used to deal with NullPointerException in Java
application.
- Example:
- Optional gender
= Optional.of("MALE");
- Optional emptyGender
= Optional.empty();
20) What
is Autoboxing and Unboxing?
- Autoboxing:
Autoboxing is the automatic conversion that the Java compiler makes
between the primitive types and their corresponding object wrapper
classes.
- Example:
converting an int to an Integer, a double to a Double, and so on.
- Unboxing:
Unboxing is the reverse process of converting an object of a wrapper type
to its corresponding primitive value.
- Example:
converting an Integer to an int, a Double to a double, and so on.
21) What
is Function Overriding and Overloading in Java?
- Method
Overriding:
- When a method
in a subclass has the same name, same parameters, and same return type as
a method in its superclass, then the method in the subclass is said to
override the method in the superclass.
- Method
Overloading:
- When a class
has multiple methods with the same name but different parameters, then
these methods are said to be overloaded.
- It can be
achieved by changing the number of arguments or changing the data type of
arguments.
22) What
is a constructor in Java?
- Constructor: A
constructor is a special method that is used to initialize objects. The
constructor is called when an object of a class is created.
- Rules for
creating a constructor:
- The
constructor name must be the same as its class name.
- A constructor
must not have an explicit return type.
- A Java
constructor cannot be abstract, static, final, and synchronized.
23) What
is Singleton class in Java?
- Singleton
class: A Singleton class in Java is a class that can have only one object
(an instance of the class) at a time.
- Example: a
database connection pool, where we want to limit the number of database
connections being created.
- To create a
Singleton class:
- Make the
constructor of the class private.
- Create a
static method that returns the instance of the class.
24) What
is the difference between ArrayList and Vector in Java?
- ArrayList:
- ArrayList is
not synchronized.
- ArrayList is
fast as it is non-synchronized.
- ArrayList
increases its size by 50% of the array size.
- ArrayList does
not define the increment size.
- Vector:
- Vector is
synchronized.
- Vector is slow
as it is thread-safe.
- Vector
increases its size by doubling the array size.
- Vector defines
the increment size.
25) What
is a Thread in Java?
- A thread in
Java is the path followed when executing a program.
- All Java
programs have at least one thread, known as the main thread, which is
created by the Java Virtual Machine (JVM).
- Threads are
also called lightweight processes.
26) What
is a package in Java?
- A package in
Java is a namespace that organizes a set of related classes and
interfaces.
- Packages are
used to group related classes and interfaces together. They provide access
protection and namespace management.
- Packages can be
categorized into two types:
- Built-in
packages: java, lang, util, etc.
- User-defined
packages
27)
Explain the 'this' keyword in Java.?
- The 'this'
keyword in Java is a reference variable that refers to the current object.
- Uses of 'this'
keyword:
- It can be used
to refer to the instance variable of the current class.
- It can be used
to invoke or initiate the current class constructor.
- It can be
passed as an argument in the method call.
- It can be
passed as an argument in the constructor call.
- It can be used
to return the current class instance.
28) What
is the final keyword in Java?
- The final keyword
in Java is used to restrict the user.
- The final
keyword can be used in many contexts. Final can be:
- Variable:
Once a final variable is initialized, it cannot be changed.
- Method: A
final method cannot be overridden.
- Class: A
final class cannot be inherited.
29) What
is Exception Handling in Java?
- Exception
Handling in Java is a powerful mechanism that handles runtime errors and
maintains the normal flow of the application.
- Exception
Handling in Java is managed via five keywords: try, catch, throw, throws,
and finally.
- try: The code
that might throw an exception is placed inside a try block.
- catch: This
block catches the exception thrown by the try block and handles it.
- throw: This
keyword is used to explicitly throw an exception.
- throws: This
keyword is used to declare an exception in the method signature.
- finally: This
block contains code that is always executed, whether an exception is
handled or not.
30) What
is the difference between an abstract class and an interface in Java?
- Abstract Class:
- An abstract
class can have abstract methods (methods without body) as well as
concrete methods (methods with body).
- Abstract class
can have member variables.
- It can have
constructors.
- It supports
multiple inheritance through interfaces.
- Interface:
- An interface
can only have abstract methods (methods without body) until Java 8. From
Java 8, it can have default and static methods with body.
- Interface
cannot have member variables.
- It cannot have
constructors.
- It supports
multiple inheritance by default.
Java Threads
31) What
is the difference between processes and threads?
- A process is an
independent program that runs in its own memory space, whereas a thread is
a smaller unit of a process that runs within the memory space of the
process.
- Processes are
isolated from each other, whereas threads share the same memory space.
- Processes are
more heavyweight and require more resources to create and manage, whereas
threads are lightweight and have lower overhead.
32)
Explain different ways of creating a thread. Which one would you prefer and
why?
- There are two
main ways to create a thread in Java:
- By extending
the Thread class: Subclass Thread and override
its run() method.
- By
implementing the Runnable interface: Implement the run() method
and pass an instance of the implementing class to a Thread object.
- Preferred
method: Implementing Runnable interface is generally preferred
because it allows the class to extend another class if needed and
separates the task from the thread itself, promoting better design and
code reuse.
33) What
is the difference between a method and a block that is synchronized?
- Synchronized
Method: Locks the object for which the method is called. Only one thread
can execute the synchronized method at a time.
- Synchronized
Block: Locks a specific object or block of code within a method. This
provides more granular control over the synchronization, potentially
improving performance by reducing the scope of the lock.
34) What
are the differences between the wait and sleep methods in Java?
- wait() is
used for inter-thread communication. It releases the lock on the object
and allows other threads to acquire the lock.
- sleep() is
used to pause the execution of the current thread for a specified period.
It does not release the lock held by the thread.
- wait() must be called within a synchronized block or method, while sleep() can be called from any context.