Chapter 4 Quiz and Answers1. What do statements do in C++?
1a. C++ statements control the sequence of execution, evaluate expressions, or do nothing at all.
2. Where can you put a compound statement?
2a. Any place you can put a single statement.
3. What’s another name for a compound statement?
3a. A block.
4. What syntax identifies the beginning and ending of a block?
4a. The curly braces. { and }.
5. What is an expression in C++?
5a. Anything that evaluates to a value.
6. Which side of the equal operator can an expression ALWAYS be on? Why not the other side?
6a. The RIGHT side. The other side if for l-values only, or rather specific addresses for storing values. You can’t store a value IN an expression.
7. What do operators act upon?
7a. Operands.
8. Can an expression be acted upon by operators?
8a. Yes. Any expression can be used as an operand of an operator.
9. What does the assignment operator do?
9a. Changes the value of the left operand (left side of operator) to match the value of the expression on the right operand (right side of operator).
10. What is an r-value and an l-value? How does this relate to question 6 above?
10a. A operand that can legally on the left side of an operator is an l-value. That which can be on the right side in an r-value. All l-values are also r-value, but the reverse is not true. An expression is an r-value, which is why it cannot go on the left side.
11. What are the 5 mathematical operators and what do they do? Give examples of inputs and outputs.
11a. Addition (+), Subtraction (-), Multiplication (*), Division (/), and modulus (%). A = 2+2; B = 2-2; C = 2*2; D=2/2; E=2%3;
12. What happens when you subtract a large unsigned number for a smaller unsigned number?
12a.You get underflow, which results in a value which is a very large number.
13.
[Extra Credit]What is integer truncation? How can it be avoided?
13a. Integer truncation is what happens when a real number with a fractional value is stored in an integer variable. To be more specific, the fractional component is “lopped off,” in essence rounding down to the nearest whole number. For example: int myValue = 2.2; would result in myValue being 2. Note that sometimes this problem can be less obvious. In the example I provided before we were explicitly using fractional values, but keep in mind the problem can occur when using entirely integers as well. For example…what’s 2/3? .66666…. But when you divide 2/3 and store it in an integer variable, the result is ‘truncated’ to 0. int myValue = 2/3; [myValue := 0] The solution is to be aware of your data, and have a clear understanding of the types of results to expect within your expressions. If there is a chance that an expression will have a fractional component and you NEED that fractional component, store your results in ‘float’ or ‘double’ types, rather than integer types.
14. What are the increment/decrement operators, and how are they used. Show examples.
14a. ++ and --. They are used to take a variable, increment or decrements its value and store the result back in the same variable. This is most commonly used in iterative devices such as for loops or while loops. Example:
for( int i = 0; i < 10; i++ ){ // use i as an index into an array, etc…}
15. What is the difference between prefix and postfix increment/decrement? What are the results? Show examples.
15a. With prefix the value of the variable is changed before the assignment operator is applied. With postfix the value of the variable is changed after evaluation of the assignment operator.
Example:
// In this example I is set to 10, then the value of I is stored in j, and finally the value stored in I is incremented.
int i = 10;
int j = i++;
// In this example, I is set to 10, then it’s incremented to 11, and finally the result is stored in j
int j = ++i;
16. What is the precedence of the mathematical operators?
16a. %, * and / are evaluated first, in order from left to right…. and then + and -, again from left to right.
17. Are the operators left-to-right or right-to-left?
17a. Left to right
18. What about the assignment operator?
18a. Right to left
19. What operator can ALWAYS be used to change the precedence of an expression?
19a. The parentheses
20. How are nested parentheses read? Show examples.
20a. From the inside out. 2 * ( 5 – (2 + 2 ) ) ; Here the 2 + 2 is evaluated first, even though its lowest in precedence, and is on the right side of the expression. Next the 5- is evaluated, and finally the 2*. So as you can see by using parentheses you can change both the precedence and the left to right order of the expression.
21. Can all expressions be evaluated for their truth?
21a. Yes. All expressions return a value, and the result of that value can be tested for zero or non-zero. 0 is treated as false, non-zero is treated as true.
22. What data type is exclusively for truthiness? (tip of the hat to Stephen Colbert)
22a. bool.
23. What is the ANSI standard size of that data type?
23a. 1 byte.
24. What are the six relational operators? Show examples of what values they return with different inputs.
24a. equals (==), not equals (!=), Greater than (>), Greater than or equal to (>=), Less than (<), Less than or equal to (<=).
1 == 1; // true
1 == 2; // false
1 != 2; // true
1 != 1; // false
3 > 2; // true
3 > 3; // false
2 >= 2; // true
2 >= 3; false
3 < 5; // true
3 < 3; // false
3 <= 3; // true
3 <= 2; false
25. What does an ‘if’ statement do?
25a. It enables you to test for a condition, and then branch to different parts of your code, depending on the success or failure of the test.
26. When is an if-block executed, when is it skipped?
26a. It is executed when the test for the condition passes (is true). It is skipped when the test is false.
27. What does an ‘else’ statement do?
27a. It allows you to enter a block of code if a test condition fails.
28. When is an else-block executed, when is it skipped?
28a. It is executed when no previous test in the same if-else chain succeeds. It is never skipped. If it is ‘reached,’ it is executed.
29.
[Extra Credit] What does an ‘else if’ statement do? When is executed and when is it skipped?
29a. An if-else statement combines the functionality of an if and an else statement. That is, IF some condition is met, then the block will be executed; ONLY, when no previous condition within an if-else chain has already been met. Unlike the “else” statement, an ‘if else’ statement will be skipped if the condition being tested for is not true.
30. What are the 3 logic operators in C++? How are they used? Show examples.
30a. AND (&&), OR (||), NOT (!).
&& is used to determine the union of multiple expressions. If both operands of a && are true, the result is true. If either operand is false, the result if false.
|| is used to determine the junction of multiple expressions. If either operand of || is true, the result is true. If BOTH operands are false, the result is false.
! is used to negate the value of an expression. If the operand is true, the result is false. If the operand is false, the result is true.
31. What is “Short Circuit Evaluation?” How might it be beneficial?
31a. Short circuit evaluation is a logical optimization put in place by the compiler which prevents compound expressions from being further evaluated once the result is known. For example…with AND (&&) statements, the result is true only if BOTH operands are true. So as soon as you know one is false, there is no reason to test the result of the second, because the total result of the compound expressions is already false. Likewise with OR (||) statements. If the first operand is evaluated to be true, there is no reason to test the value of the second operand, the value of the compound expressions is decidedly true.
32. What is the precedence of the logical and conditional operators?
32a. !, then &&, then ||…from left to right.
33. What is the conditional operator? How is it used? When might it be useful? Show examples.
33a. The conditional operator, also called the ternary operator as it is the only C++ operator with 3 inputs, tests the result of a logical expression. If the value is true, the result is the same as the second operand. If the result was false, the value returned is the same as the 3rd operand.
Chapter 4 Exercises1. The equation for distance traveled (dist) with a constant velocity is equal to the rate of movement (rate) multiplied by the time interval (time) traveling. (dist=rate*time) Write a program which prompts the user for rate and time, computes the distance traveled, and then outputs the result to the screen.
#include <iostream>int main( void ){ using namespace std; int dist, rate, time; cout << "Enter the rate of movement:" << endl; cin >> rate; cout << endl << "Enter the amount of time traveled:" << endl; cin >> time; dist = rate * time; cout << "The distance traveled is: " << dist << endl; return 0;}
2. Write a program that knows a secret number and prompts the user to guess the number. When the user enters the number the program should notify the user whether the number guessed was too high, too low, or juuust right. We will modify this program later when we’ve covered loops in order to allow the user to guess until they get the correct answer or run out of tries. Additionally, we will cover random number generation when we explore functions next week.
#include <iostream>int main( void ){ using namespace std; int number = 50; int guess; cout << "Enter your guess:" << endl; cin >> guess; if( guess > secret ) cout << "Sorry, your guess was too high." << endl; if( guess < secret ) cout << "Sorry, Your guess was too low." << endl; if( guess == number ) cout << "You guessed juuust right! << endl; return 0;}