Nested if Statements MCQ Practice

Question 1 of 10

An aquarium nests a second salinity check. What text does this Java code display?

int salt = 20;
if (salt > 30)
{
    if (salt > 40)
        System.out.print("high");
}
System.out.print("check");
  1. A. high
  2. B. highcheck
  3. C. nothing
  4. D. check

Question 2 of 10

The aquarium nested check is used with a medium reading. What text does this Java code display?

int salt = 36;
if (salt > 30)
{
    if (salt > 40)
        System.out.print("high");
    else
        System.out.print("mid");
}
  1. A. high
  2. B. mid
  3. C. check
  4. D. highmid

Question 3 of 10

A trail sign uses if-else-if. What text does this Java code display?

int miles = 5;
if (miles < 3)
    System.out.print("easy");
else if (miles < 8)
    System.out.print("fair");
else
    System.out.print("hard");
  1. A. easy
  2. B. hard
  3. C. fair
  4. D. easyfair

Question 4 of 10

The trail sign is used at a longer distance. What text does this Java code display?

int miles = 8;
if (miles < 3)
    System.out.print("easy");
else if (miles < 8)
    System.out.print("fair");
else
    System.out.print("hard");
  1. A. fair
  2. B. easy
  3. C. hardfair
  4. D. hard

Question 5 of 10

A weather kiosk uses multiway selection. What text does this Java code display?

int t = 22;
if (t > 20)
    System.out.print("warm");
else if (t > 10)
    System.out.print("cool");
else
    System.out.print("cold");
  1. A. cool
  2. B. warmcool
  3. C. cold
  4. D. warm

Question 6 of 10

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

int t = 22;
int flags = 0;
if (t > 20)
    flags = flags + 1;
if (t > 10)
    flags = flags + 1;
System.out.print(flags);
  1. A. 1
  2. B. 0
  3. C. 2
  4. D. 22

Question 7 of 10

A tide chart uses a nested if with an else beside the inner if. What text does this Java code display?

int depth = 6;
if (depth > 4)
    if (depth > 12)
        System.out.print("deep");
    else
        System.out.print("ok");
  1. A. deep
  2. B. ok
  3. C. deepok
  4. D. nothing

Question 8 of 10

Misleading indentation is used with a shallow reading. What text does this Java code display?

int depth = 2;
if (depth > 4)
    if (depth > 12)
        System.out.print("deep");
else
    System.out.print("ok");
System.out.print("map");
  1. A. okmap
  2. B. deepmap
  3. C. ok
  4. D. map

Question 9 of 10

A kiln nested update changes a stored temperature. What value does this Java code display?

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

Question 10 of 10

When is the inner condition of a nested if evaluated?

  1. A. Always, even if the outer condition is false.
  2. B. Only when the outer condition is true.
  3. C. Only when the outer condition is false.
  4. D. After the inner body has already run.