Informal Run-Time Analysis MCQ Practice

Question 1 of 10

A cafeteria line numbers trays from 2 through 8 inclusive. How many times is c++ executed?

int n = 2;
int c = 0;
while (n <= 8) {
    c++;
    n++;
}
  1. A. 6
  2. B. 8
  3. C. 7
  4. D. 9

Question 2 of 10

An usher counts seats with a loop that includes both endpoints. How many times is print("S") executed?

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

Question 3 of 10

Fragment G uses i < 5. Fragment H uses i <= 5. Both start i at 1 and print once per pass. How many more times does Fragment H execute print("*") than Fragment G?

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

Question 4 of 10

A mailroom nested loop stamps every inner slot. How many times is print("@") executed?

for (int i = 1; i <= 2; i++) {
    for (int j = 1; j <= 5; j++) {
        System.out.print("@");
    }
}
  1. A. 7
  2. B. 5
  3. C. 10
  4. D. 25

Question 5 of 10

How many times is print("B") executed?

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

Question 6 of 10

A square of marks uses matching bounds. How many times is print("*") executed?

for (int i = 1; i <= 6; i++) {
    for (int j = 1; j <= 6; j++) {
        System.out.print("*");
    }
}
  1. A. 12
  2. B. 36
  3. C. 6
  4. D. 18

Question 7 of 10

A triangular count uses j <= i with i running from 1 through 6. How many times is n++ executed?

int n = 0;
for (int i = 1; i <= 6; i++) {
    for (int j = 1; j <= i; j++) {
        n++;
    }
}
  1. A. 21
  2. B. 36
  3. C. 18
  4. D. 12

Question 8 of 10

A mixed nested loop resets the inner index on every outer pass. How many times is c++ executed?

int c = 0;
for (int i = 1; i <= 3; i++) {
    int j = 0;
    while (j < 5) {
        c++;
        j++;
    }
}
  1. A. 5
  2. B. 8
  3. C. 15
  4. D. 4

Question 9 of 10

A kiln controller skips values by adding 3. How many times is print("T") executed?

for (int i = 0; i < 20; i += 3) {
    System.out.print("T");
}
  1. A. 20
  2. B. 6
  3. C. 8
  4. D. 7

Question 10 of 10

After both loops finish, what text does this Java code display?

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