Docsity

Programming Fundamentals Exercises and Questions, Exams of Programming Languages

Toggle sidebar
bg1
CLOl
C1
CLOl
C2
SUI-IT
SECTION A: 30 MARIG
BAHAGIAN A: 30 MARKATT
DF'C2O 13 PROGRAMMING F'UNDA}TENTALS
INSTRUCTION:
This section consists of TWENTY (20) objective questions. Mark your answers in the OMR
form provideci.
ARAT{AN:
Bahagian ini mengandungi DUA PULUH (20) soalan objektf. Tandakan iawapan anda di
dalam borang OMRyang disedio.kan.
1. "There is anerrorduring compilingprocessbecause a semi colon is missing atthe end of
a C++ programming language"
"Terdapat ralat semasa mengkompil aturcarc kerana sewicolon tidak ditetakkan
clipenghujung pernyataan bahasq pengaturcardan C "'"
IdentiSr the type of error for the above statement.
KenaQtasti jenis ralat bagi peruryataan di atcrs.
A. Run Time Error
Ralat Masa Lariun
B. Syntax Error
Ilalat Sintak
C. Logical Eror
Ralat Logical
D. Statement Error
Ralat Pernyataan
2. Based on the picture below, identify the type of error occurred.
Berdassrksrt rajah di bswah, tentukcrn jenis ke,salahan yang berlahr.
#include <iostream.h>
int main ( )
(
1float mark;
cout (( "Enter your mark:
cin )) marki
rf (nark > 50t
cout {{ "fai}";
al c-
cout(( "pass";
ral-rrrn n "
!g!u!rr v,
)
Syntax error
Logic error
Run-time error
Compile error
A.
B
C
D
v
SULIT
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Programming Fundamentals Exercises and Questions and more Exams Programming Languages in PDF only on Docsity!

CLOl

C

CLOl

C

SUI-IT

SECTION A: 30 MARIG BAHAGIAN (^) A: 30 MARKATT

DF'C2O 13 PROGRAMMING F'UNDA}TENTALS

INSTRUCTION:

This (^) section consists of TWENTY (20) objective questions. (^) Mark your answers in the OMR form provideci.

ARAT{AN:

Bahagian ini mengandungi DUA PULUH (20) soalan objektf. Tandakan iawapan anda di

dalam borang OMRyang disedio.kan.

  1. "There^ is (^) anerrorduring compilingprocessbecause (^) a semi colon is missing atthe end of a C++ programming language"

"Terdapat ralat semasa mengkompil aturcarc kerana sewicolon tidak ditetakkan

clipenghujung pernyataan bahasq pengaturcardan (^) C "'"

IdentiSr the type of error for the above statement.

KenaQtasti jenis ralat bagi peruryataan di atcrs.

A. Run (^) Time Error

Ralat Masa Lariun

B. Syntax Error

Ilalat Sintak

C. Logical (^) Eror

Ralat Logical

D. Statement Error

Ralat Pernyataan

  1. Based on the picture below, identify (^) the type of error occurred.

Berdassrksrt rajah di bswah, tentukcrn jenis ke,salahan yang berlahr.

#include <iostream.h>

int (^) main ( (^) ) 1 ( float (^) mark;

cout ((^ "Enter your (^) mark: cin )) marki rf (nark > (^) 50t

cout {{ (^) "fai}"; al c- cout(( (^) "pass";

ral-rrrn!g!u!rr nv, (^) " )

Syntax error

Logic error

Run-time error Compile error

A.

B

C

D

v

SULIT

CLOl

C

CLOl

C

CLO 1 C

CLOl

a

SULIT (^) DFC20 (^13) PR.OGRAMMII'IG FUNDAIIE \ T {L S

  1. Which (^) of following is the correct answer (^) to display for the string "Welcome (^) to Programming Language"?

Kenalpasti jau,'apctn^ yang beh,rl untuk memaparkan perkataan "Welconre^ to ProE'antmnt:J

Lcwtguage "?

A. cin>>Welcome to Programrning Language>>endl; B. cin))"Welcome to Programming Language (^) \n"; C. cout(( Welcome (^) to Programming Language<<endl, D. cout<<"Welcome to Prograrnming Language"<<end

  1. What is (^) the value (^) of A in the following statements?

Apa,kah nilai A bagi pernyataan di bcwsh?

int A, B, C;

B=10; ra-a, A=B*C+3;

A B c D ,r

5. Which of the following is a selection control structure?

Yang mana sattt struhur kawalan pemilihan?.

l

A. (^) for B. while

C. if (^) .else

D. do..while

  1. ldentif;z the correct syntax for (^) a for loop (^) statement. Keruolpasti sintak yang betu{ wttuk gelung.{or

A. for (c:0; (^) c(:5; c++) B. for (c:0; c<:6; c++ (^) ) C. for (r:0; c<5' c++) D. for (c:0; (^) c<5; c++);

qTTS (^) T

e

CLOZ

C

CLO

C SULIT DFC2O13 PROGRA.MMING FU\DAf^ IE\T^ {L^ I

  1. Which is the correct statement related to the declaration of the following ara'

Manakah penyataan^ ycrtg betul^ urutttk^ mengistiharkan^ dan memberi^ nilai^ cn^ alott^ b,t^ <i

Array. int score[ (^) ]:{ I 0,20,30,a0}; i. The size element of the array is 4

Saiz elemen bagi array adalah^4

ii. Scorefl]:10; iii. The type oleach element is^ integer Setiap elemen adalah^ herjenis^ integer iv. The name of the array is score Narua array adalal, score A. i and ii only / i^ dan ii^ sahaict B. iii and iv^ only I ii^ dan^ iv.sahaja C. i,^ iii^ and iv^ only^ I^ i,iii^ dcn^ iv^ sahc$a D. ii, iii and iv only I ii, iii dan iv sahaia

11 The array costnum hoids the following data:

falasustrnan costnunt memegang data beriktrt:

25.99 14 55 33.65^ 25.25^45 99 499 17

What is the value in the variable costnum after the^ following^ statement^ is^ executed?

Apakah nilai pen$olehubah^ costnum^ selepas^ peny)ataan^ herikut dilaksanakan

costnum: cost[4];

A 49q B.25. c 3365 D 4599 SULIT 7

CLO?

C

CLOl

C

CLOl

C

SULIT (^) DB.C2O 13 PROGRAMMXI{G FUNDAMENTALS

  1. Based on (^) the given (^) structure, identify the objects lJerdasarkan struktur yang^ dib e ri, kenalpast i ob"j (^) e k-o b.j e k.

struct (^) workerTlpe (^) {

i-nt ID;

lwcrkcrl (^) ,worker2;

A, (^) workerl" worker 2 B. int ID C. workerType D. (^) struct workerType

  1. Choose the INCORRECTstatement (^) that is (^) related to function declaration Pilih kenyataan yang SALAII mengenai pengisytiharctnfungsi (^) ;

A. function declaration is also called a function prototype

pengisytiharan fungsi juga^ dikenali sebagai fungsi prorcttaip

B. (^) function declaration is terminated by the semicolon peng i (^) sytiharan fungs i b erakhir dengan semi kolon

C. function (^) declaration is similar to function call p e n gi syti har an (^) fungsi ada lah sama den gan .fun gsi me manggi I

D. function declaration must have the return type of the function

pengisytiharart fungsi mesti ruempzrnyai jenis pulangan data bagi

/ungsi tersebut

14. Which is the CORRECT syntax for function declaration?

I)i antara berikut, manqkah sintak,vang BETUL untuk pengis,tiharan.firngsi?

return_iype iunsiion_rrarrie(pararneier iisi);

return_type function_header(parameter list);

function_name(parameter (^) li st) (^) ; return_type (parameter list) (^) fu nction*name,

.

A. B.

L.

D.

SITI,IT

CLO

IJ

SULIT DFC2O13^ PROGRAMMING^ F'UNDAIVIENTALS

17. Determine the output^ from^ the^ following^ program.

Tentukan output daripada progrttm^ berihut.

#include (iost-ream) using namespace std;

int sum(int a, int^ b :^ 20) { int resul t;

result a - b;

return (resu-Lt^ ) ;

l

int main (^ )

i

int a :^ 100; int b :^ 2OA; int result;

-^-.,,1 rc>u.J.L +^ - >ul"ll\d/^rrm^ /- h\u)t^.

result :^ sunt(a) (^) ;

sysiem("pause"); return 0; )

A. Total value is^ :^ i

Total value is. 120

Total value is : 300

Total value is^ :^300

Total value is : 300

Total value is : 120

Total value is^ :^ tr

Total value is. 300

B.

C.

D.

SULIT

!,

!

CLO

Ci

CLO

C

{

SULIT DFC2O 13 PROGRAMMING FUND.{{^ E^ ^ T^ \L^ S

  1. What is the correct declaration for pointer? Pilih pengisytiharacn yang bentl unruk penuding?

A. int ptr*;

B. int *ptr;

C int &ptr.

D. int ptr&;

  1. Choose the CORRECT declaration based on the information given^ below.

Pilih pengisytharan yang BENAR berdasarkan ruaklumat yang diberikan di bcrutah.

A int ptr:^ i00; B int uptr:^ # C int ptr :^ 123456. D int *ptr:^ &123456.

SULIT

num [--*[l 123456 ou (^) [6fl rz34s

,J?34S

-.

CLOl

C

CLOl

C

SULIT

SECTION B: 70 MARKS

BAHAGIAN (^) B: 70 MARKAIT DFC2O (^13) PROGRA]VIMING F'UNDAMENTAIS

INSTRUCTION:

This (^) section consists (^) of FOUR (4) qlrestions. (^) Answer ALL questions ARAIIAN:

Bahagian ini mengandungi EMPAT (4) soalan. Jcrwab SEMaA soalall

QUESTTON 1

SOAT-AN 1

(a) What is syntax error?

Apakah ke sctla han^ sirnak?

(b) (i) Identify the most suitable data tirpes for the following variables.

Kenalpasri jenis data yang pctling ,sesuai bagi pembolehubah berikut.

monev age name crade [2 marks]

t^ [) ' wnrknhl'"^ --"'^ '^ '-" -t

(ii) By using the answers in (b)(i), write the syntax to declare the variables.

Dengan menggunakan jawapon^ anda dakm (b)(r,^ tuliskan sintak untuk

nte ngi syti har kan p e mb o I e hu b a h di atas "

[2 marks] l2 markahJ [2 marks]

{2 markahJ

i1 SULIT Y

Variables Data Type

CLO]

C

CLO

C

c) Answer the question based on Figure 81.

Jawab soalsn^ berdasarkan Rajah^ Bl.

int x,y; x= 1; y= 1;

while (x <:3)

x=x1; y=yx; cout << x << "\t" <<^ y; )

Figure Bl^ I Rajah^ BI

i. How many times does program loops?

Berapa kali progrant^ melahrkan ttlangan^?

ii. (^) Write the output. Tulis output.

Rewrite the segment code using do-while loop^.

Tulis semulct kod segmen meruggunakan^ gelung do-while^.

int sum: 0; while(n >^ 0) { sum:sum*n; --n; I I

DTCzO 13 PROGRAMMING T'UNDAMENTALS

| 4 marks^ l

ft markahJ

[ 2^ marks ] l2markah J

.

a

SIILIT

CLO

C

CLO

C

CLO

C

CLOl

C

SUT,IT

QUESTTON 2

SOALAN 2

DTC2O 13 PROGRAMMING^ X'^ UNDAMENTALS

(a) Write the syntax to declare an array.

'I (^) u li s si ntak b agi pengi (^) sy ti har a n t a t as?t sunan.

(b) Write the C++ statements to,..

'[ (^) uliskan pernyctaan C' (^) - urttuk...

i. declare an array my_array of 10 elements of type int.

ii. set the value of fifth element of array my_array to 3 5

iii. access the value of tenth element of array my_arrav.

(c) Based on fragment code below, write the value f,or each statement.

Berdasarkan kodfragmen, tulis^ nilai^ dari^ penyataan^ di^ bawah.

int number (^) t3ll4l :^ { {2,4,6,8}"{3,6.9,12},{1,5,10,13} };

i. cout<<numberl2l[3];

ii, (^) cout<<number[0]121.

(d) Identifu two types of function.

Kenal pasti dtta jeruisflmgsi.

iv. variable name is matrix consist 5 rows and 5^ coiumn lvith^ data t-vpe of^ double.

[ 2 marks^ ]

f2markah J

[ 4 marks^ ]

fimarkah J

[ 2 marks^ ]

{2markah J

[2 marks] 12 markah)

l.t SULIT

CLOl

C t -J'

CLO?

C SULIT (^) DFC2O13 (^) PROGRAMMING FUNDAMENT,dLS (e) (^) Create a function name "calculate".It (^) will return a double (^) type data and accept a parameter (^) name testScore(<iata (^) type cioubie). Cipta satufung'ri (^) bernoma calculate. Furugsi (^) akan mengerubalikan tlata.jenis (^) clouble dqil

menerima satu parameter testScore jenis datct double.

l2marksl [2markahJ

Question f (i)^ and^ f (ii)^ based^ on program below.

#include<iostream.h> void greet$;

----------X-----*- /lfunction prototlpe of department$

void mainQ It greet$; department$. tt void department() {

cout<<"'JTMK "

) -'! ,rr.ction definition^ of^ greet I ) (f) i. (^) State the department (^) function prototype (^) on X label.

Nyatakan prototaipfungsi bcgi department () pada labet X.

i2 marksl

12 martuh) ii. Write the definition of greet (^0) function (label (^) as Y) that can print "welcome" (^) as output. Tiiis delinisi (^) fungsi bagi gy eet 0 (label^ Y) yang (^) boleh mencetak perkataan (^) welcante ,sebagai output. f2 marksl 12 mcrrkah) 15 SIII,TT

I

Y

CLOl

C

CLOI

C

CLO 1

C

CLOl

C

SULIT

QUESTTON 3

SOALAN 3

DF'C2013 (^) PROGRAMMING F'UNDAMENTAIS

(a) Identify three (3) types of error in programming.

Kenalpasti tiga jenis kesalahan di dalam pengaturcaraan.

[3 marks] [3 markah)

(b) State the definition of identifier in the programming language and give one(1) condition

of a valid identifier.

Nyalokan (^) definisi 'identifier' (^) di dalam bahasa pengaturcaraan (^) clan berikan (^) satil q)arst

sah bagi 'identifier'.

[3 marks] 13 markahl

(c) (^) write cH statement (^) to accomplish the following (^) instructions.

Tulislran (^) pernyata*t c++ sngr|, (^) mememthi arshan-arahsrc berikut.

Declare three (3) (^) variables name x, y (^) and resurt as integer (^) aata tl.pe.

Print the message \Enter^ two integerrr.

Read two (2) integers from (^) the user and store them in the variables x and y.

Isytihar tiga (3) pembolehubah (^) x y dan (^) resuit di dalam kategori (^) inz

Papar pesattan tt4nter two integerrr.

Baca dua (2) integer dari pengguna dan simpan di dalam pembolehubah xdan y.

[3 marks] [3 rnarkah

List and define TWO (2) types of loops.

Senayaikan dan beri definisi dua jenis pengulangon.

[ 3 marks^ ] [3 markah^ I

(d)

L7 (^) STILIT

/-r r] C?

CLO

C

DFC2O 13 PROGRAMMING BUNDAMENTALS

(e) Write an appropriate if..else staterment for the following'

Tulis pernyutaan^ if,.el.se yang^ .seruai unluk^ perkara^ berikut^ :

If mark is more then 10, print^ a message"Very Good",

otherwise print^ a message^ "Good".

Jika mork adnloh lebih daripada l0 (^) , cetakmesej "^ Sangat^ Baik"^ ,

sebaliknya cetak^ mesej^ "Good^ "

[ 3 marks^ ]

{3 markah^ it

(f) Write the segment code by using C++ language^ for^ while^ loop^ based^ on^ algorithm^ beiow.

Tuliskan kod segmen mengguruakan bahasa pengaturcarnant c-^ - untuk^ gelung^ while

berc{asarkan kepada algoritma di bowah.

1. Set counter equal to value 1.

  1. Compare whether counter is less than or

equal to 10

  1. 1 if yes^ go^ to^ step 3 2.2 if no^ go^ to^ step 6

3. Display counter

4 Add counter using formula:

couirter: counter*i

  1. Repeat step 2

6. End while

[3 marks] 13 markah)

18 SULIT

CLO

C

CLO

Ci

SULIT (^) DFC2O (^13) PROGRAMMING FUNDAMENTALS

(d) Write the segment code for a function definition name calcAverage( ) that receives

four integers. The function should caicuiate the average of the four iniegers, then

return the result (which^ may contain (^) a decimal piace).

Tuli,skan k:od ,segmen untuk definasi fi*tg.si yang bernama calcAverage( ) yang

menerima empat nilai integer. Fungsi tersebut okan menerima empat nilai integer

dcn (^) memulangkan nilcti (^) vang mengondurtgi titik perpuluhatt.

[3 marks]

f3 markalo/

13 marksl

[3 narkahJ

(e) (^) What is a POLNTER (^) and give (^) declaration for POINTER

Terangkan maksttd POII\ITER don berikan (^) contoh bagi declat'cttton urttuk POINTER

20 SULIT

CLA

C

_t SUI-TT (^) DT'C2013 PROGRAMMINGFUNDAMENTALS (f) (^) Convert the following (^) algorithm into (^) C++ code: Tukarkan (^) algorithm berihfi kepada kod C., (^) -: Step i: Declare an array variable named Numlcer with size 3 of type integer. Langkah l: Isytihar pemboleh ubah tatax.rsunan l,Iumber dengan (^) sair 3 jeni.s tnLeqer.^ : ^+ ^-^

Step 2: Declare a pointer named Pnumber.

Langkah 2: Isytihar penuding^ Fnumber. Step 3: Assign Pnumber to variable Number.

Langkah 3: Ietapkatt Pnumber kepaCa pemboleh (^) ubah Number.

Step 4: Assign value of PnumJ:er with 10.

Langkah1; Tetapkannilai Pnunber kepada. 10. Step 5.^ Print^ the address of xumber (^) [0] krngkah 5: Paparkan alamat l,Junber (^) l}J. Step 6. Print the value (^) of object pointed (^) to by lTumber.

Langkah 6: Paparkan nilcru objek yung dituding kepada lJumber.

[3 marks] {j markah/

END OF QUESTIONS

SOALAN TAMAT

CTTI I'r' Y