String Interview Questions
String Interview Questions
- Answer:
A `String` in Java is a sequence of characters, represented as objects. The `java.lang.String` class is immutable, meaning once
created, the content cannot be changed.
- Answer:
- HashMap compatibility: Since `String` is widely used as a key, immutability ensures hash codes remain constant.
3. What are the key differences between `String`, `StringBuilder`, and `StringBuffer`?
- Answer:
- Answer:
- `.compareTo()`: Compares lexicographically; returns `0` if equal, negative if the first string is less, positive if greater.
- Answer:
- new String(): Creates a new object in the heap. E.g., `String s2 = new String("Hello");`
- Answer:
7. How does the `intern()` method work?
- Answer:
The `intern()` method moves the String object to the String Pool. If the pool already contains a string with the same content, it
returns the reference to the existing string.
- Answer:
- Answer:
reversed += str.charAt(i);
}
- Answer:
- Answer:
Using `toCharArray()`:
- Answer:
Use `String.join()`:
// Output: Java-is-fun
- Answer:
String formatted = String.format("Hello, %s! You have %d new messages.", "Alice", 5);
- Answer:
map.forEach((k, v) -> {
});
15. What is the difference between `StringBuffer` and `StringBuilder`?
- Answer:
Answer:
No, constructors cannot be inherited in Java. Constructors are not members of a class; they are special methods used to initialize
objects. However, a subclass can call a parent class constructor using the `super()` keyword.
- Answer:
- `super()
class Parent {
- `this()
class Example {
Example() { this("Default"); }
Note: You cannot use both `this()` and `super()` in the same constructor.
- Answer:
```java
class Singleton {
return instance;
19. What happens if a subclass does not explicitly call a superclass constructor?
- Answer:
If a subclass constructor does not explicitly call a superclass constructor using `super()`, the default (no-argument) constructor of
the superclass is automatically called.
If the superclass does not have a default constructor, the compiler will throw an error.
class Parent {
- Answer:
This ensures that the parent class is initialized before the child class.
class Parent {
- Answer:
A constructor is a special method used to initialize objects. It has the same name as the class and no return type. It is called
automatically when an object is created.
- Answer:
- Default Constructor: Provided by Java if no constructor is defined; initializes objects with default values.
- Answer:
- Answer:
class Example {
Example() {
Example(int x) {
System.out.println(x);
- Answer:
1. What is inheritance in Java? Why is it used?
- Answer:
Inheritance is a mechanism where one class (child) acquires the properties and behaviors of another class (parent). It is used to
promote code reuse and establish a relationship between classes.
- Answer:
Note: Java does not support multiple inheritance (via classes) to avoid ambiguity (diamond problem).
- Answer:
4. Can a subclass override a method from the parent class? What are the rules?
- Answer:
- The method must have the same name, return type, and parameters.
- Answer:
- `IS-A` Relationship: Achieved through inheritance; represents "is a type of" (e.g., `Dog` IS-A `Animal`).
- `HAS-A` Relationship: Achieved through composition; represents "has a" (e.g., `Car` HAS-A `Engine`).