Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 2 1) Evaluate the following: a) 18 -3 + 6 b) 32 % 12 + 6 -36 / 5 * 7 c)

ID: 3815921 • Letter: C

Question

Chapter 2

1) Evaluate the following:
a) 18 -3 + 6
b) 32 % 12 + 6 -36 / 5 * 7
c) 9 – 6 % 11
d) 15.0 + 6.0 * 3.0 / 4.0
2) If int x = 6; int y = 18; double z = 9.5; and double w = 3.5; evaluate each of the following statements, if possible. If it is not possible, state the reason.
a) (x + y) % y
b) x % y – w
c) x % (y + z)
d) (y + z) / w
3) Which of the following variable declarations are correct? If a variable declaration is not correct, give the reason and provide the correct variable declaration.
a) double conversion = 5.5;
b) char grade = ‘A+’;
c) double 38.5 = num;
d) string message = ‘ ‘ First C++ course’;
e) int age = 18 years;
f) float x, y, decimal;
4) Suppose x, y, z, and w are int variables. What value is assigned to each of these variables after the last statement executes?
x = 4; y = 22;
z = y - 2 * x;
x = z + y;
y = x + 5 * z;
w = x - y + 2 * z;
x = y + w - x;
5) 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 = 13;
y = 35;
x = x + y / 4 - 3;
z = x % 3;
w = 28 / 3 + 6.5 * 2;
t = x / 4.0 + 15 % 4 - 3.5;
6) Write C++ statements that accomplish the following:
a) Output the newline character.
b) Output the tab character.
c) Output double quotation mark.
7) 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.
8) 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.
9) Suppose a, b, and c are int variables and a = 5 and b = 6. What value is assigned to each variable
after each statement executes? If a variable is undefined at a particular statement, report UND
(undefined).
a b c
a = (b++) + 5; __ __ __
c = 2 * a + (++b); __ __ __
b = 2 * (++c) - (a++); __ __ __
10) What is printed by the following program? Suppose the input is:
Miller
24
240
#include <iostream>
#include <string>
using namespace std;
const int PRIME_NUM = 11;
int main()
{
const int SECRET = 17;
string name;
int id;
int num;
int mysteryNum;
cout << "Enter last name: ";
cin >> name;
cout << endl;
cout << "Enter a two digit number: ";
cin >> num;
cout << endl;
id = 100 * num + SECRET;
cout << "Enter a positive integer less than 1000: ";
cin >> num;
cout << endl;
mysteryNum = num * PRIME_NUM - 3 * SECRET;
cout << "Name: " << name << endl;
cout << "Id: " << id << endl;
cout << "Mystery number: " << mysteryNum << endl;
return 0;
}

Explanation / Answer

1) Evaluate the following:
a) 18 -3 + 6

Answer: 21

b) 32 % 12 + 6 -36 / 5 * 7

Answer: -35

c) 9 – 6 % 11

Answer: 3


d) 15.0 + 6.0 * 3.0 / 4.0

Answer: 19.5

2) If int x = 6; int y = 18; double z = 9.5; and double w = 3.5; evaluate each of the following statements, if possible. If it is not possible, state the reason.

a) (x + y) % y

Answer: 6

Program:

#include <iostream>
using namespace std;

int main() {
   int x = 6; int y = 18; double n, z = 9.5;
   n=(x + y) % y;
   cout<< "n="<<n<<endl;
   return 0;
}

Output:

n=6

b) x % y – w

Answer: It causes Error because x and y are integer values and w is of Type double

c) x % (y + z)

Answer: It causes Error because x and y are integer values and z is of Type double

d) (y + z) / w

Answer: n=7.85714

3) Which of the following variable declarations are correct? If a variable declaration is not correct, give the reason and provide the correct variable declaration.

a) double conversion = 5.5;

Answer: The above declaration is correct

Program:

#include <iostream>
using namespace std;

int main() {
double conversion = 5.5;
   cout<< "conversion="<<conversion<<endl;
   return 0;
}

Output:

