455,737 views
23 votes
23 votes
For questions 1-3, consider the following code:

What is output if the user types in 10 ? Click all that apply.

For questions 1-3, consider the following code: What is output if the user types in-example-1
User Jellyse
by
2.9k points

1 Answer

10 votes
10 votes

Answer:

A

B

D

Step-by-step explanation:

If the user inputs in 10, then 10 will be assigned to the variable x

The first if statement runs checking "if x != 7" which is essentially checking if x does not equal 7 which is true, since it's equal to 10, not 7. So the code in the if statement runs, outputting "A"

The second if statement runs checking "if x >= 10" which is essentially checking if x is greater than or equal to 10 which is true, since x is equal to 10. So the code in the if statement runs, outputting "B"

The third if statement runs checking "if x < 10" which is essentially checking if x is less than 10 which is false, since x is 10, and 10 is not less than 10, it's equal to it... so the code in the if statement doesn't run.

The last if statement runs checking "if x % 2 == 0" which uses the modulus operator, which is generally in the format A % B and returns the remainder from dividing A by B. If there is no remainder, that means this is equal to zero, or in other words A is divisible by B. This is especially useful when checking if a number is even, since if a number is even, it's divisible by 2. So "x % 2 == 0" is checking if the remainder from dividing x by 2 is zero, or in other words if x is even, and 10 is even so this is true, thus the if statement runs outputting "D". This if statement is only false for odd numbers since they're not divisible by 2 and will have a remainder of 1, which is not equal to zero.

User Symbiont
by
2.5k points