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
in the enum provides specific behavior.
Here's an example:
```java
interface Runnable {
void run();
enum Animal implements Runnable {
DOG {
@Override
public void run() {
System.out.println('Dog runs');
},
CAT {
@Override
public void run() {
System.out.println('Cat runs');
}
Java Programming Exam Answers
public class Main {
public static void main(String[] args) {
Animal.DOG.run();
Animal.CAT.run();
```
In this example, the `Animal` enum implements the `Runnable` interface, and each enum constant
provides its own implementation of the `run` method.
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
classes would complicate the design and usage of enums.
Java Programming Exam Answers
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
several advantages over traditional constants:
- Type safety: Enums ensure that the values are restricted to the predefined set.
- Namespace: Enums group related constants together in a single type.
- Readability: Code that uses enums is often more readable and self-documenting.
The basic syntax for declaring an enum is as follows:
```java
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
```
Here's an example program that demonstrates the use of enums:
```java
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
Java Programming Exam Answers
public class Main {
public static void main(String[] args) {
Day today = Day.MONDAY;
switch (today) {
case MONDAY:
System.out.println('Mondays are tough!');
break;
case FRIDAY:
System.out.println('Fridays are great!');
break;
default:
System.out.println('Midweek days are so-so.');
break;
```
In this example, the `Day` enum is used to represent the days of the week, and a switch statement
is used to print different messages based on the current day.
Java Programming Exam Answers
5. Explain the key concepts of generics: a. Type parameters, b. Generic methods, c. Bounded type p
#### a. Type Parameters
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
public class Box<T> {
private T content;
public void setContent(T content) {
this.content = content;
public T getContent() {
return content;
```
#### b. Generic Methods
Generic methods are methods that introduce their own type parameters. These type parameters are
independent of any generic types declared by the class.
Example:
Java Programming Exam Answers
```java
public class Util {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + ' ');
System.out.println();
```
#### c. Bounded Type Parameters
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
public <T extends Number> void printNumber(T number) {
System.out.println('Number: ' + number);
```
#### d. Parameterized Types
Parameterized types are types created when a generic type is instantiated with specific type
arguments.
Example:
Java Programming Exam Answers
```java
Box<Integer> integerBox = new Box<>();
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
can be used to provide information to the compiler, to be processed by development tools, or to be
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
them in sync with the code.
Example of a custom annotation:
```java
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation {
String value();
public class Test {
Java Programming Exam Answers
@MyAnnotation(value = 'example')
public void myMethod() {
System.out.println('Annotated method');
```
In this example, `MyAnnotation` is a custom annotation that can be applied to methods. The
`myMethod` method is annotated with `@MyAnnotation`.