UNIT-3 QUESTIONS & ANSWERS
1. What Is a Functional Interface? What Are the Rules of Defining a Functional Interface?
A functional interface is an interface that contains exactly one abstract method. It can have multiple
default or static methods. Rules:
- Only one abstract method
- Can use @FunctionalInterface annotation
- No other abstract methods are allowed
2. Describe Some of the Functional Interfaces in the Standard Library. What Is a Lambda
Expression and What Is It Used For?
Examples: Predicate<T>, Function<T, R>, Supplier<T>, Consumer<T>.
Lambda Expression is a concise way to implement functional interfaces using syntax (parameters)
-> { body }.
It is used for functional-style programming in Java.
3. What is a default method? What is a static method?
Default method: Method in an interface with a default implementation using the `default` keyword.
Static method: Method in an interface or class that belongs to the interface/class, not an instance.
4. Explain the Syntax and Characteristics of a Lambda Expression
Syntax: (parameters) -> expression or (parameters) -> { statements }.
Characteristics: concise, supports functional programming, can be passed as arguments.
5. What Is a Stream? How Does It Differ From a Collection?
Stream: Abstraction for processing sequences of elements.
Difference: Collections store data; Streams process data using functional operations like map, filter,
reduce.
6. Is it possible to define our own Functional Interface? What is @FunctionalInterface? What are the
rules to define a Functional Interface
Yes. @FunctionalInterface ensures only one abstract method is present. Rules: one abstract
method, may have default/static methods.
7. When do we go for Stream API? Why do we need to use Stream API in our projects?
Use Stream API for functional-style data processing. Benefits: concise, readable, efficient data
manipulation.
8. Explain basic Type Base64 Encoding and Decoding in Java
Base64 encoding converts binary data into an ASCII string format using java.util.Base64 class.
9. Encode simple String into Basic Base 64 format
String original = "Hello";
String encoded = Base64.getEncoder().encodeToString(original.getBytes());
10. Explain Java Type and Repeating Annotations
Java Type Annotations provide metadata for types. Repeating Annotations allow the same
annotation to be applied more than once using @Repeatable.
UNIT-4 QUESTIONS & ANSWERS
1. Explain the hierarchy of the Collection Framework in Java.
At the top is Collection interface. Subinterfaces: List, Set, Queue. Implementations include ArrayList,
HashSet, LinkedList etc.
2. What is the purpose of the Iterator interface in Java's Collection Framework?
Iterator allows sequential access of elements in a collection and supports element removal.
3. Differentiate between the Collection interface and the Collections class in Java.
Collection is an interface; Collections is a utility class with static methods like sort(), reverse().
4. Compare and contrast ArrayList, LinkedList, and Vector in Java.
ArrayList: dynamic array, not synchronized.
LinkedList: doubly linked list.
Vector: synchronized, slower than ArrayList.
5. What is the role of the Set interface in Java, and how does it differ from other collection types?
Set doesn't allow duplicate elements. Unlike List, it doesn't maintain insertion order (except
LinkedHashSet).
6. Discuss the differences between HashSet, LinkedHashSet, and TreeSet in Java.
HashSet: unordered.
LinkedHashSet: insertion order maintained.
TreeSet: sorted order, uses TreeMap.
7. How does the TreeMap class differ from HashMap and LinkedHashMap in Java?
TreeMap: sorted by keys.
HashMap: unordered.
LinkedHashMap: insertion order maintained.
8. What is the Comparable interface, and how is it used for sorting objects in Java?
Comparable allows custom objects to be sorted using compareTo() method.
9. Discuss the properties and applications of the Properties class in Java.
Properties is a subclass of Hashtable, used to maintain configuration data in key-value format.
10. How does the Hashtable class differ from HashMap in Java, and when would you prefer to use
each?
Hashtable is synchronized (thread-safe), HashMap is not. Use Hashtable in multithreaded
environments.
11. Describe the Properties class in Java and its common use cases.
Used for config files, storing settings in .properties files, supports load() and store().
12. What is the ordering of elements in a LinkedHashSet?
Maintains insertion order.
13. Write a Java program that demonstrates sorting an ArrayList of custom objects using the
Comparable interface.
class Student implements Comparable<Student> {
int marks;
public int compareTo(Student s) {
return this.marks - s.marks;
14. Write a Java program to demonstrate basic operations (addition, removal, and iteration) on a
HashSet containing strings.
HashSet<String> set = new HashSet<>();
set.add("A");
set.remove("A");
for(String s : set) System.out.println(s);
15. Implement Java program to create a TreeSet of integers and perform operations like addition,
removal, and printing in sorted order.
TreeSet<Integer> ts = new TreeSet<>();
ts.add(3);
ts.remove(3);
ts.forEach(System.out::println);