25.4k views
0 votes
What will be printed after the following code is executed? String str = "abc456"; int m = 3; while ( m =< str.length() - 1) { if (Character.isDigit(str.charAt(m))) System.out.print(Character.toUpperCase(str.charAt(m))); m++; }

User DM Graves
by
8.1k points

2 Answers

6 votes

Answer:

456 will be printed

Step-by-step explanation:

The following shows a well formatted equivalent of the code snippet:

String str = "abc456"; //Line 1

int m = 3; //Line 2

while (m <= str.length() - 1) { //Line 3

if (Character.isDigit(str.charAt(m))) //Line 4

System.out.print(Character.toUpperCase(str.charAt(m))); //Line 5

m++; //Line 6

} //Line 7

Line 1 shows the declaration and initialization of a String variable str.

Line 2 shows the declaration and initialization of an integer variable m.

Lines 3 through 7 show a while loop.

The loop starts at m = 3, increments m by one at every cycle, and ends at m = 5 which is one less than the length of the String str. i.e 6 - 1 = 5

At each cycle, the loop checks if the character at index m of the str is a digit. If it is a digit, it is converted to an uppercase letter(which is irrelevant) and then printed to the console.

At m=3, the character is a digit which is 4

At m=4, the character is a digit which is 5

At m=5, the character is a digit which is 6

Therefore, 456 is printed to the console.

Note: I have assumed that the statement in the while loop of the code you wrote was a typo and as such I have changed it to the correct syntax. That is, I have changed ;

while(m =< str.length( ) - 1) to

while(m <= str.length( ) - 1) for the code to perform the intended function.

Otherwise, if it is the case that it wasn't a typo, then the code will neither compile nor run as it contains a syntax error.

Hope this helps!

User SigGP
by
9.4k points
2 votes

Answer:

456

will be printed after execution of this code.

Step-by-step explanation:

  • while loop will run till the condition is true that given value for m is greater than or equal to (string length -1) or say 5
  • As m = 3 is given so first time the loop will run it goes to if statement which uses m value as a index to pull character from str and checks if it is a digit or alphabet. It will proceed if the character is a digit.
  • As at index 3 (starting from 0) the character is 4, so it will proceed to next statement which prints the extracted character on the screen after converting it into Upper case.
  • As digits are not affected so the digit 4 will be printed.
  • m will be increased by 1 making m = 4.
  • Now the while condition is still true so is statement will return character at index 4 that is 5.
  • It will be sent to next statement but toUpperCase function does not changes digits so 5 will be printed.
  • Increment in m makes m = 5
  • While statement is still true so 5th character from string is extracted that is 6. As it is a digit so moving to next statement will not change the character because toUpperCase does not apply on digits.
  • Now when m is incremented it becomes 6 which will give false to the while condition so the loop will be terminated.
  • Now the answer printed on to screen in 456.

I hope it will help you!

User Drkunibar
by
8.2k points