What is the output of the following JAVA program? class Test { public static void main (String [ ] args) { Test obj = new Test (); obj.start (); } void start () { String stra="do”; String strb = method (stra); System.out.print(“:” + stra + strb); } String method (String stra) { stra=stra+”good”; System.out.print (stra); return “good”; } }
dogood : dogood
Let's carefully examine the provided Java program to determine its output. The program involves a class Test with a main method, a start method, and a method method. Understanding how method calls work and how strings are handled in Java is key to finding the correct output.
The program execution begins in the main method:
Now, let's follow the execution inside the start method:
Next, the execution enters the method method, receiving "do" as its parameter (which is also named stra locally within this method):
Execution returns to the start method:
A key concept here is that strings in Java are immutable. When you perform an operation that seems to modify a string, like concatenation (`+`), a *new* string object is created. The original string object is not changed. In our program:
Let's trace the print statements in order:
| Step | Location | Code | Output Printed | Current State (start method) | Current State (method method) |
|---|---|---|---|---|---|
| 1 | start | String stra = "do"; | (None) | stra = "do" | (Not in method) |
| 2 | start calls method | method(stra); | (None) | stra = "do" | Local stra parameter gets "do" |
| 3 | method | stra = stra + "good"; | (None) | stra = "do" (unchanged) | Local stra becomes "dogood" |
| 4 | method | System.out.print(stra); | dogood | stra = "do" | Local stra is "dogood" |
| 5 | method returns | return "good"; | (None) | stra = "do" | (Exits method) |
| 6 | start | String strb = ...; | (None) | stra = "do", strb = "good" | (Not in method) |
| 7 | start | System.out.print(":" + stra + strb); | :dogood | stra = "do", strb = "good" | (Not in method) |
Combining the printed outputs in order ("dogood" then ":dogood"), the final output is "dogood:dogood".
Based on the step-by-step execution and the principle of String immutability, the program first prints "dogood" from inside the method function, and then prints ":dogood" from inside the start function. Concatenating these gives the final output.
| Concept | Description | Example |
|---|---|---|
| String Immutability | Once a String object is created, its value cannot be changed. Operations that appear to modify a string actually create new string objects. | String s = "hello"; s = s + "world"; creates a new string "helloworld". The original "hello" object remains unchanged (and might be garbage collected if no longer referenced). |
| Passing Strings to Methods | In Java, arguments are passed by value. For object types like String, the value of the reference is passed. If the method reassigns the parameter variable to point to a *new* object, it does not affect the original variable outside the method. | void change(String str) { str = "new"; } Calling change("old") will not change the value of the variable that held "old" outside the method. |
Understanding the scope of variables and how values are passed to methods is crucial in predicting program output.
This explains why modifying stra inside the method did not change the value of stra back in the start method.
Which of the following statement(s) is/are TRUE regarding Java Servelets?
(a) A Java Servlet is a server-side component that runs on the webserver and extends the capabilities of a server.
(b) A Servelet can use the user interface classes like AWT or Swing.
A java program that is embedded in HTML document and runs in the context of java capable browser is known as ______.
JAVA compiler translates source code into _______.
Which of the following statement(s) is/are correct regarding java programming?
I. Place a semicolon immediately after a method header
II. Define a method outside the braces of a class definition
Arrange the following applet life cycles methods in sequence
A. start()
B. paint()
C. init()
D. stop()
Choose the correct answer from the options given below: