Nested if Statements MCQ Practice

Question 1 of 10

A pool desk nests a second age check inside the first. What text does this Java code display?

int age = 8;
if (age >= 12)
{
    if (age >= 16)
        System.out.print("senior");
}
System.out.print("done");
  1. A. senior
  2. B. seniordone
  3. C. done
  4. D. nothing

Question 2 of 10

The same nested selection is used with a teen guest. What text does this Java code display?

int age = 14;
if (age >= 12)
{
    if (age >= 16)
        System.out.print("senior");
    else
        System.out.print("youth");
}
  1. A. senior
  2. B. youth
  3. C. senioryouth
  4. D. nothing

Question 3 of 10

The nested age check is used with an older guest. What text does this Java code display?

int age = 18;
if (age >= 12)
{
    if (age >= 16)
        System.out.print("senior");
    else
        System.out.print("youth");
}
  1. A. senior
  2. B. youth
  3. C. done
  4. D. senioryouth

Question 4 of 10

A speed sign uses multiway selection. What text does this Java code display?

int speed = 45;
if (speed < 20)
    System.out.print("slow");
else if (speed < 50)
    System.out.print("ok");
else
    System.out.print("fast");
  1. A. slow
  2. B. fast
  3. C. slowok
  4. D. ok

Question 5 of 10

The speed sign reads a larger value. What text does this Java code display?

int speed = 50;
if (speed < 20)
    System.out.print("slow");
else if (speed < 50)
    System.out.print("ok");
else
    System.out.print("fast");
  1. A. ok
  2. B. slow
  3. C. fast
  4. D. okfast

Question 6 of 10

An if-else-if chain could match more than one condition if the tests were separate. What text does this Java code display?

int n = 12;
if (n > 10)
    System.out.print("A");
else if (n > 5)
    System.out.print("B");
else
    System.out.print("C");
  1. A. B
  2. B. A
  3. C. AB
  4. D. C

Question 7 of 10

The same thresholds are written as separate if statements instead of if-else-if. What value does this Java code display?

int n = 12;
int k = 0;
if (n > 10)
    k = k + 1;
if (n > 5)
    k = k + 2;
System.out.print(k);
  1. A. 3
  2. B. 1
  3. C. 2
  4. D. 0

Question 8 of 10

An else is written next to two if headers. What text does this Java code display?

int n = 4;
if (n > 2)
    if (n > 10)
        System.out.print("hi");
    else
        System.out.print("lo");
  1. A. hi
  2. B. nothing
  3. C. hilo
  4. D. lo

Question 9 of 10

Indentation suggests the else belongs to the outer if. What text does this Java code display?

int n = 1;
if (n > 2)
    if (n > 10)
        System.out.print("hi");
else
    System.out.print("lo");
System.out.print("end");
  1. A. loend
  2. B. hiend
  3. C. end
  4. D. lo

Question 10 of 10

A nested check records whether the inner condition is reached. What value does this Java code display?

int outer = 3;
int seen = 0;
if (outer > 10)
{
    seen = seen + 1;
    if (outer > 0)
        seen = seen + 8;
}
else
{
    seen = seen + 2;
}
System.out.print(seen);
  1. A. 9
  2. B. 2
  3. C. 0
  4. D. 8