0% found this document useful (0 votes)
163 views

Sample Exercises Mat 181: (Answer)

The document provides 4 multiple choice questions about C++ programming concepts. The first question asks about the output of a sample program that takes input numbers and outputs their remainders when divided by 25. The correct answer is B, which lists the remainders of the input numbers. The second question asks which switch statements are valid, with the answer being II and IV only. The third question provides a nested loop program and asks for its output, which is correctly answered as the repeated values 3 6 4 7. The fourth question asks which program segment computes a given sum correctly, with the answer being I, III and IV only.

Uploaded by

Nada Alia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Sample Exercises Mat 181: (Answer)

The document provides 4 multiple choice questions about C++ programming concepts. The first question asks about the output of a sample program that takes input numbers and outputs their remainders when divided by 25. The correct answer is B, which lists the remainders of the input numbers. The second question asks which switch statements are valid, with the answer being II and IV only. The third question provides a nested loop program and asks for its output, which is correctly answered as the repeated values 3 6 4 7. The fourth question asks which program segment computes a given sum correctly, with the answer being I, III and IV only.

Uploaded by

Nada Alia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SAMPLE EXERCISES MAT 181

1. Suppose that the input of a program is 58 23 46 75 98 -999 What is the output of the following program segment with the above input data? int number; cin >> number; while (number != -999) { number % 25 ? cout << number % 25: cout << number; cout << " "; cin >> number; } A. 2 23 1 3 3 B. 8 23 21 75 23 (answer) C. 58 0 46 75 98 D. 8 0 21 75 23 E. None of the above. 1. Katakan input bagi suatu program ialah 58 23 46 75 98 -999 Apakah output bagi segmen program berikut dengan data input di atas? int number; cin >> number; while (number != -999) { number % 25 ? cout << number % 25: cout << number; cout << " "; cin >> number; } A. B. C. D. E. 2. 2 23 1 3 3 8 23 21 75 23 58 0 46 75 98 8 0 21 75 23 Bukan yang disebut di atas.

Choose the valid switch statements from the following. (Assume that k is a declared integer variable whose value is already assigned before this ) I. switch(2*k) { case 2: 4: 6: 8: 10: cout << Small Even; break; case 20: 24: 28: 32: cout << Big Even; break; } switch(k) { case 2: cout << Even and prime; break; case 3: cout << Prime; break; case 7: cout << Odd; break; }

II.

.../2-

III.

switch(k) { case 2, case 3: cout << Prime; break; case 4, case 8: cout << Even; break; case 7: cout << Odd; default: cout << Invalid k ; } switch(2*k) { case 2: case 4: case 6: case 8: cout << 2*k; break; case 20: case 28: cout << endl; break; }

IV.

A. B. C. D. E. 2.

I and II only II and III only II and IV only (answer) I, II and IV only None of the above

Pilih pernyataan(-pernyataan) switch yang sah dari yang berikut. (Anggapkan k adalah pembolehubah integer yang telah diisytiharkan yang nilainya telah diumpukkan sebelum ini) I. switch(2*k) { case 2: 4: 6: 8: 10: cout << Small Even; break; case 20: 24: 28: 32: cout << Big Even; break; } switch(k) { case 2: cout << Even and prime; break; case 3: cout << Prime; break; case 7: cout << Odd; break; } switch(k) { case 2, case 3: cout << Prime; break; case 4, case 8: cout << Even; break; case 7: cout << Odd; default: cout << Invalid c ; } switch(2*k) { case 2: case 4: case 6: case 8: cout << 2*k; break; case 20: case 28: cout << endl; break; }

II.

III.

IV.

A. B. C. D. E.

I dan II sahaja II dan III sahaja II dan IV sahaja I, II dan IV sahaja Bukan yang disebut di atas

.../3-

3.

Consider the following code: int i, j, k; i = 1; while (i < 3) { j = 2; while (j < 7) { k = i+j; cout << k << " " ; j += 3; } i++; cout << endl; }

The output of this program segment is A. 3 6 4 7 3 6 4 7 B. 3 6 4 7 C. 3 6 4 7 D. 3 6 4 7 E. None of the above 3. Pertimbangkan kod berikut: int i, j, k; i = 1; while (i < 3) { j = 2; while (j < 7) { k = i+j; cout << k << " " ; j += 3; } i++; cout << endl; }

