-
What is the main purpose of using generics in Java?
- A) To improve code performance
- B) To enable dynamic typing
- C) To provide type safety
- D) To reduce memory usage
-
Which collection framework interface provides dynamic resizing of arrays?
- A) List
- B) Set
- C) Map
- D) ArrayList
-
Which data structure is used by the HashSet class in Java?
- A) Array
- B) Linked List
- C) Hash Table
- D) Tree
-
In Java, which collection allows duplicate elements?
- A) Set
- B) Map
- C) List
- D) Queue
-
What is the purpose of the
Comparable
interface in Java?- A) To compare two objects based on their hash codes
- B) To specify the natural ordering of objects
- C) To provide a way to iterate over a collection
- D) To filter elements in a collection
-
Which keyword is used to explicitly throw an exception in Java?
- A) catch
- B) throw
- C) finally
- D) throws
-
In Java, checked exceptions are subclasses of which class?
- A) RuntimeException
- B) Exception
- C) Error
- D) Throwable
-
What is the purpose of Java Reflection?
- A) To create new objects
- B) To access and manipulate class metadata at runtime
- C) To handle exceptions
- D) To generate bytecode
-
Which annotation is used to mark a method that overrides a method in a superclass?
- A) @Override
- B) @OverrideMethod
- C) @Overload
- D) @Overridden
-
Which of the following statements about custom annotations in Java is true?
- A) Custom annotations can only be used by the Java standard library.
- B) Custom annotations are defined using the
@Annotation
keyword. - C) Custom annotations can have runtime retention.
- D) Custom annotations can be used to define new data types.
-
Which interface in the Java Collections Framework provides a way to store elements as key-value pairs?
- A) List
- B) Set
- C) Map
- D) Collection
- In Java, which collection class is synchronized and suitable for multithreaded applications?
- A) ArrayList
- B) HashSet
- C) ConcurrentHashMap
- D) TreeMap
- What is the key difference between ArrayList and LinkedList in Java?
- A) ArrayList is faster for random access, while LinkedList is faster for insertions and deletions.
- B) ArrayList allows duplicate elements, while LinkedList does not.
- C) ArrayList uses a doubly-linked list, while LinkedList uses a singly-linked list.
- D) ArrayList is an interface, while LinkedList is a concrete class.
- In Java, what does the wildcard character "?" represent in the context of generics?
- A) An unknown data type
- B) A wildcard character
- C) An exception
- D) A generic class
- Which method in the
Collections
class is used to sort a List in natural order?
- A)
sort()
- B)
order()
- C)
arrange()
- D)
arrangeInOrder()
- In Java, which keyword is used to specify that a method can throw multiple exceptions?
- A) catch
- B) throw
- C) throws
- D) try
- What is the purpose of the
getClass()
method in Java reflection?
- A) To create a new class instance
- B) To retrieve the runtime class of an object
- C) To throw a ClassNotFoundException
- D) To invoke a method on an object
- Which annotation is used to suppress compiler warnings in Java?
- A) @SuppressWarnings
- B) @Override
- C) @Deprecated
- D) @SafeVarargs
- What is the difference between checked and unchecked exceptions in Java?
- A) Checked exceptions are caught at compile time, while unchecked exceptions are caught at runtime.
- B) Checked exceptions are subclasses of RuntimeException, while unchecked exceptions are not.
- C) Checked exceptions are more severe than unchecked exceptions.
- D) Checked exceptions cannot be caught using try-catch blocks.
-
Which Java annotation is used to mark a class as serializable?
- A) @Serializable
- B) @Serial
- C) @SerializableClass
- D) @SerializableObject
-
Implement a generic method in Java that counts the number of occurrences of a specific element in an array.
-
Create a generic class called
Pair
that can store two different types of values. Write a Java program to demonstrate its usage. -
Write a Java program that uses reflection to create an instance of a class dynamically and invokes one of its methods.
-
Create a custom annotation called
@LogMethod
that can be used to annotate methods in a Java class. Write a Java program that uses reflection to check if a method is annotated with@LogMethod
and logs a message when the method is called. -
Write Java code to create a generic method that swaps two elements in an array.
-
Implement a custom generic class called
Stack
that supports push and pop operations. Use generics to allow the stack to store elements of any data type. -
Create a custom exception class called
InvalidAgeException
that extendsException
. Write a Java program that throws this exception when the age of a person is less than 18. -
Write a Java program that uses reflection to inspect the methods of a given class and prints their names and parameter types.
- C - To provide type safety
- D - ArrayList
- C - Hash Table
- C - List
- B - To specify the natural ordering of objects
- B - throw
- B - Exception
- B - To access and manipulate class metadata at runtime
- A - @Override
- C - Custom annotations can have runtime retention.
- C - Map
- C - ConcurrentHashMap
- A - ArrayList is faster for random access, while LinkedList is faster for insertions and deletions.
- A - An unknown data type
- A - sort()
- C - throws
- B - To retrieve the runtime class of an object
- A - @SuppressWarnings
- A - Checked exceptions are caught at compile time, while unchecked exceptions are caught at runtime.
- D - @SerializableObject
21. Implement a generic method in Java that counts the number of occurrences of a specific element in an array.
public class Main {
public static <T> int countOccurrences(T[] array, T element) {
int count = 0;
for (T e : array) {
if (e.equals(element)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1};
String[] words = {"apple", "banana", "apple", "orange", "apple", "pear"};
System.out.println(countOccurrences(numbers, 1));
System.out.println(countOccurrences(words, "apple"));
}
}
22. Create a generic class called Pair
that can store two different types of values. Write a Java program to demonstrate its usage.
public class Pair<LEFT, RIGHT> {
private LEFT left;
private RIGHT right;
public Pair(LEFT left, RIGHT right) {
this.left = left;
this.right = right;
}
public LEFT getLeft() {
return left;
}
public void setLeft(LEFT left) {
this.left = left;
}
public RIGHT getRight() {
return right;
}
public void setRight(RIGHT right) {
this.right = right;
}
@Override
public String toString() {
return "Pair{" +
"left=" + left +
", right=" + right +
'}';
}
}
23. Write a Java program that uses reflection to create an instance of a class dynamically and invokes one of its methods.
public class Main {
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("java.util.Date");
Object object = clazz.getDeclaredConstructor().newInstance();
Method method = clazz.getMethod("toString");
System.out.println(method.invoke(object));
}
}
24. Create a custom annotation called @LogMethod
that can be used to annotate methods in a Java class. Write a Java program that uses reflection to check if a method is annotated with @LogMethod
and logs a message when the method is called.
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface LogMethod {
}
class Test {
@LogMethod
public void testMethod() {
System.out.println("testMethod() called");
}
}
public class Main {
public static <T> void swap(T[] array, int i, int j) {
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
26. Implement a custom generic class called Stack
that supports push and pop operations. Use generics to allow the stack to store elements of any data type.
public class Stack<T> {
private final List<T> elements = new ArrayList<>();
public void push(T element) {
elements.add(element);
}
public T pop() {
if (elements.isEmpty()) {
throw new EmptyStackException();
}
return elements.remove(elements.size() - 1);
}
public boolean isEmpty() {
return elements.isEmpty();
}
}
27. Create a custom exception class called InvalidAgeException
that extends Exception
. Write a Java program that throws this exception when the age of a person is less than 18.
public class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class Person {
private final String name;
private final int age;
public Person(String name, int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be greater than 18");
}
this.name = name;
this.age = age;
}
}
28. Write a Java program that uses reflection to inspect the methods of a given class and prints their names and parameter types.
public class Main {
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("java.util.Date");
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
for (Parameter parameter : method.getParameters()) {
System.out.println(parameter.getType());
}
}
}
}