b) Calculate surface area of right cylinder based on formula for 15 times, A = (2 x π x r x h) + (2 x π x r x r) where π = 3.142 If the surface area is greater or equal to 1000 display message, “Wow…. So huge”, if the surface area is less than 5 display a message, “Miniature”, else “Just nice”. Draw a flowchart to answer these question. (Used your test pad to answer this question). [7 marks] Question 3 a) What is method? [1 mark] _________________________________________________________________________________________________________ _________________________________________________________________________________________________ b) Explain one (1) advantage of using methods. [2 marks] _________________________________________________________________________________________________________ _________________________________________________________________________________________________ c) Explain one (1) difference between standard library and user-defined methods in Java programming. [2 marks] _________________________________________________________________________________________________________ _________________________________________________________________________________________________ (Used your test pad to answer this question). Write a JAVA program segment to solve the following questions: [6 marks] d) A method swapNumber() is used to swap the values of two integer variables (a,b). This method receives two integer variables from a main method. Then, these two numbers are exchanged and immediately returned to a main method through corresponding parameters. e) Write a main method consists of two integer variables (x, y) with value of 43 and 57. After that, a method named swapNumber() is called to execute the swapping process of these two numbers. Note that, the both numbers which are before and after swapping processes are displayed in a main method. Question 4 (Used your test pad to answer this question). a) Program Z is used to summarize sale of a café owner for a week. By using array, write a JAVA program to calculate and display: • Total sale [9 marks] • Average sale • Number of sale that is above average • The highest sale • The lowest sale • Display “TOTAL SALE IS NOT ENOUGH.” if total sale is less than 25000. Else, display “BUSINESS ARE PROFITABLE • The following is a sample input-output to guide the program design: Sample Input Sample Output Enter sales: RM 10000 Enter sales: RM 8000 Enter sales: RM 9000 Enter sales: RM 11000 Enter sales: RM 900 Enter sales: RM -1500 PLEASE ENTER POSITIVE VALUE Enter sales: RM 10000 Enter sales: RM 1000 Total sale for a week : 49900 Average sale for a week : 7158.57 Number of sale that is above average : 5 The highest sale in a week : 11000 The lowest sale in a week : 900 BUSINESS ARE PROFITABLE. b) Write a JAVA program to solve the tasks below: [6 marks] • Ask the user to input a series of number(enter 0 to stop). • Calculate the sum of all positive numbers. • Count the number of even numbers • Count the numbers that are divisible by 3. Example : User Input : 3 4 7 -11 9 12 -13 18 20 17 0 The sum of all positif numbers is : 90 Total even number : 6 Total number divisible by : 4
NO ANSWER SCHEME EXCEL EXERCISE 2 2021/2022 KMNS MARKS SECTION A 1 a i FALSE 1 mark 1 a ii TRUE 1 mark 1 a iii FALSE 1 mark 1 a iv FALSE 1 mark 1 a v TRUE 1 mark 1b i a Hye I am Doraemon 1 mark 1b i b Hye I am Doralin 1 mark 1b i c Doralin 1 mark 1b ii 10 1 mark 2 a Type of errors – Logic error 1 mark Write the error – 1. Counter is not initialize 2. Add 10 to Counter 3. Sum = Counter 4. While Counter >= 100 then ( Choose 3 only ) 1 mark 1 mark 1 mark 1 mark 2 a i Start Sum = 0 Counter = 10 While Counter ≤ 100 then Sum = Sum + Counter Add 1 to Counter EndWhile Print message “Sum of the Numbers = “, Sum Stop ( Choose 3 only ) 1 mark 1 mark 1 mark 1 mark 2 b int i = 5 j = 15 ; t = 10 ; while ( i > j) { t = t + i ; i = i – j ; } System.out.print ( t ) ; ( Choose 3 only ) 1 mark 1 mark 1 mark 1 mark 2 c 10 2 marks 3a) i final double PI=3.142; 1 3a) ii int temp=10; char ch=’A’; 0.5 0.5 3a) iii int temp, x,y; temp=x; x=y; y=temp; 1 0.5 0.5 0.5 3a) iv for (double icount=20.0; icount>=10.0;icount=icount-0.5) 0.5, 0.5, 0.5 3b) D=Math.sqrt( ((a-b)*(a-b))+ ((c-d)*(c-d)); OR D=Math.sqrt( (Math.pow((a-b),2)+ Math.pow((c-d),2)); 2 3b) System.out.print((3*2+y)/2); 2 4 a Java Expression Explanation X = 2 Value 2 is assigned to the variable named x X == 2 The value of variable x is compared to the value 2 2 marks
4 b - Identifiers can consist of capital letter A to Z, the lowercase a to z, the digits 0 to 9, the underscore (_) character and the dollar sign ($).(Must not use any other symbols other than letters, digits, $ and _) - First character must be a letter or an underscore or a dollar sign, $ (i.e must not begin with a digit) - There can be no embedded blanks (space) - Reserved word cannot be used as identifier. - Identifier are case-sensitive. Uppercase and lowercase letter is considered different. As a result, marks, Marks and MARKS are different identifiers. 1 mark 4 c Inheritance, polymorphism, abstraction, encapsulation 2 marks 4 d i 16/9 + 9%16 = 10 1 mark 4 d ii 16 + Math.sqrt(9*9 -2*16)/2*2 = 23.0 1 mark 4 e i 13 2 marks 4 e ii -7 2 marks 4 f a Excellent 1 mark 4 f b z 1 mark SECTION B 1 a (ii) Design a solution (iii) Implementation (iv) Testing (v) Documentation 4 marks 1 b Translate the algorithm into coding 2 marks 1 c Writing comments between your lines of codes or Creating a separate text file to explain the program. 1 mark 1 d Flowchart Pseudocode 2 marks 1 e Input: N, salary Process: Calculate bonus based on salary for N times. Output: bonus 3 marks 1 f Input: price, quantity, item code Process: Calculate total price based on price and quantity and determine 6% of GST is added if the item code is 5000 and above. 2 marks 2 a Start Read company’s name, number of registrants if ( number of registrants ≥ 1 and number of registrants ≤ 3) charge = number of registrants x 150 else if ( number of registrants ≥ 4 and number of registrants ≤ 9) charge = number of registrants x 100 else if ( number of registrants ≥ 10) charge = number of registrants x 80 end if Print company’s name, number of registrants, charge Stop 7 marks
2 b 7 marks 3 a • Collection of statements that are grouped together to perform operation • Collection of statements that perform some specific task 1 mark 3 b • Code reusability (can be used many times) • Make code more readable • Code easier & spend less time to debug 2 marks 3 c Standard Library Methods - built-in methods in Java that are readily available for use User-defined Methods – methods that are defined by the programmer 2 marks 3 d void swapNumber ( int a, int b ) { int tmp = a; a = b; b = tmp; System.out.print(“After swap x :” + a + “ ,y :” + b); } 6 marks 3 e public static void main (String [ ] args){ Swap obj = new Swap( ); int x = 43, y = 57; System.out.print(“Before swap x :” + x + “ ,y :” + y); obj.swapNumber ( x,y)); }
4 a import java.util.Scanner; class prepepsm2 { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int[] sales = new int[7]; int freqsales = 0, max = -9999, min = 9999, totalsales = 0; double average = 0.0; for (int i = 0; i < sales.length; i++) { System.out.print("Enter sales: RM "); sales[i] = sc.nextInt(); if (sales[i] <= 0 ) { System.out.println("PLEASE ENTER POSITIVE VALUE"); System.out.print("Enter sales : RM "); sales [i] = sc.nextInt(); } totalsales = totalsales + sales[i]; average = totalsales / 7.0; if (sales[i] > max) max = sales[i]; if (sales[i] < min) min = sales[i]; } for (int i = 0; i < sales.length; i++) if(sales[i] > average) freqsales = freqsales +1; System.out.println("Total sale for a week : " +totalsales); System.out.println("Average sale for a week : " +average); System.out.println("Number of sale that is above average: " + freqsales); System.out.println("The highest sale in a week: " + max); System.out.println("The lowest sale in a week: " + min); if (totalsales < 25000) System.out.print("TOTAL SALE IS NOT ENOUGH"); else System.out.print("BUSINESS ARE PROFITABLE"); } } p/s : perlu guna 2 for 9 marks 4 b import java.util.Scanner; class PrePSPM2Question4b 6 marks
{ public static void main(String[] args) { Scanner sc = new Scanner (System.in); int number , sum = 0, freqeven = 0, freq3 = 0; System.out.print("User input : "); number = sc.nextInt(); while (number != 0) { if (number > 0 && number != 0) sum = sum + number; if (number % 2 == 0) freqeven = freqeven + 1; if (number % 3 == 0) freq3 = freq3 + 1; number = sc.nextInt(); } System.out.println("\nThe sum of all positif numbers is : " + sum); System.out.println("Total even number : " + freqeven); System.out.println("Total number divisible by : " + freq3); } }
Basic to Final . 2021/2022 . SC025 No Answer Mark 1 a Implementation 2 m 1 b Logic Error 2 m 1 c ✔ A well-defined step by step solution or a series of instructions to solve a problem. ✔ Help to track defects before they become code. ✔ Increases product reliability. ✔ May decreases overall costs. 2 m (Choose any 1 or any suitable answer) 1 d ✔ Helps other programmers to edit or maintain the program later. ✔ Keeps track of all parts of a software or program. ✔ Maintenance is easier. ✔ Programmers other than the developer can understand all aspects of software. ✔ Improves overall quality of the software. ✔ Assists in user training. 2 m (Choose any 1 or any suitable answer) 1 e Input Process Output item (0.5m) price(0.5 m) By using array (1 m), repeat calculate total price (1 m). total price (1 m). . 4 m 2 a Start Read hours worked, wages per hour if (hours worked>35) overtime hrs = hours worked – 35 overtime charges = overtime hrs x (2 x wages per hour) hours worked = hours worked – overtime hrs total wage = (hours worked x wages per hour) + overtime charges else total wage = (hours worked x wages per hour) end if Display total wage Stop (7 m) ½ m , ½ m 1 m ½ m ½ m ½ m 1 m ½ m 1 m 1m
2 b (7 m) Correct answer and symbol – ½ m x 12 = 6 Correct arrow / flow – ½ m Correct label (yes / no) or (true / false) – ½ m 3 a To give description about the programs or The help in documentation the programs or any acceptable answer Select any one 2 m 3 b main, while, class, if , public,double,static or any possible answer Select any two 1 m , 1 m 3 c $money = Valid #task = Invalid 1 m 1 m 3 d if (number divisible by 3) if(number%3==0) ---------------------------------------------------------------------------- y = √2 +b2 + ab y = Math.sqrt(Math.pow(a,2) + Math.pow(b,2)) + a*b; or y = Math.sqrt(a*a + b*b) + a*b; 2 m 2 m 3 e char grade=sc.nextInt( ) char grade = sc.next().charAt(0); or int grade = sc.nextInt(); ---------------------------------------------------------------------------- if grade = a; if(grade== ‘a’) 2m 2 m 3 f String fname= “Muhammad”; String sname= “Khairul”; or String fname= “Muhammad”, sname= “Khairul”; 1 m 1 m 2m
3 g i Green zone.Stay safe everyone! 1 m 3 g ii Yellow zone.Stay safe everyone! 1 m 3 g iii Red zone.Stay safe everyone! 1 m 3 h import java.util.Scanner; class Odd { public static void main(String[ ]args){ Scanner read=new Scanner(System.in); int i=0, limit=10, odd=0; while (i<limit){ int n=read.nextInt(); if(n%2==1) odd=odd+1; i=i+1; } System.out.print("Number of odd integer is "+odd); } } (3 m) ½ m ½ m ½ m ½ m ½ m ½ m 3 i i Average height is 1.646 1 m 3 i ii 6 1 m 3 i iii 5 1 m 3 j double[ ] BMI={25.1, 14.5, 16.8, 18.9, 17.4, 13.2, 20.9} ; 1 m (declare) , 1 m (initialized) 3 k double Highest (double a, double b, double c) ½ m (returntype) ½ m (method name) 1 m (list of parameters) 4 a public class Prapspm { public static void main(String[ ]args) { java.util.Scanner read = new java.util.Scanner(System.in);---- int arrNum [ ] = new int [10]; int max; int i = 0, int sum = 0; max = arrNum[0]; while (i < arrNum.length) { arrNum [i] = read.nextInt(); if(arrNum[i]%2==0) sum = sum + arrNum[i]; if (arrNum[i]>max) max = arrNum[i]; i++; } System.out.print ("Max value entered "+max); (8 m) 1 m ½ m ½ m ½ m ½ m ½ m ½ m ½ m ½ m ½ m ½ m ½ m ½ m ½ m
System.out.print ("Sum of even number "+sum); } } or using for loop ½ m 4 b public class myClass { public static void main(String[]args) { myClass call = new myClass(); int result = call.sumNumber(); System.out.print("The sum of numbers from 1 to 30 is "+result); OR System.out.print("The sum of number from 1 to 30 is "+call.sumNumber()); } int sumNumber() { int sum = 0; for(int i=1; i<=30; i=i+1) { sum = sum + i; } return sum; } OR int sumNumber() { int sum =0,i=1; while(i<=30) { sum = sum + i; i=i+1; } return sum; } (7 m) 1 m ½ m ½ m OR 1 m 1 m ½ m 1 ½ m 1 m 1 m OR 1 m ½ m , ½ m ½ m 1 m ½ m 1 m
KOLEJ MATRIKULASI PAHANG SUBJECT : Computer Science 2 CODE : SC025 SEMESTER : 02 SESSION : 2021 / 2022 BASIC TO FINAL STUDENT’S NAME : __________________________________ TUTORIAL : INSTRUCTION TO CANDIDATE: This question paper consists of 4 questions. Answer all questions in the space provided. The use of an electronic calculator is permitted. 1 Tech Superb is a software development company. They got an offer to develop an apps called Shopink for a company to assist them in online shopping with their customers. (a) Identify which of the problem solving step is taken in order to develop the Shopink apps by using the suitable integrated development environment (IDE). [2 marks] ____________________________________________________________________ (b) During the testing session, the programmer noticed that the apps is displaying the incorrect output. Identify the type of programming error based on the situation given. [2 marks] ____________________________________________________________________ (c) Discuss one (1) benefit of design a solution step before develop the Shopink apps. [2 marks] ____________________________________________________________________ (d) Justify one (1) importance of documentation in the project development. [2 marks] ____________________________________________________________________
(e) As a system analyst, create an IPO chart for the Shopink apps to accepts a list of items from customers and calculate the total price. [4 marks] Input Process Output 2 (a) The total wage of part time staff at “MAP OF THE SOUL” store is based on the wages per hour and the number of hours worked. In addition, if the staff has worked for more than 35 hours, then she or he can gets twice the wages per hour, for every extra hour that she or he has worked, otherwise the wages is based on the rate per hour. Calculate and display the total wage using the pseudocode algorithm. [7 marks]
(b) The pseudocode below is an algorithm to determine whether a number input by the user is a prime number or not. Create a flowchart based on the given pseudocode. [7 marks] Begin input number Set divisor to 2 Set isPrime to 1 while divisor < number & isPrime = 1 if number % Divisor = 0 Set isPrime to 0 Increase divisor by 1 end while if isPrime = 1 display “Prime” else display “Not Prime” End
3 Consider the following Java Segment. (a) Explain the purpose of the programming statement in line 3. [2 marks] ____________________________________________________________________ (b) Give two (2) keywords use in Java Program. [2 marks] ____________________________________________________________________ (c) State whether each of the following is Valid or Invalid Identifier. [2 marks] Identifier Valid / Invalid $money #Task (d) Convert the following into Java assignment statement. [4 marks] Statement Java statement if (number divisible by 3) y= √ 2 + 2 + ab public class Introjav{ ------------------------ Line 1 public static void main(String[]args){ --------- Line 2 // formula to calculate price ------------------ Line 3 double price=0.32 * price; --------------------- Line 4 System.out.print(“The price is:+price); ------- Line 5 } }
(e) The following java code contain syntax error. Rewrite the correct version of the code. [4 marks] Java Codes Correct version char grade=sc.nextInt( ) if grade = a; (f) Declare fname and sname as String variables, then initialize fname to Muhammad and sname to Khairul. [2 marks] ____________________________________________________________________ ____________________________________________________________________ (g) Write the output after the following program is executed. [3 marks] if(cases==0) System.out.print(“Green zone.”); else if(cases<=40) System.out.print(“Yellow zone.”); else System.out.print(“Red zone.”); System.out.print(“Stay safe everyone!”); i) 0 ____________________________________________________________ ii) 40 ____________________________________________________________ iii) 41 ____________________________________________________________
(h) Use a while statement to rewrite the following Java statement. [3 marks] import java.util.Scanner; class Odd{ public static void main(String[]args){ Scanner read=new Scanner(System.in); int limit=10, odd=0; for (int i=0; i<limit; i=i+1){ int n=read.nextInt(); if(n%2==1) odd=odd+1; } System.out.print("Number of odd integer is "+odd); } }
(i) Consider the following Java program. Supposed the input is 1.55 1.75 1.81 1.49 1.63 -1 [3 marks] import java.util.Scanner; class AverageHeight{ public static void main(String[]args){ Scanner read=new Scanner(System.in); int bil=0; double height, average, total=0; height=read.nextDouble(); while(height!=-1){ total=total+height; bil=bil+1; height=read.nextDouble();} average=total/bil; System.out.print("Average height is "+average); } } i) What will be the output of the program? ____________________________________________________________ ii) How many input entered? ____________________________________________________________ iii) How many times program is executing? ____________________________________________________________
(j) Declare and initialized a group of BMI in an array as follows: 25.1 14.5 16.8 18.9 17.4 13.2 20.9 [2 marks] _________________________________________________________________ (k) Write a method header to compute the highest of three floating-point numbers. [2 marks] _________________________________________________________________ _________________________________________________________________
4 (a) Develop an Error Free Java Program that prompt user to enter and store 10 number into array named arrNum without importing Scanner class from Java package. The program will calculate the sum of all even number and determine the highest number entered by the user. Below is the sample output and input for you to test your program Sample Input (User will enter) Sample Output 9, 4, 8, 12, 3, 5,10,15,14,18 Max value entered : 18 Sum of even number : 66 [8 marks]
(b) Write a method named SumNumber to find the sum of numbers from 1 to 30 and return the result by invoke from main method. (Use call as the object for method call) Output sample: [7 marks] public class myClass { public static void main(String[]args) {
NAME : TUTORIAL: LECTURER NAME : MATRIC NUMBER IDENTITY CARD NUMBER M S SC025 Computer Science Semester 2 2021/2022 2 hours PERAK MATRICULATION COLLEGE MINISTRY OF EDUCATION MALAYSIA TRIAL TEST (Answer Scheme) COMPUTER SCIENCE 2 HOURS DO NOT OPEN THIS BOOKLET UNTIL YOU ARE TOLD TO DO SO Question Number Mark 1 11 2 15 3 29 4 15 Total Mark 70
PRA PSPM 2021 / 2022 1 (a) Librarian Munsyi Abdullah library at KMPk decided to develop an eMunsyi application to allow their students to search book catalogues, check book status and borrow books while using mobile applications. Based on the given scenario, Identify the correct step for problem solving. [5 marks] Problem Analysis (1m) Design a solution (1m) Implementation (1m) Testing (1m) Documentation (1m) (b) Justify one (1) importance of documentation for programmers. [2 marks] Contains description of the program (1m) that helps programmers in editing, maintaining (1m) the program later (c) Considering the following scenarios, identify the input, process and output. Start Set total borrow to zero Enter eMunsyi code While emunsyi code not equal to 0 Input book serial number add 1 into the total borrow Enter eMunsyi code End while Print the total borrow Stop [3 mark] Input : eMunsyi code (½ m) , book serial number until user enter 0 (½ m) Process : Read eMunsyi Code and add 1 into total borrow. Repeat reading serial number until user enter 0 (½ m, ½ m) Output : total borrow (1m) (d) State the control structure used in the given algorithm. [1 mark] Repetition (1m)
2 Answer the question below based on the problem statement given. (a) A programmer is responsible for creating program that displays a students' performance. A student's performance is summarised in the table below. Use the appropriate control structure to display the performance when a user enters a mark. Mark Performance 80-100 Excellent 50-79 Average 0-49 Weak Based on the scenario, write a pseudocode. [8 marks] Start ---> (½ M) Read Mark ---> (1M) If (Mark ≥ 80 && Mark <= 100) ---> (1M) Display message “Excellent” ---> (1M) Else if (Mark ≥ 50) ---> (1M) Display message “Average” ---> (1M) Else ---> (½ M) Display message “Weak” ---> (1M) End if ---> (½ M) Stop ---> (½ M) ** any correct answer is acceptable Start ---> (½ M) Read mark ---> (1M) If (mark ≥ 80 && mark <= 100) ---> (1M) performance = “Excellent” ---> (1M) Else if (mark ≥ 50) ---> (1M) performance = “Average” ---> (1M) Else if (mark ≥ 0) ---> (½ M) performance = “Weak” ---> (1M) Else Display “mark out of range” End if ---> (½ M) Display performance Stop ---> (½ M) (b) Fain intends to build a programme that adds the sums of even numbers between 50 and 500. A total sum should be displayed in the programme. Draw a flowchart to assist Fain in solving the problem. [7 marks]
3 (a) Assign value z of type int into variable z of type double, then assign it into the variable x of type int. [2 marks] double z=(int)z; ---1m int x=(int)z; ----1m (b) Give the output of the following Java statements. int num = 0; num = num + 5; - num = num / 10; System.out.print(num); [2 marks] 0 (c) Suppose a, b and c are integer variables with values 4, 8 and 12. Output the contents of variables, d based on the following expressions. (i) d = (a + b) - b ; [1 mark] 4 (ii) d = Math.pow(a,b) + c ; [1 mark] 65548
(iii) d = c % a / b + a * b ; [1 mark] 32 (iv) d = (b / a) + a * c / c ; [1 mark] 6 (d) Trace the error(s) and rewrite the correct code for the following Java codes. (i) [3 marks] if (number % 2 = 0){ System.out.println(number + " is an odd number"); } else if{ System.out.println(number + " is an even number"); } Error Correction if( number % 2 == 0) - ½ m if (number % 2 != 0) - ½ m else if - ½ m else - ½ m if (number % 2 = 0) - ½ m if (number % 2 == 0) - ½ m (ii) [3 marks] if ((year % 4 == 0) || (year % 100 != 0) || (year % 400 == 0)) System.out.println(year + " is a leap year") else System.out.println(year , " is an not a leap year"); Error Correction
if (year % 4 == 0) || (year % 100 != 0) || (year % 400 == 0) - ½ m if (year % 4 == 0) && (year % 100 != 0) && (year % 400 == 0) - ½ m System.out.println(year, " is an not a leap year"); - ½ m System.out.println(year + " is an not a leap year"); - ½ m System.out.println(year + " is a leap year") - ½ m System.out.println(year + " is a leap year"); - ½ m (e) Give the output of the following program segment if the input is 35, Y, 100.70, Y, 70, Y, 55, Y, 60.5, N char answer='Y'; double price, total_price=0; while (answer != 'N') { price=kmpk.nextDouble(); total_price += price; answer=kmpk.next().charAt(0); } total_price=0.90*total_price; System.out.println("Total price = " + total_price); [4 marks] Total price = 289.08 (f) Write a program segment using for to sum all numbers divisible by 7 between 71 to 177. [6 marks] int sum=0; for(int i=71; i<177; i++) { if(i%7==0) sum=sum+i; } System.out.println(sum); @ int sum=0; for(int i=77; i<=175; i=i+7) sum=sum+i; System.out.println(sum); sum=0 1/2 m Initial value 1m condition 1m updating value 1m
if 1m sum=sum+i 1m System.out.print 1/2m (g) Rewrite the following program segment into while coding and state its output. public static void main(String[ ]args) { int i; int z = 5, y = 0, x = 2; for(i = 1; i < 4; i++) { y = (z * x) + 15; z = y + 2; } System.out.print(z); } [5 marks] public static void main(String[ ]args) { int i = 1; int z = 5, y = 0, x = 2; while(i < 4) { y = (z * x) + 15; z = y + 2; i++; } System.out.print(z); } Output : 159 Initial value 1m Condition 1m Updating value 1m System.out.print 1m Output 1m
4 Write a Java program for a class named HighestClass to find the highest mark from an array. the class shall contain the main method and findHighest methods. (i) In main method : - Enter n (number of students) - declare and create an integer array named mark[n] - Use a loop statement to input mark[i] - assign the value returned by method findHighest to a variable named resultHighest. - display resultHighest. (ii) The findHighest method shall : - use a loop statement to determine the maximum value. - receive an integer array of values (m) through its parameter. - return the highest. [15 marks] Header (hingga create object) - 3 marks Main method (input, call method, output) - 6.5 marks Method - 5.5 marks Total 15 marks import java.util.Scanner; //0.5 mark class HighestClass { //0.5 mark int highest = 0; //0.5 mark (boleh juga declare dalam method. Markah sama public static void main(String [ ] args) { //0.5 mark Scanner sc = new Scanner (System.in); //0.5 mark HighestClass obj = new HighestClass(); //0.5 mark System.out.print("Enter number of students, n : "); int n = sc.nextInt(); //1 mark int [ ] mark = new int[n]; //1 mark for (int i = 0; i < mark.length; i++) { //1.5 mark System.out.print("Enter mark : ");
mark[i] = sc.nextInt(); //1 mark } int resultHighest = obj.findHighest (mark); //1 mark System.out.print("Highest mark : "+ resultHighest); //1 mark }//close main int findHighest (int [ ] m) { //1.5 mark //int highest = 0; (0.5 mark) for (int i=0; i<m.length; i++) { //1 mark if (m[i] > highest) //1 mark highest = m[i]; //1 mark } return highest; //1 mark }//close method }//close class
NAME : TUTORIAL: LECTURER NAME : MATRIC NUMBER IDENTITY CARD NUMBER M S SC025 Computer Science Semester 2 2021/2022 2 hours PERAK MATRICULATION COLLEGE MINISTRY OF EDUCATION MALAYSIA TRIAL TEST COMPUTER SCIENCE 2 HOURS DO NOT OPEN THIS BOOKLET UNTIL YOU ARE TOLD TO DO SO Question Number Mark 1 11 2 15 3 29 4 15 Total Mark 70
PRA PSPM 2021 / 2022 1 (a) Librarian Munsyi Abdullah library at KMPk decided to develop an eMunsyi application to allow their students to search book catalogues, check book status and borrow books while using mobile applications. Based on the given scenario, Identify the correct step for problem solving. [5 marks] (b) Justify one (1) importance of documentation for programmers. [2 marks] _____________________________________________________________________ _____________________________________________________________________ (c) Considering the following scenarios, identify the input, process and output. Start Set total borrow to zero Input eMunsyi code While emunsyi code not equal to 0 Input book serial number add 1 into the total borrow Input eMunsyi code End while Print the total borrow Stop [3 marks] Input : _____________________________________________________________ Process : ___________________________________________________________ Output :___________________________________________________________ (d) State the control structure used in the given algorithm. [1 mark] _____________________________________________________________________ 1
2 Answer the question below based on the problem statement given. (a) A programmer is responsible for creating a program that displays a students' performance. A student's performance is summarised in the table below. Use the appropriate control structure to display the performance when a user enters a mark. Mark Performance 80-100 Excellent 50-79 Average 0-49 Weak Based on the scenario, write a pseudocode. [8 marks] (b) Fain intends to build a programme that adds the sums of even numbers between 50 and 500. A total sum should be displayed in the programme. Draw a flowchart to assist Fain in solving the problem. [7 marks] 2
3 (a) Assign value z of type int into variable z of type double, then assign it into the variable x of type int. [2 marks] (b) Give the output of the following Java statements. int num = 0; num = num + 5; num = num / 10; System.out.print(num); [2 marks] (c) Suppose a, b and c are integer variables with values 4, 8 and 12. Output the contents of integer variables, d based on the following expressions. (i) d = (a + b) - b ; [1 mark] (ii) d = Math.pow(a,b) + c ; [1 mark] (iii) d = c % a / b + a * b ; [1 mark] (iv) d = (b / a) + a * c / c ; [1 mark] 3
(d) Trace the error(s) and rewrite the correct code for the following Java codes. (i) [3 marks] if (number % 2 = 0){ System.out.println(number + " is an odd number"); } else if{ System.out.println(number + " is an even number"); } Error Correction (ii) [3 marks] if ((year % 4 == 0) || (year % 100 != 0) || (year % 400 == 0)) System.out.println(year + " is a leap year") else System.out.println(year , " is an not a leap year"); Error Correction 4
(e) Give the output of the following program segment if the input is 35, Y, 100.70, Y, 70, Y, 55, Y, 60.5, N char answer='Y'; double price, total_price=0; while (answer != 'N') { price=kmpk.nextDouble(); total_price = total_price+ price; answer=kmpk.next().charAt(0); } total_price=0.90*total_price; System.out.println("Total price = " + total_price); [4 marks] (f) Write a program segment using for to sum all numbers divisible by 7 between 71 to 177. [6 marks] (g) Rewrite the following program segment into while coding and state its output. public static void main(String[ ]args) { int i; int z = 5, y = 0, x = 2; for(i = 1; i < 4; i++) { y = (z * x) + 15; z = y + 2; } System.out.print(z); } 5
[5 marks] 4 Write a Java program for a class named HighestClass to find the highest mark from an array. the class shall contain the main method and findHighest methods. (i) In main method : - Enter n (number of students) - declare and create an integer array named mark[n] - Use a loop statement to input mark[i] - assign the value returned by method findHighest to a variable named resultHighest. - display resultHighest. (ii) The findHighest method shall : - use a loop statement to determine the maximum value. - receive an integer array of values (m) through its parameter. - return the highest. [15 marks] 6
7
1 ANSWER SCHEME SC025 2021/2022 SESSION 1. (a) Identify the steps in problem solving based on the given statements. [4 marks] Statement Step in Problem Solving (i) Change the value of a variable a few times to ensure that the formula is correct. Testing ----J1 (ii) Make a simple explanation on the codes. Documentation --- J1 (iii) Programmers determine type of control structure to identify process. Problem analysis --- J1 (iv) Write a Pseudocode to find the largest number from three different numbers entered by user. Design a solution--- J1 (b) MYShop gives RM30 voucher to customers for every RM300 purchase. Calculate the value of vouchers Datin Halimah will get if she goes shopping at MYShop on the first day of May. State the input, process and output involved in this transaction. [3 marks] Input purchase – J½ Process To determine voucher based on the purchase --- J2 Output voucher --- J½ (c) Get two numbers from the user and display the larger number. [4 marks] Input num1, num2 – J1 Process To determine the larger number between two numbers--- J2 Output larger – J1 2 (a) Identify the control structure based on the given description. (i) "Continuous checking of user data enters until an acceptable entry is made, such as a valid password; counting and accumulating running totals; and recurrent acceptance of input
2 data and recalculation of output values that only end when a designated value is entered" [1 mark] Repetition/Looping/Iteration ----------------J1 (ii) Conditional statements are features of a programming language, which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false [1 mark] Selection ------------ J1 (b) SukaHati Company offers an incentive for Hari Raya Aidilfitri to the staff as follows: Work Experience Monthly Sales Incentive 5 years and above RM50 000 and above RM10 000 Less than RM50 000 RM5 000 Others Not specified RM2 000 Write a pseudocode to calculate the incentives that will be received by the employee. [6 marks] START Read WorkEx, MonthlySales --------- J½, J½ if (WorkEx = 5 ) ----------J1 if (MonthlySales >= 50000) ----------J1 Incentive = 10000 ----- J1 else Incentive = 5000 -----J1 end if else Incentive = 2000 -------J1 endif STOP (c) Draw a flowchart for a program that asks the user for the number of children in each family. If the number of children is larger than three, the computer will calculate the total of a large family. When the user enters -1, the program will stop and display the total of the large family with the message "Family needs MPV." [6 marks]
3 3 (a) Write Java statements to execute the following tasks: (i) Declare a constant named PRICE, where the value is 40.00. [2 marks] final double PRICE=40.00; or final float PRICE = 40.00; (ii) Calculate the area of the triangle. area = ½bh [2 marks] double area = (b * h)/2; or area = 0.5 * b * h; or area=1.0/2 * b * h; or area=1/2.0 * b * h; or area=(1 /2)*b*h; (b) Identify the most suitable primitive data types for the following situations. (i) To store the choice to print the receipt, whether Y (for Yes) or N (for No). [1 mark] char -----------J1 (ii) To store the value of an average temperature in a week. [1 mark] double/float ---------- J1 J ½ J ½ J ½ J ½ J ½ J ½ J ½ J ½ J ½ J ½ J ½ J ½
4 (iii) To perform logical operations, most commonly to determine whether some condition is false. [1 mark] boolean -----------J1 (iv) To store the COVID-19 cases per 100 people in last 6 months in Malaysia [1 mark] int -------------- J1 (c) Construct the following expressions in correct Java syntax. (i) x 3 – 6yz [1 mark] (x * x * x) – 6 * y * z ; or Math.pow(x, 3) – 6 * y * z; (ii) 1+2 3 + 4 (+)(5−) [1 mark] ((1 + 2 *a)/3) + (4 * (b + c)* (5 – d))/f; (d) Solve the following expression by analysing the precedence of the respective operator. (i) ( 5.0 * (98.4 - 32.0) ) / 9.0 [1 mark] 36.89 (ii) 3 + 6 / 2 * 3 - 1 + 2.0 [1 mark] 13.0 (e) What is the output of the following code? (i) t = 10; if(t < 15) System.out.print("AAA"); System.out.print("BBB"); [1 mark] AAABBB (ii) b = 1; while(b > 8) { System.out.print(b + " ");
5 b = b + 2; } [1 mark] No output (f) Suppose target is a variable of type int. Write a program segment that displays “Excellent!” if the value of the variable target is greater than 10, otherwise display “Try again! “. Assume sc as a scanner object. [3 marks] int target = sc.nextInt( ); ---------- J1 if (target > 10) ---------- J1 System.out.print(“Excellent!”); ---------J½ else System.out.print(“Try again!”); ---------J½ (g) Convert the for loop to a while loop without changing the output. Assume all variables have been declared. [3 marks] int R; for (int q=15; q >=11; q++) R = q+ 2 System.out.println(R); int q=15, R;------------ J1 while ( q >= 11){-----------------J1 R = q + 2; q++;-----------------------J1 } System.out.println(R); (h) The following Java codes contain syntax errors. Rewrite the correct version of each code. [2 marks] Java codes contain syntax errors Correct Version int x = 200; System.out.println (Value x = , x); //Required output : Value x = 100 int x = 100; System.out.println (“Value x =” + x); /* To output Hello string System.out.println(Hello); // To output Hello string System.out.println(“Hello”); 4 (a) Construct a non-static user-defined method name statusAchievement, which receives a value from a method call. The method display message “Congratulation, you are among a top achiever ” if sales is greater than RM5000. Otherwise display “Keep working hard”. [4 marks] void statusAchievement ( double sales){ if (sales>5000)----J1
6 System.out.print ( “Congratulation, you are among a top achiever”); ---- J½ else System.out.print ( “Keep working hard”); --- J½ } *- void: J½, method name- J½, parameter list: ------J1 (b) Answer the following questions based on FIGURE 1. FIGURE 1 (i) Identify the structure of the code shown in FIGURE 1. [5 marks] Method Structure Code User-defined method name qMaths -----------------------------------------------------------------------1 mark Example of pre-defined method main(String[] args) ---------------------------------------------------------1 mark or println() ---------------------------------------------------------1 mark or Math.pow(a,b) ---------------------------------------------------------1 mark Argument/arguments 16,2 ---------------------------------------------------------1 mark or 5, 2 ---------------------------------------------------------1 mark Create object for method PowDemo objek=new PowDemo();-----------------------------------1 mark Method call objek.qMaths(16,2); -----------------------------------------------------1 mark or objek.qMaths(5,2); ---------------------------------------------------------1 mark (c) Computer Science Continuous Assessment is determined from several quizzes and practical tests. Write a full Java program for n students that can store each student’s name and marks from 2 practical tests in an array name studID, pracOne, and pracTwo. The program will determine the best student of the practical test by comparing the best score of
7 each student. The first line inputs n is representing the number of students, followed by n lines, each represents the student’s name, first practical test score, and second practical test score. Display the best student with his/her highest score. [15 mark] Sample Input Sample Output 3 Ayuni 80.6 97.5 Safwan 98.2 69.9 Delila 85.7 94.1 Best student is Safwan Highest score is 98.2 //PrePspm 2022 import java.util.Scanner;-------------------------J½ public class prePspm {-------------J½ public static void main (String [] args){------------J½ //create object Scanner input = new Scanner (System.in);-----------J½ //get n size int studSize = input.nextInt(); -----------J½ //declare an array String [] studName = new String [studSize];--------J½ double [] pracOne = new double [studSize];---------J½ double [] pracTwo = new double [studSize];---------J½ double max = 0,highScore = 0;-------J½, J½ String bestStudent = " ";------ J½ //get input for (int i = 0; i< studSize; i++)------J1,J1,J1 { studName [i] = input.next();---------J½ pracOne [i] = input.nextDouble();--------J½ pracTwo [i] = input.nextDouble();-------J½ if (pracOne[i] > pracTwo[i])-------J1 max = pracOne[i];------- J½ else max = pracTwo[i]; ------- J½ //endif if (max>highScore)-------J1
8 { highScore = max; ------- J½ bestStudent = studName[i]; ------- J½ }//endif }//endfor System.out.println("Best student is "+bestStudent);-----J½ System.out.print("Highest score is "+highScore); ------- J½ } }
1 KOLEJ MATRIKULASI PULAU PINANG SEMESTER / SESI : 02 - 2021 / 2022 NAMA SUBJEK: SAINS KOMPUTER 2 PRA PSPM SC025 MASA : 120 MINIT NAMA PELAJAR : KUMP. PRAKTIKUM : Answer all the questions in the space provided in the question paper. 1. (a) Identify the steps in problem solving based on the given statements. [4 marks] Statement Step in Problem Solving (i) Change the value of a variable a few times to ensure that the formula is correct. (ii) Make a simple explanation on the codes. (iii) Programmers determine type of control structure to identify process. (iv) Write a Pseudocode to find the largest number from three different numbers entered by user. (b) MYShop gives a RM30 voucher to customers for every RM300 purchase. Calculate the value of vouchers Datin Halimah will get if she goes shopping at MYShop on the first day of May. State the input, process and output involved in this transaction. [3 marks] Input Process Output (c) Get two numbers from the user and display the larger number. [4 marks] Input Process Output Section A Q1 : / 11 Q2 : / 14 Q3: / 22 Q4: /23 Total : /70 TOTAL :
2 2 (a) Identify the control structure based on the given description. (i) "Continuous checking of user data enters until an acceptable entry is made, such as a valid password; counting and accumulating running totals; and recurrent acceptance of input data and recalculation of output values that only end when a designated value is entered" [1 mark] _________________________________________________________ (ii) Conditional statements are features of a programming language, which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false [1 mark] ______________________________________________________________ (b) SukaHati Company offers an incentive for Hari Raya Aidilfitri to the staff as follows: Work Experience Monthly Sales Incentive 5 years and above RM50 000 and above RM10 000 Less than RM50 000 RM5 000 Others Not specified RM2 000 Write a pseudocode to calculate the incentives that will be received by the employee. [6 marks]
3 (c) Draw a flowchart for a program that asks the user for the number of children in each family. If the number of children is larger than three, the computer will calculate the total of a large family. When the user enters -1, the program will stop and display the total of the large family with the message "Family needs MPV." [6 marks] 3 (a) Write Java statements to execute the following tasks: (i) Declare a constant named PRICE, where the value is 40.00. [2 marks] ____________________________________________________ (ii) Calculate the area of the triangle. area = ½bh [2 marks] ______________________________________________________________ (b) Identify the most suitable primitive data types for the following situations. (i) To store the choice to print the receipt, whether Y (for Yes) or N (for No). [1 mark] ______________________________________________________________
4 (ii) To store the value of an average temperature in a week. [1 mark] ______________________________________________________________ (iii) To perform logical operations, most commonly to determine whether some condition is false. [1 mark] ______________________________________________________________ (iv) To store the COVID-19 cases per 100 people in last 6 months in Malaysia [1 mark] ______________________________________________________________ (c) Construct the following expressions in correct Java syntax. (i) x3 – 6yz [1 mark] ____________________________________________________ (ii) 1+2 3 + 4 (+)(5−) [1 mark] ____________________________________________________ (d) Solve the following expression by analysing the precedence of the respective operator. (i) ( 5.0 * (98.4 - 32.0) ) / 9.0 [1 mark] ______________________________________________________________ (ii) 3 + 6 / 2 * 3 - 1 + 2.0 [1 mark] ______________________________________________________________ (e) What is the output of the following code? (i) t = 10; if(t < 15) System.out.print("AAA"); System.out.print("BBB"); [1 mark] ______________________________________________________________