0% found this document useful (0 votes)
3 views

1. In Java java 2 class test, the-WPS Office

The document provides an overview of Java inheritance types, including single inheritance and multiple inheritance through interfaces, as well as the use of the `final` keyword. It covers exception handling, thread creation, applet lifecycle, and file operations, including methods for copying files and writing to them. Additionally, it discusses Java's package structure and the hierarchy of stream classes.

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

1. In Java java 2 class test, the-WPS Office

The document provides an overview of Java inheritance types, including single inheritance and multiple inheritance through interfaces, as well as the use of the `final` keyword. It covers exception handling, thread creation, applet lifecycle, and file operations, including methods for copying files and writing to them. Additionally, it discusses Java's package structure and the hierarchy of stream classes.

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

In Java, the types of inheritance supported are single inheritance (where a class inherits from only one
superclass) and multiple inheritance through interfaces (where a class can implement multiple
interfaces).

2. The `final` keyword, when used with respect to inheritance, prevents a class from being extended
further. It essentially makes the class immutable in terms of inheritance, prohibiting subclasses from
being created.

3. Demonstrating multiple inheritance directly in Java isn't possible due to the absence of support for it
in the classical sense. However, you can achieve similar behavior using interfaces, where a class can
implement multiple interfaces.

4. Method overriding in Java allows a subclass to provide a specific implementation of a method that is
already defined in its superclass. For example:

```java

class Animal {

void sound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

void sound() {

System.out.println("Dog barks");

```
5. Three uses of the `final` keyword in Java:

- It can be applied to variables to make them constants.

- It can be applied to methods to prevent overriding.

- It can be applied to classes to prevent inheritance.

6. Four inbuilt packages in Java are:

- `java.lang`

- `java.util`

- `java.io`

- `java.net`

7. A package in Java is a way to organize related classes and interfaces. To create a user-defined package
in Java, you create a directory structure that matches the package name and place the Java files inside it.
To access a user-defined package, you use the `import` statement followed by the package name.

8. Multilevel hierarchy in Java involves creating a chain of inheritance where a subclass extends another
subclass. For example:

```java

class A {

// Some code

class B extends A {

// Some code

}
class C extends B {

// Some code

```

9. A thread in Java represents an independent path of execution within a program. Two ways to create a
thread in Java are by extending the `Thread` class and by implementing the `Runnable` interface.

10.

(i) Thread priority in Java determines the importance of a thread. Threads with higher priority are
scheduled to run before threads with lower priority.

(ii) Types of exceptions in Java include checked exceptions, unchecked exceptions, and errors.

11. ```java

class EvenThread extends Thread {

public void run() {

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

System.out.println(i);

class OddThread extends Thread {

public void run() {

for (int i = 11; i <= 19; i += 2) {


System.out.println(i);

public class Main {

public static void main(String[] args) {

EvenThread evenThread = new EvenThread();

OddThread oddThread = new OddThread();

evenThread.start();

oddThread.start();

```

12.

(i) `wait()`: It causes the current thread to wait until another thread invokes the `notify()` method or
the `notifyAll()` method for this object.

(ii) `notify()`: It wakes up a single thread that is waiting on this object's monitor.

13. ```java

import java.util.Scanner;

class InvalidAgeException extends Exception {

InvalidAgeException(String s) {

super(s);
}

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter age:");

int age = scanner.nextInt();

scanner.close();

try {

if (age < 18) {

throw new InvalidAgeException("Invalid Age");

} else {

System.out.println("Valid Age");

} catch (InvalidAgeException e) {

System.out.println(e.getMessage());

```

Two common Java exceptions:

- `NullPointerException`: Occurs when you try to use a reference variable with a `null` value.

- `ArrayIndexOutOfBoundsException`: Occurs when you try to access an array element with an invalid
index.
14. The `try-catch` statement in Java is used to handle exceptions. For example:

```java

try {

// code that may throw an exception

} catch (ExceptionType e) {

// exception handling code

```

Example:

```java

try {

int result = 10 / 0; // This will throw ArithmeticException

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero");

```

15. ```java

class MyRunnable implements Runnable {

public void run() {

System.out.println("Thread is running");
}

public class Main {

public static void main(String[] args) {

MyRunnable myRunnable = new MyRunnable();

Thread thread = new Thread(myRunnable);

thread.start();

```

16. The lifecycle of an applet in Java consists of four methods:

- `init()`: Initializes the applet.

- `start()`: Starts the execution of the applet.

- `stop()`: Stops the execution of the applet.

- `destroy()`: Cleans up resources used by the applet.

17. Parameters can be passed to an applet through HTML using the `<param>` tag. Example:

```html

<applet code="MyApplet.class" width="300" height="300">

<param name="username" value="John">

</applet>

```
Java code to access the parameter:

```java

public void init() {

String username = getParameter("username");

System.out.println("Hello " + username);

```

18.

(i) Draw poly:

```java

drawPolygon(int[] xPoints, int[] yPoints, int nPoints);

```

(ii) Draw Rect:

```java

drawRect(int x, int y, int width, int height);

```

(iii) Fill Oval:

```java

fillOval(int x, int y, int width, int height);

```
(iv) Draw Arc:

```java

drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);

```

19. Please provide the figure for the `drawLine()` method output.

20. An applet in Java is a small program that runs within a web browser. Example:

```java

import java.applet.Applet;

import java.awt.Graphics;

public class WelcomeApplet extends Applet {

public void paint(Graphics g) {

g.drawString("Welcome to Java Applet", 20, 20);

```

21. Applet attributes include `code`, `codebase`, `width`, `height`, `alt`, `align`, `vspace`, `hspace`,
`name`, `archive`, `object`, and `mayscript`.

22. The hierarchy of stream classes in Java includes InputStream,Sure, I can help with that:
21. **Applet attributes:**

- **code:** Specifies the applet's codebase-relative URL.

- **width:** Defines the width of the applet's display area in pixels.

- **height:** Specifies the height of the applet's display area in pixels.

- **align:** Defines how the applet should be aligned within its display area.

- **archive:** Specifies a comma-separated list of archive file URLs.

- **name:** Specifies the name of the applet.

- **alt:** Specifies an alternate text message for non-Java-enabled browsers.

22. **Hierarchy of Stream Classes:**

```

+----------------------+

| InputStream |

+----------------------+

^ ^

| |

+---------+--+ +--+-------------+

| OutputStream| |Reader |

+--------------+ +-------------+

^ ^

| |

+---------------+ +-------------------+

| | | |

+------+-------+ +---+------+ +-----+-------+ +-------+------+

|FileInputStream| |ByteArrayInputStream| |FileReader| |StringReader|


+------+-------+ +---+------+ +-----+-------+ +-------+------+

| | | |

+------+---------+ +------+--------+ +--+-------+ +-------+--------+

|DataInputStream| |ObjectInputStream| |BufferedReader| |PipedReader|

+---------------+ +----------------+ +--------------+ +---------------+

```

23. **Two methods of File Class:**

- **`exists()`:** This method checks whether the file or directory denoted by this abstract pathname
exists or not. It returns true if the file or directory exists; otherwise, it returns false.

- **`delete()`:** This method deletes the file or directory denoted by this abstract pathname. It
returns true if and only if the file or directory is successfully deleted; otherwise, it returns false.

24. **FileOutputStream Class:**

`FileOutputStream` is a class used for writing raw bytes to a file. It's typically used to write data that
doesn't require any formatting, like images, audio, or binary data. Here's an example:

```java

import java.io.*;

public class FileOutputExample {

public static void main(String[] args) {

try {

String content = "This is an example of file output stream.";

FileOutputStream fos = new FileOutputStream("output.txt");

byte[] bytes = content.getBytes();

fos.write(bytes);
fos.close();

System.out.println("Data written to the file successfully.");

} catch (IOException e) {

e.printStackTrace();

```

25. **Program to copy contents of one file to another (using character stream):**

```java

import java.io.*;

public class FileCopy {

public static void main(String[] args) {

try (FileReader reader = new FileReader("input.txt");

FileWriter writer = new FileWriter("output.txt")) {

int character;

while ((character = reader.read()) != -1) {

writer.write(character);

System.out.println("File copied successfully.");

} catch (IOException e) {

e.printStackTrace();
}

```

This program reads characters from the input file using a `FileReader` and writes them to the output
file using a `FileWriter`.

You might also like