The learner will work with if statements and answer questions about them.

Evaluating If Statements

Question 1 of 10

Evaluating If Statements

Correct!

Which of these lines of code will set x equal to 0 when value is greater than 50?

Correct Answer

They all will

Explanation:

Each of the given if statements are formatted correctly to do what is asked.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

Which of these lines of code will set x equal to 0 when value is greater than 50?

Your Answer

Correct Answer

They all will

Explanation:

Each of the given if statements are formatted correctly to do what is asked.

 Next Question
Question 1 of 10

Evaluating If Statements

Which of these if statements should be used if the statement's code is to execute when value is equal to 0?

Question 1 of 10

Evaluating If Statements

Correct!

Which of these if statements should be used if the statement's code is to execute when value is equal to 0?

Correct Answer

if (value == 0)

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

Which of these if statements should be used if the statement's code is to execute when value is equal to 0?

Your Answer

Correct Answer

if (value == 0)

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

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;

Question 1 of 10

Evaluating If Statements

Correct!

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;

Correct Answer

8

Explanation:

Since value is not equal to 5, the line z = 8; will execute, setting z to 8.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

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;

Your Answer

Correct Answer

8

Explanation:

Since value is not equal to 5, the line z = 8; will execute, setting z to 8.

 Next Question
Question 1 of 10

Evaluating If Statements

What will the value of w be after the following lines of code are executed?

int w;
int test = 0;

if (test)
   w = 100;

Question 1 of 10

Evaluating If Statements

Correct!

What will the value of w be after the following lines of code are executed?

int w;
int test = 0;

if (test)
   w = 100;

Correct Answer

w has not been initialized or set to a value

Explanation:

Since test is 0, the statement if (test) will not execute its code.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

What will the value of w be after the following lines of code are executed?

int w;
int test = 0;

if (test)
   w = 100;

Your Answer

Correct Answer

w has not been initialized or set to a value

Explanation:

Since test is 0, the statement if (test) will not execute its code.

 Next Question
Question 1 of 10

Evaluating If Statements

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;

Question 1 of 10

Evaluating If Statements

Correct!

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;

Correct Answer

150

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

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;

Your Answer

Correct Answer

150

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

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;

Question 1 of 10

Evaluating If Statements

Correct!

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;

Correct Answer

20

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

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;

Your Answer

Correct Answer

20

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

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;

Question 1 of 10

Evaluating If Statements

Correct!

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;

Correct Answer

26

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

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;

Your Answer

Correct Answer

26

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

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;

Question 1 of 10

Evaluating If Statements

Correct!

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;

Correct Answer

7

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

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;

Your Answer

Correct Answer

7

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

What will the value of check be after the following code is executed?

int check = 9;
int value = 21;

if (value > 50);
   check = 10;

Question 1 of 10

Evaluating If Statements

Correct!

What will the value of check be after the following code is executed?

int check = 9;
int value = 21;

if (value > 50);
   check = 10;

Correct Answer

10

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

Incorrect

What will the value of check be after the following code is executed?

int check = 9;
int value = 21;

if (value > 50);
   check = 10;

Your Answer

Correct Answer

10

Explanation:

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.

 Next Question
Question 1 of 10

Evaluating If Statements

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;

Question 1 of 10

Evaluating If Statements

Correct!

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;

Correct Answer

10

Explanation:

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.

 Finish
Question 1 of 10

Evaluating If Statements

Incorrect

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;

Your Answer

Correct Answer

10

Explanation:

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.

 Finish
Question 1 of 10
Evaluating If Statements

Score

You have answered 5 of 10 questions correctly.

50%

Start Over

See other flashcards and apps related to:

Question 1 of 10
Author
Published
8/27/2015
Last Updated
8/27/2015
Tags

No Comments Yet