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

Final Assignment

Uploaded by

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

Final Assignment

Uploaded by

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

PART A (20 MARKS)

1. What is the output of the following expression?

12 > 6 || 8 < 20 && 9 >=10

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

3. Identify the VALID constant of type char.

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

5. Identify the preprocessor directive to be included if mathematical function such as pow() or


sqrt()is used.

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

7. Show the output of the following program segment.

int y[] = {4, 5, 2, 6, 8, 3, -2};


float w = y[1] + y[4];
cout << w;

A. 3
B. 10
C. 11
D. 13

8. What is the prototype of a function?

A. The first line of the function definition


B. The last line of the function definition
C. A declaration of the function before its actual definition
D. The return type of the function

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

10. What is the correct syntax for function definition in C++?

A. int function add (int a, int b) { }


B. add (int a, int b): int { }
C. int add (int a, int b) { }
D. function int add (int a, int b) { }
PART B (60 MARKS)

QUESTION 1

a) Given the following C++ statements:

int number1, number2;


int a, m, n, d;

//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;

cout << "Message zzz " << a << endl;

i) Explain using your own words about purpose of process 1 and process 2?
(2 marks)

ii) Write a suitable message to replace “Message zzz”.


(1 mark)

iii) What is the output if number1 is 15 and number2 is 7.


(2 marks)

b) Write a C++ program segment for each of the following:

i) To display the year that divisible by 4.


(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?

for (int i = 2; i >= 1; --i) {


for(int j = 1; j <= i; ++j) {
cout << i;
}
cout << endl;
}
(2 marks)
c) Write a program segment that prompts the user to enter a series of numbers
repeatedly until the user enters 0 to exit the data entry. The program then calculates
and displays the average of the entered numbers.
(4 marks)

d) Given the following declarations

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

a) Given the logical representation of an array height.

0 1 2 3 4 5
height 98.5 27.3 64.7 29.4 16.9 85.6

Answer the following questions:

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)

b) Determine the output of the following code segment:

int num[3];
int value = 77;
int y = 67;
int x = 300;

num[0] = value % 3 * 100;


num[1] = y + 100 /3;

if ( num[0] < num[1])


{
num[2] = num[0];
num[0] = num[1];
num[1] = num[2];
}
else
{
num[2] = num[1];
num[1] = num[0];
num[0] = num[2];
}

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

Display the result in the caller function.


(3.5 marks)
c) Write a main program to call funcX() defined in b) and display the output. Then call
funcY() defined also in question b) then display the output . Make sure the main
program asks relevant input to be sent to each function.
(4 marks)
PART C (20 MARKS)

QUESTION 1

CakeLover is now upgrading their cake ordering system where a program to manage orders and
sales need to be developed.

Code Cake Name The Price of a Discount


Cake

C1 Fruit Cake RM109

C2 Rainbow Cake RM99


5% discount for purchase of 3
C3 Cheese Cake RM89 cakes and above
C4 Onde-Onde Cake RM79.00

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.

Enter code (C1/C2/C3/C4): C1


Enter quantity: 3
Do you want to make another order? (Y/N): Y

Enter code (C1/C2/C3/C4): C7


Wrong item code entered!
Do you want to make another order? (Y/N): Y

Enter code (C1/C2/C3/C4): C3


Enter quantity: 3
Do you want to make another order? (Y/N): Y

Enter code (C1/C2/C3/C4): C1


Enter quantity: 1
Do you want to make another order? (Y/N): N

Cake Name Quantity Price (RM)


--------------------------------------
Fruit Cake 4 436.00
Rainbow Cake 0 0.00
Cheese Cake 3 267.00
Onde-Onde Cake 0 0.00
-------------------------------------
Total 7 703.00
Discount 35.15

Total Amount Due 667.85

You might also like