Java Concepts Answer Sheet
SECTION-A (08 Marks)
Q1. Explain forEach() with example. (02 Marks)
The forEach() method is used to iterate over each element in a collection. It uses lambda expressions or
method references.
Example:
List<Integer> list = Arrays.asList(1, 2, 3);
list.forEach(e -> System.out.println(e));
Q2. Write short note on ArrayList. And Write a program to create an ArrayList(AL) having 5 elements.
Remove 2 elements of AL and sort the remaining elements. (03 Marks)
An ArrayList is a resizable array in Java from java.util package. It allows duplicate elements and maintains
insertion order.
Program:
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>(List.of(5, 3, 8, 1, 7));
al.remove(1); // removes 3
al.remove(2); // removes 7
Collections.sort(al);
System.out.println(al);
Page 1
Java Concepts Answer Sheet
Q3. Define IoC Container. Differentiate Between BeanFactory and ApplicationContext? (03 Marks)
IoC Container manages objects using Dependency Injection in Spring.
BeanFactory is a basic container, lazy in nature, and loads beans on demand.
ApplicationContext is an advanced container that loads beans eagerly and supports internationalization,
event propagation, and more.
It is preferred in real-time applications over BeanFactory.
Q4. Elaborate following key terms with example: (04 Marks)
a. Annotation: Annotations are metadata added to code that provide data to the compiler or runtime.
Example:
@Override
public void display() { ... }
b. Lambda Expression: Lambda expressions allow treating code as data. They simplify passing behavior.
Syntax: (parameters) -> expression
Example:
List<Integer> list = Arrays.asList(1, 2, 3);
list.forEach(n -> System.out.println(n));
SECTION-B (12 Marks)
Q5. Explain the hierarchy of the Collection framework in Java. (04 Marks)
The Java Collection Framework is a set of classes and interfaces that implement commonly reusable data
structures.
Page 2
Java Concepts Answer Sheet
At the top is the Collection interface, extended by List, Set, and Queue.
- List (e.g., ArrayList, LinkedList) allows ordered duplicates.
- Set (e.g., HashSet, LinkedHashSet) disallows duplicates.
- Queue (e.g., PriorityQueue) follows FIFO order.
Another root interface is Map, which maps keys to values (e.g., HashMap, TreeMap).
Q6. What do you mean by Bean Scope? Explain any 2 with example. (04 Marks)
Bean Scope defines the lifecycle and visibility of a Spring bean. Common scopes are:
1. Singleton (default): One instance per Spring container.
@Component @Scope("singleton")
public class MyBean { }
2. Prototype: A new instance is created each time it is requested.
@Component @Scope("prototype")
public class MyBean { }
Singleton beans are reused, while prototype beans are not shared. Scope can be set using @Scope
annotation.
Page 3