Which of the expression correctly defines the statement? Check whether the…

2023

Which of the expression correctly defines the statement? Check whether the values of 3 variables age1, age2 and age3 are equal to each other.

  1. A.

    age1 == age2 == age3

  2. B.

    age1 == age2 and age2 == age3

  3. C.

    age1 != age2 != age3

  4. D.

    age1 = age2 and age2 = age3

  5. E.

    Question not attempted

Attempted by 1106 students.

Show answer & explanation

Correct answer: B

To check if three variables age1, age2, and age3 are equal, we need to ensure that all three have the same value. Let's analyze each option:

Option A: age1 == age2 == age3

This expression is evaluated as (age1 == age2) == age3. Since (age1 == age2) returns a boolean (True or False), this boolean is then compared with age3. This does not correctly check if all three variables are equal.

Option B: age1 == age2 and age2 == age3

This expression checks if age1 equals age2 and age2 equals age3. If both conditions are true, then by transitivity, age1, age2, and age3 must all be equal. This correctly verifies that all three variables have the same value.

Option C: age1 != age2 != age3

This expression checks if age1 is not equal to age2 and age2 is not equal to age3. It does not ensure that all three values are different, nor does it check for equality. This is not the correct way to verify that all three variables are equal.

Option D: age1 = age2 and age2 = age3

This expression uses the assignment operator '=' instead of the equality operator '=='. This will assign values rather than compare them, leading to a syntax or logical error. Therefore, this expression is incorrect.

Explore the full course: Up Lt Grade Assistant Teacher 2025