All Exams Test series for 1 year @ ₹349 only
Question

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”;

}

}

The correct answer is

dogood : dogood

Understanding Java Program Output: A Step-by-Step Analysis

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.

Program Execution Flow

The program execution begins in the main method:

  1. Test obj = new Test();: An object of the Test class is created.
  2. obj.start();: The start method of the created object is called.

Now, let's follow the execution inside the start method:

  1. String stra = "do";: A string variable stra is initialized with the value "do".
  2. String strb = method(stra);: The method method is called with the current value of stra ("do") as an argument. The value returned by method will be assigned to strb.

Next, the execution enters the method method, receiving "do" as its parameter (which is also named stra locally within this method):

  1. String method (String stra): The method receives "do" as the value for its local parameter stra.
  2. stra = stra + "good";: The local stra variable within method is updated by concatenating "good". Its value becomes "dogood". Important: This modification only affects the local stra inside this method. The original stra in the start method remains unchanged due to String immutability in Java.
  3. System.out.print (stra);: The program prints the current value of the local stra inside method, which is "dogood". This is the first part of the overall output.
  4. return "good";: The method returns the string "good".

Execution returns to the start method:

  1. String strb = method(stra);: The returned value ("good") is assigned to strb in the start method. So, strb is now "good". The value of stra in start is still "do".
  2. System.out.print (":" + stra + strb);: The program prints the concatenation of ":", the value of stra in start ("do"), and the value of strb ("good"). This results in printing ":dogood". This is the second part of the overall output.

Understanding String Immutability in Java

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:

  • In start, stra points to a string object "do".
  • When method(stra) is called, a *copy* of the reference to the string "do" is passed.
  • Inside method, stra = stra + "good"; creates a *new* string object "dogood", and the local stra variable inside method is updated to point to this new object. The original string object "do" is not touched, and the stra variable in the start method still points to "do".

Summary of Output Generation

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".

Final Output Analysis

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.

Revision Table: Java String Handling

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.

Additional Information: Method Calls and Scope

Understanding the scope of variables and how values are passed to methods is crucial in predicting program output.

  • Scope: Variables declared within a method (local variables and parameters) are only accessible within that method. Their values are independent of variables with the same name in other methods.
  • Pass-by-Value: Java uses pass-by-value for all argument types. For primitive types (like int, boolean), the actual value is copied. For object types (like String, arrays, custom objects), the value of the *reference* (the memory address) is copied.
  • When a String reference is passed, the method gets a copy of the reference. Both the original variable and the method parameter initially point to the same string object. However, if the method parameter is reassigned (e.g., stra = "new value"; or stra = stra + "more";), the parameter will then point to a different string object, while the original variable outside the method still points to the original string object.

This explains why modifying stra inside the method did not change the value of stra back in the start method.

Was this answer helpful?

Important Questions from Java

  1. 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.

  2. A java program that is embedded in HTML document and runs in the context of java capable browser is known as ______.

  3. JAVA compiler translates source code into _______.

  4. 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

  5. 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:

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App