21. What is the purpose of the ‘default’ keyword when used in a switch statement in Java?
- ‘default’ is used to specify the code block that should be executed if none of the ‘case’ values match the expression in a switch statement.
22. Explain the concept of method references in Java.
- Method references provide a shorthand notation to reference methods using different types of expressions. They simplify code, especially when working with functional interfaces.
23. What is autoboxing and unboxing in Java?
- Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class, while unboxing is the reverse process.
24. What is the ‘strictfp’ keyword in Java, and when is it used?
- ‘strictfp’ ensures that floating-point calculations are performed consistently across different platforms, making the results predictable.
25. Explain the ‘assert’ statement in Java and its usage.
- The ‘assert’ statement is used for debugging and is typically enabled during development. It allows you to check assumptions and throw an AssertionError if they fail.
26. What is the ‘instanceof’ operator used for in Java?
- ‘instanceof’ is used to test if an object is an instance of a particular class or interface and returns a boolean value.
27. What is the purpose of the ‘enum’ keyword in Java?
- ‘enum’ is used to define enumerated types, which represent a fixed set of values. It’s often used for defining constants.
28. Explain the ‘break’ and ‘continue’ statements in Java.
- ‘break’ is used to exit a loop or switch statement, while ‘continue’ is used to skip the current iteration of a loop and proceed to the next one.
29. What is a package in Java, and how does it help in organizing code?
- A package is a way to organize classes and interfaces into a namespace, which helps prevent naming conflicts and organizes code hierarchically.
30. Explain the ‘instance initializer’ block in Java.
- An instance initializer block is used to initialize instance variables when an object is created, and it’s executed before the constructor.
31. What is method shadowing in Java?
- Method shadowing occurs when a subclass defines a method with the same name as a method in its superclass, effectively hiding the superclass method.
32. Explain the ‘static import’ feature in Java.
- ‘static import’ allows you to import and use static members (variables and methods) of a class without specifying the class name, improving code readability.
33. What is a ‘String’ pool in Java, and how does it work?
- The String pool is a pool of String objects stored in a memory area. Strings with the same value share a single instance in the pool to save memory.
34. What is the purpose of the ‘try-catch-finally’ block in Java exception handling?
- ‘try’ is used to enclose code that may throw an exception, ‘catch’ handles exceptions, and ‘finally’ is used for code that always runs, whether an exception occurs or not.
35. Explain the ‘StringBuffer’ and ‘StringBuilder’ classes and their differences.
- ‘StringBuffer’ and ‘StringBuilder’ are used to manipulate strings, but ‘StringBuffer’ is synchronized for thread safety, while ‘StringBuilder’ is not.
36. What is a ‘JVM Shutdown Hook’ in Java, and when is it executed?
- A JVM Shutdown Hook is a thread that runs when the JVM is shutting down, allowing you to perform cleanup tasks or save data.
37. What is the ‘Bitwise AND’ operator in Java, and how is it used?
- The ‘Bitwise AND’ operator (&) performs a bitwise AND operation on individual bits of two integers, producing a new integer with the result.
38. What is method hiding in Java?
- Method hiding occurs when a subclass defines a static method with the same name and signature as a static method in its superclass, effectively hiding the superclass method.
39. Explain the ‘this’ and ‘super’ keywords and their usage in Java.
- ‘this’ refers to the current object, and ‘super’ refers to the superclass of the current object. They are used to access instance variables and methods in the current and parent classes.
40. What is the ‘var’ keyword introduced in Java 10, and how is it used?
- ‘var’ is used for local variable type inference, allowing you to declare variables without explicitly specifying their data types, improving code conciseness.