Menu
Topics Index
...
`

String Length


If you need explanation Read this topic

If you need Answer Take test on this topic

String Length
What will be the output of the following program?
public class StringDemo {
    public static void main(String[] args) {
        String m = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        System.out.println(m.length());
        m += "abcdefghijklmnopqrstuvwxyz";
        System.out.println(m.length());
        m = "0123456789";
        System.out.println(m.length());
    }
}

A. 26
52
62
B. 26
26
10
C. 26
52
10
D. Compilation Error or Runtime Error
Topic: Java length() Method | length() Method In Java - Strings

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.

Answer is C...m is initially assigned with ABCD.....XYZ and so the length will be 26....next we are appending existing string with abcdef..........z...so the lenght will be 52....finally m is assigned with "0123456789" which is a new object and its lenght is 10

Posted by Shashanka Mogaliraju    2015-03-13 13:47:44


here  option is C

in main() function we creating a string m="ABCDEFGHIJKLMNOPQRSTUVWXYZ"=>length=26
we print ist length is=>26
next another statement m+="abcdefgijklmnopqrstuvwxyz" means==>m=m+"abcdefgijklmnopqrstuvwxyz";
here main point + operator using b/w two number its becom a adition but it will be use in two strings mean it acts like concatination of the strings
thats why m="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgijklmnopqrstuvwxyz" and its length is 52. we print m.length()=> ist will print 52;
next another statement m="0123456789";means already m is refering to"A...z" but we assigning "0123...9" that why refere only presnt one m="0123456789"=>length is 10
we printing that length=>10
ourput is:
26
52
10

Posted by Goutham Singarapu    2015-03-13 16:51:13


ANS is C

Posted by Mânïshå Mùlchåndânï    2015-03-13 17:03:18


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Goutham Singarapu,  for 'Best Comment' and Shashanka Mogaliraju 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-03-16 02:28:12


© meritcampus 2019

All Rights Reserved.

Open In App