AP Computer Science A · Topic 2.6

String Objects: Concatenation, Literals, and More Practice

Part of Using Objects.

Practice questions

27

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 27 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 2/5

    String s = "Score: ";
    int n = 5;
    System.out.println(s + 3 + n);

    What does this print?

    • A

      Score: 35

      check_circle
    • B

      Score: 8

    • C

      Score: 53

    • D

      Compile error

    Why

    Left-to-right + with a String produces String concatenation: "Score: " + 3 = "Score: 3", then + 5 yields "Score: 35".

  2. Sample 2difficulty 2/5

    What does <code>"Hi " + 5 + 2</code> produce?

    • A

      7 Hi

    • B

      Hi 52

      check_circle
    • C

      Hi 7

    • D

      Hi 5 2

    Why

    Left-to-right: "Hi " + 5 → "Hi 5"; + 2 → "Hi 52" (string concatenation, not addition).

  3. Sample 3difficulty 2/5

    String a = "Hello";
    String b = "World";
    System.out.println(a + " " + b);

    What is printed?

    • A

      Hello+World

    • B

      HelloWorld

    • C

      Hello World

      check_circle
    • D

      Hello World

    Why

    The + operator concatenates the strings with a space in between.

  4. Sample 4difficulty 3/5

    String a = "Cat";
    String b = "cat";
    System.out.println(a.equals(b));

    What is printed?

    • A

      true

    • B

      false

      check_circle
    • C

      1

    • D

      0

    Why

    equals is case-sensitive; "Cat" and "cat" differ in case, so it returns false.

  5. Sample 5difficulty 3/5

    What does <code>"" + 1 + 2 + 3</code> produce?

    • A

      Compile error

    • B

      6

    • C

      123

      check_circle
    • D

      1.2.3

    Why

    Empty string forces concatenation; result is "123".