Informal Code Analysis (Loop Tracing)

AP Computer Science A· difficulty 4/5

String result = "";
for (int i = 1; i <= 3; i++) {
  for (int j = 1; j <= i; j++) {
    result += "*";
  }
  result += "|";
}
System.out.println(result);

What is printed?

  • A

    <strong>|</strong><em>|</em>***|

  • B

    <em><strong>|</strong>|</em>|

  • C

    <em>|</em>|*|

  • D

    <em>|<strong>|</strong></em>|

    check_circle

Explanation

For i=1: one "<em>", then "|". For i=2: two "</em>", then "|". For i=3: three "<em>", then "|". Result: "</em>|<strong>|</strong>*|".

Want 10 more like this — adaptive to your weak spots?

Related questions