Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Copyright©Zainab Othman

Exercises:
Chapter 1 and Chapter 2

PART1 :
1. Which of the following is NOT a high level programming language?
A. Machine Language
B. Java
C. Pascal
D. C++

2. A/An _________ is a program that translates the source code into object code.
A. editor
B. linker
C. operating System
D. compiler

3. Missing a colon (:) at the end of the if statement will cause a ______ error.
A. logic
B. syntax
C. runtime
D. typing

4. Which of the following is NOT an arithmetic operator in?


A. %
B. *
C. ==
D. +

5. Which of the following statements are FALSE?

i. All variables must be declared before they are used.


ii. Comment (#) is using to explain about the purposes of the coding/program
written and would not be compiled by the translater.
iii. The modulus operator (%) can be used only with integer operands.
iv. C++ considers the variables total and TotAl to be the same.

A. i and ii
B. i and iv
C. ii and iii
D. ii and iv

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman
6. Which of the identifiers are VALID?
i. 1001Night
ii. distanceInKM
iii. my_name
iv. RM109.00

A. i and ii
B. ii and iii
C. i, ii and iii
D. ii, iii and iv

7. What is the output of the following program segment?

int x = 3, y = 5, z = 2;
cout<< (x % y + y * z * z % 6);

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

8. Which of the following describes the activities done in the design phase in the
Software Development Life Cycle (SDLC)?

A. Compiling and testing the program with sample data.


B. Construct step-by-step solutions using pseudocode or flowchart.
C. Provide written documentation of the developed program.
D. Wilting codes using selected programming language.

9. Listed below are the three common types of error while constructing a program,
EXCEPT

A. logic error
B. parsing error
C. run-time error
D. syntax error

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman

10. Identify which of the following statements are TRUE about comments in C++.

I. A single line comment begins with the symbol / / and continues to the end of
the line.
II. Block comments begin the symbol /and end with the symbols /.
III. Comments are placed in the program to improve its readability.
IV. Comments can be placed anywhere within a program and they will not be
executed by the compiler.

A. I, II and III only.


B. I, III and IV only.
C. II, III and IV only.
D. I, II, III and IV.

11. Choose the BEST elaboration for the given statement written in C++.

const float LENGTH = 2.71;

A. It declares a variable of type float and assigns the variable to an initial value of
2.71
B. It declares a variable to store any number with the name LENGTH.
C. It defines a constant of type float named LENGTH with value of 2 . 7 1.
D. It defines a variable named LENGTH and assigns the variable with an initial
value Of 2.71

12. The purpose of the maintenance phase in the problem-solving process is

A. Compiling the program.


B. Defining and analyzing the problem.
C. Modifying and upgrading the program.
D. Writing the code after a problem has been defined.

13. Which of the following statements is NOT TRUE about flowcharts?

A. It shows instructions of an algorithm in a human natural language style.


B. It is used to show instructions of an algorithm in a graphical representation.
C. It provides a detailed flow of instructions to a programmer for the next step
in writing codes using a programming language.
D. It is used to define and analyze the logical processes.

14. Why is indentation important in writing codes?

A. It makes a program more readable.


B. It makes a program run faster.
C. It helps reduce programming error.
D. It makes a program easy to compile.

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman

15. This program translates a computer program written in a high-level programming


language to a machine language.

A. Translator
B. Editor
C. Interpreter
D. Compiler

16. What are the processes involve in Program Development Life Cycle?

A. Algorithm design, problem analysis, implementation, testing, maintenance


B. Problem analysis, algorithm design, implementation, maintenance,testing
C. Problem analysis, algorithm design, implementation, testing, maintenance
D. Algorithm design, implementation,problem analysis, testing, maintenance

17. There are three main sections to do the algorithm. What is the section that required
to accomplish the task?

A. Processing
B. Output
C. Input
D. flowchart

18. Which of the following statement is TRUE about flowchart?

A. It is used to define and analyze the flow of the process.


B. It represented by using English natural language.
C. It can be compiled or executes by compiler C++.
D. It express in a code of programming.

19. How to declare a constant MAX_LENGTH with the value of 99.98?

A. const MAX_LENGTH = 99.98;


B. const long MAX_LENGTH = 99.98;
C. const double MAX_LENGTH = 99.98;
D. double MAX_LENGTH = 99.98;

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman

PART2 :
1. What is a correct syntax to output "Hello World" in C++?

2. Which symbol is used to represent an input/output in a systems flowchart?

3. What does the Start/End symbol do?

4. What is the difference between a flowchart and pseudocode?


5. Complete the problem definition for the problems below. What the information is,
what data are needed, what formulas are involved and how an expected screen
looks like.

a. Calculate the total of four marks.

b. Calculate the total of five numbers. The first three numbers are 34, 92 and
55 .

c. Calculate the total area and volume of a cylinder with diameter 14.5m.

d. Calculate the area of a triangle with the following formula :

area = [ s(s-a)(s-b)(s-c)]
where s = a + b + c
2
a, b and c are triangle measurements.

e. Calculate the total commission for the first six months that will be paid to En
Halim. En Halim is a salesman in Syarikat ABC. The company pays monthly
commission based on the monthly sales. The commission rate is 5% of
monthly sales.

f. Calculate the net salary for a worker. The net salary is the different of gross
salary and total deduction. The gross salary is the total of basic salary and
total allowances. The allowances are housing allowance, telephone
allowance and transportation. The allowance are RM500, RM700 and
RM750. The worker has to contribute to EPF deduction 9% of gross salary
and contribution for SOCSO 2% of gross salary.

g. En. Ahmad wants to buy a house. He decides to pay RM20,000 as the


deposit and get a loan from the bank to pay the balance. The bank ABC
offers a loan with 2% interest rate for the first two years and 5% for the
remaining. Loan duration is 15 years. Assume that En.Ahmad accepts the
offer. Calculate the monthly payment. Use the following formulas:

Monthly payment = total loan


(Loan duration * 12)

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman
Total loan = loan amount + total interest
Total interest = 2 * loan amount * 2% + 13 * loan amount * 5%
Loan amount = price of the house – deposit

6. Write a program that takes as input the purchase price of an item (P), its
expected number of years of service (Y) and its expected salvage value (S).
Then outputs the yearly depreciation for the item (D). Use the formula: D = (P - S)
Y.

7. Write an appropriate declaration for each of the following :


i) A character variable named first
ii) A string variable named company name
iii) A floating-point variable named root
iv) An integer variable named quantity
v) A constant floating-point variable named gravity that holds a value of
9.8.

