Question 6
What happens when a class variable is declared with the 'static' modifier in Java?
Option A is correct. Static variables belong to the class rather than individual instances. They are initialized once when the class is loaded into memory.
What happens when a class variable is declared with the 'static' modifier in Java?
Option A is correct. Static variables belong to the class rather than individual instances. They are initialized once when the class is loaded into memory.
Which Java access modifier restricts variable or method visibility strictly to the declaring class itself?
Option C is correct. The 'private' access modifier provides the highest level of encapsulation, restricting access strictly to members of the defining class.
What is the primary characteristic of a String object created in Java?
Option B is correct. In Java, String objects are immutable. Any operation that appears to modify a String actually creates a new String object in memory (String Constant Pool / Heap).
Which class in Java provides a mutable, thread-safe sequence of characters for string manipulation?
Option C is correct. StringBuffer is mutable and thread-safe because its public methods are synchronized. StringBuilder is also mutable but non-synchronized (not thread-safe).
In Java exception handling, which code block is guaranteed to execute regardless of whether an exception is thrown or caught?
Option B is correct. The 'finally' block always executes after try/catch blocks complete, ensuring cleanup operations (like closing file streams or database connections) are executed.