

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be happy to review your answers with you during office-hours, via Email, or instant messaging.
(a) double O2pct;
Solution: OK
(b) integer X;
Solution: Invalid: integer should be int.
(c) let a_crowd = 3;
Solution: Invalid: let is not a variable type.
(d) bool switch = false;
Solution: Invalid: switch is a reservd word.
(e) const E = exp(1);
Solution: Invalid: missing variable type.
(f) double oxygen%;
Solution: Invalid: percent signs not allowed.
(g) const int ROWS(1024);
Solution: OK
(h) bool register;
Solution: Invalid: register is a reserved word.
(i) int 2TooMany = 3;
Solution: Invalid: cannot begin with numeral.
(j) PI const double = 3.14;
Solution: Invalid: const double comes first.
Solution: Yes, X has a value: X is simpy the name for 32 bits of memory, those 32 bits can always be interpretted as an integer value. Since X wasn’t initialized with a particular initial value, we say that is holds “garbage” or has a “garbage value.” I don’t expect people to get the last question correct, but it is important to think about it. In some instances you cannot predict the value of X, but in many cases it is at least theoretically possible to do so. It takes a lot of work to do so, but programmers have done it before. In fact, being able to predict the value of X is the fundamental building block of stack smashing and buffer overrun exploits that allow malicious programmers to attack and gain control of other users personal computers or Internet servers.
Solution: Binary operators take take two arguments, usually one on either side of the operator symbol. Unary operators take one argument.
Solution: Modulus has equal precedence as multiplication, so the expression simplifies to 3 + 2 * 5 = 3 + 10 = 13.
Solution: Precedence refers to the order in which operators are evaluated. Lower precedence operators are evaluated before higher precedence operators. () has the lowest precedence (evaluated first), while assign- ment operators (=, +=,... ) have the highest precedence and are evaluated last.
Pre Execution Post x y z C++ statement x y z 1 2 3 x = y++ - z; -1 3 3 1 2 3 y = --z - x++; 2 1 2 1 2 3 y = z-- - ++x; 2 1 2 1 2 3 x = y = z; 3 3 3 1 2 3 z = y = x; 1 1 1 1 2 3 z *= y - ++x; 2 2 0 1 2 3 z += y - x++; 2 2 4
const double PI( acos(-1) );
Solution: The programmer cannot assign a new value to PI.
Solution: Three: bool, short, int.
Solution: False. Negative integers, 0, and the positive integers.
Solution: False, there is a small “hole” around (but not containing) zero.
Solution: False. It stores upper and lower case English letters, but also numerals, punctuation, tab characters, and other formatting specific character codes.
Solution:
int theInt ( 724 ); int theInt = 724; int^ theInt ; Page 2 theInt = 724;