Advanced struc Prog with C++ ELET 422-02 Due: 04/03/2017 3 of 8 constexpr. d. no
ID: 3810381 • Letter: A
Question
Advanced struc Prog with C++ ELET 422-02 Due: 04/03/2017 3 of 8 constexpr. d. noexcept. Section 4.5 if Selection Statement 9. If grade has the value of 60, what will the following code display? if grade 60) cout Passed a. nothing. b, 60 c. Passed d. cout Passed 10. The data type bool: a. Can take on values true and false. b. Can take on any expression as a value. c can take on values -1.0 or 1. d. can only be used in a selection statement. Conditional operator (?:0 11 The conditional operator a. Is the only temary operator in C+. b. Is a unary operator. c. Associates from left to right. d. Accepts two operands. 12. Which of the following does not display correct if answer is equal to 7 and incorrect if answer is not equal to 7?Explanation / Answer
(9). The code snippet has an if condition which prints "Passed" using cout if the grade is either 60 or more.
So, as the grade has the value of 60, condition becomes true and so Passed is displayed.
So the answer is (c). Passed
(10). Data type bool is designed to accept the two values only:
true
false
So the answer is option (a).
Here, if it is represented in numbers, true is equivalent to 1 and false is equivalent to 0. But not -1. So option C is not completely correct.
So the answer is (a). can take on values true and false.
(11). The correct answer is (a) option itself.
The conditional operator requires 3 things: 1 expression, 1 value if the conditional expression is true and 1 value if that condition is false. So total 3 things is needed. And it is the only ternary operator in C++.
So the right answer is (a). Is the only ternary operator in C++.
(12). Option (a) is printing it in the correct way.
Option (b) is the same thing written in (a) but using conditional operator. But this will give a compile time error as answer == 7 condition should be enclosed within ( ). Otherwise it was also the right option.
Option (c) is valid one and it also has ( ) so it first evaluates the expression inside the brackets and the result is sent to cout to print. So this is also correct.
Option (d) is also valid implementation as we are first checking the condition first and then defining cout and the thing to print it with. This is also the valid one.
So, the answer is option (b) does not display the output as per mentioned.
(13). A block is a compound statement. The block is defined within { and } as ..
int main()
{ // starting of the block
...
return 0;
} // ending of the block
So the block will contain several statements and so the number of statements are not limited to 3. So option (a) is not valid. And it also contains declarations so option (b) is also invalid. And it is represented by { } not by ; so option (d) is also not right.
So the right answer is option (c). Is a compound statement.
(14).
Nested if-else statements are like:
if() //outer if
{
if() //inner if
{
...
}
else
{
...
}
}
else
{
...
}
Option (a) is invalid as we can have nesting of if statements till we need.
Option (b) is saying about a simple condition this also does not seem valid as the condition can also be a combination of 2 or 3 conditions like if(a>b && a>10) this is not a simple condition but can be written.
Option (c) is valid one. If the outer if condition is true, it goes inside its body and it has if-else which is the nested one so definitely they are executed. Moreover, the body of the outer if (i.e. the inner if-else) is only executed if the condition is correct. So this is a valid option.
This option is correct but not along with the word 'always'.
See the following structure for nested if-else:
if() //assume this condition is false
{
if() //assume this condition is true
{
...
}
else
{
...
}
}
else
{
if() //this condition is not true
{
...
}
else //ultimately this will be executed
{
...
}
}
Here, consider the inner if of the outer if. The condition is true for inner if() but the condition of outer if() is false so its body is not executed and control goes to the outer else statement. So, even if the condition is true for inner if() inside if(), it won't get executed.
Now the control has gone to else and inner if will return false so its else part gets executed. So ultimately the inner else of outer else gets executed in this case even though the inner if condition of outer if is true. So the word 'always' becomes false here.
So option (d) is also not valid.
So the correct answer is option (c). In an if body, an inner if...else executes only if the outer if statement's condition is true.
(15).
The given while loop is invalid because it is going to be an infinite loop because its condition is always becoming true.
Say for example we got sum = 900. So the while loop condition sum<=1000 is fulfilled so control goes inside the while loop and subtracts 30 from sum and stores in sum itself. So, now sum becomes 870.
Again the while loop condition is checked and as 870 < 1000, it is true so again the loop gets executed.
In this way, all the generated sum values will be less than 1000 and so it is going to be an infinite loop as it is never stopped because its condition is becoming right in every iteration. This is the wrong with the given while loop.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.