Java Objective Questions & Answers Guide
Java Objective Questions & Answers Guide
The 'break' statement terminates the closest enclosing loop prematurely when encountered. In the given loop, 'break' is triggered when i equals 3, causing the loop to stop executing, resulting in an output of 12 as only numbers 1 and 2 are printed before the loop exits .
An uncaught exception terminates the program's normal flow, leading to a crash because Java has no mechanism to handle the exception, leaving the program in an undefined state. This can be prevented by using try-catch blocks to handle exceptions or propagating exceptions using the 'throws' keyword to higher levels where they can be managed effectively .
This code prints false because '==' compares object references, not the content they point to. 's1' is interned in the string pool, while 's2' is a new object with the same content, but a different reference. This illustrates the concept of reference equality versus object equality in Java .
The expression 'System.out.println(++a + a++)' involves both pre-increment and post-increment operations. In this case, '++a' increments a to 11 before it is used, and 'a++' uses 11 but increments a afterwards. Thus, the expression evaluates to 11 + 11 with a final value of 12 after both operations, resulting in the printed output of 22 .
'Throwable' is the superclass of all errors and exceptions in Java, forming the root class of the exception hierarchy. It allows error handling mechanisms to catch and handle different errors and exceptions in a uniform way .
A 'static' method belongs to the class, not instances, allowing it to be called on the class itself without creating an object. The 'main' method in Java must be static so it can be called by the Java runtime without an object. A 'final' method cannot be overridden in subclasses, ensuring the method's implementation remains consistent across inheritance chains. The 'main' method cannot be marked final as it would not serve any specific purpose given its static nature and typical usage .
A 'constructor' method is automatically called when an object is created in Java. It initializes the newly created object and can set initial values for object attributes or perform setup procedures necessary at the time of object creation .
The 'continue' statement skips the current iteration of a loop, proceeding with the next iteration without terminating the loop entirely. Unlike 'break', which exits the loop completely when encountered, 'continue' allows the loop to continue running, just bypassing the current cycle of execution .
In Java, dividing two integers uses integer division, which truncates the decimal part and only returns the quotient. Therefore, when 'System.out.println(x / y)' is called with x = 5 and y = 2, the result is 2, not 2.5, because integer division discards any remainder .
Inheritance in OOP allows a new class to acquire the properties and methods of an existing class, promoting code reuse and hierarchical classification. In Java, the keyword 'final' is used to prevent a class from being extended, thereby inhibiting inheritance .