Strings & the String class

Computer Science · Java

Question 1 of 10

0

    What number does this Java code print? public class Main { public static void main(String[] args) { String a = "ivory"; String b = "cargo"; System.out.println((a + b).length()); } }

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Strings & the String class explained

    A String in Java is an object that holds a sequence of characters. Strings are immutable, which means their contents cannot change after creation. Methods such as length, charAt, substring and toUpperCase return values and never modify the original String.

    Create a string with double quotes, as in String name = "Ada";. Compare strings with equals, not ==. The == operator checks whether two references point to the same object, so two strings with the same text can still give false. Indexes start at 0, so charAt(0) is the first character, and substring(start, end) includes start but excludes end. Because strings are immutable, a line such as word.toUpperCase(); on its own changes nothing, and the result must be assigned. Building a long string by repeated + in a loop creates many objects, so StringBuilder is the better choice there.

    String word = "java";
    word.toUpperCase();
    System.out.println(word.charAt(1) + word.substring(2));
    

    Worked example

    Trace the program above.

    1. word.toUpperCase() returns "JAVA", but the result is discarded, so word still holds "java".
    2. charAt(1) is the character at index 1, which is 'a'.
    3. substring(2) starts at index 2 and runs to the end, giving "va".
    4. The char 'a' joined to the String "va" gives "ava", so the output is ava.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      What number does this Java code print?

      public class Main { public static void main(String[] args) { String a = "echo"; String b = "hotel"; System.out.println(a.concat(b).length()); } }

    • Medium

      What number does this Java code print?

      public class Main { public static void main(String[] args) { String s = "anchor"; System.out.println(s.lastIndexOf('c')); } }

    • Hard

      What number does this Java code print?

      public class Main { public static void main(String[] args) { String s = "timber"; s.concat("kilo"); s.toUpperCase(); s = s.substring(2, 5); s = s + "kil"; System.out.println(s.length()); } }