Summer 2027 Solution - Java
Summer 2027 Solution - Java
Answer:
z
specific entity and encapsulates both data (state) and behaviours
aa
(methods). Objects are created based on the defined class and
can interact with each other.
Aw
3. Inheritance: In Java, inheritance is a mechanism that allows a
class to inherit properties and behaviours from another class. The
subclass (derived class) inherits the attributes and methods of the
ut
z
automatically called when an object of a class is created. It
aa
initialises the object's state and performs necessary setup
operations.
Aw
Answer:
StringBuffer StringBuilder
z
Suitable for multithreaded
environment
aa
Requires external synchronisation
in threads
Aw
More memory overhead due to
Less memory overhead
internal synchronisation
Used when thread safety is Used when thread safety is not
ut
required required
gr
Ja
Answer:
z
aa
● How Objects are Constructed: To construct an object, you use
the new keyword followed by a call to the constructor of the class.
Aw
The constructor allocates memory for the object and initialises its
data members. The general syntax for creating an object and
invoking a constructor is as follows:
ut
gr
class Car {
String brand;
String color;
int year;
// Default constructor
Car() {
brand = "Unknown";
color = "Unknown";
year = 0;
}
// Constructor with brand and year parameters
Car(String carBrand, int carYear) {
z
brand = carBrand;
color = "Unknown";
year = carYear;
aa
}
w
// Constructor with brand, color, and year parameters
A
color = carColor;
year = carYear;
gr
}
}
Ja
Answer:
● Types of Arrays:
z
aa
1. Single-Dimensional Array: A single-dimensional array is
the most common type of array. It stores elements in a linear
Aw
fashion, allowing access to elements using a single index.
2. Multi-Dimensional Array: A multi-dimensional array is an
array of arrays. It represents a tabular or matrix-like structure
ut
● Array Methods:
Ja
Answer:
1. Encapsulation:
● Encapsulation is a mechanism that combines data and the
methods (or functions) that operate on that data into a single
unit called a class.
● It allows for the bundling of related data and functionality,
hiding the internal details of how the class works from the
outside world.
● The key idea is to provide a public interface (public methods)
through which external code can interact with the class, while
z
keeping the internal implementation details hidden and
aa
protected (private or protected data members and methods).
● Encapsulation provides benefits such as data protection,
Aw
code maintainability, and flexibility in modifying the internal
implementation without affecting the external code that uses
the class.
ut
2. Abstraction
gr
Answer:
z
● Favour composition over inheritance: Use composition to
aa
build complex classes by combining simpler, reusable
components.
Aw
● Keep class dependencies minimal: Minimise
dependencies between classes to reduce coupling and
improve flexibility.
ut
z
Example: aa
w
class Counter {
A
Counter() {
count++;
gr
}
}
Ja
Example:
class MathUtils {
static int multiply(int a, int b) {
return a * b;
}
}
z
public class Main {
public static void main(String[] args) {
aa
int result = MathUtils.multiply(5, 3);
System.out.println(result); // Output: 15
Aw
}
}
ut
gr
Ja
OR
Answer:
1. Inheritance:
z
● The class that is being inherited from is called the superclass
aa
or parent class, and the class that inherits is called the
subclass or child class.
w
● Inheritance is denoted by the "extends" keyword.
A
Example:
ut
gr
class Animal {
void eat() {
Ja
System.out.println("Animal is eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking...");
}
}
Example:
class Animal {
void makeSound() {
z
System.out.println("Animal is making a
sound...");
}
aa
w
}
A
void makeSound() {
System.out.println("Dog is barking...");
gr
}
}
Ja
Answer:
1. Manipulation Methods:
● These methods modify or manipulate the content of the
string.
● Examples: substring(), concat(), replace(), toLowerCase(),
toUpperCase().
● They enable operations like extracting substrings,
concatenating strings, replacing characters, and changing
the case of characters.
2. Comparison Methods:
z
● These methods are used to compare strings for equality or to
determine their ordering. aa
● Examples: equals(), equalsIgnoreCase(), compareTo(),
Aw
compareToIgnoreCase().
● They allow comparing strings based on their content,
ignoring case differences, or based on lexicographic
ut
ordering.
3. Searching and Matching Methods:
gr
Answer:
Access Specifiers:
z
encapsulation.
aa
4. Default (Package-private): Limits access to only within the same
package, maintaining package-level encapsulation.
Aw
Modifiers:
ut
class extension.
Ja
Answer:
Exception:
z
● Exceptions allow programmers to handle errors gracefully and take
appropriate actions.
aa
Aw
Exception Hierarchy:
1. Checked Exceptions:
Example:
z
aa
2. Catching Exceptions: To catch exceptions, use the try-catch
block. The try block encloses the code that may throw an
Aw
exception. The catch block catches and handles the thrown
exception.
Example:
ut
gr
try {
// Code that may throw an exception
Ja
} catch (IOException e) {
// Handling the IOException
}
Example:
try {
// Code that may throw an exception
} catch (IOException e) {
// Handling the IOException
} catch (NullPointerException e) {
// Handling the NullPointerException
} finally {
// Code that always executes
z
}
aa
Aw
ut
gr
Ja
OR
Answer:
z
immutable variable, is a field that cannot be modified once it is
aa
initialised. It is marked with the final keyword. Once a final field is
assigned a value, it cannot be changed. Final fields are typically
Aw
used to define constants or values that should remain unchanged
throughout the program's execution.
ut
Answer:
z
aa
1. Modularity and Organization: Packages allow for logical
grouping of classes, making it easier to manage and maintain
code. It promotes code reusability and enhances the overall
Aw
classes and their members. Classes within the same package can
gr
z
public class MyClass {
}
// Class implementation aa
Aw
4. Compilation and Execution: Compile the Java file using the
javac command, ensuring that the directory structure matches the
package hierarchy. To execute the compiled class, use the fully
ut
Answer:
Inner Class:
z
Types of Inner Classes:
aa
Aw
1. Member Inner Class:
private members.
Ja
z
Example: aa
w
public class OuterClass {
A
}
}
z
NestedClass nested = new NestedClass();
nested.display(); aa
Aw
LocalClass local = new LocalClass();
local.display();
runnable.run();
ut
}
gr
Answer:
Dynamic Binding:
Example:
z
class Animal {
aa
public void makeSound() {
w
System.out.println("Animal makes a sound");
A
}
}
ut
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.makeSound(); // Output: Dog barks
animal2.makeSound(); // Output: Cat meows
}
}
Answer:
z
● Reader and Writer: For reading and writing character data.
aa
2. Byte Streams (InputStream/OutputStream): Byte-oriented
Aw
streams are used for reading and writing raw binary data, like files
or images. Examples: FileInputStream, FileOutputStream,
BufferedInputStream, BufferedOutputStream.
ut
BufferedWriter.
Answer:
1. New: The thread is in the new state when it is created but has not
yet started.
2. Runnable: The thread is in the runnable state when it is ready to
run and waiting for its turn on the CPU.
3. Blocked: The thread is in the blocked state when it is waiting for a
z
monitor lock to enter a synchronised block or waiting for I/O
operations to complete.
aa
4. Waiting: The thread is in the waiting state when it waits indefinitely
Aw
until another thread notifies it to resume execution.
5. Timed Waiting: The thread is in the timed waiting state when it
waits for a specific amount of time before resuming execution.
ut
Thread Properties:
z
coupled with inter-thread communication mechanisms, such as
aa
wait(), notify(), and notifyAll(), to enable threads to communicate
and coordinate their activities.
A w
ut
gr
Ja
OR
Answer:
Finalization:
z
● It allows for releasing resources, closing connections, or
performing other cleanup tasks. aa
● However, it's important to note that the use of finalize() is
Aw
discouraged due to its unpredictable execution timing and potential
performance impact.
● It is recommended to use alternative resource management
ut
Answer:
z
aa
1. Introspection: Reflection allows you to obtain information about
classes, interfaces, methods, and fields at runtime. You can
Aw
access class metadata, such as its name, superclass,
implemented interfaces, constructors, and methods.
2. Dynamic Class Instantiation: Reflection enables the creation of
ut
runtime.
3. Method Invocation: Reflection allows you to invoke methods
dynamically based on their names, even if the methods are not
known at compile time. You can obtain method references, set
their accessibility, and invoke them using different arguments.
4. Field Manipulation: Reflection allows you to access and modify
field values dynamically, including private fields. You can read and
set field values using reflection, which is useful for scenarios
where direct field access is not possible or desirable.
5. Annotations and Annotations Processing: Reflection enables
the inspection of annotations at runtime. You can retrieve
annotation details, check for their presence, and use them to
perform custom processing or apply runtime behaviors.
Answer:
interface Circle {
double calculateArea();
}
interface Square {
double calculateArea();
}
z
aa
class AreaCalculator implements Circle, Square {
private double radius;
private double side;
Aw
this.side = side;
}
gr
@Override
Ja
z
aa
Aw
ut
gr
Ja
Answer:
Callback:
z
aa
Aw
ut
gr
Ja
Answer:
Interface:
z
Example:
// Interface definition
aa
w
interface Animal {
void makeSound(); // Abstract method declaration
A
}
ut
// Implementing class
gr
// Implementing class
class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Cat meows");
}
}
z
aa
Aw
ut
gr
Ja
Answer:
z
aa
1. Code Reusability: Generics allow the creation of generic
algorithms and data structures that can be used with different
Aw
types of data, eliminating the need to write duplicate code for each
specific type.
2. Type Safety: Generics provide compile-time type checking,
ut
class Box<T> {
private T item;
public T getItem() {
return item;
}
}
z
public static void main(String[] args) {
aa
Box<Integer> integerBox = new Box<>();
integerBox.setItem(10);
int value = integerBox.getItem(); // No casting needed
A w
Box<String> stringBox = new Box<>();
stringBox.setItem("Hello");
ut
}
Ja
z
they can be used with generic methods in a static context.
aa
A w
ut
gr
Ja
OR
Answer:
z
must provide. Interfaces allow for abstraction, multiple inheritance
aa
of type, and promote loose coupling between components.
Aw
3. Methods: Methods in Java are blocks of code that define the
behaviour of a class. They represent actions or operations that an
object can perform. Methods encapsulate logic and allow for code
ut
reuse and modular design. They can have parameters and return
values, and their visibility can be controlled with access modifiers.
gr
Ja
Answer:
z
events can include clicking, dragging, moving, and releasing the
aa
mouse. These events can be captured and handled in a Java
program to perform specific actions or trigger specific behavior.
A w
Example:
ut
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
gr
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked at (" + e.getX() + ", "
+ e.getY() + ")");
}
}
mouseClickListener.mouseClicked(mouseEvent);
}
}
z
aa
Aw
ut
gr
Ja
Answer:
z
aa
Aw
ut
gr
Ja
z
entering data.
aa
● The view notifies the controller of the user action.
● The controller updates the model based on the user action.
Aw
● The model updates its data and notifies the view(s) of the
changes.
● The view retrieves the updated data from the model and refreshes
ut
the UI.
gr
z
aa
Aw
ut
gr
Ja