PPS Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Fundamental

Questions of Java
Q 1. What is the difference between JDK and JRE?
Ans: The JDK (Java Development Kit) is used by developers for
creating Java applications and includes the necessary tools,
libraries, and compilers.

The JRE (Java Runtime Environment) is used by end-users to run


Java applications and provides the runtime environment and
essential class libraries, but does not include development tools.

Q 2. What are the benefits of using Java?


Ans: These are the benefits of using Java:
Portability: Java code can be run on any platform that has a
Java Virtual Machine (JVM).
Security: Java has a built-in security model that helps to
protect users from malicious code.
Object-oriented: Java is an object-oriented programming
language, which makes it easy to create modular and reusable
code.
Robust: Java is a robust language that is designed to be
reliable and efficient.
Widely used: Java is a widely used language that has a large
community of developers and support resources.

01
Q 3. What are the different components of the Java platform?
Ans: The Java platform is a software environment that provides a
standard way for developing and running Java applications. It
consists of the following components:

Java Virtual Machine (JVM)


Java Runtime Environment (JRE)
Java Development Kit (JDK)

Q 4. What are the different types of Java data types?


Ans: There are two types of data types in Java: primitive data types
and non-primitive data types.
Primitive data types Non-primitive data types
boolean String
byte Array
short Class
int Interface
long Enum
float
decimal places
double
char

Q 5. What are the different types of Java control statements?


Ans: There are three types of control statements in Java:
Decision-making statements (if, if else & switch)
Looping statements (while, do while & for)
Jump statements (continue & return)

02
Q 6. What are the different types of Java exceptions?
Ans: There are two types of exceptions in Java: checked exceptions
and unchecked exceptions.
Checked exceptions are exceptions that must be declared in
the method signature. If a checked exception is thrown in a
method, the method must either handle the exception or
declare it to be thrown. If the method does not handle the
exception, the compiler will generate an error.

Unchecked exceptions are exceptions that do not need to


be declared in the method signature. Unchecked exceptions
can be thrown by any method, and the compiler will not
generate an error if they are not handled.

03
Q 7. What are the different types of Java classes & Java
interfaces?
Ans: There are two main types of Java classes:
Normal classes are the most common type of class in Java.
They can have fields, methods, and constructors.
Abstract classes are classes that cannot be instantiated. They
can only be used as a base class for other classes.

There are also two main types of Java interfaces:


Normal interfaces are a collection of abstract methods. A
class can implement an interface, thereby inheriting the
abstract methods of the interface.
Marker interfaces are interfaces that do not contain any
methods. They are used to indicate that a class has a certain
property or behavior.

Q 8. What are the different types of Java libraries & Java


frameworks?
Ans: A Java library is a collection of reusable Java classes and
interfaces.
some examples of Java libraries:
Apache Commons
Google Guava
Joda-Time
JUnit
Mockito
A Java framework is a collection of reusable Java classes,
interfaces, and code that provides specific functionality.
some examples of Java libraries:
Spring
Hibernate
JSF
Grails
Struts
04
Q 9. What are the different types of Java tools?
Ans: There are two types of threads in Java: user threads and daemon
threads.
User threads are the threads that are created by the user or
application. They are high-priority threads and the JVM will
wait for any user thread to finish its task before terminating it.
Daemon threads are the threads that are created to provide
services to user threads. They are low-priority threads and are
only needed while user threads are running. Once all user
threads have finished their execution, the JVM will terminate
even if there are daemon threads still running.

Q 10. What are the different types of Java networking?


Ans: There are two main types of Java networking:
Client-server networking is a type of networking where there
is a client application that requests a service from a server
application. The server application then provides the service
to the client application.
Peer-to-peer networking is a type of networking where two or
more applications communicate directly with each other
without the need for a server.

05
Object-Oriented Programming:

Q 1. What is the difference between Procedural


programming and OOP?
Ans: Procedural programming is a top-down approach to
programming, where the program is divided into a series of
functions that each perform a specific task.

OOP, on the other hand, is a bottom-up approach to


programming, where the program is divided into objects that
each represent a real-world entity.

Q 2. What are the core concepts of OOP?


Ans: The core concepts of OOP are:
Abstraction: Abstraction is the process of hiding the
implementation details of an object from the user. This allows
the user to focus on the object's functionality without having
to worry about how it works.
Encapsulation: Encapsulation is the bundling of data and
codes into a single unit. This makes it easier to maintain and
update the code, and it also makes it more difficult for users
to accidentally modify the data.
Inheritance: Inheritance is the ability of an object to inherit
the properties and methods of another object. This allows
developers to reuse code and create more complex objects
with fewer lines of code.
Polymorphism: Polymorphism is the ability of an object to
behave differently depending on its context. This allows
developers to write code that is more flexible and easier to
maintain.

06
Q 3. What is the difference between Overloading and
Overriding?
Ans: Overloading refers to the ability to have multiple methods with
the same name, but different parameters.

Overriding refers to the ability to have a method in a subclass


that has the same signature as a method in a superclass.

Q 4. What is the difference between static and dynamic


binding?

