Vous êtes sur la page 1sur 13

(BITP 1113) SULIT

PART A: PRINCIPLE KNOWLEDGE, TRACING AND PROGRAM


TROUBLESHOOTING (70 MARKS)
ANSWER ALL QUESTIONS.

QUESTION 1 (10 MARKS)


a) Name the appropriate hardware components based on the Figure 1 and give an example
each.

Figure 1: Computer System

i) __Input devices_________________ example: __mouse / keyboard / etc.___

ii) __Central Processing Unit (CPU)___ example: __Intel Pentium / AMD____

iii) __Main memory_________________ example: __RAM________________

iv) __Output devices________________ example: __monitor / printer / etc.___

v) __Storage devices________________ example: __hard disk / pen drive____

(8 marks)

a) Name the two levels of programming languages and what is the major difference
between them.

ANSWER
Low-level: close to machine language
High-level: close to human language

(2 marks)

-11- SULIT
(BITP 1113) SULIT

QUESTION 2 (30 MARKS)


Each question is worth 1 mark.

a) Fill in the blank for questions (i) to (v).

i. C++ program always begins execution with __main()____________ function.

ii. <include cmath> is a __preprocessor directive__________________.

iii. You cannot use any of the C++ __keywords/reserved words__ as an identifier.

iv. A variable is a storage location in a __computer memory_________________.

v. A variable must be __declared/defined__ before it can be used.

b) CIRCLE the correct answer in the table below for questions (i) to (x).

ANSWER

i. A B C D

ii. A B C D

iii. A B C D

iv. A B C D

v. A B C D

vi. A B C D

vii. A B C D

viii. A B C D

iv. A B C D

x. A B C D

-12- SULIT
(BITP 1113) SULIT

i. What is the correct value to return to the operating system upon the successful
completion of a program?
A. -1
B. 1
C. 0
D. Program do not return a value.

ii. What punctuation is used to signal the beginning and end of code blocks?
A. { }
B. -> and <-
C. BEGIN and END
D. ( and )

iii. Which of the following is TRUE about C++ comments?


A. Comments are used by programmers as notes that explain the
purpose of a program, a function, astatementand etc.
B. Comments are executed by the compiler
C. Comments are ignored by the complier
D. A and C

iv. What is (are) the C++ command to take the cursor to the beginning of the next
line?
A. newline
B. \n
C. endl
D. B and C

v. Which statement represents a memory constant in C++?


A. const double GST = 0.006;
B. const GST 0.06;
C. double const GST = 0.006;
D. const GST = 0.06;

-13- SULIT
(BITP 1113) SULIT

vi. Which statement represents a defined constant in C++?


A. #define PI 3.14159
B. #define PI 3.14159;
C. #define PI = 3.14159
D. #define PI = 3.14159;

vii. Which is NOT C++ type of operators?


A. Arithmetic operators
B. Logical operators
C. Relational operators
D. Associative operators

viii. What is the output of the following C++ statement?


cout << 91.0 / 7;

A. 31.0
B. 31
C. 31.00
D. Error

ix. Given this declaration: z = 12 % 5. What is the value assigned to z?


A. 0
B. 1
C. 2
D. 3

x. What is the C++ operator for equality?


A. ==
B. &&
C. ||
D. =

-14- SULIT
(BITP 1113) SULIT

QUESTION 3 (15 MARKS)

a) What is the output of the following program?

void printNumber(int& a, int& b, int c );

int main()
{
int x = 5, y = 10, z = 2;
cout << x << ' ' << y << ' ' << z << endl;
printNumber (x, y, z);
cout << x << ' ' << y << ' ' << z << endl;
return 0;
}

void printNumber(int& a, int& b, int c )


{
a = a * 2;
b = b + a * c;
c = a - b;
cout << a << ' ' << b << ' ' << c << endl;
}

(10 marks)

ANSWER

5 10 2 (3M)
10 30 -20 (3M)
10 30 2 (4M)

-15- SULIT
(BITP 1113) SULIT

b) Find and fix the error of the following program based on the output given:

Line 1 void calc (int, int);


Line 2
Line 3 int main()
Line 4 {
Line 5 int d = 1, e = 2, f = 3;
Line 6 calc(d, e);
Line 7 cout << d << ' ' << e << ' ' << f << endl;
Line 8 return 0;
Line 9 }
Line 10
Line 11 void calc (int x, int y)
Line 12 {
Line 13 int z = x + 2;
Line 14 x = x * 3;
Line 15 y = z + x;
Line 16 }

Output

(5 marks)

ANSWER

void calc (int, int&); - Find 1M (Line 1), Fix 1M

void calc (int x, int& y) - Find 1M (Line 11), Fix 2M

-16- SULIT
(BITP 1113) SULIT

QUESTION 4 (15 MARKS)

a) If originally x = 0, y = 0, z = 1, what are the values of x, y, and z after executing


the following code?

switch (x)
{
case 0:
x = 2;
y = 3;
case 1:
x = 4;
break;
default:
y = 3;
x = 1;
}

