KOLEJ MATRIKULASI PULAU PINANG Section A
SEMESTER / SESI : 02 - 2020 / 2021
NAMA SUBJEK: SAINS KOMPUTER 2 Q1 : / 28
PRA PSPM SC025 Q2 : / 13
MASA : 90 MINIT Q3: / 12
NAMA PELAJAR :
KUMP. PRAKTIKUM : Q4: /17
TOTAL : / 70
Total : /70
Answer all the questions in the space provided in the question paper.
1 (a) Considering the following scenarios, identify the input, process and
output.
(i) Given two integers, FirstNum and SecondNum. Swap and display if the first
number is larger than the second number.
[4 marks]
Input:
FirstNum, SecondNum ----------------J½, J½
Process:
To determine whether to swap the numbers based on FirstNum and
SecondNum ------------------J2
Output:
FirstNum, SecondNum----------------J½, J½
(ii) The rate of charge for the first 200 units of electricity used is RM0.218 per unit.
For the following unit of electricity used is RM0.334 per unit. A penalty of
1.5% of the unpaid balance if the unpaid balance is greater than zero. Calculate
the customer’s penalty and electricity bill. Electricity unit and unpaid balance
are inputted by user.
[4 marks]
Input: unit,upaid balance----------------J½, J½
Process:
To determine the electricity bill based on unit used and unpaid balance
--------------------------J2
Output: penalty, bill ----------------J½, J½
(b). Given the following algorithm, identify the input, process and output.
Start
Read X, Y
While (X ≠ Y) DO
Print Y
Y = Y + 1
Print “The last y is:”,Y
Stop
[4 marks]
Input: X, Y ----------------J½, J½
1
Process:
Repeat to display Y until X is equal to Y. ----------------J2
Output: “The last y is:”,Y
(c). The given algorithm below shows a specific procedure for solving a well-defined
computational problem.
Start
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Set the class average to the total divided by ten
Print the class average
Stop
FIGURE 1
(i) Name the type of algorithm shown in FIGURE 1.
[1 mark]
Pseudocode ---------------J1
(ii) Describe the control structure used in the given algorithm.
[2 marks]
Looping/Repetition [J1] - directs the computer to repeat one or more
program instructions/statements until some condition is met.--------------
J1
(d). Write pseudocode that examines the temperature. Then display the following
messages, depending on the temperature.
Temperature Message
Less than 0: ICE
Between 0 and 100: WATER
Exceeds 100: STEAM
[6 marks]
START
Read temp --------------------------J½
if (temp<=0) -----------------J2
print "ICE" --------------------------J½
else
if (temp<=100)-------------------J2
print "WATER" --------------------------J½
else
print "STEAM"--------------------------J½
STOP
(e). Draw a flowchart for a program that asks a user to guess a number between 1 to 50
(inclusive) and determine whether it is equal to the magic number. Calculate the
user’s total attempt guessing the magic number. Once the guess number is equal to
the magic number, display “Finally, You Got It” message and the user's total
attempt. (Assigned magic number to 47)
[6 marks]
2
START
magicNumber=47
--------------J½, J½
totalAttempt=0
--------------J½
Read
guessNumber
--------------J1
N
print
while --------------J½, J½
“Finally, You Got
(guessNumber!=47) It”,
totalAttempt
Y
--------------J½ STOP
totalAttempt=totalAttempt+1
--------------J½
Read
guessNumber
Y - ½, N - ½, flow- ½
2 (a) Identify the characteristic of object-oriented programming based on the given
description.
[1 mark]
Description Characteristic.
“It allows us to apply consistent approach to
Polymorphism – J1
inconsistent but related behavior.”
(b) What is the output produced by the following statement?
System.out.println (“50 plus 25 is”+50+25);
[1 mark]
50 plus 25 is5025 --------------J1
(c) Complete the program to reverse a number.
class Main {
public static void main(String[] args) {
3
int num = 1234, reversed = 0,digit;
while(num != 0) {
(i) digit = num % 10; ----------J1
(ii) reversed = reversed * 10 + digit; ----------J1
(iii) num = num / 10; ------------J1
}
System.out.println("Reversed Number: " + reversed);
}
}
Output: Reversed Number : 4321
[3 marks]
(d) You are assigned to create a simple Java program to calculate an area of a rectangle.
(i) Give one (1) example of invalid identifier that you cannot use.
[1 mark]
Any acceptable invalid identifier ------------------J1
(e) Write a Java statement that declare and initialize a variable double named constant
serviceRate whose value is 5 %.
[1 mark]
final double SERVICERATE = 0.05; ----------------J1
(f) Write the following mathematical expressions in Java. Be sure that you give complete
Java statements. Assume all variables are already declared
(i)
[1 mark]
u= s + (v * t) + (0.5 * (g * t * t))----------------J1
or
u = s + (v * t) + (0.5 * g * (Math.pow (t,2)))------------J1
(ii)
[1 mark]
y = Math.sqrt ((a * a) + (b * b) – 2 * a * b * cos c ----------J1
(g) Determine if the following tests are true or false based on the values of the given
variables:
int a = 0;
int b = 1;
(i) (a < b || b < a);
[1 mark]
true -------------- J1
(h) Suppose a, b and c are integer variables, w and y are double variables.
w = 12.9;
y = 3.2;
a = 3;
b = 10;
c = 7;
double resultA,resultB;
4
int resultC;
resultA = a % b / y;
System.out.println (“Result of a % b /y is: “ + resultA);
resultC = a – b /c;
System.out.println (“Result of a - b /c is: “ + resultC);
resultB = a + w / b;
System.out.println (“Result of a + w /b is: “ + resultB);
State the output for above program.
[3 marks]
Result of a % b /y is: 0.9375 ----------J1
Result of a - b /c is: 2 ------------------J1
Result of a + w /b is: 4.29 -------------J1
3 (a) Circle the error(s) in the program segment below.
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int feet;
int inches;
System.out.println("Enter value for feet and inches.");
feet = input.nextInt();----------------J1(wrong object)
inches = console.nextInt();
System.out.println("feet = " + feet);
System.out.println("inches = " inches);--------J1(missing +)
}
[2 marks]
(b) Consider the following code fragment. What would be the output if temp is 3?
if (temp >= 0)
if (temp == 0)
System.out.println("Mask On");
else
System.out.println("Obey SOP");
System.out.println("Use hand sanitizer");
[2 marks]
Obey SOP ----------J1
Use hand sanitizer -----------------J1
(c) Write a Java code fragment that will display the following output.
[4 marks]
1,2,3,4,5
6,7,8,9,10
YEA!!!
for (int i = 1; i <= 10; i++){ -------------J½, J½, J½
if (i % 5 == 0) -------------- J1
System.out.println (i); -------------J½
5
else
System.out.print (i + “,”); -------------J½
}
System.out.println (“YEA!!!”); -------------J½
(d) Write a Java statement(s) for the following questions.
(i) Declare and initialize an array named Month to JAN,FEB, MAC,APRIL,
MEI, JUN.
[1 mark]
String [ ] Month = {“JAN”,”FEB”,”MAC”,”APRIL”,”MEI”,”JUN”); - J1
(ii) Create another array named bonus with double value for each month.
[1 mark]
double [] bonus = new double [6]; ------------------J1
(iii) Swap the first and second element of the array bonus.
[2 mark]
int temp = bonus [0]; --- J1
bonus[0]=bonus[1];-------J½
bonus[1]=temp; ------------J½
4 (a) Answer the following questions based on the given program.
class Main {
public int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
public static void main(String[] args) {
int num1 = 25;
int num2 = 15;
Main obj = new Main();
int result = obj.addNumbers(num1, num2);
System.out.println("Sum is: " + result);
}
}
(i) Identify one (1) example of predefined method/standard library.
[1 mark]
println ----------J1
(ii) State the name of user-defined method.
[1 mark]
addNumbers --------J1
(iii) Name the method call used in this program.
[1 mark]
obj.addNumbers (num1,num2) --------J1
(b) Consider the following Java program.
import java.util.Scanner;
public class evenOrOdd
6
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int num;
System.out.print("Enter an integer: ");
num = input.nextInt();
if(isEven(num))
System.out.println("Number is even");
else
System.out.println("Number is odd");
}
public static boolean isEven(int number)
{
if(number % 2 == 0)
return true;
else
return false;
}
}
Based on the program above identify:
(i) Method name.
[1 mark]
isEven ----------J1
(ii) Return type.
[1 mark]
boolean ----------J1
(iii) Formal parameters.
[1 mark]
number ----------J1
(iv) Actual parameters/arguments.
[1 mark]
num ----------J1
(v) Display the output based on program above if user enter 0.
[1 mark]
true ----------J1
(c) Based on the java statements below:
public class Kmpp
{
void displayMessage() {
{
System.out.println("Welcome To KMPP");
}
public static void main(String[] args)
{
______________(i)_________________
______________(ii)________________
}
}
7
(i) Write a Java statement to create an object named obj.
[1 mark]
Kmpp obj = new Kmpp(); ------------ J1
(ii) Write a Java statement to call non-static method.
[1 mark]
obj.displayMessage();----------- J1
(d) Complete a given program below.
A program that reads two numbers and then determined whether the second number
is multiple of the first number. Display appropriate message. The program should
have a method named multipleOfFirst that receives two parameters and return true if
the second parameter is multiple of the first number, or vise versa, and false
otherwise.
[8 marks]
import java.util.*;
public class Multiple{
public static void main (String []args){
Scanner get = new Scanner (System.in)
int num1,num2;
num1=get.nextInt(); J1
num2=get.nextInt() J1
System.out.println (multipleOfFirst (num1,num2); J2
}
public static boolean multipleOfFirst (int no1, int no2){ J2
if (no2 % no1 = 0) J1
return true; J½
else
return false; J½
}
}
8
END OF QUESTION PAPER