(answer)

A. 3 6 4 7 3 6 4 7 B. 3 6 4 7 C. 3 6 4 7 D. 3 6 4 7

E.

Bukan yang disebut di atas

.../4-

4.

Which of the program segments compute the following sum correctly?

1 1 1 1 1 1 .... 25 27 29 31 103 105


I. sum = 0; for (i = 25; i<= 105; i+=2) sum = sum + 1.0/i; sum = 0; i = 1; do { sum = sum + 1.0/2*i; i++; } while (i <= 105); sum = 0; for (i = 25; i<= 105; i++) sum = sum + 1.0/i++; sum = 0; for (i = 13; i<= 105; i+=2) sum = sum + 1.0/(i+12); I, II and III only I and III only I, III and IV only I and II only None of the above.

II.

III.

IV.

A. B. C. D. E. 4.

(answer)

Segmen program manakah yang mengira jumlah berikut dengan betul?

1 1 1 1 1 1 .... 25 27 29 31 103 105


I. sum = 0; for (i = 25; i<= 105; i+=2) sum = sum + 1.0/i; sum = 0; i = 1; do { sum = sum + 1.0/2*i; i++; } while (i <= 105); sum = 0; for (i = 25; i<= 105; i++) sum = sum + 1.0/i++; sum = 0; for (i = 13; i<= 105; i+=2) sum = sum + 1.0/(i+12); I, II dan III sahaja I dan III sahaja I, III dan IV sahaja I dan II sahaja Bukan yang disebut di atas.

III.

III.

IV.

A. B. C. D. E.

.../5-

5.

