Menu
Topics Index
...
`

Substring2993


If you need explanation Read this topic

If you need Answer Take test on this topic

Substring2993
What will be the output of the following program?
public class Sample {
    public static void main(String[] args) {
        String s = "Infinity2K13";
        StringBuffer sb = new StringBuffer(s);
        for (int i = 0; i < s.length(); i++) {
            String current = s.charAt(i) + "";
            s.replace(current, current.toUpperCase());
        }
        sb.replace(9, 10, "20");
        System.out.println(sb.substring(9));
    }
}

A. 2013
B. 2K13
C. Some other output
D. Compilation Error or Runtime Error
Topic: Conclusion Of Strings In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

a.

Posted by Shubham Bansal    2015-01-13 08:55:54


i'll go with Option A .. when execution Starts from main() method .. s is initialized with "Infinity2k13" and in next Step StringBuffer "sb " is delcared with the same value(by passing as constructor argument) ..  next in for loop ... String "s" value is changed to upper case(Not sb's value) .. next Step we used - StringBuffers Replace Function and it replaces -2k with "20" ( as index starts from 0) and in next Step by using Sysout .. "2013" is displayed   as substring function of StringBuffer takes the Starting index and prints till the End of the String...

Posted by ?????????? ?????    2015-01-13 09:00:30


Ans is OptionA...  execution start from main() method, after execution of first statement the variable s will be initialized with the value "Infinity2k13" after that stringbuffer variable sb will be initialized with the same value as String Variable 's' with "Infinity2k13" after that compiler will enter into for-loop with the range from 0 to s.length() means 12  and inside the loop we are taking a String variable name current to store the word "Infinity2k13" with concatanation operator(+)after that we have s.replace statement with the execution of this statement there is no change of the variable String s because string's will replace character by character so we have another variable to store those characters so the value of "s" remains same after the execution of loop.....  then next statement sb.replace will execute, it   Replaces the characters in a substring of this sequence with characters in the specified String ,  the substring begins at the location 9 and extends to the location 10 with the string "20" so "Infinity2k13" will become "Infinity2013" and in the next line println statement will execute in println we have sb.substring(9) it will print the substring from the index 9 to the end of the sequence here the value of index 9 is 2 so output is 2013 will print....... String Buffer is muttable object so Option A is true

Posted by Bhagi Bhagyasri    2015-01-13 09:54:05


ANS IS A (2013)

Here replace() method of String and StringBuffer class is used.

--In above example,String s is declared with the content "Infinity2k13".
Then it is converted into StringBiffer object variable with name as "sb".

--After that "for Loop" is used which runs till the length of string s.Inside this for loop each character is assigned to "current" string.
Then,Replace function is used with String variable "s" where Each variable is replaced witg the uppercased variable of it. i.e. String s will be converted into uppercase.

--Now "Replace() of StringBuffer" class is used which is defined as follows:

Syntax:
Here is the syntax of this method:

public StringBuffer replace(int start, int end, String str)

Parameters:
Here is the detail of parameters:

start -- The beginning index, inclusive.

end -- The ending index, exclusive.

str -- String that will replace previous contents.

--9th position of sb is "k" so it is replaced with "
20".

--After that substring() method of stringbuffer is used which extracts the character from 9th position i.e. ---> "2013" will get printed.

Posted by Mânïshå Mùlchåndânï    2015-01-13 10:21:34


ans is A ...given sting s="Infinity2k13"  then sb is converted to stringbuffer object sb in th for loop current is of type string .charAt (i)  method returns the character located at the String's specified index. The string indexes start from zero. here current =s.charAt(i)+"";here + is used for concatenation . here   replace (current,current.toUpperCase()) it converts the current string to uppercase letters ..the loop continues untill the condition becomes false... replace 9 and 10 positions with "20"and print statement prints the substring  with the index 9 it prints 2013..

Posted by Ramya Reddy Addulla    2015-01-14 06:28:58


This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment', Bhagi Bhagyasri, Mânïshå Mùlchåndânï,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-19 05:22:07


© meritcampus 2019

All Rights Reserved.

Open In App