KMPH PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

KOLEJ MATRIKULASI PAHANG

SUBJECT : Computer Science 2


CODE : SC025
SEMESTER : 02
SESSION : 2022 / 2023
BASIC TO FINAL
STUDENT’S NAME :   __________________________________ 
TUTORIAL :                                            

INSTRUCTION TO CANDIDATE:
This question paper consists of 6 questions. 
Answer ALL questions in the space provided.
The use of non-programmable scientific calculator is permitted.

1 A computer program is a series of instructions that directs a computer to perform task.


(a) Compare specifically the difference between procedural and object-oriented in
terms of security. [2 marks]

Procedural Object-oriented

Security

(b) Describe the function of each language translators listed below. [2 marks]

Translator Function

Interpreter

Compiler

1
2 The input-process-output (IPO) model, or input-process-output pattern, is a widely
used approach in systems analysis and software engineering for describing the structure of
an information processing program or other process. Based on the problem below, prepare
an IPO chart.

(a) A games company sells games consoles for RM80 each. They also offer some
discounts based on the total quantity purchased as below.

Quantity Discount
1 - 10 10%
11 - 20 15%
21 or more 20%
Discount Table
This program will display the amount of the discount and the total bill after the discount.
[5 marks]

(b) This loop program lets the user enter a series of integers. The user should
enter -111 to signal the end of the series. After all numbers have been entered, the program
should display the total number entered and the average of all numbers.
[6 marks]

2
3 (a) Write a pseudocode to calculate the factorial of a number.
(Hint : Factorial of 5, written as 1 x 2 x 3 x 4 x 5 = 120) [8 marks]

(b) Draw the flowchart based on the steps below, to find the largest among three
numbers. [8 marks]

Step 1 : Read X, Y, Z
Step 2 : if X > Y continue step 5
Step 3 : if Y > Z then print “Y is largest” and continue step 7
Step 4 : Continue step 6
Step 5 : if X > z then print “X is largest” and continue step 7
Step 6 : Print “Z is largest”
Step 7 : Stop

3
4 Java is a high-level, class-based, object-oriented programming language that is designed
to have as few implementation dependencies as possible.

(a) State the following identifier are valid or not valid. [2 marks]
Identifier Valid / Invalid

$price

Total-price

(b) Identify output for the following Java statement. [1 mark]


        int y=18;
           if(y<20)
              if(y<15)
                  x=5*y;
               else 
                   x=4*y;
            else
                x=3*y;
         
System.out.print(x);

__________________________________________________________________

(c) Evaluate the following [3 marks]


i. 5*3–5%2
__________________________________________________________________

ii. 10 + 3 / 2 * 4 % 9
__________________________________________________________________

iii. ! ( 2 < = 3 ) & & ( 4 > = 5 ) | | ( 15 > 10 )


__________________________________________________________________

(d) Convert the following expression into Java syntax. [2 marks]


Statement Java syntax

4
Volume of sphere = 3
πr3

4
2
4𝑎𝑏
𝑥= 2𝑎𝑐

(e) Write the declaration statement based on the given variable below. [2 marks]
i. Declare variable named diploma and assign diploma with computer
science.

__________________________________________________________________
ii. Declare and assign a constant discount to 0.25
__________________________________________________________________

5 (a) Write a program segment for the given flowchart [6 marks]

5
(b) Write the output for the given program segment. Assume the input data is 8.
[2 marks]

int n = input.nextInt();
int d = n – 1;
while(d > 1) {
    if ( n % d == 0)
       System.out.println(d);
    d--;
}

(c) Rewrite the program segment given in question 5 (b) by using for statement.
[3 marks]

6 (a) Method can be divided into two (2) types, predefined method and user-defined
method.
i. Give an example for the given method structure or type based on the
following code segment. Identify pre-defined method used and give one
(1) usage of predefined method listed in the program below. 
public ststic void main(String[ ] args){
double x=Math.sqrt(100.0); }
[3 marks]

6
__________________________________________________________________
__________________________________________________________________

ii. Consider the following Java program. Write the output of the program
below.

class MainApp{
static void printMessage(){
System.out.print(“Welcome”);
}
public static void main (String[] args){
printMessage();
System.out.print(“ to the Method Program.”);
}}
[1 mark]
__________________________________________________________________

iii. Write a method header for a method named calcSum that receives
two (2) int parameters a and b. The method shall return a int value.
[2 marks]
__________________________________________________________________
__________________________________________________________________
iv. Write a method call for a method named calcBMI that receives two (2)
double as parameters w and h. This method will return a double
value. 
[2 marks]
__________________________________________________________________
__________________________________________________________________

(b) Complete the Java program given to solve the following problems: 

The factorial of an integer number n is the product of all numbers from 1 up to n.


The output as given in figure 1 below

7
Input an integer number n, create a method name calcFac to calculates and return the
result to the output statement given in the main method.
[10 marks]

class n_factorial{
public static void main(String[]args){
java.util.Scanner sc = ____________________________________
int n; System.out.print("Enter your n value: ");
____________________________________________         
System.out.print (_________________________);
//invoke method
}//end of main method

static int calcFac(int a){


System.out.print ("\nFactorial value: ");
int ___________________, factorial = 1;
while (i <= a) {
if (__________________)
_____________________________________ //display “=”
else
_____________________________________ //display “x”
_____________________________________
________________________ //updating counter}
________________________ factorial; 
}//end of user define method
}//end of class

You might also like