Consider the following statements: float number = 34.7845; cout << setfill(*) << setw(5)<< "#" << setiosflags(ios::left) << setiosflags(ios::fixed) << setw(8)<< setprecision(2) << number << "$"; cout << endl; cout << setfill(#) << resetiosflags(ios::left) << "&" << setw(4) << "zz" << "$"; What is being printed by the above program segment? A. ****#34.78***$ &##zz$ B. #****34.78***$ &##zz$ C. ****#34.78***$ ##&zz$ D. ****#34.78$*** &##zz$ E. #*******34.78$ &##zz$

(ans)

6.

What is the value of z after the following code segment is executed? float x = 6.8, y = 25.0; int z; if (x >= y) z = x; else if (2.5*x >= y) z = (0.5*x / y == 1.0); else z = (x < 7.3 && y >= x == 1.0); A. B. C. D. E. 6.8 25 Runtime error occurs. 0 1 (ans)

7.

Consider the following: float a, c; int b; c = 77.67; b = c; a = b; cout << a; These statements result in the variable a receiving the value A. B. C. D. 78 77 77.67 77.7

(ans)

.../6-

E.

None of the above

8.

What is the output produced by the following program? Apakah output yang dihasilkan oleh program berikut? #include <iostream> using namespace std; int calc1(int x,int y, int z){ x = y + z; return x;} int calc2(int x,int y, int z){ z = y - x; return z;} void main(){ int x = 10 , y = 10 , z = 10; y = calc1(x,y,z); z = calc1(x,z,y); cout << x <<" "<< y <<" "<< z << "\n" << z <<" "<< y << " "<< calc2(y,z,x); } A. B. C. 10 20 30 10 20 30 10 20 30 30 20 10 10 20 30 30 20 20 10 20 30 30 20 40 Compilation error occurs. Ralat kompilasi berlaku. (answer)

D. E.

9.

Consider the following program: Pertimbangkan program berikut: void main(){ int score[7] = {1,1,1,1,1,1}; for (int i=1 ; i<6 ; i++) score[i+1] = score[i+1] + score[i]; for (int i=0 ; i<6 ; i++) cout << score[i] << " ";}

What is the output of this program? Apakah output program ini? A. B. C. D. 1 2 3 4 5 6 1 1 2 4 8 16 1 2 4 8 16 32 1 1 2 3 4 5 (answer)

.../7-

E.

1 1 2 3 4 5 5

10.

Consider the following program. void main(){ int a = 20, d = 10; int *b; int *c; b = &a; c = &d; ... cout << a << " " << d;} Which of the following statements should be placed before the cout statement in order to obtain the value of a = 200 and d = 200? A. B. C. D. E. a = *c*a; d = *b*d; a = a*c; d = d*b; a = &c*a; d = &b*d; a = a**c; d = d**b; None of the above. (answer)

10.

Pertimbangkan program berikut. void main(){ int a = 20, d = 10; int *b; int *c; b = &a; c = &d; ... cout << a << " " << d;} Yang manakah di antara pernyataan berikut patut diletakkan sebelum pernyataan cout untuk mendapatkan nilai a = 200 dan d = 200? A. B. C. D. E. a = *c*a; d = *b*d; a = a*c; d = d*b; a = &c*a; d = &b*d; a = a**c; d = d**b; Bukan yang disebut di atas.

11.

Consider the following struct declaration.

.../8-

struct food { int price; int calories; } pizza, burger, pasta; Which of the following are TRUE about the above struct declaration? I. II. III. The variables pizza, burger and pasta are of the structure type called food. The struct members price and calories must be of the same types. The member price of the object pizza can be assessed directly by using the name pizza.price.

A. B. C. D. E. 11.

I only I and II only I and III only II and IV only I, II and III only (answer)

Pertimbangkan pengisytiharan struct berikut. struct food { int price; int calories; } pizza, burger, pasta;

Yang manakah antara berikut adalah BENAR mengenai pengisytiharan struct di atas? I. Pembolehubah pizza, burger dan pasta merupakan jenis struktur bernama food. Ahli struct bernama price dan calories mesti dari jenis yang sama. Ahli bernama price kepada objek pizza boleh terus diakses dengan menggunakan nama pizza.price. I sahaja I dan II sahaja I dan III sahaja II dan IV sahaja I, II dan III sahaja

II. III.

A. B. C. D. E.

12.

Consider the following statements: I. II. III. IV. A global function is declared or defined outside of any function in a program. Variables declared in the main function can be used by other functions in the program. A local function can only be used by the function that declared or defined it. Void type function can return value to the calling function.

.../9-

Choose all the CORRECT statement(-s). A. B. C. D. E. IV only. All of them are correct. I and III only (answer) II only. II and IV only.

12.

Pertimbangkan pernyataan-pernyataan berikut: I. II. III. IV. Fungsi sejagat diisytiharkan atau ditakrifkan di luar sebarang fungsi dalam suatu program. Pembolehubah yang diisytiharkan dqlam funsi main boleh digunakan oleh fungsi yang lain dalam program. Fungsi setempat hanya boleh digunakan oleh fungsi yang mengisytiharkannya atau menafrifkannya sahaja. Fungsi berjenis void boleh mengembalikan nilai kepada fungsi yang memanggilnya.

Pilih semua pernyataan yang BENAR. A. B. C. D. E. 13. IV sahaja. Semuanya adalah benar. I dan III sahaja. (ans) II sahaja. II dan IV sahaja.

Find the output of the following program: Cari output program berikut: #include <iostream.h> void main() { void F(int); int i; for(i=0; i<6; ++i) F(++i); cout << i ; } void F(int a) { cout << ++a << ; }

A. B. C. D. E.

1356 2466 2467 1456 2456

(ans)

14.

Choose all the CORRECT way(s) to define an array of five integers. I. II. int A[ ] = {1, 2, 3, 4, 5}; int B[5]; //the values will then be assigned later in the program

.../10-

III.

#define index 5 : int C[index] = {1, 2, 3, 4, 5, 0}; int E[5] = {1}; I and III only. I, III and IV only. I, II, III and IV only. I, II and III only. I, II and IV only. (ans)

IV. A. B. C. D. E.

15.

Find the output of the following program segment: Cari output segmen program berikut: int arr[3][4] = {1, 2, 3, 4, 5, 6. 7, 8, 9, 10, 11, 12}; for (int j = 0; j < 3; ++j) { for (int k = 0; k < 4; ++k) { arr[j][k] = (j+1)*(k+1); cout << arr[j][k] << ; } cout << endl; }

A.

1234 2468 3 6 9 12 (ans)

B. 1 2 3 4 2468 3 6 9 10

C. 1 2 3 4 2468 3579

D. 1 2 3 4 2468 3 6 8 10

E. 1 2 3 4 2468 3679

16.

Find the program segment which will not result in an error. I. int *ptr; int x =512; : ptr = &x; void max (int, int, int *); int total; : max (2, 6, total); void max (int, int, int &); int total; : max ( 2, 6, & total); int * a; int p[5] = {2, 4, 6, 8, 10}; : a = p[3];

II.

III.

IV.

A. B. C. D.

I and III only. IV only. I only. (ans) II and III only.

.../11-

E. II and IV only.

17.

Find the output of the program below. Cari output program di bawah.

void main() { char S1[24] = "Learning to program"; char S2[24]; strncpy_s(S2, S1, 5); cout << S2 << endl; strcpy_s(S2, S1); cout << S2 << endl; } A. Learning Learning to program B. Learni Learning to program C. LearnLearning to program D. Learn (answer) Learning to program E. LearningLearning to program

SAMPLE PROGRAMMING EXERCISES

1.

The volume of a cone is given as

1 3

r 2 h where r is the base circle radius and h is the height of

the cone. By taking 3.142 , write a complete C++ program to make a table of volume when the base radius is constant and the height varies. A sample output when the base radius is 3 and the height varies from a = 1 to b = 5 is as follows: Base radius 3.00 3.00 3.00 3.00 3.00 Height 1.00 2.00 3.00 4.00 5.00 Volume 9.43 18.85 28.28 37.70 47.13

The base radius r and the range of the height, a and b, should be entered as user input. 1. Isipadu kon diberikan oleh

1 3

r 2 h di mana r ialah jejari bulatan tapak dan h ialah tinggi kon.

Dengan mengambil 3.142 , tuliskan suatu program C++ yang lengkap untuk membuat suatu jadual isipadu apabila jejari tapak tetap dan tinggi berubah. Suatu contoh output apabila jejari tapak 3 dan tinggi berubah dari a = 1 ke b = 5 adalah seperti berikut:

.../12-

Base radius 3.00 3.00 3.00 3.00 3.00

Height 1.00 2.00 3.00 4.00 5.00

Volume 9.43 18.85 28.28 37.70 47.13

Jejari tapak r serta julat bagi tinggi, a dan b, perlu dimasukkan sebagai input pengguna.

2.

Write a C++ program which will sort three integer numbers into ascending order. The program must contain a function Sort (..) of the type void which is passed three integer values as reference parameters small, mid and large. The function then uses the single choice if statement to arrange the values of these into ascending order. For example, if the function receives the values small = 23, mid = 2, large = 18 when it is called, the values of these parameters will be small = 2, mid = 18 and large = 23 after this function is executed. Within this Sort(..) function, you will thus use statements of the following form: if (small > mid) Swap(small, mid); if (mid > large) : : where it is assumed that Swap(..) is another function which is passed two not in the right order values and arranges these in order by exchanging (or swapping) their values. Assume that the three integer values are entered as input in the main function. The main function must call the Sort(..) function, while the Sort(..) function must call the Swap(..) function.

2.

Tulis satu program C++ yang mengisih tiga nombor integer dalam tertib menaik. Program ini perlu mengandungi fungsi Sort(..) berjenis void yang dihantar tiga nilai integer sebagai parameter rujukan small, mid dan large. Fungsi ini kemudiannya menggunakan pernyataan if pilihan tunggal untuk menyusun nilai-nilai ini dalam tertib menaik. Contohnya, jika fungsi ini menerima nilai-nilai small = 23, mid = 2, large = 18 apabila ia dipanggil, nilai-nilai parameter ini akan menjadi small = 2, mid = 18 dan large = 23 selepas fungsi ini dilaksanakan. Dalam fungsi Sort(..) ini, anda perlulah menggunakan pernyataan berbentuk berikut: if (small > mid) Swap(small, mid); if (mid > large) : : di mana dianggapkan Swap(..) ialah satu fungsi lain yang dihantar dua nilai yang tidak dalam aturan yang betul ini dan menyusun mereka mengikut aturan dengan saling menukar nilai mereka. Anggapkan bahawa tiga nilai integer tersebut dimasukkan sebagai input dalam fungsi main. Fungsi main mestilah memanggil fungsi Sort(..), manakala fungsi Sort(..) mestilah memanggil fungsi Swap(..).

- ooo O ooo -

.../13-

You might also like