Data Types, Operators, and Expressions: For Multiple-Choice and Essay Questions
Data Types, Operators, and Expressions: For Multiple-Choice and Essay Questions
Data Types, Operators, and Expressions: For Multiple-Choice and Essay Questions
1
A. Multiple-choice questions and essay questions (10pts)
A.1. Which of the following is used to indicate a block comment in C/C++ source code?
% Comment % { Comment } /* Comment */ (* Comment *)
A.2. Which of the following is used to create a variable with the numeric value 5?
double x = 5; int x = 5; num x = 5; x = 5;
A.3. Which of the following is used to create a variable with the floating number 2.8?
A.4. Identify whether C/C++ standards allow for each of the following cases. If a case is
unallowable, suggest a workaround solution.
A.6. Add the most appropriate data type for the following variables
_____________ myNum = 9;
_____________ myDoubleNum = 8.99;
_____________ myLetter = 'A';
_____________ myBool = false;
_____________ myText = "Hello World";
A.7. What is the output of the following code fragment? Explain for every line.
int x = 5, y = 5, z;
z = ++x;
y = --y;
z = x++ + y--;
cout << z;
2
A.8. The following int variables hold the same decimal value, yet they are represented in
different number bases. Which decimal value is held in these variables? Identify the base in
each variable.
int a, b, c, d, e;
a = 223;
b = 0337;
c = 0xdf;
d = 0xDF;
e = 0b11011111;
A.9. In your opinion, discuss the advantages and disadvantages of C++ over C.
A.10. List all primitive data types available in C++ and their ranges.
3
B. Programming questions (10pts)
You can answer these questions by any possible mean while not worrying about loops or conditions
(which will be available in the following weeks).
B.1. Write a C++ program to prompt the user to input three integer values and print these values
in forward and reversed order.
Test Data :
Input the first value: 12
Input the second value: 45
Input the third value: 78
Expected Output:
Forward order: 78 45 12
Backward order: 12 45 78
B.2. Write a program that prompts the user to enter some temperature value in the Celsius scale
and then converts it to corresponding value in the Fahrenheit scale.
Test Data :
Input the temperature in Celsius scale: 36.5
Expected Output:
The temperature in Fahrenheit scale: 97.7
B.3. Write a program that prompts the user to enter some distance value in kilometers and then
converts it to corresponding value in miles.
Test Data :
Input the distance in kilometers: 36.5
Expected Output:
The distance in miles: 22.68
B.4. Write a program that prompts the user to enter a three-bit binary value by sequentially
inputting the bits, from lowest to highest. The program then calculates the corresponding
decimal value from the given binary value.
Test Data :
Input the first (lowest) bit: 0
Input the second bit: 1
Input the third bit: 1
Expected Output:
The decimal value of the given binary value is 6
4
B.5. Write a program to calculate a bike’s average consumption from the given total distance
traveled (in km, integer value) and spent fuel (in liters, float number with 2 decimal point).
Test Data :
Input total distance in km: 350
Input total fuel spent in liters: 5.25
Expected Output:
Average consumption (km/lt): 66.67
B.6. Write a program that accepts an employee's total worked hours of a month and the amount
of money received per hour, and then the program prints the employee's salary (with two
decimal places).
Test Data :
Input the working hours of a month: 30
Salary amount/hour: 99.99
Expected Output:
Salary = 2999.7
B.7. Write a program to calculate the Euclidean distance between two 2-dimensional coordinates
inputted by the users.
Test Data :
Input x1: 25
Input y1: 15
Input x2: 35
Input y2: 10
Expected Output:
Distance between the two given points: 11.1803
B.8. Write a program that accepts two numbers, val1 and val2 (val2 0), and calculates the sum
(val1+val2), difference (val1-val2), product (val1*val2) and division (val1/val2) of the two
numbers.
Test Data :
Input the first number: 7.5
Input the second number: 2.5
Expected Output:
The sum of two numbers is 10
The difference between two numbers is 5
The product of two numbers is 18.75
The division of two numbers is 3
5
B.9. Write a program that prompts the user to enter some number of pennies (1-cent coins),
nickels (5-cent coins), dimes (10-cent coins), quarters (25-cent coins), half-dollar coins (50-
cent coins), and one-dollar coins (100-cent coins), and then prints out the value of all coins
in cents. The program should query the user separately for the number of each size coin, e.g.,
“How many pennies do you have?”
Test Data :
How many pennies do you have? 23
How many nickels do you have? 17
How many dimes do you have? 14
How many quarters do you have? 7
How many half-dollar coins do you have? 3
How many one-dollar coins do you have? 1
Expected Output:
The value of all your coins is 673 cents
B.10. Write a program that functions reversely to the above problem. The user enters some
number of cents. The program exchanges the given number of cents to some numbers of
pennies, nickels, dimes, quarters, half-dollar coins (50-cent coins), and one-dollar coins.
Coins of larger values are preferred in the exchange, that is the program should first
exchange the given number of cents to one-dollar coins, the remaining cents are then
exchanged to half-dollar coins, and so on.
Test Data :
How many cents do you have? 573
Expected Output:
You have 5 one-dollar coins, 1 half-dollar coins, 0 quarters, 2 dimes, 0 nickels and 3
pennies.