Java Interview Questions – Part 5
━━━━━━━━━━━━━━━━━━━━━━
21. What is a String in Java?
A String represents a sequence of characters. Java String objects are immutable, meaning their contents cannot be changed after creation.
Code Example
Output
22. What is the Difference Between StringBuilder and StringBuffer?
Both classes allow mutable string operations. StringBuilder is generally preferred for single-threaded operations, while StringBuffer synchronizes its methods.
Key Points
- StringBuilder is generally faster without synchronization
- StringBuffer provides synchronized methods
- Both allow string modification
23. What is ArrayList in Java?
ArrayList is a resizable collection that implements the List interface and supports ordered elements and duplicates.
Code Example
Output
24. What is HashMap in Java?
HashMap stores values as key-value pairs. Keys are unique, while values can be duplicated.
Code Example
Output
25. What is the Difference Between ArrayList and HashMap?
ArrayList stores elements in an indexed sequence, whereas HashMap stores values associated with keys.
Key Points
- ArrayList uses indexes
- HashMap uses keys
- ArrayList permits duplicate elements
- HashMap keys must be unique
━━━━━━━━━━━━━━━━━━━━━━
UPDATEGADH | Technology Tutorials & Interview Questions
#Java #JavaInterviewQuestions #CoreJava #JavaCollections #Programming #UPDATEGADH
━━━━━━━━━━━━━━━━━━━━━━
21. What is a String in Java?
A String represents a sequence of characters. Java String objects are immutable, meaning their contents cannot be changed after creation.
Code Example
String name = "Java";
System.out.println(name);
Output
Java
22. What is the Difference Between StringBuilder and StringBuffer?
Both classes allow mutable string operations. StringBuilder is generally preferred for single-threaded operations, while StringBuffer synchronizes its methods.
Key Points
- StringBuilder is generally faster without synchronization
- StringBuffer provides synchronized methods
- Both allow string modification
23. What is ArrayList in Java?
ArrayList is a resizable collection that implements the List interface and supports ordered elements and duplicates.
Code Example
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();
names.add("Amit");
names.add("Rahul");
System.out.println(names);
Output
[Amit, Rahul]
24. What is HashMap in Java?
HashMap stores values as key-value pairs. Keys are unique, while values can be duplicated.
Code Example
import java.util.HashMap;
HashMap<String, Integer> marks = new HashMap<>();
marks.put("Amit", 85);
marks.put("Rahul", 90);
System.out.println(marks.get("Amit"));
Output
85
25. What is the Difference Between ArrayList and HashMap?
ArrayList stores elements in an indexed sequence, whereas HashMap stores values associated with keys.
Key Points
- ArrayList uses indexes
- HashMap uses keys
- ArrayList permits duplicate elements
- HashMap keys must be unique
━━━━━━━━━━━━━━━━━━━━━━
UPDATEGADH | Technology Tutorials & Interview Questions
#Java #JavaInterviewQuestions #CoreJava #JavaCollections #Programming #UPDATEGADH