Oops Answer Key
Oops Answer Key
PART A
Java
The StackPane layout places its children nodes on top of each other in the order they are
added. The last node added will be on top.
PART B
11. a) Write a Java program to demonstrate the use of constructors. Explain the concept of
default and parameterized constructors with examples.(13)
Java
class Employee {
String name;
int id;
// Default constructor
Employee() {
name = "Default";
id = 0;
}
// Parameterized constructor
Employee(String name, int id) {
this.name = name;
this.id = id;
}
void display() {
System.out.println("Name: " + name + ", ID: " + id);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
Employee emp1 = new Employee(); // Using default constructor
emp1.display();
11. b) Explain the different types of data types available in Java. Give examples of
primitive data types and reference data types.(13)
Primitive data types: Represent basic data values. Examples: int, double, char, boolean.
Reference data types: Represent objects (instances of classes). Examples: String,
Employee, ArrayList.
Note: The answer key for Part B is based on the prompt provided and the image of the question
paper. The actual answer key may vary depending on the specific requirements and expectations
of the instructor or examination board.
Definition: Method overloading is the ability to define multiple methods with the same
name but different parameters in a class. This allows you to create methods that perform
similar tasks but with varying input arguments.
Example:
Java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
12 b) Interfaces(13)
Definition: Interfaces in Java are blueprints for classes. They define a set of methods that
a class must implement to adhere to the interface contract.
Difference from Abstract Classes:
o Interfaces can only have abstract methods (no method bodies), while abstract
classes can have both abstract and concrete methods.
o Interfaces cannot have instance variables, while abstract classes can.
o A class can implement multiple interfaces but can only extend one abstract class.
Role in Multiple Inheritance: Interfaces enable multiple inheritance in Java, as a class
can implement multiple interfaces, effectively inheriting behavior from multiple sources.
Example:
Java
interface Drawable {
void draw();
}
13 a) Built-in Exceptions(13)
Definition: Built-in exceptions are predefined classes in Java that represent exceptional
conditions that can occur during program execution.
Examples:
o NullPointerException: Thrown when trying to access a member of an object that
is null.
o ArithmeticException: Thrown when an arithmetic operation fails, such as division
by zero.
o IOException: Thrown when an input/output operation fails, such as reading from
or writing to a file.
Definition: Autoboxing is the automatic conversion of primitive types (like int, double,
etc.) to their corresponding wrapper class objects (like Integer, Double, etc.). Unboxing is
the reverse process.
Example:
Java
int num = 10; // Primitive type
Integer numObject = num; // Autoboxing
int num2 = numObject; // Unboxing
14 a) Generic Methods(13)
Definition: Generic methods are methods that can work with different data types. They
are defined using type parameters (like <T>) to represent the type of data they can
handle.
Example:
Java
public class GenericExample {
public static <T> T findMax(T[] array) {
T max = array[0];
for (T element : array) {
// Assuming a Comparable interface is implemented for T
if (element.compareTo(max) > 0) {
max = element;
}
}
return max;
}
}
Process:
1. Create a File object representing the file to be read or written.
2. Create a FileReader or FileWriter object to read or write data to the file.
3. Use a BufferedReader or BufferedWriter to efficiently read or write data in
chunks.
4. Close the streams after finishing the operations.
Example:
Java
import java.io.*;
Definition: Mouse events represent user interactions with the mouse, such as clicking,
dragging, and hovering.
Handling: You can handle mouse events using event listeners attached to nodes in a
JavaFX scene.
Examples:
o MouseEvent.MOUSE_CLICKED: Occurs when the mouse button is pressed and
released.
o MouseEvent.MOUSE_PRESSED: Occurs when the mouse button is pressed.
o MouseEvent.MOUSE_DRAGGED: Occurs when the mouse is dragged while a
button is pressed.
15 b) Menus in JavaFX(13)
Creating Menus:
1. Create a MenuBar object.
2. Create Menu objects for each main menu item.
3. Create MenuItem objects for each sub-item within a menu.
4. Add MenuItem objects to their respective Menu objects.
5. Add Menu objects to the MenuBar.
Example:
Java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
16 a) Thread Control(15)
Suspending: Temporarily pausing the execution of a thread without terminating it. This
can be achieved using methods like Thread.suspend(). However, Thread.suspend() is
discouraged in modern Java due to potential deadlocks.
Resuming: Restarting the execution of a suspended thread. This is done using
Thread.resume().
Stopping: Terminating the execution of a thread completely. This can be achieved using
Thread.stop(), but it is also discouraged as it can lead to unpredictable behavior.
Example:
Java
public class ThreadControlExample {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
Java
class InvalidStringException extends Exception {
public InvalidStringException(String message) {
super(message);
}
}
Java
public class CustomExceptionExample {
public static void main(String[] args) {
String input = "Hello, world!";
try {
validateString(input);
System.out.println("String is valid.");
} catch (InvalidStringException e) {
System.out.println("Error: " + e.getMessage());
}
}