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
Finding the Maximum Value
Proses Mencari Nilai Maksimum (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.
Introduction to Standard Libraries
Kelebihan & Pengenalan Standard Library (Bahagian 3.1.5)
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 likemath.sqrt()(square root) andmath.pow()(exponents).date.js: Used for date tracking functions such asdate.now()and formatting.
Fungsi math.js (Mathematical Library Methods)
Jadual 3.1.30 Fungsi-fungsi dalam math.js
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) |
Modular Programming & Sub-routines
Rajah 3.1.44 Modul Utama dan Subatur Cara
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.
Procedures vs Functions
Perbezaan antara Prosedur dengan Fungsi (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); |
Array Declaration & Manual Retrieval
Mengisytiharkan Array & Akses Satu per Satu (ms206(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.
// 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
// 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) |
Loops and Memory Blocks
Menggunakan Ulangan untuk Menjumlahkan Senarai (ms206(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:
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);
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);
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);
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);
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);
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:
PHP & Pengendalian Fail Teks
Server-side scripting with PHP — fopen, fwrite, fclose, file reading & form handling
Introduction to PHP & File Operations
Pengenalan PHP & Mencipta/Membuka Fail Teks (ms227)
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
File Access Modes — "w", "r", "a"
Mod Capaian Fail dalam fopen (ms228)
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. |
File Writing Code Sequence
Susunan Atur Cara — fopen, fwrite, fclose (ms229)
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:
HTML Form + PHP File Logging
Atur Cara Borang & Penulisan Log ke Fail (ms231 — 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:
Click any line on the left to see a detailed explanation of what that line of code does.
File Reading & PHP Variable Syntax
Membaca Fail & Sintaks PHP dengan Simbol $ (ms240 — Rajah 3.2.16)
For ms240, two key exam topics:
- Tag ① — What is
"r"? The fileNombor.txtis 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
MySQL Database Connection Functions
Fungsi Sambungan Pangkalan Data MySQL (ms244)
For ms244, you must know which part of each function call does what. Click each code line in the inspector to learn its parameters:
Click a line on the left to see what each parameter in the MySQL function means.
Database Query Code Sequence
Susunan Kod Pertanyaan Pangkalan Data (ms245 — 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:
Feedback & Queries
Submit form questions or overall website feedback