Menu
Topics Index
...
`

String Test2510


If you need explanation Read this topic

If you need Answer Take test on this topic

String Test2510
What will be the output of the following program?
public class StringTest {
    public static void main(String[] args) {
        String String = "String";
        int temp = 2;
        Object : for (int main = 0; main < String.charAt(main); main++) {
            System.out.print(String.charAt(main) + "~");
            if (main == temp) {
                continue Object;
            }
        }
    }
}

A. S~t~r~
B. S~t~r~i~
C. S~t~r~i~n~g~
D. Some other output
E. Compilation Error or Runtime Error
Topic: charAt() Method In Java - Java Character Extraction

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 E...here we have the String as "String"..Strings are immutable class which means we cant chagne the content of the String object...if we modify than new instance get created...
here we haave label named as object which is used to control the control...we can use break and continue on the label...

here we have main as loop control..and condition is based on the in the String..here loop will repeat more than than 6 which is the length of string here we put condition as  
main<charAt(main) means it will give the ascii value of the char here we have "String" so ascii values i--greater than 65 so here JVM will raise an Exception saying StringOutOfBoundsException...

Posted by Uday Kumar    2014-12-17 14:24:09


ans e.in this program initially string object is initialized with"String".thn int temp initialized with 2.for loop local variable main initialized with 0, thn main<char ( main) is givn condition for loop get executed if the condition satisfied. thn increment operator used for iteration. here iteration follows main=0,main<Unicode of 'S' ,sop prints S~ ,main=1,main< Unicode of 't',sop prints t~ ,similarly it works upto main=5,main< ascii of 'g' sop prints g~ ,upto this for loop condition never get failed,aft this main =6,main< string.charat(6),here we attempt to access index of out of bound of the string.so runtime error- string IndexoutOfBoundException get thrown.in for loop we used 1 if condition,but this condition not play big role,instead of continue if we giv break,thn for loop iterate upto temp value sop print S~t~r~

Posted by Maheshwari Natarajan    2014-12-17 18:55:23


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Asad Ahmed. 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    2014-12-18 04:30:33


© meritcampus 2019

All Rights Reserved.

Open In App