(6 marks)

ANSWER

x = 4, y = 3, z = 1 2m each = 6m

-17- SULIT
(BITP 1113) SULIT

b) Rewrite the following program using switch statement?

if (selection == 1)
cout << "PI times radius squared\n";
else if (selection == 2)
cout << "Length times width\n";
else if (selection == 3)
cout << "PI times radius squared times height\n";
else if (selection == 4)
cout << "Well okay then, good bye!\n";
else
cout << "Not good with numbers, eh?\n";
return 0;

(9 marks)

ANSWER

switch (selection) (1m)


{
case 1:
cout<<"PI times radius squared\n"; (1m)
break; (0.5m)
case 2:
cout<<"Length times width\n"; (1m)
break; (0.5m)
case 3:
cout<<"PI times radius squared times height\n"; (1m)
break; (0.5m)
case 4:
cout<<"Well okay then, good bye!\n"; (1m)
break; (0.5m)
default: (1m)
cout<<"Not good with numbers, eh?\n"; (1m)
}

-18- SULIT
(BITP 1113) SULIT

PART B: PROBLEM SOLVING AND SHORT PROGRAMMING (30 MARKS)


ANSWER ALL QUESTIONS.

QUESTION 1 (10 MARKS)

Refer to the problem statement below:


Calculate the total cost to install new tiles for every bathrooms in your house. The cost
must be below RM5000 or else the installation couldnt be done.

Based on the Pseudocode as shown below, design the possible solution for the problem by
using flowchart. The design must have one function; calculateCost().

Pseudocode to Calculate Bathrooms Cost


1 Prompt user and read tiles price per square feet
2 Prompt user and read number of bathrooms
3 Prompt user and read bathroom length and width
4 Call calculateCost() function to calculate cost of tiles installation
a. Set total bath area and total cost to zero
b. Total bath area = number of bathrooms * bath length * bath width
c. Total cost = Total bath area * tiles price per square feet
d. Return total cost
5 If the cost of installation is below RM5000 then
a. Print the message bathroom tile installation is successful
6 Else
a. Print error message the bathroom tile installation is NOT successful
End Pseudocode to Calculate Bathrooms Cost

(10 marks)

-19- SULIT
(BITP 1113) SULIT

ANSWER

Start calculateCost(p,
num, l, w)

Read tiless price per square feet


Read number of bathrooms totalBathArea = 0
Read bathroom length and width totalCost = 0

totalCost = totalBathArea = num * l * w


calculateCost(p, num, l, w)

totalCost = totalBathArea * p
totalCost< 5000

no
yes

return totalCost
Display message Bathroom tile
installation is Successful

Display message Bathroom tile


installation is NOT Successful

End

-20- SULIT
(BITP 1113) SULIT

QUESTION 2 (5 MARKS)

Complete the following skeleton by writing appropriate selection statement that displays:
One if the user has entered 1
Two if the user has entered 2
Three if the has entered 3

If a number other that 1, 2 or 3 is entered, the program should display an error message.

void display_number()
{
int usernum;
cout << "Enter one number: ";
cin >> usernum;

//write the selection statement here

if (usernum == 1) 1.5m
cout<<"One\n";
else if (usernum == 2) 1.5m
cout<<"Two\n";
else if (usernum == 1) 1m
cout<<"One\n";
else
cout<<"Error!\n"; 1m
}

(5 Marks)

-21- SULIT
(BITP 1113) SULIT

QUESTION 3 (15 MARKS)

Refer to Figure 1. Write a program to ask a user to enter a value of a radius in main() function.
The value is pass to function perimeter() to calculate the perimeter of a circle. The
perimeter of a circle is given by the formula of 2 x PI x radius. Also, pass the radius to function
area() to calculate the area of a circle. An area of a circle is given by the formula of PI x
radius2. Next, pass both answers to function display() to print the results. The PI is define
as a constant variable of 3.142 and the value for radius must be greater than zero. If user enters
value less than zero, prompt an alert statement and end the program.

Perimeter = 2 x pi x r
Area = pi x r 2

Figure 1.
(15 marks)

-22- SULIT
(BITP 1113) SULIT

ANSWER
#include<iostream>
#define PI 3.142
using namespace std;

double perimeter(double);
double area (double);
void display (double,double);

void main()
{
double r,p,a;

cout<<"Enter radius : ";


cin>>r;
if (r>0.0)
{
//call function to calculate perimeter
p=perimeter(r);
//call function to calculate area
a=area(r);
//call to display
display(p,a);
}
else
cout<<"A radius value must be greater than 0"<<endl;
}

double perimeter(double r)
{
double a= 2*PI*r;
return a;
}

double area (double r)


{
double a= PI*r*r;
return a;
}

void display (double p, double a)


{
cout<<"The perimeter is: "<<p<<endl;
cout<<"The area is: "<<a<<endl;
}

double area (double r)


{
double a= PI*r*r;
return a;
}

-END-

-23- SULIT

Vous aimerez peut-être aussi