Beruflich Dokumente
Kultur Dokumente
1. State whether the following list of identifiers are valid or invalid. If it is not valid, state the reason why.
Nyatakan samada senarai pencam berikut sah atau tidak sah. Jika ia tidak sah, nyatakan sebabnya.
1time :
Invalid, identifiers cannot begin with number
two fold :
Invalid, identifiers cannot have space in between
c3p0:
Valid
[5 marks]
2. Identify and correct the errors in each of the following programming segment. (Note : There may be more
than one error in each piece of code).
Kenalpasti dan betulkan ralat dalam setiap segmen atur cara berikut. (Nota: Kemungkinan terdapat lebih
daripada satu ralat dalam setiap kod).
The following statement should determine if count is outside the range of 0 through 100.
Pernyataan berikut mengenalpasti samada count berada di luar julat 0 hingga 100.
if (count < 0 && count > 100)
if (count < 0 || count > 100)
The following statement should assign 0 to z if a is less than 10, otherwise it should assign 7 to z.
Segmen berikut mengumpukkan 0 kepada z sekiranya a kurang daripada 10, jika tidak ia
mengumpukkan 7 kepada z.
if (a < 10);
z == 0;
else
z == 7;
if (a < 10)
z = 0;
else
z = 7;
[6 marks]
4. Write a program to ask the user to input two numbers, compare the numbers, and then prints the larger
number followed by the words "is larger". If the numbers are equal, print the message "These numbers
are equal."
Tuliskan atur cara yang menyuruh pengguna memasukkan dua nombor, membandingkan nombor
tersebut, dan mencetak nombor yang lebih besar. Jika nombor tersebut sama, cetak mesej Nombor
tersebut adalah sama.
#include <iostream.h>
void main()
{ int num1, num2;
cout <<"Enter two numbers : ";
cin >> num1 >> num2;
if (num1 > num2)
cout << num1 << " is larger. ";
else if (num2 > num1)
cout << num2 << " is larger. ";
else if (num1 = = num2)
cout << " These numbers are equal. ";
[10 marks]
5. Write C++ statements to print the integers from 1 to 20 using while loop and the counter variable x.
Assume that the variable x has been declared. Print only 5 integers per line. (Hint : Use the calculation x
% 5. When the value of this is 0, print \n (a new line character), otherwise print \t (a tab character)).
Tulis pernyataan C++ untuk mencetak integer daripada 1 hingga 20 menggunakan gelung while dan
pembolehubah pembilang x. Andaikan pembolehubah x telah diisytihar. Cetak hanya 5 integer pada satu
baris. (Kunci: Gunakan pengiraan x % 5. Apabila nilai didapati 0, cetak \n, jika tidak cetak \t).
x = 1;
while ( x <= 20)
{ cout << x << "\t";
if (x % 5 = = 0)
cout << "\n";
x++;
}
[10 marks]
1. State whether the following list of identifiers are valid or invalid. If it is not valid, state the reason why.
Nyatakan samada senarai pencam berikut sah atau tidak sah. Jika ia tidak sah, nyatakan sebabnya.
income#1
case
_staff_num
Valid
[5 marks]
2. Identify and correct the errors in each of the following. (Note : There may be more than one error in each
piece of code).
Kenalpasti dan betulkan ralat dalam setiap segmen atur cara berikut. (Nota: Kemungkinan terdapat lebih
daripada satu ralat dalam setiap kod).
The following code should output the even integers from 2 to 100
Kod berikut mengeluarkan output integer ganjil daripada 2 ke 100
char counter = 2;
do
{
cout << counter << endl;
counter = counter + 2;
} While (counter < 100);
int counter = 2;
do
{
cout << counter << endl;
counter = counter + 2;
} while (counter < 100);
The following statement should determine if count is within the range of 0 through 100.
Pernyataan berikut mengenalpasti jika count berada dalam julat 0 hingga 100.
if (count >=0 || count <= 100)
if (count >=0 && count <= 100)
[6 marks]
5. Write C++ statements to print the integers from 1 to 20 using while loop and the counter variable x.
Assume that the variable x has been declared. Print only 5 integers per line. (Hint : Use the calculation x
% 5. When the value of this is 0, print \n (a new line character), otherwise print \t (a tab character)).
Tulis pernyataan C++ untuk mencetak integer daripada 1 hingga 20 menggunakan gelung while dan
pembolehubah pembilang x. Andaikan pembolehubah x telah diisytihar. Cetak hanya 5 integer pada satu
baris. (Kunci: Gunakan pengiraan x % 5. Apabila nilai didapati 0, cetak \n, jika tidak cetak \t).
x = 1;
while ( x <= 20)
{ cout << x << "\t";
if (x % 5 = = 0)
cout << "\n";
x++;
}
[10 marks]