203k views
3 votes
What is output by the code below?

String m = "h";
int n =0;
do{
m=m+"h";
n++;
}while(! ("hhhhh"));
System.out.println(m + " - " + n);

1 Answer

4 votes

Final answer:

The code will output 'hhhhh - 5'.

Step-by-step explanation:

The code provided is a combination of string manipulation and a do-while loop in Java. It initializes a string variable 'm' with the value "h" and an integer variable 'n' with the value 0. The do-while loop appends an additional "h" to 'm' and increments 'n' until the condition in the while loop is satisfied, i.e., when 'm' becomes equal to "hhhhh".

Therefore, the output of the code will be:

hhhhh - 5

User Pankaj Agrawal
by
8.3k points