Implementing Selection and Iteration Algorithms MCQ Practice

Question 1 of 10

Hall lockers on odd numbers open north. What text does this Java code display?

int locker = 25;
if (locker % 2 == 0) {
    System.out.print("south");
} else {
    System.out.print("north");
}
  1. A. south
  2. B. north
  3. C. 1
  4. D. 25

Question 2 of 10

A countdown visits even integers and counts those divisible by 5. What text does this Java code display?

int n = 20;
int hits = 0;
while (n >= 8) {
    if (n % 5 == 0) {
        hits++;
    }
    n -= 2;
}
System.out.print(hits);
  1. A. 2
  2. B. 7
  3. C. 3
  4. D. 20

Question 3 of 10

A ZIP helper peels the ones digit, then the tens digit. What text does this Java code display?

int zip = 9403;
int ones = zip % 10;
zip /= 10;
int tens = zip % 10;
System.out.print(tens);
System.out.print(ones);
  1. A. 30
  2. B. 03
  3. C. 94
  4. D. 3

Question 4 of 10

How many odd digits does 1350 contain? What text does this Java code display?

int n = 1350;
int odd = 0;
while (n > 0) {
    int d = n % 10;
    if (d % 2 != 0) {
        odd++;
    }
    n /= 10;
}
System.out.print(odd);
  1. A. 4
  2. B. 1
  3. C. 3
  4. D. 1350

Question 5 of 10

Contract: Input.readInt() reads the next integer the user types and returns it as int. Each call consumes one integer. Remembering Scanner method names is not required. Six quiz scores are typed in this order: 2, 7, 6, 9, 4, 8. What text does this Java code display?

int left = 6;
int high = 0;
while (left > 0) {
    int score = Input.readInt();
    if (score > 6) {
        high++;
    }
    left--;
}
System.out.print(high);
  1. A. 3
  2. B. 6
  3. C. 2
  4. D. 8

Question 6 of 10

How many integers from 1 through 12 are even and also greater than 5? What text does this Java code display?

int count = 0;
for (int k = 1; k <= 12; k++) {
    if (k % 2 == 0) {
        if (k > 5) {
            count++;
        }
    }
}
System.out.print(count);
  1. A. 6
  2. B. 4
  3. C. 12
  4. D. 3

Question 7 of 10

Contract: Input.readInt() reads the next integer the user types and returns it as int. Each call consumes one integer. Remembering Scanner method names is not required. Five temperatures are typed in this order: 5, 12, 9, 20, 7. What text does this Java code display?

int left = 4;
int biggest = Input.readInt();
while (left > 0) {
    int next = Input.readInt();
    if (next > biggest) {
        biggest = next;
    }
    left--;
}
System.out.print(biggest);
  1. A. 20
  2. B. 5
  3. C. 12
  4. D. 7

Question 8 of 10

A halving process tracks the smallest int seen. What text does this Java code display?

int value = 18;
int smallest = value;
while (value > 3) {
    value = value / 2;
    if (value < smallest) {
        smallest = value;
    }
}
System.out.print(smallest);
  1. A. 18
  2. B. 4
  3. C. 3
  4. D. 2

Question 9 of 10

Only even digits of 2861 are added, peeling from the right. What text does this Java code display?

int n = 2861;
int total = 0;
while (n > 0) {
    int d = n % 10;
    if (d % 2 == 0) {
        total += d;
    }
    n /= 10;
}
System.out.print(total);
  1. A. 17
  2. B. 16
  3. C. 8
  4. D. 2861

Question 10 of 10

The integers 1 through 4 are averaged with int division. What text does this Java code display?

int total = 0;
int terms = 0;
for (int n = 1; n <= 4; n++) {
    total += n;
    terms++;
}
System.out.print(total / terms);
  1. A. 2
  2. B. 10
  3. C. 2.5
  4. D. 4