Name:
Student ID:
DUE: 10 PM Sunday, September 27. Email the instructor a text
file that contains your answers. Use "EE261 Quiz1" as subject of your
email.
1. Which of the following are valid C++ identifiers? [0.5%]
a. theDog
b. all-In-One
c. all_in_one
d. 2morrow
e. page#
f. AMOUNT
g. C++
h. i
i. _function
j. 100
Answer: a, c, f, h, i
2. Which of the following are string literal constants? [0.5%]
a. 's'
b. 256
c. "Hi, I am not a string literal..."
d. 015
e. 1024f
f. 3.1415926
g. "s"
h. "256"
Answer: c, g, h
3. [0.5%]
a) Write a C++ statement that declares a named constant PI of double
type with a value of 3.14.
Answer: const double PI = 3.14;
b) what are the advantages of using named constants
over literal constants?
Answer:
Named constants can have at least two advantages:
(1) make it easier to understand the meaning of the constants; and
(2) make it easier to change the constants.
4. What is the value of each of the following expressions: [0.5%]
a.
27+8/5-7
answer: 21
b.
27+8/5.0-7
answer: 21.6
c. 25%7-4
answer: 0
d. int(15+12*2.3-3*14) answer: 0
5. Match the following terms to the definitions given below. [0.5%]
a. Unary operator
b. Binary operator
c. Type conversion
d. Type coercion
e. Mixed type expressions
f. Argument list
g. Void function
1. A computation that involves both floating point values and integer
values.
2. An operator with two operands.
3. A function that is called as a separate statement.
4. Explicitly changing a value of one type to another.
5. The values that appear in the parentheses in a function call.
6. An operator with just one operand.
7. Implicitly changing a value of one type to another.
Answer: a-6, b-2, c-4, d-7, e-1, f-5,
g-3
6. Given the following statements: [0.5%]
string str1="Gone with the wind.";
string str2="Dance with wolves.";
what is the result of each of the following expressions?
a.
str1.length()
answer: 19
b.
str1.find(str2)
answer: string::nopos or
4294967295
c. str1.substr(5,
4)
answer: "with"
d. str2.find(str1.substr(5,4)) answer: 6
7. Write a C++ expression for each of the following formulas. Assume
that the variables (a, b, x, y) are already defined. [0.5%]
a. The square root of the absolute value of
(a-b). Answer: sqrt(fabs(a-b))
b. x to the power of cosine
y.
Answer: pow(x, cos(y))
8. Match the following terms to their definitions given below. [0.5%]
a. Machine language
b. Assembler
c. Compiler
d. Source program
e. Object program
1. A program that translates a high-level language program into machine
code.
2. The machine language version of a source program.
3. A program that translates an assembly language program into machine
code.
4. A language, made up of binary coded instructions, that is used
directly by the computer.
5. A program written in a high-level programming language.
Answer: a-4, b-3, c-1, d-5, e-2
9. Match the following terms to their definitions given below. [0.5%]
a. Function
b. Variable
c. Literal
d. Data type
e. Expressions
1. An identifier referring to a value that can be changed.
2. A specific set of values, along with operations that can be applied
to those values.
3. A value that appears in a program.
4. A subprogram in C++.
5. An arrangement of identifiers, literals, and operators that can be
evaluated to obtain a value.
Answer: a-4, b-1, c-3, d-2, e-5
10. Given these assignments to string variables: [0.5%]
string1 = "Bjarne Stroustrup";
string2 = "C";
string3 = "programming language";
string4 = "++ because it is a
successor to the ";
string5 = " named his new ";
What is the value of the following expression?
string1+string5+string3+"
"+string2+string4+string2+" "+string3+"."
Answer:
"Bjarne Stroustrup named his new
programming language C++ because it is a successor to the C programming
language."
Bonus question: (1%)
Assume that the following function isLeapYear()
is available, which will return true when the parameter year is a leap year.
boolean isLeapYear(int year) {... }
Write a new function named nextLeapYear(int
year). This function should return year if the value of year represents a leap year,
otherwise, return the next leap year.
For example, nextLeapYear(2004)
should return 2004, and nextLeapYear(1999) should return
2000.
int nextLeapYear(int year) {
// one implemention is as
follows. Other implementation is also possible.
if (isLeapYear(year))
return year;
if (isLeapYear(year+1))
return year+1;
if (isLeapYear(year+2))
return year+2;
if (isLeapYear(year+3))
return year+3;
if (isLeapYear(year+4))
return year+4;
if (isLeapYear(year+5))
return year+5;
if (isLeapYear(year+6))
return year+6;
if (isLeapYear(year+7))
return year+7;
}