1. In Java java 2 class test, the-WPS Office
1. In Java java 2 class test, the-WPS Office
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() {
void sound() {
System.out.println("Dog barks");
```
5. Three uses of the `final` keyword in Java:
- `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
System.out.println(i);
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;
InvalidAgeException(String s) {
super(s);
}
System.out.println("Enter age:");
scanner.close();
try {
} else {
System.out.println("Valid Age");
} catch (InvalidAgeException e) {
System.out.println(e.getMessage());
```
- `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 {
} catch (ExceptionType e) {
```
Example:
```java
try {
} catch (ArithmeticException e) {
```
15. ```java
System.out.println("Thread is running");
}
thread.start();
```
17. Parameters can be passed to an applet through HTML using the `<param>` tag. Example:
```html
</applet>
```
Java code to access the parameter:
```java
```
18.
```java
```
```java
```
```java
```
(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;
```
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:**
- **align:** Defines how the applet should be aligned within its display area.
```
+----------------------+
| InputStream |
+----------------------+
^ ^
| |
+---------+--+ +--+-------------+
| OutputStream| |Reader |
+--------------+ +-------------+
^ ^
| |
+---------------+ +-------------------+
| | | |
| | | |
```
- **`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.
`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.*;
try {
fos.write(bytes);
fos.close();
} catch (IOException e) {
e.printStackTrace();
```
25. **Program to copy contents of one file to another (using character stream):**
```java
import java.io.*;
int character;
writer.write(character);
} 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`.