Implementing Selection and Iteration Algorithms MCQ Practice

Question 1 of 10

A prize booth checks whether a ticket number is even. What text does this Java code display?

int ticket = 18;
if (ticket % 2 == 0) {
    System.out.print("even");
} else {
    System.out.print("odd");
}
  1. A. even
  2. B. odd
  3. C. 0
  4. D. 18

Question 2 of 10

A clerk counts even booth numbers from 7 through 12. What text does this Java code display?

int n = 7;
int evens = 0;
while (n <= 12) {
    if (n % 2 == 0) {
        evens++;
    }
    n++;
}
System.out.print(evens);
  1. A. 6
  2. B. 3
  3. C. 2
  4. D. 12

Question 3 of 10

A lock code is peeled from right to left. What text does this Java code display?

int code = 582;
int last = code % 10;
code /= 10;
int middle = code % 10;
System.out.print(last);
System.out.print(middle);
  1. A. 28
  2. B. 58
  3. C. 85
  4. D. 25

Question 4 of 10

A clerk adds every digit of a parcel number. What text does this Java code display?

int n = 407;
int sum = 0;
while (n > 0) {
    sum += n % 10;
    n /= 10;
}
System.out.print(sum);
  1. A. 11
  2. B. 407
  3. C. 4
  4. D. 18

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. A ride measures five rider heights. The operator types 4, then 9, then 12, then 5, then 8. What text does this Java code display?

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

Question 6 of 10

How many integers from 4 through 16 are divisible by 3? What text does this Java code display?

int count = 0;
for (int k = 4; k <= 16; k++) {
    if (k % 3 == 0) {
        count++;
    }
}
System.out.print(count);
  1. A. 4
  2. B. 13
  3. C. 5
  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 crate weights are typed in this order: 11, 4, 9, 4, 7. What text does this Java code display?

int remaining = 5;
int smallest = Input.readInt();
remaining--;
while (remaining > 0) {
    int next = Input.readInt();
    if (next < smallest) {
        smallest = next;
    }
    remaining--;
}
System.out.print(smallest);
  1. A. 11
  2. B. 7
  3. C. 9
  4. D. 4

Question 8 of 10

A generator updates a scalar and tracks the largest value seen. What text does this Java code display?

int value = 3;
int largest = value;
for (int i = 1; i <= 4; i++) {
    value = value * 2 - 1;
    if (value > largest) {
        largest = value;
    }
}
System.out.print(largest);
  1. A. 33
  2. B. 3
  3. C. 17
  4. D. 31

Question 9 of 10

A scanner counts even digits of a badge number, peeling from the right. What text does this Java code display?

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

Question 10 of 10

A clerk averages the integers 5 through 9 using int division. What text does this Java code display?

int total = 0;
int howMany = 0;
int n = 5;
while (n <= 9) {
    total += n;
    howMany++;
    n++;
}
System.out.print(total / howMany);
  1. A. 35
  2. B. 5
  3. C. 7
  4. D. 9