Computer Applications
Write the values that will be assigned to x, y and t after executing the following code.
String s1, s2, x, y;
int t;
s1 = "classxii";
s2 = "cbseboard";
x = s1.substring(5, 8);
y = s2.concat(s1);
t = y.length();
System.out.println("x=" + x);
System.out.println("y=" + y);
System.out.println("t=" + t);
Java String Handling
4 Likes
Answer
Output
x=xii
y=cbseboardclassxii
t=17
Explanation
The substring() method returns a substring beginning from the startindex and extending to the character at index endIndex - 1. Here, startindex is 5 and end index is 7 (8 - 1). Since the string starts from index 0, the extracted substring is "xii". Thus x = xii.
The concat() method adds one string (s1) at the end of another string (s2). Thus, "classxii" is added to the end of "cbseboard". Hence, y = "cbseboardclassxii.
The length() method returns the total number of characters in a string. Thus, t = 17.
Answered By
2 Likes
Related Questions
Write the values of c and d after execution of following code.
int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++;Observe the following code and write how many times will the loop execute?
a = 5; b = 2; while(b != 0) { r = a % b; a = b; b = r; } System.out.println(" " + a);Write the values that will be stored in variables num and sum after execution of the following code.
int sum = 0, num = -2; do { sum = sum + num; num++; } while (num < 1);The following code has some error(s). Rewrite the correct code and underlining all the corrections made.
integer counter = 0; i = 10; num; for (num = i; num >= 1; num--); { If i % num = 0 { counter = counter + 1; } }