Informal Run-Time Analysis MCQ Practice

Question 1 of 10

A stopwatch club counts ticks. How many times is ticks++ executed?

int ticks = 0;
for (int i = 1; i <= 5; i++) {
    ticks++;
}
  1. A. 4
  2. B. 5
  3. C. 6
  4. D. 1

Question 2 of 10

A lap board prints a letter while a counter stays positive. How many times is print("A") executed?

int k = 3;
while (k > 0) {
    System.out.print("A");
    k--;
}
  1. A. 2
  2. B. 4
  3. C. 3
  4. D. 0

Question 3 of 10

A starter pistol prints a cue. How many times is print("Go") executed?

for (int i = 0; i < 4; i++) {
    System.out.print("Go");
}
  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 0

Question 4 of 10

Two fragments print a dash. How many more times does Fragment P execute print("-") than Fragment Q?

for (int i = 1; i <= 6; i++) {
    System.out.print("-");
}
for (int i = 1; i < 6; i++) {
    System.out.print("-");
}

Fragment P is the first loop. Fragment Q is the second loop.

  1. A. 0
  2. B. 2
  3. C. 6
  4. D. 1

Question 5 of 10

A warehouse stamps nested labels. How many times is c++ executed?

int c = 0;
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 4; j++) {
        c++;
    }
}
  1. A. 7
  2. B. 12
  3. C. 4
  4. D. 3

Question 6 of 10

A printer marks an outer header and inner items. How many times is print("I") executed?

for (int i = 1; i <= 2; i++) {
    System.out.print("O");
    for (int j = 1; j <= 3; j++) {
        System.out.print("I");
    }
}
  1. A. 2
  2. B. 3
  3. C. 5
  4. D. 6

Question 7 of 10

A triangular stamp count uses an inner bound that depends on the outer variable. How many times is t++ executed?

int t = 0;
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {
        t++;
    }
}
  1. A. 16
  2. B. 10
  3. C. 8
  4. D. 4

Question 8 of 10

Originally the outer loop uses i <= 4 and the inner loop uses j <= 5 around a print(".") statement. If only the outer bound is changed to i <= 6, how many times does print(".") execute?

for (int i = 1; i <= 6; i++) {
    for (int j = 1; j <= 5; j++) {
        System.out.print(".");
    }
}
  1. A. 20
  2. B. 24
  3. C. 30
  4. D. 11

Question 9 of 10

Compare the two print statements. How many more times is print("R") executed than print("L")?

for (int a = 0; a < 5; a++) {
    System.out.print("L");
    for (int b = 0; b < 3; b++) {
        System.out.print("R");
    }
}
  1. A. 10
  2. B. 15
  3. C. 5
  4. D. 8

Question 10 of 10

Fragment P and Fragment Q each increment x. Which statement is true?

int x = 0;
for (int i = 1; i <= 10; i++) {
    x++;
}
int y = 0;
int i = 1;
while (i <= 10) {
    y++;
    i += 2;
}

Fragment P is the for loop. Fragment Q is the while loop.

  1. A. P executes x++ 10 times and Q executes y++ 10 times
  2. B. P executes x++ 11 times and Q executes y++ 5 times
  3. C. P executes x++ 10 times and Q executes y++ 5 times
  4. D. P executes x++ 10 times and Q executes y++ 6 times