Hawassa University Fundamentals of Programming in C++ Worksheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Hawassa University

Fundamentals of Programming in C++


Worksheet
1) Mark the following statements as true or false.
a) An identifier can be any sequence of digits and letters.
b) In C++, there is no difference between a reserved word and a predefined identifier.
c) A C++ identifier can start with a digit.
d) The operands of the modulus operator must be integers.
e) If a = 4; and b = 3;, then after the statement a = b; the value of b is still 3.
f) In the statement cin >> y;, y can only be an int or a double variable.
g) In an output statement, the newline character may be a part of the string.
h) The following is a legal C++ program:
int main()
{ return 0; }
i) In a mixed expression, all the operands are converted to floating-point numbers.
j) Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6.
k) 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.
2) 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
th
h. Update Grade i. 4 j. New_Student
3) Which of the following is a reserved word in C++?
a. Const b. include c. Char d. void e. int f. Return
4) What is the difference between a keyword and a user-defined identifier?
5) Are the identifiers firstName and FirstName the same?
6) 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

1|Page Semester I Year 2010E.C


7) 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
8) 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);
9) 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;
10) 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

2|Page Semester I Year 2010E.C


double x, y, z; //Line 4
11) 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%;
12) 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 additional variables, 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
13) . 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
14) 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++;

3|Page Semester I Year 2010E.C


15) 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;
16) Suppose x, y, and z are int variables and x = 2, y = 5, and z = 6. What is the output of each of
the following statements?
a. cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
b. cout << "x + y = " << x + y << endl;
c. cout << "Sum of " << x << " and " << z << " is " << x + z << endl;
d. cout << "z / x = " << z / x << endl;
e. cout << "2 times " << x << " = " << 2 *x << endl;
17) 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 << a + b – c << endl;
b. cout << 15 / 2 + c << endl;
c. cout << 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;
18) Write C++ statements that accomplish the following.
a) Output the newline character.
b) Output the tab character.
c) Output double quotation mark.
19) Which of the following are correct C++ statements?
a. cout << "Hello There!" << endl;
b. cout << "Hello";
<< " There!" << endl;
c. cout << "Hello"
<< " There!" << endl;
d. cout << 'Hello There!' << endl;

4|Page Semester I Year 2010E.C


20) 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.
21) 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.
22) The following program has syntax mistakes. Correct them. On each successive line, assume
that any preceding error has been corrected.
#include <iostream>
const int SECRET_NUM = 11,213;
const PAY_RATE = 18.35
main()
{
int one, two;
double first, second;
one = 18;
two = 11;

first = 25;
second = first * three;

second = 2 * SECRET_NUM;
SECRET_NUM = SECRET_NUM + 3;
cout << first << " " << second << SECRET_NUM << endl;
paycheck = hoursWorked * PAY_RATE
cout << "Wages = " << paycheck << endl;
return 0;

5|Page Semester I Year 2010E.C

You might also like