Ans: Static binding and dynamic binding are two different ways of
resolving function calls in object-oriented programming (OOP).

Static binding occurs when the compiler determines the


method to be called at compile time. This is the most common
type of binding in OOP, and it is used for both static and non-
virtual methods.

Dynamic binding occurs when the method to be called is not


determined until runtime. This is used for virtual methods,
which allow for polymorphism.

Feature Static Binding Dynamic Binding

When does binding occur? Compile time Runtime

Performance Faster Slower

Flexibility Less flexible More flexible

Error handling More error-prone Less error-prone

07
Q 5. What is the difference between Abstract class and
Interface?
Ans: Here is a table that summarizes the key differences between
abstract classes and interfaces:
Feature Abstract Class Interface

Can be instantiated No No

Can have abstract methods Yes Yes

Can have non-abstract methods Yes No

Can have state Yes No

Can be extended by other classes Yes No

Can be implemented by other classes Yes Yes

Q 6. Why Java doesn't support Multiple Inheritance?


Ans: Java doesn't support multiple inheritance because it can lead to
a number of problems, including:
Ambiguity
Circular dependencies
Complexity

08
Q 7. When do you use interface and abstract class in
Java?
Ans: Abstract classes and interfaces are both used to achieve
abstraction in object-oriented programming.
Abstract classes are similar to normal classes, with the
difference that they can include abstract methods, which are
methods without a body. Abstract classes cannot be
instantiated.

Interfaces are a kind of code contract, which must be


implemented by a concrete class. Interfaces cannot have
state, whereas the abstract class can have state with instance
variables.

Q 8. What are the challenges of using OOP in Java?


Ans: There are some challenges associated with using OOP in Java.
These challenges include:
Complexity: OOP can make code more complex, especially
when dealing with large and complex systems.
Overhead: OOP can add some overhead to code, as objects
need to be created and managed.
Testing: OOP can make code more difficult to test, as objects
need to be tested in isolation and in combination.
Performance: OOP can impact performance, as objects can
add some overhead.

09
Data Structures & Algorithms
Q 1. What is the difference between an array
and a linked list?
Ans: In general, arrays are a good choice for data structures where the
data is accessed frequently and the order of the data is
important.

Linked lists are a good choice for data structures where the data
is inserted or deleted frequently and the order of the data is not
important.

Feature Array Linked List

Data storage Contiguous memory Non-contiguous memory

Access efficiency High Low

Insertion/deletion efficiency Low High

Order of data Important Not important

Q 2. Explain the concept of a hash table.


Ans: A hash table is a data structure that maps keys to values. It is a
very efficient data structure for storing and retrieving data, as it
can access data in constant time.
put(key, value): This method stores the key-value pair in the
hash table.
get(key): This method returns the value associated with the
key.
remove(key): This method removes the key-value pair from
the hash table.

10
Q 3. What is the time complexity of various operations in a
binary search tree (BST)?
Ans: The time complexity of various operations in a binary search tree
(BST) depends on the height of the tree. The height of a BST is the
number of nodes on the longest path from the root node to a leaf
node.

The following table shows the time complexity of various


operations in a BST:

Operation Time complexity

Search O(log n)

Insert O(log n)

Delete O(log n)

Inorder traversal O(n)

Preorder traversal O(n)

Postorder traversal O(n)

Q 4. Describe the difference between breadth-first search


(BFS) and depth-first search (DFS) algorithms.
Ans: Here is a table that summarizes the key differences between BFS
and DFS:
Feature BFS DFS

All nodes at the current level As far as possible down one path
Explores
before moving on to the next level before backtracking

Time
O(V+E) V
complexity

Space
O(v) V
complexity

Finding the shortest path, finding Finding all of the nodes in a graph,
Use cases all of the nodes in a graph that are finding all of the paths between
reachable from a given node two nodes
11
Q 5. Explain the concept of a priority queue and provide an
example of its application.
Ans: A priority queue is a data structure that stores elements along
with their associated priorities. It allows efficient retrieval of the
element with the highest (or lowest) priority. The priority
determines the order in which elements are processed or
accessed.

For example, a priority queue can be used to schedule tasks in a


time-critical application. Each task is assigned a priority, and the
tasks are scheduled in order of decreasing priority. This ensures
that the most important tasks are always scheduled first.

Q 6. Explain the concept of dynamic programming and


provide an example problem where it can be applied.
Ans: Dynamic programming is a problem-solving technique that
involves breaking down complex problems into smaller,
overlapping subproblems and solving them in a bottom-up
manner.

Example: knapsack problem, In the knapsack problem, you are


given a set of items, each with a weight and a value, and a
knapsack with a limited capacity. The goal is to find the subset of
items that has the maximum value and that fits in the knapsack.

12
Q 7. How does a HashSet work internally in Java?
Ans: A HashSet internally uses a HashMap to store its elements. When
you add an element to a HashSet, it is first hashed using the
hashCode() method.

The hash code is then used to find the corresponding bucket in


the HashMap. If the bucket is empty, the element is added to the
bucket. If the bucket is not empty, the element is compared to
the other elements in the bucket using the equals() method. If the
element is equal to any of the other elements in the bucket, it is
not added to the HashSet.

Q 8. What is the time complexity of various operations in a


hash table?
Ans: The time complexity of various operations in a hash table
depends on the hash function used and the number of elements
in the hash table. In general, the time complexity of the following
operations is:
Insertion: O(1) on average, O(n) in the worst case
Search: O(1) on average, O(n) in the worst case
Deletion: O(1) on average, O(n) in the worst case

13
Multi-threading
Q 1. What is multithreading, and why is it important in
Java?
Ans: Multithreading is a programming concept that allows multiple
tasks to be executed concurrently. In Java, multithreading is
implemented using the Thread class. A Thread object represents
a single thread of execution.

There are many reasons why multithreading is important in Java.


Some of the most important reasons include:
Increased performance:
Improved responsiveness:
Reduced resource usage:

Q 2. How can you create a thread in Java?


Ans: There are two ways to create a thread in Java:
By extending the Thread class
By implementing the Runnable interface

Q 3. What is the difference between a process and a


thread?
Ans: A process is a program in execution. It has its own memory space,
its own stack, and its own set of resources.

A thread is a lightweight process that shares the same memory


space and resources as other threads in the same process.

Some of the key differences between processes and threads:


Processes are independent of each other
Processes are heavier than threads.
Processes are more difficult to create and manage than
threads.

14
Q 4. How does synchronization work in Java? Explain the
concepts of synchronized methods and blocks.
Ans: Synchronization in Java is a mechanism that allows multiple
threads to access shared resources safely. When a thread is
synchronized on a resource, it is the only thread that can access
that resource.

This prevents race conditions, which are situations where two or


more threads are trying to access the same resource at the same
time.

There are two ways to synchronize in Java:


Using synchronized methods
Using synchronized blocks

Synchronized methods
A synchronized method is a method that can only be executed by
one thread at a time. To declare a method as synchronized, you
need to use the synchronized keyword.

Synchronized blocks
A synchronized block is a block of code that can only be executed
by one thread at a time. To declare a block of code as
synchronized, you need to use the synchronized keyword and
specify the object that the block is synchronized on.

Q 5. What is a deadlock, and how can it be avoided?


Ans: A deadlock is a situation where two or more threads are waiting
for each other to finish. This can happen when two threads are
each trying to acquire a lock on the same resource.

To avoid deadlocks, we can do this:


Avoid using locks unnecessarily.
Use locks in a consistent order.
Use deadlock detection and prevention tools.

15
Q 6. What are the differences between the Thread class and
the Runnable interface in Java?
Ans: The Thread class is a concrete class. while the Runnable interface
is an abstract interface. This means that you can create a new
thread by extending the Thread class, or you can create a new
thread by implementing the Runnable interface.
The key differences between the Thread class and the Runnable
interface:

Feature Thread class Runnable interface

Type Concrete class Abstract interface

Inheritance Can be extended Cannot be extended

Implementation Must override the run() method Must implement the run() method

Memory usage More memory is required Less memory is required

Flexibility Less flexible More flexible

Q 7. What is the purpose of the volatile keyword in Java?


Ans: The volatile keyword is used to ensure that all threads see the
same value of a variable, even if the value is changed by another
thread.

Q 8. Explain the difference between preemptive scheduling


and time-slicing in the context of thread scheduling.
Ans: Preemptive scheduling is when the operating system can forcibly
remove a thread from the CPU and give it to another thread.
Time-slicing is when each thread is given a certain amount of time
to run on the CPU.

The main difference is that in preemptive scheduling, the


operating system can interrupt a thread at any time, while in
time-slicing, the thread is only interrupted when it has used up its
allotted time.
16
Exception Handling
Q 1. What is an exception in Java, and why is exception
handling important?
Ans: In Java, an exception is an event that occurs during the execution
of a program that disrupts the normal flow of instructions. It is an
object which is thrown at runtime.

Here are some of the benefits of exception handling:


Prevents program crashes
Allows you to recover from errors
Provides information about the error
Makes your code more robust
Makes your code easier to read and understand

Q 2. How does Java handle exceptions.


Ans: Java handles exceptions by using a mechanism called exception
propagation. When an exception is thrown, it is propagated up
the call stack until it is caught. If the exception is not caught, the
program will crash.

17
Q 3. Describe the try-catch-finally block and its purpose in
exception handling.
Ans: The try-catch-finally block is a Java syntax that allows you to
handle exceptions gracefully. It consists of three parts:
The try block
The catch block
The finally block

Here are some of the benefits of using try-catch-finally blocks:


Prevents program crashes
Allows you to recover from errors
Provides information about the error
Makes your code more robust
Makes your code easier to read and understand

Q 4. What is the difference between the throw and throws


keywords in Java?
Ans: The throw and throws keywords in Java are used to handle
exceptions.

The throw keyword is used to explicitly throw an exception


The throws keyword is used to declare that a method can
throw an exception.
Q 5. How can you create custom exceptions in Java?
Ans: To create a custom exception in Java, you need to create a class
that extends the Exception class. The custom exception class can
have its own constructors, methods, and fields.

18

You might also like