0% found this document useful (0 votes)
7 views

Programming Tutorials - First Test

Uploaded by

ncascious
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Programming Tutorials - First Test

Uploaded by

ncascious
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

INTRODUCTION TO C++ FUNDAMENTALS

FIRST TEST
Instructions: Answer All Questions Duration: 2 Hours

QUESTION 1 (20 Marks)


1. Write assignment statements that perform the following operations with the variables ;
a, b, and c. [5 Marks]
i. Adds 2 to a and stores the result in b
ii. Multiplies b times 4 and stores the result in a
iii. Divides a by 3.14 and stores the result in b
iv. Subtracts 8 from b and stores the result in a
v. Stores the value 27 in a.
2. Convert the following pseudocodes to C++ programs. Be sure to define the appropriate variables.
[5 Marks]
i.
Store 20 in the speed variable.
Store 10 in the time variable.
Multiply speed by time and store the result in the distance variable.
Display the contents of the distance variable.
ii.
Store 172.5 in the force variable.
Store 27.5 in the area variable.
Divide area by force and store the result in the pressure variable.
Display the contents of the pressure variable.
3. With clear examples, distinguish between a variable and a constant. [4 Marks]
4. How may the double variables temp, weight, and age be defined in one statement?
[1 Mark]
5. How may the int variables months, days, and years be defined in one statement, with months
initialized to 2 and years initialized to 3? [1 Mark]

6. Modify the following program so it prints two blank lines between each line of text. Be sure to
correct the syntax errors as well. [3 Marks]
$include <iostream.h>
using name spacestd;
int main
{
cout << ‘Two mandolins like creatures in the’;
cout << "dark";
cout << "Creating the agony of ecstasy.";
cout << " - George Barker";

reverse 0;
}
7. Why is variable initialization important? [1 Mark]
QUESTION 2 (20 MARKS)
8. Evaluate the following [2 Marks]

i. y = (7/3 + 10%5)
ii. y = (20 >= 7 + 7 && 5 == 6 || 6 != 4)
9. Show what the following expressions evaluate to when you assume
x = 6 and y = 2. [4 Marks]

i. ! (x > 2)
ii. x > y) && (y > 0)
iii. (x < y) && (y > 0)
iv. (x < y) || (y > 0)
10. Convert the following into C++ expressions. [4 Marks]
𝐴+2𝑘 4
i. 𝑏= 𝑚3

ii. 𝑣 = √ 𝑢2 + 2𝑎𝑑
√ 𝑏 2 +𝑐 2
iii. 𝑥= 2𝑚
𝑦2 +3
iv. 𝑏= 𝑏3

11. Let z = 10, x = 15 and y = 15. Evaluate the following expressions. [3 Marks]
i. 13 % 6 > 14 % 6 && !(14 - 4) == z || x % y == x
ii. !(y == x) || (z > 100)
iii. x != y && z >= y || x == y && y > x

12. The following code shows two separate if statements


if (A == true)
{
C = true;
}
if (B == true)
{
C = true;
}
Illustrate how the if statements could be merged into a single statement having the same logical
outcomes. [3 Marks]
13. Identify the technique you have used in (12) and state two reasons why it is important in
programming. [2 Marks]
14. Compare syntax errors to logical errors. [1 Mark]
15. How would you get an entire line of input from the user? [1 Mark]

QUESTION 3 (20 MARKS)


16. Given that the login credentials of a system are username = SIN (Actual SIN) and password =
program (Actual Program), write a program that implements this and prints “Access Denied” the
user enters wrong details and “Access Granted” when the user enters the write details. Use the
ternary Operator. [5 Marks]
17. Write a pseudocode algorithm for a program that asks the user to enter a golfer’s score for three
games of golf, and then displays the average of the three scores. After you write the pseudocode
algorithm, convert it to a complete C++ program. [5 Marks]
18. Write an if/else statement that assigns 0 to x when y is equal to 10. Otherwise, it should assign 1
to x. [2 Marks]
19. Write a program that checks whether the year entered by the user is a leap year or not. [5 Marks]
20. Write a C++ program that prompts the user to enter two integers and a floating-point number.
Calculate the sum and product of the two integers, and display both results along with the square
of the floating-point number. [3 Marks]

You might also like