1.
Enumerations: values () and valueOf () Methods (10 Marks)
Question:
Explain how Java Enumerations are different from regular classes. Demonstrate the use of values ()
and valueOf () methods with an example program.
Expected Code:
java
CopyEdit
// Defining an Enum for Days of the Week
enum Days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
public class EnumExample {
public static void main(String[] args) {
// Using values() method to iterate over all constants
for (Days d : Days.values()) {
System.out.println(d);
// Using valueOf() method to get a specific enum constant
Days selectedDay = Days.valueOf("WEDNESDAY");
System.out.println("Selected Day: " + selectedDay);
2. Autoboxing and Unboxing in Expressions (10 Marks)
Question:
What is autoboxing and unboxing in Java? Explain how Java automatically converts primitive types to
their corresponding wrapper classes and vice versa. Write a program to demonstrate autoboxing and
unboxing in expressions.
Expected Code:
java
CopyEdit
public class AutoBoxingExample {
public static void main(String[] args) {
Integer num1 = 50; // Autoboxing (int → Integer)
Integer num2 = 30; // Autoboxing
// Performing arithmetic operations (Unboxing occurs automatically)
Integer sum = num1 + num2;
Integer product = num1 * num2;
System.out.println("Sum: " + sum); // Output: 80
System.out.println("Product: " + product); // Output: 1500
3. Annotations and Reflection to Access Metadata at Runtime (10 Marks)
Question:
What are annotations in Java? Explain how the @Retention policy affects the visibility of annotations
at runtime. Write a program to define a custom annotation and use reflection to retrieve its values
at runtime.
Expected Code:
java
CopyEdit
import java.lang.annotation.*;
import java.lang.reflect.*;
// Define a custom annotation with RetentionPolicy.RUNTIME
@Retention(RetentionPolicy.RUNTIME)
@interface DeveloperInfo {
String author();
int version();
}
// Using the custom annotation
@DeveloperInfo(author = "John Doe", version = 2)
class SampleClass {}
public class AnnotationExample {
public static void main(String[] args) throws Exception {
// Using Reflection to get the annotation value
Class<SampleClass> obj = SampleClass.class;
DeveloperInfo annotation = obj.getAnnotation(DeveloperInfo.class);
// Printing annotation values
System.out.println("Author: " + annotation.author());
System.out.println("Version: " + annotation.version());
4. Collections: Iterating Over a List Using an Iterator (10 Marks)
Question:
Explain the Java Collections Framework and the purpose of using an Iterator. Write a Java program
to store user-defined objects in an ArrayList and iterate over them using an Iterator.
Expected Code:
java
CopyEdit
import java.util.*;
// Defining a Student class
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
@Override
public String toString() {
return "Student{name='" + name + "', age=" + age + "}";
public class CollectionExample {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Alice", 21));
students.add(new Student("Bob", 22));
students.add(new Student("Charlie", 20));
// Using an Iterator to access elements
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());