0% found this document useful (0 votes)
25 views4 pages

Java Test Solutions

The document provides solutions to a Java programming class test covering key concepts such as the `super` keyword in inheritance, exception handling mechanisms, multithreading, and the differences between abstract classes and interfaces. It includes code examples for multithreading and custom exceptions, as well as a comparison table highlighting the features of abstract classes versus interfaces. Additionally, it emphasizes the importance of the `finally` block in exception handling for cleanup tasks.

Uploaded by

neerajmajila257
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Java Test Solutions

The document provides solutions to a Java programming class test covering key concepts such as the `super` keyword in inheritance, exception handling mechanisms, multithreading, and the differences between abstract classes and interfaces. It includes code examples for multithreading and custom exceptions, as well as a comparison table highlighting the features of abstract classes versus interfaces. Additionally, it emphasizes the importance of the `finally` block in exception handling for cleanup tasks.

Uploaded by

neerajmajila257
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Programming Class Test Solutions

Q1. Explain the role of the `super` keyword in Java inheritance...


The `super` keyword in Java refers to the parent class object. It helps in:

1. Accessing parent class methods and variables.

2. Calling parent class constructor using `super()`.

**Method Overriding**: A subclass provides a specific implementation of a method already


defined in the parent class.

**Constructor Chaining**: The `super()` call ensures the parent constructor is executed first
in multilevel inheritance.

**In Multilevel Inheritance**, `super` assists in managing hierarchy by reusing and


accessing parent-level logic.

Q2. Describe how Java's exception handling mechanism works...


Java provides structured exception handling using `try`, `catch`, `throw`, and `finally`.

- `try`: Code that may raise exceptions.

- `catch`: Handles specific exceptions.

- `finally`: Executes always, used for cleanup.

- `throw`: Throws a specific exception.

**Custom Exception**:

```java

class MyException extends Exception {

public MyException(String msg) {


super(msg);

```

**Package-protected use**: Controls visibility. Internal logic is hidden using package-


private exceptions.

**Access protection**: Ensures modularity and safe use across packages.

Q3. Develop a multithreaded Java application...


```java

class EvenThread extends Thread {

public void run() {

for (int i = 2; i <= 20; i += 2) {

System.out.println("Even: " + i);

class OddThread extends Thread {

public void run() {

for (int i = 1; i <= 20; i += 2) {

System.out.println("Odd: " + i);

}
public class Main {

public static void main(String[] args) {

new EvenThread().start();

new OddThread().start();

```

Q4. Compare and contrast abstract classes and interfaces...


| Feature | Abstract Class | Interface |

|--------|----------------|-----------|

| Methods | Abstract + Concrete | Only Abstract (default since Java 8) |

| Variables | Instance allowed | Only static final |

| Constructor | Yes | No |

| Inheritance | Single | Multiple |

**Use abstract class** when shared code is needed.

**Example**:

```java

abstract class Animal {

abstract void sound();

void eat() { System.out.println("Eats"); }

class Dog extends Animal {


void sound() { System.out.println("Barks"); }

```

**Method overriding** allows subclasses to define specific behavior.

Q5. Exception Handling and the Finally Block...


The `finally` block is always executed after `try` or `catch`. It is used for cleanup tasks.

**Scenario**:

```java

try {

FileInputStream f = new FileInputStream("data.txt");

// operations

} catch (IOException e) {

System.out.println("Error");

} finally {

f.close(); // Cleanup

```

**Access protection**: Exception classes should be `public` if accessed from other packages.

Try, catch, and finally together ensure clean error handling and recovery across different
classes and packages.

You might also like