Java_Programming_Exam_Answers
Java_Programming_Exam_Answers
1. Discuss how enums can implement interfaces in Java. Can an enum extend a class in Java? Just
In Java, enums can implement interfaces, which allows enums to provide implementations for
methods defined by the interface. This feature is useful when we want to ensure that every constant
Here's an example:
```java
interface Runnable {
void run();
DOG {
@Override
System.out.println('Dog runs');
},
CAT {
@Override
System.out.println('Cat runs');
}
Java Programming Exam Answers
Animal.DOG.run();
Animal.CAT.run();
```
In this example, the `Animal` enum implements the `Runnable` interface, and each enum constant
Enums in Java cannot extend other classes because they implicitly extend the `java.lang.Enum`
class. Since Java does not support multiple inheritance, an enum cannot extend any other class.
The primary reason is to maintain a clean and simple type system. Allowing enums to extend
4. What are enumerations in Java? How do they differ from traditional constants? Explain the basic
Enumerations (enums) in Java are a special type of class that represents a group of constants
(unchangeable variables, like final variables). Enums are used to define collections of constants in a
type-safe manner.
Traditional constants in Java are typically defined using `static final` fields. However, enums provide
- Type safety: Enums ensure that the values are restricted to the predefined set.
- Readability: Code that uses enums is often more readable and self-documenting.
```java
enum Day {
```
```java
enum Day {
}
Java Programming Exam Answers
switch (today) {
case MONDAY:
break;
case FRIDAY:
break;
default:
break;
```
In this example, the `Day` enum is used to represent the days of the week, and a switch statement
5. Explain the key concepts of generics: a. Type parameters, b. Generic methods, c. Bounded type p
Type parameters allow you to define a generic type. They act as placeholders for the types that will
be used in the class, method, or interface. Type parameters are enclosed in angle brackets (`<>`)
and are typically represented by a single letter, like `T`, `E`, `K`, `V`, etc.
Example:
```java
private T content;
this.content = content;
public T getContent() {
return content;
```
Generic methods are methods that introduce their own type parameters. These type parameters are
Example:
Java Programming Exam Answers
```java
System.out.println();
```
Bounded type parameters restrict the types that can be used as arguments for a type parameter.
This is done using the `extends` keyword for upper bounds and `super` for lower bounds.
Example:
```java
```
Parameterized types are types created when a generic type is instantiated with specific type
arguments.
Example:
Java Programming Exam Answers
```java
integerBox.setContent(123);
System.out.println(integerBox.getContent());
```
Java Programming Exam Answers
6. What are annotations in Java? What purpose do they serve? How do annotations differ from com
Annotations in Java are a form of metadata that provide data about a program but are not part of the
program itself. They have no direct effect on the operation of the code they annotate. Annotations
processed at runtime.
Annotations differ from comments in that they are structured and can be interpreted by the compiler
and runtime, whereas comments are ignored by the compiler and are meant only for human
readers.
Annotations also differ from traditional metadata, which might be stored in separate files or
databases. Annotations are embedded directly in the code, making it easier to maintain and keep
```java
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation {
String value();
@MyAnnotation(value = 'example')
System.out.println('Annotated method');
```
In this example, `MyAnnotation` is a custom annotation that can be applied to methods. The