The learner will work with if statements and answer questions about them.
Which of these lines of code will set x equal to 0 when value is greater than 50?
Which of these lines of code will set x equal to 0 when value is greater than 50?
They all will
Each of the given if statements are formatted correctly to do what is asked.
Which of these lines of code will set x equal to 0 when value is greater than 50?
They all will
Each of the given if statements are formatted correctly to do what is asked.
Which of these if statements should be used if the statement's code is to execute when value is equal to 0?
Which of these if statements should be used if the statement's code is to execute when value is equal to 0?
if (value == 0)
The only if statement shown that uses the equality comparison is if (value == 0). Note that if (value = 0) will set value to 0 AND not execute the if statement's code.
Which of these if statements should be used if the statement's code is to execute when value is equal to 0?
if (value == 0)
The only if statement shown that uses the equality comparison is if (value == 0). Note that if (value = 0) will set value to 0 AND not execute the if statement's code.
What will the value of z be after the following lines of code are executed?
int z;
int value = 6;
if (value != 5)
z = 8;
What will the value of z be after the following lines of code are executed?
int z;
int value = 6;
if (value != 5)
z = 8;
8
Since value is not equal to 5, the line z = 8; will execute, setting z to 8.
What will the value of z be after the following lines of code are executed?
int z;
int value = 6;
if (value != 5)
z = 8;
8
Since value is not equal to 5, the line z = 8; will execute, setting z to 8.
What will the value of w be after the following lines of code are executed?
int w;
int test = 0;
if (test)
w = 100;
What will the value of w be after the following lines of code are executed?
int w;
int test = 0;
if (test)
w = 100;
w has not been initialized or set to a value
Since test is 0, the statement if (test) will not execute its code.
What will the value of w be after the following lines of code are executed?
int w;
int test = 0;
if (test)
w = 100;
w has not been initialized or set to a value
Since test is 0, the statement if (test) will not execute its code.
What will the value of look be after the following lines of code are executed?
int look = 900;
int value = 75;
look = look - value;
if (value)
look = 2 * value;
What will the value of look be after the following lines of code are executed?
int look = 900;
int value = 75;
look = look - value;
if (value)
look = 2 * value;
150
Tracing through, look = look - value; sets look to 900 - 75, which is 825. As value is nonzero, the if (value) will execute its code. Then the look = 2 * value; will set look to 2 * 75, or 150.
What will the value of look be after the following lines of code are executed?
int look = 900;
int value = 75;
look = look - value;
if (value)
look = 2 * value;
150
Tracing through, look = look - value; sets look to 900 - 75, which is 825. As value is nonzero, the if (value) will execute its code. Then the look = 2 * value; will set look to 2 * 75, or 150.
What will the value of x be after the following lines of code are executed?
int value = 100;
int x;
if (value > 50)
x = 10;
if (value > 80)
x = 20;
if (value > 100)
x = 30;
What will the value of x be after the following lines of code are executed?
int value = 100;
int x;
if (value > 50)
x = 10;
if (value > 80)
x = 20;
if (value > 100)
x = 30;
20
First, value is 100 so if (value > 50) will execute its code x = 10; setting x to 10. Next, since value is still 100, if (value > 80) will execute its code x = 20; setting x to 20. Finally, as value is 100, if (value > 100) will not execute its code. Thus the value of x will be 20.
What will the value of x be after the following lines of code are executed?
int value = 100;
int x;
if (value > 50)
x = 10;
if (value > 80)
x = 20;
if (value > 100)
x = 30;
20
First, value is 100 so if (value > 50) will execute its code x = 10; setting x to 10. Next, since value is still 100, if (value > 80) will execute its code x = 20; setting x to 20. Finally, as value is 100, if (value > 100) will not execute its code. Thus the value of x will be 20.
What will the value of y be after the following lines of code are executed?
int value = 100;
int y = value;
if (value > 10)
value = value / 2;
if (value < 100)
{
value = value / 2;
y = 1;
}
if (value > 0)
y = value + 1;
What will the value of y be after the following lines of code are executed?
int value = 100;
int y = value;
if (value > 10)
value = value / 2;
if (value < 100)
{
value = value / 2;
y = 1;
}
if (value > 0)
y = value + 1;
26
Initially value is 100 and y is set to match value, so it's 100 as well. Since 100 > 10, if (value > 10) will execute its code value = value / 2; changing value to 50.
As 50 < 100, if (value < 100) will execute its two lines of code value = value / 2; and y = 1; so we have value set to 25 and y set to 1.
As 25 > 0, if (value > 0) will execute its code y = value + 1;. Since value is 25, y will be 25 + 1 or 26.
What will the value of y be after the following lines of code are executed?
int value = 100;
int y = value;
if (value > 10)
value = value / 2;
if (value < 100)
{
value = value / 2;
y = 1;
}
if (value > 0)
y = value + 1;
26
Initially value is 100 and y is set to match value, so it's 100 as well. Since 100 > 10, if (value > 10) will execute its code value = value / 2; changing value to 50.
As 50 < 100, if (value < 100) will execute its two lines of code value = value / 2; and y = 1; so we have value set to 25 and y set to 1.
As 25 > 0, if (value > 0) will execute its code y = value + 1;. Since value is 25, y will be 25 + 1 or 26.
What will the value of x be after the following code is executed?
int x;
int count = 5;
if (count == 5)
x = 7;
count = count - 1;
if (count < 0)
x = 1;
What will the value of x be after the following code is executed?
int x;
int count = 5;
if (count == 5)
x = 7;
count = count - 1;
if (count < 0)
x = 1;
7
At first x has no value. count has a value of 5. Since 5 == 5 is true (recall we need to use the equality check operation of two equal signs here) if (count == 5) will execute its code x = 7; setting the value of x to 7.
Next count = count - 1; will set count to 5 - 1 which is 4.
Since 4 < 0 is false, if (count < 0) will not execute is code, so x will still have the value of 7.
What will the value of x be after the following code is executed?
int x;
int count = 5;
if (count == 5)
x = 7;
count = count - 1;
if (count < 0)
x = 1;
7
At first x has no value. count has a value of 5. Since 5 == 5 is true (recall we need to use the equality check operation of two equal signs here) if (count == 5) will execute its code x = 7; setting the value of x to 7.
Next count = count - 1; will set count to 5 - 1 which is 4.
Since 4 < 0 is false, if (count < 0) will not execute is code, so x will still have the value of 7.
What will the value of check be after the following code is executed?
int check = 9;
int value = 21;
if (value > 50);
check = 10;
What will the value of check be after the following code is executed?
int check = 9;
int value = 21;
if (value > 50);
check = 10;
10
To start with, check is 9 and value is 21. Since 21 > 50 is false, if (value > 50) will not execute its code. However, that line is actually written if (value > 50); and the ; at the end will make the code of that if statement blank.
Thus the program will continue to the next line having completed the if statement and execute check = 10; setting the value of check to 10.
What will the value of check be after the following code is executed?
int check = 9;
int value = 21;
if (value > 50);
check = 10;
10
To start with, check is 9 and value is 21. Since 21 > 50 is false, if (value > 50) will not execute its code. However, that line is actually written if (value > 50); and the ; at the end will make the code of that if statement blank.
Thus the program will continue to the next line having completed the if statement and execute check = 10; setting the value of check to 10.
What will the value of lit be after the following code is executed?
int value = 7;
int lit = 5;
if (value > 10)
value = 0;
lit = 10;
What will the value of lit be after the following code is executed?
int value = 7;
int lit = 5;
if (value > 10)
value = 0;
lit = 10;
10
Initially, lit has a value of 5. Since 7 > 10 is false, the if (value > 10) will not execute its code. However, notice that the code for the if statement is only value = 0; as there are no curly brackets. Therefore, the program will continue and execute lit = 10; so the value of lit will be 10.
What will the value of lit be after the following code is executed?
int value = 7;
int lit = 5;
if (value > 10)
value = 0;
lit = 10;
10
Initially, lit has a value of 5. Since 7 > 10 is false, the if (value > 10) will not execute its code. However, notice that the code for the if statement is only value = 0; as there are no curly brackets. Therefore, the program will continue and execute lit = 10; so the value of lit will be 10.
You have answered 5 of 10 questions correctly.
50%