177k views
2 votes
What will be the output when the following code segment is executed?

A) String str = "welcome to CST1201";
B) String code;
C) int len = str.length();
D) System.out.println(len);
E) System.out.println(str.charAt(3));
F) System.out.println(str.subString(len-4);

User Pkubik
by
5.4k points

1 Answer

7 votes

Answer:

18

c

1201

Step-by-step explanation:

  • In step A, a string str is initialized as "welcome to CST1201"
  • In step B, a string naming code is declared.
  • Step C says that the length of the str (number of characters in str) is stored in an integer len.
  • Step D will print out the integer len which is equal to 18.
  • Step E will print the character of string str at index 3. As indexes start from 0 so "c" will be at index 3.
  • Step F will print the sub string from string str which will start from 4th last character of string str till end of str. So it will be 1201.

I hope it will help you!

User Xiu Shi
by
5.0k points