SPM Trial Revision 2026

Sains Komputer Form 5 Trial Tips

Your interactive revision portal for the upcoming Sains Komputer trials. We've compiled textbook diagrams directly from chapters covering algorithms, standard libraries, client-side scripting (JavaScript), procedures/functions, and memory visualization.

Algoritma & Pengaturcaraan Berstruktur

Standard libraries, sub-routines, arrays, loops & memory visualisation

Tips 01–07
01

Finding the Maximum Value

Proses Mencari Nilai Maksimum (Rajah 3.1.28)

Proses mencari nilai maksimum textbook diagram
Click to enlarge (Rajah 3.1.28)

This diagram outlines the logical sequence for finding the maximum value within a collection of items (such as an array):

  • Beri nilai awal maksimum (Initialize Max Value): Assign the first element of the array or a very small default number as the initial maximum.
  • Banding nilai dalam senarai (Compare with current items): Loop through the remaining elements and compare each item against the current maximum value.
  • Dapat nilai maksimum terkini (Update new maximum): If a compared element is larger than the stored maximum, update the stored value with the new maximum. Repeat until all elements have been evaluated.
02

Introduction to Standard Libraries

Kelebihan & Pengenalan Standard Library (Bahagian 3.1.5)

Pengenalan standard library textbook page
Click to enlarge (Halaman 194)

A Standard Library (Perpustakaan Standard) is a collection of built-in methods and functions supplied by the programming language specification to assist programmers in coding common operations.

  • Efficiency: Saves development time by providing pre-built functions for data structures, algorithms, math operations, and input/output mechanisms.
  • Common JavaScript Standard Libraries in the Textbook:
    • math.js: Used for mathematical formulas like math.sqrt() (square root) and math.pow() (exponents).
    • date.js: Used for date tracking functions such as date.now() and formatting.
03

Fungsi math.js (Mathematical Library Methods)

Jadual 3.1.30 Fungsi-fungsi dalam math.js

Fungsi-fungsi dalam math.js textbook list
Click to enlarge (Jadual 3.1.30)

The textbook specifies five core operations within the math.js standard library that you should memorize for paper questions:

Fungsi (Method Call) Penerangan (English Translation)
math.add(x, y) Menambah dua nombor (Adds two numbers)
math.divide(x, y) Membahagi dua nombor (Divides x by y)
math.subtract(x, y) Menolak dua nombor (Subtracts y from x)
math.pow(x, y) Mengira kuasa kepada nombor (Raises x to the power of y)
math.sqrt(x) Mengira punca kuasa bagi nombor (Calculates square root of x)
04

Modular Programming & Sub-routines

Rajah 3.1.44 Modul Utama dan Subatur Cara

Modul utama yang dipecahkan textbook diagram
Click to enlarge (Rajah 3.1.44)

Modular programming divides a single script into smaller, isolated components. In client-side scripting, this is achieved using subatur cara (sub-routines).

  • Modul Utama (Main Module): Represents the main script flow which invokes helper modular blocks.
  • Sub-routine Categories:
    • Prosedur (Procedure): A block of code designed to perform a task but does not return a value back to the caller.
    • Fungsi (Function): Performs operations and returns a value back to the caller using a return statement.
05

Procedures vs Functions

Perbezaan antara Prosedur dengan Fungsi (Jadual 3.1.35)

Perbandingan prosedur dan fungsi textbook comparison
Click to enlarge (Jadual 3.1.35)

Understanding when to use a Procedure vs a Function is a guaranteed scoring topic. Here is the formal breakdown:

Aspek Prosedur (Procedure) Fungsi (Function)
Memulangkan Nilai Tidak memulangkan nilai Memulangkan nilai
Contoh Panggilan toCelcius(fahrenheit); celcius = toCelcius(fahrenheit);

Textbook Typo Alert: Under "Contoh definisi" for Fungsi in Jadual 3.1.35, the code has: celcius = (5/9) * fahrenheit - 32);. Note the trailing closing parenthesis. Avoid copying this syntax typo in your theory papers; it will lead to runtime syntax errors in actual code!

06

Array Declaration & Manual Retrieval

Mengisytiharkan Array & Akses Satu per Satu (ms206(a))

Mendapatkan nombor dalam senarai satu per satu textbook page
Click to enlarge (Jadual 3.1.39(a))

For ms206(a), the textbook demonstrates retrieving elements one by one manually using indices to sum up numbers. You must know how arrays are declared and written in both JavaScript and Java, and understand their core differences.

javascript // Declaring and initializing array in JavaScript var no = [5, -1, 4, 12, 8]; var jumlah = 0; // Adding values manually element-by-element jumlah = no[0] + no[1] + no[2] + no[3] + no[4]; document.write(jumlah); // Outputs: 28
java // Declaring and initializing array in Java (Fixed data types) int[] no = {5, -1, 4, 12, 8}; int jumlah = 0; // Adding values manually element-by-element jumlah = no[0] + no[1] + no[2] + no[3] + no[4]; System.out.println(jumlah); // Outputs: 28
Feature JavaScript Array Java Array
Type System Dynamic typing (can hold multiple types: strings, numbers, etc.) Static typing (must declare type e.g., int[], homogeneous elements)
Declaration Syntax var no = [5, -1, 4, 12, 8]; int[] no = {5, -1, 4, 12, 8};
Size Constraint Dynamic size (can grow/shrink) Fixed size (once declared, the size cannot be changed)
07

Loops and Memory Blocks

Menggunakan Ulangan untuk Menjumlahkan Senarai (ms206(b))

Menggunakan ulangan untuk mendapatkan nombor textbook page
Click to enlarge (Jadual 3.1.39(b))

For ms206(b), you must know how to change loops from for to while, while to for, or do-while. Additionally, you are required to know how to draw a Blok Memori (Block Memory) diagram for arrays.

Loop Conversion Playground

Select a language and loop type to view side-by-side structures:

javascript var no = [5, -1, 4, 12, 8]; var jumlah = 0; var i; for (i = 0; i < 5; i++) { jumlah = jumlah + no[i]; } document.write(jumlah);
javascript var no = [5, -1, 4, 12, 8]; var jumlah = 0; var i = 0; // Initialize control variable while (i < 5) { // Loop condition jumlah = jumlah + no[i]; i++; // Increment variable } document.write(jumlah);
javascript var no = [5, -1, 4, 12, 8]; var jumlah = 0; var i = 0; // Initialize control variable do { jumlah = jumlah + no[i]; i++; // Increment variable } while (i < 5); // Loop condition (executes at least once) document.write(jumlah);
java int[] no = {5, -1, 4, 12, 8}; int jumlah = 0; for (int i = 0; i < 5; i++) { jumlah = jumlah + no[i]; } System.out.println(jumlah);
java int[] no = {5, -1, 4, 12, 8}; int jumlah = 0; int i = 0; // Initialize control variable while (i < 5) { // Loop condition jumlah = jumlah + no[i]; i++; // Increment variable } System.out.println(jumlah);
java int[] no = {5, -1, 4, 12, 8}; int jumlah = 0; int i = 0; // Initialize control variable do { jumlah = jumlah + no[i]; i++; // Increment variable } while (i < 5); // Loop condition System.out.println(jumlah);

Interactive Block Memory Visualizer (Blok Memori)

In SPM exams, you must draw array memory as contiguous boxes. Hover/click cell nodes to view index accesses:

no[0] 5 0x7FFF1000
no[1] -1 0x7FFF1004
no[2] 4 0x7FFF1008
no[3] 12 0x7FFF100C
no[4] 8 0x7FFF1010
> Click on a block memory cell above to trace.

PHP & Pengendalian Fail Teks

Server-side scripting with PHP — fopen, fwrite, fclose, file reading & form handling

Tips 08–12
08

Introduction to PHP & File Operations

Pengenalan PHP & Mencipta/Membuka Fail Teks (ms227)

Pengenalan PHP textbook
Click to enlarge (ms227a — Pengenalan PHP)
Mencipta membuka fail teks fopen syntax
Click to enlarge (ms227b — fopen syntax)

PHP (Hypertext Preprocessor) is a skrip pelayan (server-side scripting) language — it runs on the web server, not the browser. PHP provides programming capability to process tasks and the result is returned to the browser.

Before any operation on a text file can be performed, the file must be opened first using the fopen function:

  • $f = fopen("nama fail teks", mod capaian);
  • "nama fail teks" → the nama fail (filename) you want to open, e.g. "LogMasuk.txt"
  • mod capaian → the access mode that determines how the file is opened (read, write, or append — see next tip!)
  • $f → a pemboleh ubah (variable) that stores the file handle for use in subsequent operations

Exam Tip: In ms227(b), know that the fopen() function takes two arguments: the filename in quotes, and the access mode. The variable $f is the PHP variable (note the $ prefix — all PHP variables start with $!) that holds a reference to the opened file.

09

File Access Modes — "w", "r", "a"

Mod Capaian Fail dalam fopen (ms228)

Contoh 1 fopen LogMasuk.txt w mode
Click to enlarge (ms228a — Contoh 1 fopen)
Jadual 3.2.1 Mod capaian dan penerangan
Click to enlarge (ms228b — Jadual 3.2.1)

For ms228(a), the key thing to learn is the meaning of "w" in:

$f = fopen ("LogMasuk.txt", "w");

The second argument is the mod capaian (access mode). The complete list from Jadual 3.2.1:

Mod Capaian Meaning Penerangan
"r" Read Fail dibuka hanya untuk dibaca sahaja (read only)
"w" Write Fail dibuka hanya untuk ditulis — starts writing from the beginning of the file (bermula dari awal fail). Overwrites existing content!
"a" Append Fail dibuka untuk ditulis — starts writing from the end of the file (bermula di akhir fail). Preserves existing content and adds new content after it.
10

File Writing Code Sequence

Susunan Atur Cara — fopen, fwrite, fclose (ms229)

Contoh 2 fopen fwrite fclose sequence
Click to enlarge (Rajah 3.2.3 — SimpanTeks.php)

For ms229, the exam will ask you to arrange the code in the correct sequence. The three key PHP file writing functions must always appear in this order:

  • ① Open: fopen() — opens/creates the file first before anything else can be done.
  • ② Write: fwrite() — writes content to the opened file using the file handle $f.
  • ③ Close: fclose() — closes the file after use to free server resources.

Practice arranging the blocks below, then click Verify:

1 $f = fopen("LogMasuk.txt", "w");
2 fclose($f);
3 fwrite($f, "Selamat Datang");
11

HTML Form + PHP File Logging

Atur Cara Borang & Penulisan Log ke Fail (ms231 — Rajah 3.2.6)

Atur cara borang html php log file
Click to enlarge (Rajah 3.2.6)

For ms231, you need to know the correct code sequence and the function of lines 7 & 8. Click each highlighted line below to inspect what it does:

1 <form method='POST'>
2 if (isset($_POST["Submit"]))
3 $f = fopen("LogMasuk.txt", "a");
4 $nama = $_POST['namapengguna'];
5 $tarikh = date('d/m/Y h:i:s a', time());
6 $log = $nama.":".$tarikh.PHP_EOL;
7 fwrite($f, $log);
8 fclose($f);
Select a line

Click any line on the left to see a detailed explanation of what that line of code does.

12

File Reading & PHP Variable Syntax

Membaca Fail & Sintaks PHP dengan Simbol $ (ms240 — Rajah 3.2.16)

Atur cara membaca fail nombor dan mengira purata
Click to enlarge (Rajah 3.2.16 — Purata.php)

For ms240, two key exam topics:

  • Tag ① — What is "r"? The file Nombor.txt is opened in mod baca (read mode). "r" means the file is opened for reading only. No writing or appending is possible in this mode.
  • PHP Variable Syntax — $ prefix: In PHP, every variable must start with the dollar sign $. For example: $f, $arrNombor, $bilangan, $jumlah, $purata, $x. This is different from JavaScript (var) and Java (typed declarations).
Tag Code Function
fopen("Nombor.txt","r")Opens file for reading. "r" = read only
while (!feof($f))Loop until penghujung fail (end of file) is reached
explode(',', fgets($f))Read one line, split by comma into an array
count($arrNombor)Count total elements in the array
for ($x = 0; $x < $bilangan; $x++)Loop through each array element using $x as counter
print $arrNombor[$x]."<br>"Display each number on a new line
fclose($f)Close the file after reading is done

Pangkalan Data MySQL

Connecting, selecting databases, querying with mysqli functions & closing connections

Tips 13–14
13

MySQL Database Connection Functions

Fungsi Sambungan Pangkalan Data MySQL (ms244)

mysqli_connect mysqli_select_db mysqli_query functions
Click to enlarge (ms244 — DB Functions)

For ms244, you must know which part of each function call does what. Click each code line in the inspector to learn its parameters:

1 $con = mysqli_connect("localhost","halim","1234");
2 mysqli_select_db($con, "dbPelajar");
3 $hasil = mysqli_query($con,"SELECT * from MURID");
Select a function

Click a line on the left to see what each parameter in the MySQL function means.

14

Database Query Code Sequence

Susunan Kod Pertanyaan Pangkalan Data (ms245 — Rajah 3.2.21)

Atur cara pertanyaan pangkalan data sequence
Click to enlarge (Rajah 3.2.21)

For ms245, the exam will ask you to arrange the code sequence for connecting to and querying a MySQL database. The 4 steps must always follow this order:

  • ① Connect: mysqli_connect() — establish connection to the MySQL server first.
  • ② Select DB: mysqli_select_db() — choose which database to use.
  • ③ Query: mysqli_query() — run the SQL query to fetch or manipulate data.
  • ④ Close: mysqli_close() — close the connection to free server resources.

Practice the sequence below — use the arrows to reorder, then click Verify:

1 mysqli_select_db($con,"dbPelajar");
2 mysqli_close();
3 $con = mysqli_connect("localhost","root","");
4 $hasil = mysqli_query($con,"SELECT * from MURID");