Final Assignment
Final Assignment
A. true
B. false
C. syntax error occurs
D. null
2. Based on the following program segment, choose the correct output that will be
displayed.
int a=30,b=0;
if (a>24)
b=23;
if (a%2==0)
a=a+2;
else
a=b;
b=b+1;
cout<<a<<" "<<b;
A. 30 23
B. 17 0
C. 32 24
D. 32 1
I. ‘M’
II. “Welcome”
III. Z
IV. ‘5’
A. I, II, III
B. I, IV
C. I, III
D. II, IV
4. Identify which of the following condition that will test if the value in the integer variable num is
odd?
A. (num % 2) == 1
B. (num % 2) == 0
C. (num / 2) == 0
D. (num / 2) == 1
A. iostream
B. iomanip
C. math
D. string
6. What is the output of the following code segment:
int x = 6;
switch (x % 5)
{
case 0 : cout << ”ZERO”;
case 1 : cout << ”ONE”;
case 2 : cout << ”TWO”; break;
case 3 : cout << ”THREE”;
}
A. ONE
B. ONETWO
C. ZEROONE
D. NONE OF THE ABOVE
A. 3
B. 10
C. 11
D. 13
9. What is the return type of a function that doesn't return any value?
A. void
B. int
C. null
D. none of the above
QUESTION 1
//input section
cout << "Enter the first number : ";
cin >> number1;
cout << "Enter the second number : ";
cin >> number2;
//process 1
if (number1 > number2)
m = number1;
else
m = number2;
//process 2
if (number1 < number2)
n = number1;
else
n = number2;
//process 3
a = m - n;
i) Explain using your own words about purpose of process 1 and process 2?
(2 marks)
ii) To prompt the user to enter a number between 0 and 100 and store the value into
variable score. Display the error message if user input the invalid number.
(3 marks)
c) Based on the following flowchart, write the program segment.
(5 marks)
QUESTION 2
a) Given the following flowchart, convert it into a C++ program.
(4 marks)
b) What is the output from the following code fragment?
int main() {
string correctUsername = "guest";
string correctPassword = "abc123";
string enteredUsername;
string enteredPassword;
Write the remaining code that prompts the user to input a username and a password. The
intended program should limit the user to only TWO attempts to enter the correct
authentication data. Upon successful input, provide the user with an appropriate success
message or notification.
(Hint: Consider using a for loop. If user correctly inputs username and password in the first
attempt, exit the loop using the break statement.)
Sample outputs are given below:
(5 marks)
QUESTION 3
0 1 2 3 4 5
height 98.5 27.3 64.7 29.4 16.9 85.6
i) Declare and initialize an array named height of type float based on the diagram
above.
(3 marks)
ii) Write a program segment to determine and display the lowest height in the array.
(6 marks)
int num[3];
int value = 77;
int y = 67;
int x = 300;
cout<< " First number = " << num[0] << ", ";
cout<< " Second number = " << num[1] << ", ";
cout<< " Third number = " << x << endl;
(6 marks)
QUESTION 4
a) Trace and write the output for the following program segments.
void main ()
{
int x = 7, y = 2, z;
z = subtraction (x,y);
cout<< "The first result is " << z<<endl;
cout<< " The second result is " << subtraction(20,y)<<endl;
z= 4 + subtraction (5,10);
cout<< " The third result is " << z <<endl;
}
int subtraction (int a, int b)
{
int result;
result = a - b;
return result;
}
(4 marks)
b) Write a function definition for the following sub programs:
i) A function named funcX()that receives two values which are testMark and
percentageRate from a main function. This function calculates the
totalTestMark by multiplying testMark with percentageRate. The result of this
function shall be displayed in the main function.
(3.5 marks)
ii) A function named funcY()that receives one argument (values) named number by
a reference from the caller. This function is used to update the value of variable
number using the following formula:
number2 + 2* number; 0 number 10
number ÷ 2 + number; number > 10
QUESTION 1
CakeLover is now upgrading their cake ordering system where a program to manage orders and
sales need to be developed.
The program should allow the user to input code and its quantity. User can make another order for
other cake by repeating the process until user enters ‘N’ or ‘n’ to stop.
If user enters the same code, its quantity will be updated. Display an error message for an invalid
code. Then display the details of order made by the customer (including cake name, quantity, price
and discount and total price for all cakes).
Write a complete C++ program where the input and the output should appear as in the sample shown
below.