Practice Challenges for Fun
Explore how to implement decision-making in Java by working through practical challenges that use if statements, comparisons, and boolean expressions. This lesson helps you gain confidence in writing Java code to compare values, test conditions, and adjust variables based on criteria.
We'll cover the following...
Quiz
Attempt the following quiz questions
Which Java statement(s) assign the larger of two given integers, n1 and n2, to the variable larger. You can select multiple correct answers. Multi-select
if (n1 < n2)
larger = n2;
else
larger = n1;
larger = n1;
if (n1 < n2)
larger = n2;
if (n1 > n2)
larger = n2;
else
larger = n1;
if (n1 < n2)
larger = n1;
else
larger = n2;
Evaluate each of the following expressions by hand, where x, y, and z contain 1, 3, and 5, respectively
(x == y) || (y > z)
True
False
Consider the boolean variables x, y, and z. What is the value of the expression (x && y) || !z in each of the following cases?
When x, y, and z are true, (x && y) || !z is
True
False
Consider the boolean variables x, y, and z. What is the value of the expression !(x && (y || !z)) in each of the following cases?
When x, y, and z are true, !(x && (y || !z)) is
True
False
How would you compare two variables having the same primitive type and how would you compare two variables having the same object type.
Write an assignment statement that is equivalent to the if-else statement shown below:
int count;
. . .
boolean result;
if (count % 10 == 0)
result = true;
else
result = false;
Challenge 1: Compare integers
Add Java statements to the following program that compares two given integers, n1 and n2 ...