8. Write C++ statements to accomplish each of the following tasks:


i) Declare a variable to store the value of an allowance. Assign a float
value of 4.5 to it.
ii) Declare variables of payment per hour, total working hours in a month
and name of a staff
iii) Declare a variable of total salary to store floating numbers
iv) Prompt the user to enter all the information in question (ii).
v) Calculate the total salary including the allowance.

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman
vi) Display the total salary including the allowance.

9. Write a C++ statements to accomplish each of the following tasks:


i) Initialized value 50.5 to a variable named allowance.
ii) Declare a constant variable named gravity and assign the value of 9.8 to
it.
iii) Assign value ‘ABC Enterprise’ to a variable named company.

10. Write a C++ assignment statement for each of the following algebraic equations:
i) E= MC2 a(rn-1)
ii) Sum = 2r + 1 + 15e
iii) P = 4m – n
iv) A = 2x2 + 4x + 2y2
v) C = 34x + 4(32x)
vi) Y = (2 – 3x2)a
vii) A = 5xy – 3x(y – (2 – 5x))2x 2

11. Convert the Algebra expression to arithmetic expression in programming


language :

a.

b.

c.

d.

f.

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman

12. Given is the compound assignment statement and answer the question:

Compound Equivalent to
assignment

c += 7 Example: c = c + 7;

c-=3

c*=4

c/=2

c^=2

13. Evaluate the given arithmetic expression:

i) 3 / 2 + 5.5 Example answer : 1 + 5.5 = 6.5


ii) 15.6 / 2 + 5
iii) 4 + 5 / 2.0
iv) 4 * 3 + 7 / 5 - 25.5
v) 34 % 5
vi) 4%6

14. Explain the order of precedence for the following arithmetic expressions:

3*7–6+2*5/4+6

15. What is the output for the program below:


int a,b,c;
double x,y,z;
a = 13;
b = 4;
x = 3.3;
y = 15.78;
c = a + b;
cout<<"a + b is "<<c<<<"x + y is "<<z<<<"a / b is "
<<c<<<"a % b is "<<c<<endl;

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman

16. Write a C++ program segment for the following problem:

a. Calculate the price after discount . The company offers 20% off for each
product.You need to insert the price of a product and the quantity of that
product.
b. Calculate the commission will be paid to a salesman. The company pays
15% commission of total sales made by the salesman. The total sales is item
price sold multiply how many item sold.
c. Ask ther user to enter his/her monthly salary. Calculate the yearly salary.
Then calculate the tax. The tax rate is 2.5% of yearly salary.
d. Calculate the deduction will be earned by a tax payer. The tax deduction for
a RM500 per child, RM8000 for his self and RM5000 for his wife. Assume
that the tax payer is a man with a wife and few children.

e. Determine the weight of 5 different steel cylinders by using the following


formula:

Weight = density X π X radius2 X height

Prompt the user to enter the radius and height separately for the 5 steel
cylinders given density is the same for all cylinder which is 0.284.

f. A block of iron that is 9 cm long, 4.5 cm wide and 5 cm thick has a mass of
1800 g. Calculate its density in g cm-3.

mass
Given the formula for density is : density = and
volume

iron volume = length X width X height

17. Write a C++ program to find the area of shaded region in the figure above.
Triangles A and B have exactly the same dimensions and area, where both
the length and width are equal. The user will be required to enter the
area of a triangle.
You are given the following formula for the above diagram:

radius of circle =√2 ∗ 𝑎𝑟𝑒𝑎 𝑜𝑓 𝑎 𝑡𝑟𝑖𝑎𝑛𝑔𝑙𝑒

a) Identify the processes for this problem

PAGE \* MERGEFORMAT 1
Copyright©Zainab Othman
b) Write the complete code to solve this problem.

18. The following code contains a few syntax errors, make correction to the
errors
int main()

float a, b; int C;

cout<<”Enter two numbers: “;


cin>>a>>b;
c = a % b;
cout<<”The result is “<<c;

return 0

PAGE \* MERGEFORMAT 1

You might also like