conversion=5.5

b) char grade = ‘A+’;

Answer: The above declaration is Incorrect because we are using ‘ ’ instead of ' ' & in char data type it takes only single value only..

c) double 38.5 = num;

Answer: The above declaration is Incorrect because we are using num as double value it cannot access string for double datatype

d) string message = ‘ ‘ First C++ course’;

Answer: The above declaration is Incorrect because we are using ‘ ’ instead of ' '

e) int age = 18 years;

Answer: The above declaration is Incorrect because it cannot take string as integer data type.

f) float x, y, decimal;

Answer: The above declaration is correct .

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

Answer:

x =28
y =106
z =14
w =-42

Program:

#include <iostream>
using namespace std;

int main() {
int x,y,z,w;
x = 4; y = 22;
z = y - 2 * x;
x = z + y;
y = x + 5 * z;
w = x - y + 2 * z;
x = y + w - x;
   cout<< "x ="<<x <<endl;
   cout<< "y ="<<y <<endl;
   cout<< "z ="<<z <<endl;
   cout<< "w ="<<w <<endl;
   return 0;
}

Output:

x =28
y =106
z =14
w =-42

5) 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 = 13;
y = 35;
x = x + y / 4 - 3;
z = x % 3;
w = 28 / 3 + 6.5 * 2;
t = x / 4.0 + 15 % 4 - 3.5;

Answer:

x =18
y =35
z =0
w =22
t =4

Program:

#include <iostream>

using namespace std;

int main() {
int x,y,z;double w,t;
x = 13;
y = 35;
x = x + y / 4 - 3;
z = x % 3;
w = 28 / 3 + 6.5 * 2;
t = x / 4.0 + 15 % 4 - 3.5;
   cout<< "x ="<<x <<endl;
   cout<< "y ="<<y <<endl;
   cout<< "z ="<<z <<endl;
   cout<< "w ="<<w <<endl;
   cout<< "t ="<<t <<endl;
   return 0;
}

Output:

x =18
y =35
z =0
w =22
t =4

9) Suppose a, b, and c are int variables and a = 5 and b = 6. What value is assigned to each variable
after each statement executes? If a variable is undefined at a particular statement, report UND
(undefined).
a b c
1) a = (b++) + 5; __ __ __

Answer: a =11

Program:

#include <iostream>
using namespace std;

int main() {
int a = 5,b = 6;
a = (b++) + 5;
   cout<< "a ="<<a <<endl;
   return 0;
}

Output:

a =11

2) c = 2 * a + (++b); __ __ __

Answer: c =17

Program:

#include <iostream>
using namespace std;

int main() {
int a = 5,b = 6,c;
c = 2 * a + (++b);
   cout<< "c ="<<c <<endl;
   return 0;
}

Output:

c =17

3) b = 2 * (++c) - (a++); __ __ __

Answer: b =-3

Program:

#include <iostream>

using namespace std;

int main() {
int a = 5,b = 6,c;
b = 2 * (++c) - (a++);
   cout<< "b ="<<b <<endl;
   return 0;
}

Output:

b =-3

10) What is printed by the following program? Suppose the input is:

Miller
24
240
#include <iostream>
#include <string>
using namespace std;
const int PRIME_NUM = 11;
int main()
{
const int SECRET = 17;
string name;
int id;
int num;
int mysteryNum;
cout << "Enter last name: ";
cin >> name;
cout << endl;
cout << "Enter a two digit number: ";
cin >> num;
cout << endl;
id = 100 * num + SECRET;
cout << "Enter a positive integer less than 1000: ";
cin >> num;
cout << endl;
mysteryNum = num * PRIME_NUM - 3 * SECRET;
cout << "Name: " << name << endl;
cout << "Id: " << id << endl;
cout << "Mystery number: " << mysteryNum << endl;
return 0;
}

Output:

Enter last name: Miller
Enter a two digit number: 24
Enter a positive integer less than 1000: 240
Name: Miller
Id: 2417
Mystery number: 2589

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote