Which code snippet prints 'Adult' when age is 18 or more, otherwise prints 'Child'?

Study for the End of Year 8 Computer Science Test. Prepare with flashcards and multiple choice questions, each with hints and explanations. Get ready for your exam!

Multiple Choice

Which code snippet prints 'Adult' when age is 18 or more, otherwise prints 'Child'?

Explanation:
The key idea is to use a condition that includes the age at which someone is considered an adult. Since adulthood starts at 18, you need to check if age is greater than or equal to 18. The correct approach prints "Adult" when this condition is true and "Child" otherwise, which matches the requirement for ages 18 and up. If you used a strict greater-than check (age > 18), someone who is exactly 18 would be treated as a child. If you used a higher threshold like age >= 21, ages 18, 19, and 20 would be misclassified as not adult. And if the outputs were swapped, adults would be labeled as "Child." So the inclusive check with age >= 18 and the corresponding print statements is the right fit.

The key idea is to use a condition that includes the age at which someone is considered an adult. Since adulthood starts at 18, you need to check if age is greater than or equal to 18. The correct approach prints "Adult" when this condition is true and "Child" otherwise, which matches the requirement for ages 18 and up.

If you used a strict greater-than check (age > 18), someone who is exactly 18 would be treated as a child. If you used a higher threshold like age >= 21, ages 18, 19, and 20 would be misclassified as not adult. And if the outputs were swapped, adults would be labeled as "Child." So the inclusive check with age >= 18 and the corresponding print statements is the right fit.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy