You are on page 1of 4

1) Pertimbangkan segmen program Java di bawah.

Consider the segment of Java program below.


Scanner input = new Scanner(System.in);
int count = 1;
int number;
for (int i = 1; i <10; i++){
for (int j = 0; j <10; j++){
System.out.print(Enter an integer number :);
number = input.nextInt();
if ((number % 3 > 0) && (number > 20))
count+=2;
}
}
System.out.println(The answer is +count);

(a) Jejak segmen program dengan menunjukkan nilai-nilai bagi setiap


pemboleh ubah jika input kepada program adalah nombor-nombor berikut:
Trace the program segment by showing the values for each variable if the
inputs to the program are the following numbers:
20 34 48 22 90 25 10 55 65 20
(b) Bagi input dalam soalan (a), apakah output yang dipaparkan?
For the given input in question (a), what is the output displayed?

[4M]

[1M]

(c) Tuliskan semula segmen program dengan menggunakan gegelung while


bagi menggantikan gegelung for.
Rewrite the program segment using while loop to replace for loop.
[5M]

2) Diberi sebuah tatasusunan bernama finalList yang memegang 10 nilai


integer dan sebuah tatasusunan 6-kali-7 dua dimensi bernama matrix. Tuliskan
pernyataan-pernyataan bagi pelaksanaan seperti berikut:
Given an array named finalList that holds 10 integer values and a 6-by-7
two-dimensional array named matrix. Write statements to do the following:
(a) Bina tatasusunan bernama finalList yang memegang 10 nilai integer.
Create the array finalList to hold 10 integer values.

[1M]

(b) Cari elemen terkecil dalam tatasusunan finalList.


Find the smallest element in the array finalList.

[4M]

(c) Tulis sebuah metod bernama calcAverage yang menerima tatasusunan


finalList dan hantar kembali nilai purata kesemua elemen-elemen dalam
tatasusunan tersebut.
Write a method named calcAverage that receives the array finalList
and return the average of all elements in the array.
[5M]
(d) Masukkan tatasusunan matrix dengan nilai-nilai rawak daripada 5 hingga
99.
Insert the array matrix with random values from 5 to 99.
[5M]

(a) Answer:
i
number
1
15
2
34
3
89
4
18
5
90
mark
6
45
7
10
8
5
9
65
10
20
11

count
0
0
1
1
1
2
3
3
3
4
4

correct values for count = 2 marks;


correct values for sequence of numbers read in = 1
correct values for i with the last is 11 = 1 mark

(b) Answer: The answer is 4


(c) Answer
Scanner input = new Scanner(System.in);
int count = 0;
int number;
int i = 1;
while (i <= 10) {
[1M]
while (j <= 10) {
[1M]
System.out.print(Enter an integer number :);
number = input.nextInt();
if ((number % 3 == 0) && (number > 30))
count++;
j ++;
[1M]
}
[0.5M]
i ++;
[1M]
}
[0.5M]
System.out.println(The answer is , + count);

(a)
(b)

int[] finalList = new int[10];

int largest = finalList[0];


for (int i = 1; i < finalList.length; i++){
if (finalList[i] > largest)
largest = finalList[i];
}

(c)

public static double calcAverage(int[] finalist){


double total = 0.0;
for (int i = 0; i < finalList.length; i++) {
total = total + finalList[i];
}
return (total / finalList.length);
}

(d)

for (int row = 0; row < matrix.length; row++) {


for (int column = 0; column < matrix[row].length; column++)

{
matrix[row][column] = (int)(Math.random() * 100);
}
}