BOOK EXERCISES (BASIC ELEMENTS OF C++) Flashcards
Mark the following statements as true or false.
An identifier can be any sequence of digits and letters.
false
Mark the following statements as true or false.
In C++, there is no difference between a reserved word and a pre-defined identifier.
false
Mark the following statements as true or false.
A C++ identifier can start with a digit.
false
Mark the following statements as true or false.
The operands of the modulus operator must be integers.
true
Mark the following statements as true or false.
If a = 4; and b = 3;, then after the statement a = b; the value of b is still 3.
true
Mark the following statements as true or false.
In the statement cin»_space; y;, y can only be an int or a double variable.
false
Mark the following statements as true or false.
In an output statement, the newline character may be a part of the string.
true
Mark the following statements as true or false.
The following is a legal C++ program:
int main()
{
return 0;
}
true
Mark the following statements as true or false.
In a mixed expression, all the operands are converted to floating-point numbers.
false
Mark the following statements as true or false.
Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6.
true
Mark the following statements as true or false.
Suppose a = 5. After the statement ++a; executes, the value of a is still 5 because the value of the expression is not saved in another variable.
false
Which of the following are valid C++ identifiers?
a. myFirstProgram b. MIX-UP c. C++Program2 d. quiz7
e. ProgrammingLecture2 f. 1footEquals12Inches
g. Mike’sFirstAttempt h. Update Grade i. 4th
j. New_Student
a, d, e, j
What is the difference between a reserved word and a user-defined identifier?
reserved words (int, float, void, return) can’t be redefined in the program, whereas user defined identifiers can be redefined
Which of the following is a reserved word in C++?
a. Const b. include c. Char d. void e. int f. Return
b, d, e
Are the identifiers firstName and FirstName the same?
NOPE
The identifiers firstName and FirstName are not the same. C++ is case-sensitive. The first letter of firstName is lowercase f, whereas the first character of FirstName is uppercase F. So these identifiers are different
Evaluate the following expressions.
a. 25 / 3
b. 20 - 12 / 4 * 2
c. 32 % 7
d. 3 - 5 % 7
e. 18.0 / 4
f. 28 - 5 / 2.0
g. 17 + 5 % 2 - 3
h. 15.0 + 3.0 * 2.0 / 5.0
a. 8
b. 14
c. 4
d. -2
e. 4.5
f. 25.5
g. 15
h. 16.2
If x = 5, y = 6, z = 4, and w = 3.5, evaluate each of the following statements, if possible. If it is not possible, state the reason.
a. (x + z) % y
b. (x + y) % w
c. (y + w) % x
d. (x + y) *w
e. (x % y) % z
f. (y % z) % x
g. (x *z) % y
h. ((x *y) *w) *z
a. 3
b. Not possible. Both of the operands of the operator % must be integers. Because the second operand, w, is a floating-point value, the expression is invalid.
c. Not possible. Both of the operands of the operator % must be integers. Because the first operand, which is y + w, is a floating-point value, the expression is
invalid.
d. 38.5
e. 1
f. 2
g. 2
h. 420.0
Given:
int num1, num2, newNum;
double x, y;
Which of the following assignments are valid? If an assignment is not valid, state the reason.
When not given, assume that each variable is declared.
a. num1 = 35;
b. newNum = num1 – num2;
c. num1 = 5; num2 = 2 + num1; num1 = num2 / 3;
d. num1 * num2 = newNum;
e. x = 12 * num1 - 15.3;
f. num1 * 2 = newNum + num2;
g. x / y = x * y;|
h. num2 = num1 % 2.0;
i. newNum = static_cast<int> (x) % 5;
j. x = x + y - 5;
k. newNum = num1 + static_cast<int> (4.6 / 2);</int></int>
a. true VALID
b. true VALID
c. true VALID
d. false NOT VALID - assignable value has to be on the left side of = operator
e. true VALID
f. false NOT VALID - assignable value has to be on the left side of = operator
g. false NOT VALID - assignable value has to be on the left side of = operator
h. false NOT VALID - can’t use % operator with double values
i. true VALID
j. true VALID
k. true VALID
Do a walk-through to find the value assigned to e. Assume that all variables
are properly declared.
a = 3;
b = 4;
c = (a % b) * 6;
d = c / b;
e = (a + b + c + d) / 4;
7
Which of the following variable declarations are correct? If a variable declaration is not correct, give the reason(s) and provide the correct variable declaration.
n = 12; //Line 1
char letter = ; //Line 2
int one = 5, two; //Line 3
double x, y, z; //Line 4
a. incorrect // type of variable is not defined
b. incorrect // not correct letter doesn’t contain any character
c. incorrect // should not be spelled out????
d. correct
Which of the following variable declarations are correct? If a variable declaration is not correct, give the reason(s) and provide the correct variable declaration (1,7)
double conversion = 2.5; //Line 1
char grade = ‘B+’; //Line 2
double 28.5 = num //Line 3
string message = ‘‘First C++ course’; //Line 4
int age = 18 years; //Line 5
int perfectSquare; //Line 6
float x, y, decimal; //Line 7
1: Valid
2: Not Valid: Multicharacter Constant – char only allows one variable to be placed in the data type
3: Not Valid: need semicolon and can’t define a number
4: Not Valid: need double quotation marks
5: Not Valid: cant put years
6: Valid
7: Valid
Which of the following are valid C++ assignment statements? Assume that i, x, and percent are double variables.
a. i = i + 5;
b. x + 2 = x;
c. x = 2.5 *x;
d. percent = 10%;
a and c are valid.
Write C++ statement(s) that accomplish the following.
a. Declare int variables x and y. Initialize x to 25 and y to 18.
b. Declare and initialize an int variable temp to 10 and a char variable
ch to ‘A’.
c. Update the value of an int variable x by adding 5 to it.
d. Declare and initialize a double variable payRate to 12.50.
e. Copy the value of an int variable firstNum into an int variable tempNum.
f. Swap the contents of the int variables x and y. (Declare additionalvariables, if necessary.)
g. Suppose x and y are double variables. Output the contents of x, y, and the expression x + 12 / y - 18.
h. Declare a char variable grade and set the value of grade to ‘A’.
i. Declare int variables to store four integers.
j. Copy the value of a double variable z to the nearest integer into an int variable x.
statement a.
int x, y; x = 25; y = 18;
statement b.
int temp = 10; char ch = ‘A’;
statement c.
int x; x = x + 5;
statement d.
double payRate = 12.50;
statement e.
int firstNum, tempNum; tempNum = firstNum;
statement f.
int x, y; int temp = x; x = y; y = temp;
statement g.
double x, y; cout «_space;x «_space;endl; cout «_space;y «_space;endl; cout «_space;x + 12 / y - 18 «_space;endl;
statement h.
char grade; grade = ‘A’;
statement i.
int i, j, k, m;
statement j.
double z; int x; x = static_cast(z + 0.5);
Write each of the following as a C++ expression.
a. 32 times a plus b
b. The character that represents 8
c. The string that represents the name Julie Nelson.
d. (b2 - 4ac) / 2a
e. (a + b)/c(ef)-gh
f. (-b + (b2 - 4ac)) / 2a
a. 32 * a + b
b. ‘8’
c. “Julie Nelson”
d. (b * b – 4 * a * c) / (2 * a)
e. (a + b) / c * (e * f) - g * h
f. (-b + (b * b - 4 * a * c)) / (2 * a)