Topics Index

Count ts and Ts2882

If you need explanation Read this topic

If you need Answer Take test on this topic

Count ts and Ts2882
What will be the output of the following program?
public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer givenString = new StringBuffer("Now you are attempting CountCharacters program. Thank you.");
        int count = 0;
        for (int i = 0; i < givenString.length(); i++) {
            if (givenString.charAt(i) == 't' || givenString.charAt(i) == 'T') {
                givenString.setCharAt(i, '_');
        System.out.println("Found " + count + " t's in the givenString.");

A. Found 6 t's in the givenString.
Now you are a__emp_ing Coun_Charac_ers program._hank you.
B. Found 6 t's in the givenString.
Now you are at_emp_ing Coun_Charac_ers program. _hank you.
C. Found 6 t's in the givenString.
Now you are attempting CountCharacters program. Thank you.
D. Compilation Error
E. Runtime Error
Topic: Java charAt() And setCharAt() Methods in StringBuffer

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.

Ans is B.
There are 6 t's in the given string., so the count will be 6. And every 't' or 'T' in the given string are replaced by '_'.

Posted by Gopinath Manchikanti    2014-08-18 08:51:17

ans a.
t counts 6.
t chr replaced with _

Posted by Maheshwari Natarajan    2014-08-18 08:56:29

A and B are same which is the answer.Replaces alll small and capital T's to _

Posted by Shashanka Mogaliraju    2014-08-18 08:57:39

A and B both are same as well as correct at every appearance of 't' or 'T' in givenstring count is incremented and that character will get replaced by '_'.

Posted by Nishant Desai    2014-08-18 10:52:41

Congratulations Gopinath Manchikanti. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-19 04:33:08

© meritcampus 2019

All Rights Reserved.

Open In App