BOOK EXERCISES (BASIC ELEMENTS OF C++) Flashcards

1
Q

Mark the following statements as true or false.

An identifier can be any sequence of digits and letters.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Mark the following statements as true or false.

In C++, there is no difference between a reserved word and a pre-defined identifier.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Mark the following statements as true or false.

A C++ identifier can start with a digit.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Mark the following statements as true or false.

The operands of the modulus operator must be integers.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Mark the following statements as true or false.

In the statement cin&raquo_space; y;, y can only be an int or a double variable.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Mark the following statements as true or false.

In an output statement, the newline character may be a part of the string.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Mark the following statements as true or false.

The following is a legal C++ program:
int main()
{
return 0;
}

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Mark the following statements as true or false.

In a mixed expression, all the operands are converted to floating-point numbers.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Mark the following statements as true or false.

Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

a, d, e, j

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between a reserved word and a user-defined identifier?

A

reserved words (int, float, void, return) can’t be redefined in the program, whereas user defined identifiers can be redefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which of the following is a reserved word in C++?

a. Const b. include c. Char d. void e. int f. Return

A

b, d, e

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Are the identifiers firstName and FirstName the same?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

a. 8
b. 14
c. 4
d. -2
e. 4.5
f. 25.5
g. 15
h. 16.2

17
Q

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

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

18
Q

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

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

19
Q

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;

A

7

20
Q

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

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

21
Q

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

A

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

22
Q

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

a and c are valid.

23
Q

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.

A

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 &laquo_space;x &laquo_space;endl; cout &laquo_space;y &laquo_space;endl; cout &laquo_space;x + 12 / y - 18 &laquo_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);

24
Q

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

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)

25
Q

Suppose x, y, z, and w are int variables. What value is assigned to each of these variables after the last statement executes?
x = 5; z = 3; y = x - z; z = 2 * y + 3; w = x - 2 * y + z; z = w - x; w++;

A

The values of the variables after the last statement executes are:

x = 5;
y = 2;
z = 3;
w = 9;

26
Q

Suppose x, y, and z are int variables and w and t are double variables.
What value is assigned to each of these variables after the last statement
executes?
x = 17;
y = 15;
x = x + y / 4;
z = x % 3 + 4;
w = 17 / 3 + 6.5;
t = x / 4.0 + 15 % 4 - 3.5;

A

x = 20
y = 15
z = 6
w = 11.5
t = 4.5

27
Q

What is the output of the following statements? Suppose a and b are int
variables, c is a double variable, and a = 13, b = 5, and c = 17.5.
a. cout &laquo_space;a + b – c &laquo_space;endl;
b. cout &laquo_space;15 / 2 + c &laquo_space;endl;
c. cout &laquo_space;a / static_cast<double>(b) + 2 * c << endl;
d. cout << 14 % 3 + 6.3 + b / a << endl;
e. cout << static_cast<int>(c) % 5 + a – b << endl;
f. cout << 13.5 / 2 + 4.0 * 3.5 + 18 << endl;</int></double>

A

a. 0.50;
b. 24.50;
c. 37.6;
d. 8.3;
e. 10;
f. 38.75

28
Q

Which of the following are correct C++ statements?
a. cout &laquo_space;“Hello There!” &laquo_space;endl;
b. cout &laquo_space;“Hello”; &laquo_space;” There!” &laquo_space;endl;
c. cout &laquo_space;“Hello” &laquo_space;” There!” &laquo_space;endl;
d. cout &laquo_space;‘Hello There!’ &laquo_space;endl;

A

a and c are correct.

29
Q

Give meaningful identifiers for the following variables.
a. A variable to store the first name of a student.
b. A variable to store the discounted price of an item.
c. A variable to store the number of juice bottles.
d. A variable to store the number of miles traveled.
e. A variable to store the highest test score.

A

string name;
or string firstName = “Jack”;

double discountedPrice;
or double discounted price = price * discount;

int numJuiceBottles;
or int numBottles = 10;

double milesTraveled;
or int miles = 500;

int highestTestScore;
or int scoreHighest = 100;

30
Q

Write C++ statements to do the following.
a. Declare int variable num1 and num2.
b. Prompt the user to input two numbers.
c. Input the first number in num1 and the second number in num2.
d. Output num1, num2, and 2 times num1 minus num2. Your output must
identify each number and the expression.

A

a. int num1;
int num2;
b. cout &laquo_space;“Enter two numbers separated by spaces.” &laquo_space;endl;
c. cin&raquo_space; num1&raquo_space; num2;
d. cout &laquo_space;“num1 = “ &laquo_space;num1 &laquo_space;“num2 = “ &laquo_space;num2
&laquo_space;“2 * num1 - num2 = “ &laquo_space;2 * num1 - num2 &laquo_space;endl;

31
Q

The following program has syntax mistakes. Correct them. On each
successive line, assume that any preceding error has been corrected.
const char = STAR = ‘*’
const int PRIME = 71;
int main
{
int count, sum;
double x;

  count = 1; 
  sum = count + PRIME; 
  x := 25.67; 
  newNum = count * ONE + 2; 
  sum + count = sum; 
  x = x + sum * COUNT; 
 cout << " count = " << count << ", sum = " << sum 
          << ", PRIME = " << Prime << endl;  }
A

include <iostream></iostream>

A correct answer is:

using namespace std;

const char STAR = ‘*’;
const int PRIME = 71;

int main()
{
int count, sum;
double x;
int newNum; //declare newNum

 count = 1;
 sum = count + PRIME;
 x = 25.67; // x = 25.67;
 newNum = count * 1 + 2; //newNum = count * ONE + 2;
 sum = sum + count; //sum + count = sum;
 x = x + sum * count; // x = x + sum * COUNT;
 cout << " count = " << count << ", sum = " << sum
          << ", PRIME = " << PRIME << endl; return 0; }
32
Q

What action must be taken before a variable can be used in a program?

A

An identifier must be declared before it can be used.

33
Q

Preprocessor directives begin with which of the following symbols:
a. * b. # c. $ d. ! e. None of these.

A

#

34
Q

Write equivalent compound statements if possible.
a. x = 2 *x
b. x = x + y - 2;
c. sum = sum + num;
d. z = z *x + 2 *z;
e. y = y / (x + 5);

A

a. x *= 2;
b. x += y - 2;
c. sum += num;
d. z *= x + 2;
e. y /= x + 5;

35
Q

Write C++ statements that accomplish the following (10)
a) Outputs the newline character
b) outputs the tab character
c) outputs double quotation mark

A

a) cout &laquo_space;” \n” &laquo_space;endl;
b) cout &laquo_space;“\t” &laquo_space;endl;
c) cout &laquo_space;””” &laquo_space;endl;

36
Q

Which of the following are correct C++ statements?
a) cout &laquo_space;“Programming with C++!” &laquo_space;endl;
b) cout &laquo_space;” Programming “ &laquo_space;” with “ «
&laquo_space;” C++” &laquo_space;endl;
c) cout &laquo_space;” Programming “
&laquo_space;” with C++!” &laquo_space;‘\n’
d) cout &laquo_space;“Programming with C++!’ &laquo_space;endl;

A

A & C

37
Q

What actions must be taken before a variable can be used in a program?

A

A variable should be declared before it is used