35. What are design patterns?
Design patterns are reusable solutions to common software design problems. Examples: Singleton, Factory, Observer.
36. Explain the Singleton design pattern.
Restricts a class to one instance and provides a global access point to it.
java CopyEdit
class Singleton {
private static Singleton instance;
private Singleton() ()
public static Singleton getInstance() {
if (instance = null) (
instance new Singleton();
}
return instance;
)
}
37. What is JDBC? How is it used?
JDBC (Java Database Connectivity) is an API for connecting to databases.
Steps:
1. Load driver.
2. Establish connection.
3. Execute SQL queries.
4. Close connection.
38. Discuss the differences between Statement and Prepared Statement.
Statement: Used for static queries.
Prepared Statement: Precompiled and supports dynamic queries.
39. What is the purpose of the transient keyword?
Excludes fields from serialization.
40. Explain serialization and deserialization.
Serialization: Converts an object to a byte stream.
Deserialization: Converts a byte stream back to an object.
41. What are inner classes?
Classes defined within another class. Types: static, non-static, local, and anonymous.
42. Describe the use of the synchronized keyword.
Locks a block/method to allow only one thread access at a time.
43. What is the difference between String, StringBuilder, and StringBuffer?
String: Immutable.
StringBuilder: Mutable, non-thread-safe.
StringBuffer: Mutable, thread-safe.
44. Explain the concept of immutability in Java.
Immutable objects cannot be modified after creation, e.g.. String.
45. How does Java handle memory leaks?
Java uses garbage collection but memory leaks can occur if references to unused objects are maintained.
46. What are functional interfaces?
Interfaces with a single abstract method, e.g., Runnable.
47. Discuss the role of the default keyword in interfaces.
Allows adding methods to interfaces without breaking existing implementations.
48. What is the enum type in Java?
Used to define a set of named constants.
Example:
java CopyEdit
enum Day (MONDAY, TUESDAY)
49. Explain the concept of reflection in Java.
Allows inspection and modification of classes, methods, and fields at runtime.
50. What are modules in Java?
Introduced in Java 9, modules allow better packaging, encapsulation, and dependency management.