Winter 2022 Solution - Java
Winter 2022 Solution - Java
Answer:
1. Platform Independent:
● Bytecode allows Java programs to run on any platform with a
Java Virtual Machine (JVM) installed, enabling "write once,
run anywhere" portability.
2. Execution Policy:
● The JVM executes bytecode efficiently, employing
optimizations like JIT compilation and garbage collection.
3. Security:
z
● Bytecode verification ensures code adheres to security
aa
constraints, enhancing Java's security and preventing
malicious code execution.
Aw
ut
gr
Ja
Answer:
z
● The garbage collection process is automatic and runs in the
aa
background without any intervention from the programmer.
● Java uses a mark and sweep algorithm to identify objects that are
Aw
no longer being referenced by the program.
● This algorithm works by first marking all objects that are still being
referenced and then sweeping through the heap and releasing the
ut
Answer:
z
3. Polymorphism: The ability to use a single interface to represent
aa
multiple types of objects.
4. Abstraction: The ability to focus on the essential features of an
w
object, ignoring its implementation details.
A
Inheritance:
ut
gr
class Animal {
void makeSound() {
[Link]("The animal makes a sound");
}
}
z
public static void main(String[] args) {
Animal animal = new Animal(); aa
[Link](); // Output: The animal makes a sound
w
Dog dog = new Dog();
A
}
gr
● In this example, the Animal class is the parent class, and the Dog
class is the child class that extends the Animal class.
Ja
Answer:
Constructor:
Example:
z
private String name;
private int age; aa
w
public Person(String name, int age) {
[Link] = name;
tA
[Link] = age;
[Link]("A new person has been created!");
}
u
gr
}
}
Output:
Answer:
● length()
● substring()
● equals()
● toUpperCase()
● toLowerCase()
● indexOf()
z
● charAt()
● concat()
aa
Aw
1. length(): This method returns the length of the string, which is the
number of characters in the string.
ut
Example:
gr
Example:
Answer:
1. Public:
Public members are accessible to all classes, regardless of
the package they belong to. Public members can be
z
accessed by any other class in the same program.
aa
2. Private:
Aw
Private members are only accessible within the same class.
They cannot be accessed by any other class, even if it
belongs to the same package.
ut
3. Protected:
gr
z
aa
Aw
ut
gr
Ja
OR
Answer:
String StringBuffer
String is an immutable class, StringBuffer, on the other hand, is
which means that once a string a mutable class, which means that
object is created, its value cannot its value can be changed after it is
be changed. created.
String objects are inherently
z
thread-safe, as they are StringBuffer, on the other hand,
immutable and can be safely
shared across multiple threads
aa
provides synchronised methods,
making it thread-safe.
without synchronisation.
Aw
substring, indexOf)
append, insert, replace)
Ja
z
char c = 'l';
aa
int count = countOccurrences(str, c);
[Link]("The character '" + c + "' occurs " +
count + " times in the string '" + str + "'");
Aw
}
}
ut
Output:
gr
Ja
Answer:
● There are a few reasons why you might want to make a class final:
z
performance in heavily used or performance-critical code,
aa
since the compiler can make certain optimizations that would
not be possible if the class could be subclassed.
Aw
3. Clarity: Final classes can also be used to make code more
clear and readable by indicating that a class is not intended
to be subclassed, which can help prevent errors or
ut
confusion.
gr
Ja
Answer:
z
handle different types of exceptions.
aa
2. throw: The throw statement is used to manually throw an
exception. It is typically used in situations where a specific
Aw
condition is met and the program needs to signal an exceptional
situation. The throw statement is followed by an instance of an
exception class.
ut
Answer:
1. Overloading:
z
which can include differences in the number, type, or order of
aa
parameters. During compilation, the appropriate method is
chosen based on the arguments provided at the call site.
A w
Example:
ut
return a + b;
Ja
// Usage:
Calculator calculator = new Calculator();
int result1 = [Link](2, 3); // Calls the
first add method
int result2 = [Link](2, 3, 4); // Calls the
second add method
Example:
z
[Link]("Animal makes a sound.");
}
} aa
Aw
public class Cat extends Animal {
@Override
public void makeSound() {
ut
[Link]("Meow!");
}
gr
}
Ja
// Usage:
Animal animal = new Animal();
[Link](); // Output: Animal makes a sound.
OR
Answer:
z
aa
Aw
ut
gr
Ja
Answer:
Inheritance:
1. Single inheritance
z
2. Multilevel inheritance
3. Hierarchical inheritance
aa
4. Multiple inheritance
Aw
Example:
ut
class Vehicle {
void start() {
gr
[Link]("Vehicle started.");
Ja
}
}
Answer:
class Vehicle {
String brand;
z
Vehicle(String brand) {
[Link] = brand;
aa
Aw
}
void start() {
[Link]("Vehicle started.");
ut
}
}
gr
String model;
void displayDetails() {
[Link]("Brand: " + [Link]); //
Accessing superclass member
[Link]("Model: " + [Link]);
}
}
class MathUtils {
static final double PI = 3.14159; // Static constant
z
static int add(int a, int b) { // Static method
}
return a + b; aa
}
Aw
class Circle {
final double PI = 3.14159; // Final constant
z
public class Main {
aa
public static void main(String[] args) {
Circle circle = new Circle();
[Link](2.5);
Aw
}
}
ut
gr
Ja
class Circle {
final double PI = 3.14159; // Final constant
z
public class Main { aa
public static void main(String[] args) {
Aw
Circle circle = new Circle();
[Link](2.5);
}
}
ut
gr
Ja
Answer:
1. BorderPane
2. HBox
3. VBox
4. StackPane
5. GridPane
6. FlowPane
7. TilePane
z
8. AnchorPane
9. Pane aa
Aw
ut
gr
Ja
Answer:
z
language that separates the presentation of the user interface from
aa
the logic of the application. Developers can create custom styles
for each node in the Scene Graph, or use pre-defined styles from
Aw
the JavaFX library.
3. Event Handling: JavaFX provides a rich set of event handlers that
allow developers to handle user input, such as mouse clicks and
ut
user actions.
4. Animation: JavaFX provides a powerful animation framework that
Ja
Answer:
1. BufferedInputStream:
z
such as bytes or characters.
Example:
aa
A w
import [Link].*;
ut
int data;
while ((data = [Link]()) != -1)
{
[Link]((char) data);
}
} catch (IOException e) {
[Link]();
}
}
}
Example:
import [Link].*;
z
public class BufferedOutputStreamExample {
aa
public static void main(String[] args) {
try (BufferedOutputStream bufferedOutputStream = new
Aw
BufferedOutputStream(new FileOutputStream("[Link]"))) {
String data = "Hello, World!";
[Link]([Link]());
} catch (IOException e) {
ut
[Link]();
}
gr
}
}
Ja
OR
Answer:
z
9. TableView: A control that displays data in a tabular format.
aa
10. DatePicker: A control that allows the user to select a date.
Aw
ut
gr
Ja
Answer:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
z
@Override
public void start(Stage primaryStage) {
// Create a rectangle
aa
Rectangle rectangle = new Rectangle(50, 50, [Link]);
Aw
TranslateTransition([Link](2), rectangle);
[Link](250); // Move the rectangle to
gr
[Link]([Link]);
// Repeat the animation indefinitely
z
aa
Aw
ut
gr
Ja
Answer:
import [Link].*;
import [Link];
import [Link];
z
private String name;
private String address; aa
Aw
public Student(int id, String name, String address) {
[Link] = id;
[Link] = name;
[Link] = address;
ut
}
gr
class StudentManager {
private static final String FILE_NAME = "[Link]";
saveStudentsToFile(students);
List<Student> loadedStudents = loadStudentsFromFile();
[Link]("Loaded students:");
for (Student student : loadedStudents) {
[Link]([Link]() + " " +
[Link]() + " " + [Link]());
}
}
z
{
try (FileOutputStream fos = new
FileOutputStream(FILE_NAME);
aa
Aw
ObjectOutputStream oos = new ObjectOutputStream(fos))
{
[Link](students);
} catch (IOException e) {
ut
[Link]();
}
gr
}
Ja
Answer:
z
larger Collections Framework, which includes the Collection,
aa
List, Set, Queue, and Map interfaces, along with their
respective implementations and utility classes.
Aw
2. Dynamic Data Structures: Java Collection provides
dynamic data structures such as ArrayList, LinkedList,
HashSet, TreeSet, HashMap, and TreeMap, among others.
ut
requirements.
3. Standardised API: Java Collection offers a standardised
Ja
Answer:
1. boolean hasNext():
z
● This method checks if there is a next element in the
aa
collection. It returns true if there is at least one more
element to iterate over, and false otherwise.
Aw
2. E next():
ut
3. void remove():
Answer:
1. Set:
● A Set is an interface in the Java Collections Framework that
represents a collection of unique elements.
● It does not allow duplicate values, and the elements are
unordered.
● The Set interface is implemented by classes such as
HashSet, TreeSet, and LinkedHashSet.
Example:
z
import [Link];
import [Link];
aa
w
public class SetExample {
A
[Link]("Apple");
Ja
[Link]("Banana");
[Link]("Orange");
// Duplicate element, will not be added
[Link]("Apple");
Example:
import [Link];
import [Link];
z
Map<String, Integer> ages = new HashMap<>();
aa
// Adding key-value pairs to the map
[Link]("John", 25);
Aw
[Link]("Jane", 30);
[Link]("Bob", 28);
ut
[Link]("John"));
Ja
OR
Answer:
z
resizes itself as elements are added or removed.
aa
● However, it is important to note that the Vector class is considered
to be less efficient than the ArrayList class because of its
Aw
synchronised nature.
● Therefore, it is recommended to use ArrayList instead of Vector in
most cases, unless synchronisation is specifically required.
ut
gr
Ja
Answer:
The life cycle of a thread in Java can be described using the following
diagram:
z
aa
Aw
1. New: The thread is in the new state when it is created, but it has
not yet started to execute.
2. Runnable: When the start() method is called on a thread object, it
ut
enters the runnable state. In this state, the thread is ready to run,
but the operating system has not yet assigned a processor to it.
gr
thread, it enters the running state. The thread executes the code in
its run() method.
4. Blocked: A thread enters the blocked state when it is waiting for a
monitor lock to be released, such as when it is waiting to enter a
synchronised block.
5. Waiting: A thread enters the waiting state when it is waiting for
another thread to perform a particular action, such as when it calls
the wait() method.
6. Timed Waiting: A thread enters the timed waiting state when it is
waiting for a specified period of time, such as when it calls the
sleep() method.
7. Terminated: When a thread completes its run() method or throws
an unhandled exception, it enters the terminated state and cannot
be restarted.
Answer:
z
Example: aa
w
class Counter {
A
}
Ja
z
[Link]();
[Link](); aa
Aw
[Link]();
[Link]();
}
}
gr
Ja