DCA7102 Java
DCA7102 Java
ROLL NO 2314503637
SUBJECT PROGRAMMING IN JAVA
SEMSTER III
COURSE MCA
Answer of Question 1:
1) Platform Freedom
a) How it works:
At the point when you compose a Java program, the Java compiler (javac) changes
over your source code into bytecode. This bytecode is then deciphered by the JVM
introduced on any gadget, no matter what the hidden engineering.
b) Advantages:
ii) Saves time and exertion while conveying applications across different stages.
Once compiled, the .class file (bytecode) can run on any JVM, whether on Windows,
macOS, or Linux.
1) Object-Oriented
Java is an object-oriented programming language, which means it is based on real-world
entities such as objects and classes. The primary principles of OOP—Inheritance,
Polymorphism, Abstraction, and Encapsulation—are seamlessly integrated into Java.
a) Core Principles:
i) Encapsulation: Bundling data (variables) and code (methods) together.
ii) Inheritance: Reusing code by inheriting properties from parent classes.
iii) Polymorphism: Writing one interface or method to represent multiple
implementations.
iv) Abstraction: Hiding complex implementation details and showing only essential
features.
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
2. Robust
b) Exception Taking care of: Java gives a strong mistake dealing with system
through attempt, get, at long last, and toss develops.
Output:
3. Secure
5. Multithreading
Benefits:
Example:
5. Ternary Operator
Example:
int x = 5;
int y = (x > 3) ? 10 : 20;
System.out.println(y); // Output: 10
Answer of Question 3
Array: An exhibit in Java is an information structure that permits you to store numerous
upsides of similar information type in a solitary, bordering block of memory. Exhibits are
helpful when you really want to deal with an assortment of information components, like
a rundown of numbers, strings, or items, without proclaiming separate factors for each.
Clusters make it more straightforward to repeat through, control, and access information
proficiently.
Key Highlights of Arrays:
1. Homogeneous Components:
Exhibits can store components of similar information type. For instance, a whole number
exhibit can store numbers.
2. Fixed Size:
When a cluster is proclaimed, its size can't be changed. For example, a variety of size 5
can accumulate to 5 components.
3. Indexed Access:
Exhibits utilize zero-based ordering, meaning the primary component is gotten to with
record 0, the second with 1, etc.
4. Random Access:
Any component in a cluster can be gotten to straightforwardly utilizing its record, making
recovery quick and effective.
5. Memory Effectiveness:
Clusters store components in bordering memory areas, which makes them memory-
proficient.
Pronouncing and Instating a Cluster:
In Java, a cluster can be proclaimed and introduced in two stages or consolidated into
one.
Declaration:
Syntax:
dataType [] arrayName;
Example:
int[] numbers;
Initialization:
Syntax:
Example:
Example:
Example of an Array:
Here’s a practical example that describe the declaration, initialization, and use of an array:
Output:
First Element: 10
Array Length: 5
Array Elements:
Index 0: 10
Index 1: 20
Index 2: 30
Index 3: 40
Index 4: 50
Benefits of Clusters:
1. Easy Access: Components can be gotten to straightforwardly utilizing their record.
2. Code Streamlining: Clusters lessen the above of pronouncing different factors.
3. Iterative Handling: Utilizing circles, you can proficiently deal with all components in
a cluster.
Impediments of Clusters:
1. Fixed Size: The size of an exhibit not set in stone at the hour of its creation and can't
be changed powerfully.
2. Homogeneous Information: Clusters can hold components of one information type.
Set 2
Answer of Problem 4
Errors in Java: Blunders in Java are difficult issues that happen at runtime and are in many
cases outside the control of the application. These are normally connected with the climate
where the application is running.
1. Nature: Errors are by and large brought about by the working framework, equipment
issues, or the Java Virtual Machine (JVM).
Models:
• Out of Memory Blunder: Tossed when the JVM runs out of memory.
• Stack Flood Mistake: Tossed when as far as possible is surpassed,
frequently because of profound or boundless recursion.
• Virtual Machine Blunder: Demonstrates an inward mistake in the JVM.
2. Handling: Mistakes are not intended to be gotten or dealt with utilizing attempt get
blocks, as they address unrecoverable issues.
Special cases: Exemptions are occasions that happen during program execution and disturb
the ordinary progression of the program. They are regularly brought about by sensible
blunders or unexpected circumstances in the code.
1. Nature: Exemptions are application-explicit and can frequently be dealt with
automatically.
Models:
• Invalid Pointer Special case: Tossed when a program endeavors to utilize
invalid where an item is required.
• Number juggling Special case: Tossed when an unlawful math activity
happens, like division by nothing.
• Document Not Tracked down Special case: Tossed when a record can't be
found.
2. Handling: Exemptions are dealt with utilizing attempt get blocks, permitting the
program to recuperate and proceed with execution.
Key Contrasts among Mistakes and Special cases:
Aspect Errors Exceptions
Severity Severe, often unrecoverable Less severe, recoverable
Cause Environmental or system- Application logic or
related unforeseen events
Handling Can’t be handled Handled using try-catch
programmatically blocks
Examples Out of Memory Error, Stack Null Pointer Exception IO
Over flow error Exception.
Recovery Program termination is likely Can recover and continue
execution.
Example:
Error vs. Exception
Error Example:
Output:
Exception Example:
Output:
1. Checked Execptions:
• Happen at runtime.
Answer of Question 5
1. Thread Impedance:
• Numerous strings getting to shared assets all the while may bring about
conflicting information.
Example:
class Counter {
private int count = 0;
public void increment() {
count++;
}
public int get Count() {
return count;
}
}
public class Main {
public static void main(String[] args) {
Counter counter = new Counter();
Thread t1 = new Thread(() -> counter.increment());
Thread t2 = new Thread(() -> counter.increment());
t1.start();
t2.start();
System.out.println(counter.getCount()); // Output maybe inconsistent
}
}
2. Data Consistency:
Synchronization ensures only one thread can access the resource at a time.
1. Synchronized Methods:
Syntax:
Example:
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
2. Synchronized Blocks:
Syntax:
synchronized(object) {
// Code block
}
Example:
1. Bank exchanges.
Answer of Question 6
A servlet is a Java program that sudden spikes in demand for a web server, processes
client demands, and produces reactions. The existence pattern of a servlet is overseen
by the servlet compartment and includes the accompanying stages:
• The servlet holder stacks the servlet class and makes an occasion of the
servlet.
• The init technique is called once when the servlet is first stacked to perform
introduction errands.
Sentence structure:
// Instatement code
• The help strategy is required each client demand. It decides the sort of
solicitation (GET, POST, and so on) and agents to the suitable technique.
Sentence structure:
public void service(Http Servlet Solicitation demand, Http Servlet reaction) tosses
Servlet Special case, IO Exemption {
// Demand taking care of code
3. Obliteration (Annihilate Technique): The annihilate strategy is called once when the
servlet is being taken out from administration, considering cleanup undertakings.
Syntax: