KMS 2018/2019 PRE PSPM SC025
Name : ________________________________________ Class : _______________
SECTION A
1 In order to instruct a computer correctly, programmer must design the algorithm of the
problem to be solved.
(a) Give two (2) techniques that can be used to represent algorithm.
[2 marks]
Pseudocode
Flowchart
(b) Describe one (1) of the techniques given in 1(a).
[2 marks]
Pseudocode – the use of artificial language to develop algorithm.
Flowchart – graphical representation of algorithm that uses special purpose symbols,
connected by arrows
2 Java programming language is a powerful, general purpose, platform independent and object
oriented programming language.
(a) What is Object Oriented Programming?
[1 mark]
Object-oriented programming (OOP) is a programming technique that uses “objects” to
design the applications and computer programs.
(b) Identify the features in Object-Oriented Programming based on statement given below.
(i) The ability to process objects differently based on the input.
[1 mark]
Polymorphism
(ii) This feature protects data and methods from outside access or misuse.
[1 mark]
Encapsulation
(iii) A process of hiding the implementation details and showing only functionality to
the user.
[1 mark]
Abstraction
(c) Classes and objects are the fundamental building block of the Object-Oriented Approach.
(i) Define class.
[1 mark]
Class is a template or blueprint to create an object to defines the variable and functions
that are common to the objects of its type.
(ii) State two (2) elements of an object.
[2 marks]
Identity, State/attribute, behaviour
1 | Page
KMS 2018/2019 PRE PSPM SC025
3 (a) Variables and constants are the basic data objects that will be used in a program,
while the declaration lists the type of variables with their initial values. Identify the
valid/invalid variable names below, give reason why it is valid/invalid.
[3 marks]
Identifiers Valid/Invalid Reason
Best! Invalid Identifier cannot consist of symbol
$rm Valid can start with $
number1# Invalid Identifier cannot consist of symbol
(b) Solve the following expressions by analyzing the precedence of the respective operators.
(i) 48 % 5 / (2 + 1)
[1 mark]
1
(ii) 5 + 15 / 3 * 2 – 8 % 3
[1 mark]
13
(c) Given two numbers with variable name num1 and num2. Operation of these two
numbers will be stored in Val. Give valid java expression in the operation below.
[2 marks]
Operation Expression
Cubic of num1 Val = num1*num1*num1; @ Val= Math.pow(num1,3);
Product Val = num1*num2;
(d) Array helps in managing large number of variables.
(i) Identify the array index for each value below.
int arr[] = {2,4,6,8};
[2 marks]
Value Index
0 No index
6 2
(ii) Identify the valid/invalid elements in the following. Give reason.
[4 marks]
int mark[] = new mark[25];
Element Valid/Invalid Reason
mark[0] valid Index number must start with 0
mark[1] valid the index is in the range of array size
mark[-1] invalid index is out of bound
mark[25] invalid index is out of bound
2 | Page
KMS 2018/2019 PRE PSPM SC025
(iii)
class Value {
public static void main (String args[]) {
int num[ ] ={1,3,5,7};
num[0] = 23;
num[3] = num[1];
System.out.println (num[0] + “ “ + num[3]);
}
}
What is the output for the above program?
[1 mark]
23 3
4 In Java programming, a big program can be split into a number of smaller programs that use a
concept called Method.
(a) (i) What is a Method?
[1 mark]
A method is a program module that contains a series of statements that carry
out a task.
(ii) Give one (1) advantage of using Method
[1 mark]
Reusability / more readable / easier to debug
(iii) State two (2) types of Method.
[2 marks]
Standard Library Method and User-Defined Method
(iv) Give three (3) components of user-defined method.
[3 marks]
return type, method name and parameters
(b) (i) Write a method definition named isDivisible that takes two (2) integers a and b
and return true or false value.
[3 marks]
boolean isDivisible (int a, int b)
(iv) Give the difference between parameters and arguments in method.
[2 marks]
Parameters Arguments
Variable declaration that is used in method Value that is pass to the method
3 | Page
KMS 2018/2019 PRE PSPM SC025
(v) Why void is used as return type in Method?
[1 mark]
void is used as a function return type, it indicates that the function does not
return a value.
(vi) class Cube {
public static void main (String args[]){
Scanner sc = new Scanner (System.in);
int num = sc.nextInt( );
Cube mtd = new Cube ( );
System.out.println (“The cube of “ + num + “ is “ + mtd.cube(num));
}
int cube (int x) {
return x*x*x;
}
}
Based on Java program above, identify the appropriate components as below.
[5 marks]
Components Components Name
Class name Cube
Object method name mtd
Method name cube
Return type int
Method Call mtd.cube(num)
4 | Page
KMS 2018/2019 PRE PSPM SC025
SECTION B
5 (a) Programmers need to follow a few steps of problem solving in order to create a
complete program coding. Identify steps in problem solving based on the scenario
below.
[2 marks]
Scenario Steps in Problem Solving
Mr Rudi has to ensure the compliance of the
application with the real scenario based on the Testing
user’s need.
The main purpose of this step is to describe the
use, operation, maintenance or design of software
or hardware through the use of manuals, listings, Documentation
diagrams and other hard or softcopy written and
graphic materials.
(b) Identify the input, process and output for the problem given below.
(i) Cynthia was borrowing a book from ABC library. She was returning a book late.
Calculate the total fine charged to Cynthia by library for late return book. The
charge is RM0.20 per day.
[3 marks]
Input: late day
Process: Calculate total fine charged based on late day
Output: total fine charged
(ii) Find the lowest number of three numbers that are randomly inserted by the user.
[3 marks]
Input: number 1, number 2, number 3
Process: Determine the lowest number based on three numbers entered
Output: lowest number
(iii) Calculate the sum of 500 numbers that are being input interactively using loop.
[3 marks]
Input: number
Process: Calculate sum for 500 numbers based on number entered
Output: sum
5 | Page
KMS 2018/2019 PRE PSPM SC025
(c) (i) Puan Tipah is planning to prepare a healthy breakfast for her family. She needs to
buy a few items and needs to calculate the total price based on the price and quantity
of each item that she wants to buy. For items with the item code of 5000 and
above, 6% of Sales Tax and Service Tax (SST) will be imposed. The calculation
process is finished when she enters 0 for the value of item code. Write a
pseudocode to calculate and display the total price including SST, if any.
[7 marks]
Start
read item code
total price = 0
repeat while (item code != 0)
read price , quantity
if (item code >= 5000)
total price = total price + (price x quantity x 1.06)
else
total price = total price + (price x quantity)
read item code
end repeat
print total price
End
6 | Page
KMS 2018/2019 PRE PSPM SC025
(ii) This program requires a user to enter two non-negative integer values. If any of the values
entered is negative, the message “Number must be non-negative.” is displayed and the
algorithm terminates. Otherwise, it displays the sum of the two values if the first value is less
than the second value. Or else, the product of the two values is displayed. Prepare a
flowchart for this program.
[8 marks]
7 | Page
KMS 2018/2019 PRE PSPM SC025
6 (a) A Java file basically consist of three (3) main components. Write a Java statement as
below.
(i) Read two (2) numbers and display the product of the numbers.
[2 marks]
(ii) Write two (2) ways of comments in “This is a Java programming “.
[2 marks]
(b) Swap the contents of the char variable name1 and name2. (Declare additional
variables, if necessary).
[3 marks]
(c) Consider the following variable declarations:
int m, w = 10;
double x = 5.0;
int y;
double z;
For each assignment statement below, write the value assigned.
(i) z = 2 / w – x / w;
[1 mark]
-0.5
(ii) z = w + w * x;
[1 mark]
60.0
(d) Convert the following algebraic expression into Java expression.
2
(i) Q =10x + 8x - 2
[1 mark]
Q = 10*x*x + 8*x – 2;
2
2
(ii) val = p + r
[1 mark]
val = p*p + r*r;
8 | Page
KMS 2018/2019 PRE PSPM SC025
(e) What is the value of x after the following program segment is executed if y is 15?
[2 marks ]
75
7 (a) Write the output of the following program.
[3 marks]
import java.util.Scanner;
public class MyName
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double myWeight,
myHeight;
//Example weight is 68.5 kg; height is 161.3 cm
System.out.println("Enter your weight in kg:");
myWeight = sc.nextDouble();
System.out.println("Enter your height in cm:");
myHeight = sc.nextDouble();
System.out.print ("\nYour weight in kg is " +myWeight);
System.out.print (", your height is cm is " +myHeight);
}
}
Enter your weight in kg:
68.5
Enter your height in cm:
161.3
Your weight in kg is 68.5, your height is cm is 161.3
(c) Write the Java code segment to print the odd numbers from 1 to 99 using a for statement.
Print only 3 numbers per line.
[6 marks]
for(int i=1;i<=99;i=i+2)
{
System.out.print(i + "\t");
if(i%3==2)
System.out.println();
}
9 | Page
KMS 2018/2019 PRE PSPM SC025
(f) Statements below contain an error. Explain the type of error and write the correct
statement.
(i) if ( gender == M)
[2 marks]
Syntax error is an error in the syntax of a coding or programming language which
occurs when the syntax is violated.
if (gender ==’M’) @ if (gender ==”M”)
(ii) int x= 5, y = 2;
System.out.print ( x / 0 );
[2 marks]
Run-time error that can occurs during the execution of a program.
System.out.print ( x / y );
8 (a) Create a method name roundNumber that will round off any integer entered by user to
the nearest ten. If -17 is entered, the output is -20 and if 37 is entered, the output is 40.
[4 marks]
10 | Page
KMS 2018/2019 PRE PSPM SC025
(b) Write a complete Java program that asks the user to enter temperatures for a number of
days user wants to process and stores all temperatures entered into an array. Then the
program reports the average temperature, the number of days above average, the lowest
and highest temperature entered.
[11 marks]
End of Questions Paper
11 | Page