Chapter 3 review. Are the following statements true or false? Explain why or why not.
a) A C++ statement must end with a semi-colonso watch your grammar!
True. The syntax for a C++ statement is an expression followed by a semi-colon (;).
A C++ compiler will complain if it doesn't find a semi-colon where a statement should end.
b) Comments can only contain legal C++ keywords and identifiers.
False. Since they are ignored by a C++ compiler, comments can contain anything
you want.
c) When a program contains a //, it prints the
rest of the line on the screen while executing.
False. Comments have no effect on the rest of the program during compilation and
certainly not during execution.
d) A comment describing a variable keeps its intended meaning from changing.
False. Though it is desirable to use comments to describe the invariant meaning of a
variable, it's up to the programmer(s) to preserve this meaning in their code. It's
good programming practice to preserve a single meaning for most variables; if necessary,
change the comment to explain any changes in meaning.
e) The insertion operator writes data into an output stream (such as ostream).
True. Note that the << operator changes the state of an output stream.
f) The extraction operator pulls teeth and puts them under pillows.
False. There are no fairy god-mothers in C++, but there is a >> operator,
which reads data from an input stream into a variable, i.e., cin
>> pounds;
g) The words cout and cin are keywords built into the C++ language.
False. They are declared in the header file iostream.h (or in the new standard C++,
simply iostream). If a program doesn't have #include
iostream.h, a C++ compiler will not recognize cout
or cin.
h) To get two lines of output, a program must have two output statements each using cout.
False. You start a new line of output by inserting either endl
or '\n' into cout, e.g.,
either
cout << "My name is:\n\t\Glenn
David Blank";
or cout << "My name is:"
<< endl << "\tGlenn David Blank";
will cause the following output from the program:
My name is:
Glenn David Blank
i) All C++ identifiers must begin with a letter or underscore.
True. Adam and _adam are legal C++ identifiers, but 2ndAdam is notafter all,
anything beginning with a digit should be a numberand neither is +Adamsince +
is an operator.
j) C++ considers the identifiers holy, HOLY and HoLy to be equivalent.
False. C++ is case-sensitive (i.e., distinguishes lower and UPPER case), so these
are three distinct identifiers.
k) All variables must be declared before they can be used.
True, for all intents and purposes. A C++ compiler needs to know the type of a
variable before it can figure out how it is being used in an expression.
Technically, C++ compilers may assume that an undeclared variable is an int, but usually this assumption is either wrong or leads to
problems later.
l) All variables must be declared before any executable statements.
False. C++ (unlike it's predecessor, C) lets you mix declarations and executable
statements, e.g.,
int i,j;
//declare some
variables
cin >> i >> j;
//executable statement
float x=7.7;
//another declaration
cout << i * j * x; //another executable
statement
m) C++ operators can mean different things depending on the types of their operands.
True. For example, 3 / 2 produces 1 (integer division, since both operands are
integers), whereas 3 / 2.0 produces 1.5 (floating point division, since one of the
operands is a real number. Giving the same operators different meanings is called operator
overloading. C++ permits programmers to overload operators to give them
additional meanings. Later you'll learn how the LOOKOUT library overloads the +
operator so that "but" + "ton" can produce
"button".
n) All arithmetic operators have the same level of precedence.
False. As in mathematics, multiplicative operators evaluate before (have higher precedence
than) additive operators, so that 2 + 3 * 4 produces 14, not 20.
o) When a variable is declared, it is automatically initialized to a default value.
False. C++ does not guarantee the initialization of variables (as some programming
languages do). The initial value of a variable is whatever happens to be in that
location of memory, i.e., garbage. It's therefore a good idea to initialize a
variable immediately, either as part of a declaration or in an input statement following
closely after the declaration.
p) Procedural abstraction hides the implementation details of a function.
True. You don't need to know how to implement the sqrt()
function to use it!
q) Symbolic constants are harder to maintain than magic numbers.
False. It's the other way around. Magic numbers, i.e., numeric constants
sprinkled in the code of a program, are hard to maintain. If someone wants to change
a magic number, he or she may need to look throughout the program for this number and
cannot easily tell whether all the uses of this number are for the same purpose. On
the other hand, with symbolic constants, one can just change the value of the constant in
one place, its definition, and that value will be propagated everywhere the constant
appears in the program.
r) A char constant is one or more characters
appearing between single quotation marks.
False. A char constant is just one
character between single quotation marks. The only apparent exception is the use of
an escape character, i.e., '\n', but in this case, the escape character is really helping
to form one character. To produce a string constant, put zero or more characters
between double quotation marks, i.e., "mazeltov!"
s) Sending the escape sequence \t to cout emits a tab, and \n advances the cursor to a new line. True. Each escape sequence forms a special character.
t) All char values have a numeric value, which
can be used in arithmetic.
True. The ASCII code defines the association of character values with their binary
representation. So in C++, since the ASCII code for the char value 'A' is binary
01000001 or decimal 66 and 'a' is 98, then 'a' - 'A' is 32, which happens to be the ASCII
code for a blank space.
v) A C++ compiler will automatically convert int
values into float, and vice versa.
True, though generally it will prefer to convert from int
to float (type promotion) then the other way
around (type demotion). I.e., both of the following are legal in C++:
int i=7;
float x; x=i; //x is now 7.0
int j; float y=7.7; j=y; //j is now 7
though many compilers will issue a warning about the second assignment, which clearly loses information.