You are on page 1of 14

UNIVERSITI TENAGA NASIONAL College of Information Technology

BACHELOR OF MECHANICAL ENGINEERING (HONS.) BACHELOR OF CIVIL ENGINEERING (HONS.)

FINAL EXAMINATION SEMESTER II 2010/2011 PRINCIPLES OF PROGRAMMING (CSEB113)

March 2011

Time allowed: 3 hours + 10 minutes for reading

INSTRUCTIONS TO CANDIDATES 1. 2. 3. The total marks for this exam is 100 marks. There are THREE (3) SECTIONS to this paper: Section A, Section B, and Section C. Answer ALL questions in the answer booklet provided.

DO NOT OPEN THIS QUESTION PAPER UNTIL YOU ARE INSTRUCTED TO DO SO THIS QUESTION PAPER CONSISTS OF 14 PRINTED PAGES INCLUDING THIS PAGE

SECTION A: MULTIPLE-CHOICE (20 QUESTIONS, 30 MARKS) Instruction: Please select the BEST answer from the given choices.

1.

C programming language is type of ___________? (a) (b) (c) (d) machine language assembly language high-level language natural language

2.

Which of the following is a valid comment in C programming? (a) (b) (c) (d) /* This is comment */ */ This is comment /* ** This is comment ** This is comment //

3.

What is the meaning for the flowchart symbol named label-X in Figure 1?

Begin

label-X

End

Figure 1 (a) (b) (c) (d) selection symbol process symbol input-Output symbol terminal symbol

Page 2 of 14
Semester II 2010/2011 Principles of Programming

4.

Every C program begins execution at the function ________. (a) (b) (c) (d) prototype main called include

5.

A (n)________ is a variable whose content is read only and cannot be changed during the program's execution. (a) (b) (c) (d) operator literal named constant None of the above

6.

A procedure for solving a problem in terms of the actions to be executed and the order in which the actions should be executed is called a(n) ________ . (a) (b) (c) (d) source code c programming algorithm None of the above

7.

What would be the value of x after the following statements are executed?
#include<stdio.h> int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; break; default: x *= 3; }

(a) (b) (c) (d)

5 20 25 30

Page 3 of 14
Semester II 2010/2011 Principles of Programming

8.

The expression in an if statement must be evaluate to ________? (a) (b) (c) (d) 0 or 1 +1 or -1 true or false T or F

9.

Which of the following is a logical operator? (a) (b) (c) (d) >= && != ==

10.

What is the output of the following program?
#include<stdio.h> int main() { int a = 3 ; if ( a++ == 3) printf("Three\n"); else printf("Four\n"); printf("Five\n"); return 0; }

(a) (b) (c) (d)

Three Four Three Five Four Five

Page 4 of 14
Semester II 2010/2011 Principles of Programming

11.

How many lines of output will be displayed on the screen when the following program is executed?
#include<stdio.h> int main() { int i=12,j=31,count=0; for (; i<j; i++) { if(i%4==1) printf("%d\n",i); } return 0; }

(a) (b) (c) (d)

8 6 5 18

12.

What is the result of this 17 % 4 + 3 statement? (a) (b) (c) (d) 7.25 4.5 7 4

13.

What is the escape characters that causes the cursor to back up, or move left, one position? (a) (b) (c) (d) \t \n \a \b

Page 5 of 14
Semester II 2010/2011 Principles of Programming

14.

Which of the following statements correctly tests the integer variable x to see if it is equals to zero?

(a) (b) (c) (d) 15.

if (x > 0) if (x >= 0) if (x == 0) if (x <= 0)

How many times is the following loop body repeated?

#include<stdio.h> int main() { int i=1; while(i < 10) if((i++) % 2 ==0) printf("%d\n",i); return 0; }

(a) (b) (c) (d) 16.

4 5 7 None of the above

Which of the following is TRUE about recursion? (i) (ii) (iii) Recursion is where a function calls itself Recursion is a process to call another function to complete the tasks The series of factorial(n) is one of the example process using recursion technique (iv) Recursion does not consume more internal memory to complete the process (a) (b) (c) (d) (ii), (iii) (i), (iii) (i), (ii) (i), (ii), (iii) Page 6 of 14

Semester II 2010/2011

Principles of Programming

17.

Which of these is a valid array initialization in C programming? (a) (b) (c) (d) marks [5] = (55, 33, 86, 81, 67); marks [5] = {55, 33, 86, 81, 67}; marks [5] = {55}, {33}, {86}, {81}, {67}; marks [0] = {55}; marks [1] = {33}; marks [2] = {86}; marks [3] = {81}; marks [4] = {67};

18.

Which one of the following is TRUE? (a) (b) The reserve words static and auto could be used in array The elements of an array can be initialized in two ways, such as using two curly brackets ({}) and a comma (,) (c) (d) Arrays are a series of elements of the different data type placed Array must be declared after it is used

19.

Which one of the following is the correct built-in function to perform the following tasks: “Returns a true value if c is a lowercase letter, and 0 otherwise” (a) (b) (c) (d) int isdigit(int c); int istolower(int c); int islower(int c); int c;

20.

Which one of the following is TRUE? (a) (b) (c) char num = 1 and char num = ‘1’ are the same ASCII value for A is 64 A string may be assigned (in a declaration) to either a char array or to a char pointer (d) The word ‘NULL’, is the NULL keyword which indicates the end of the string

Page 7 of 14
Semester II 2010/2011 Principles of Programming

SECTION B: SHORT ANSWERS (6 QUESTIONS, 40 MARKS) Instruction: Please answer ALL the questions.

Question 1 (a) Convert the following arithmetic expression to C programming statements. (i) (ii) (iii) a = 3 [ ( x + y )-5 ] ÷ 4 x = 3 [ (a - b)(a + b) ] y = 5 [x ÷ ( y – 5 ) ] [3 marks]

(b)

Write the pseudocode for the given problems definition: (i) A program that converts pounds into kilograms. The program prompts the user to enter a number in pounds, converts it to kilograms, and displays the results. One pound is equivalent to 0.454 kilograms. [2 marks]

(ii)

A program that calculates one child’s allowance, based upon 75 cents per year age if s/he is under 10, and $1 per year if 10 or over. [2 marks]

Question 2 (a) Identify errors in the following program segment and show how the errors can be corrected.

Page 8 of 14
Semester II 2010/2011 Principles of Programming

#include<stdio.h> #define float x = 3.14 int main() { int struct, j, k, SUM=0; int i = 3; for(; i < 17 ; i += 7) { for(j = 9 ; j > 2; j -= 3); { printf("%d x %d =%d\n",i ,j ,i*j ); k = 2 % SUM; } } printf("The SUM is %d\n",sum); return 0; }

[5 marks] (b) Assume there are two variables declared as follows:
int my_var1 = ‘1’; char my_var2 = ‘2’;

What would be the output of the following statements? (i) (ii)
printf(“my_var1, my_var2”); printf(“%d %c, my_var1, my_var2”);

(iii) printf(“%d %c”, my_var1, my_var2); [3 marks]

Question 3 (a) Fill in the blanks, (numbered (i) – (viii)) with a value either TRUE or FALSE. x FALSE TRUE TRUE TRUE y FALSE TRUE FALSE TRUE x && y (i) (ii) (iii) (iv) !(x || y) (v) (vi) (vii) (viii) [4 marks]

Page 9 of 14
Semester II 2010/2011 Principles of Programming

(b)

What is the end-value of variables A and B?
B=3; A=++B;

[2 marks] (c) What is the result of the following statements?
7==5+2 ? 4 : 3; !(5>3) ? a : b; //statement 1 //statement 2

[2 marks]

Question 4 (a) Write an if statement for the following problems: (i) Assign 0 to x when y is equals to 20. [2 marks] (ii) For all employees over the age of 25 and before 50, bonus is increased by 5%. [3 marks] (b) Study the C code below:
int x; if (x % 2 == 0) { printf(“one\n”); x = x % 2; if ((x + 1) >= 5) { printf(“two\n”); } else { printf(“three\n”); } } else { printf(“none\n); }

Page 10 of 14
Semester II 2010/2011 Principles of Programming

What would be the output of the program given if x is equal to the following values? (a) (b) 5 8 [3 marks] Question 5 (a) Give ONE (1) example for each the following control structure in C. (i) (ii) Repetition Selection [2 marks] (b) Study the code below and determine the exact output when the break and continue keyword are applied in the C program. (i)
int x; for(x=1; x<=5; x++) { if(x== 4)break; printf("%d ", x); } printf("Broke out at x == %d\n",x);

[2 marks] (ii)
intx, y; for(x=1; x<=4; x++) { if(x==3) { y = x; continue; } printf("%d ", x); } printf("Skipped x == %d\n",y);

[2 marks]

Page 11 of 14
Semester II 2010/2011 Principles of Programming

(c)

Study the code below:
int element[5] = {5, 4, 3, -2, -1}; int i, sum=0; int average; for (i=0; i < 5; i++) { sum = sum + element[i]; } average = sum / 5;

What is the value of sum and average when the above sample code is executed? [3 marks]

Page 12 of 14
Semester II 2010/2011 Principles of Programming

SECTION C: DESIGNING/ PROGRAMMING (2 QUESTIONS, 30 MARKS) Instruction: Please answer ALL the questions. Question 1 The C program below make use of THREE (3) functions: getInput(), listInput() and calAvg().
int main(void) { int myArray[100], myNum; float avg; printf(“How many numbers to be entered? ”); scanf(“%d”, &myNum); getInput(myArray, myNum); listInput(myArray, myNum); avg = calAvg(myArray, myNum);

printf(“Average of numbers entered is = %.2f”, avg); return 0; }

(a)

Based on the program written above, write down the function prototypes for the THREE (3) functions: getInput(), listInput() and

calAvg(). [3 marks] (b) Define the function getInput(). In this function, ask the users to get a list of integers and store them in the array passed as the first argument. The number of integers to be entered is passed as the second argument. [4 marks]

(c)

Define the function listInput(). In this function, print out the integers inside the array passed in the first argument. The number of integers stored in the array is passed as the second argument. [4 marks] Page 13 of 14

Semester II 2010/2011

Principles of Programming

(d)

Write the function calAvg(). In this function, sum up all the integers inside the array, and calculate the average. The function must return the value which is the result of the average. [4 marks]

Question 2 Write a C program that contains the following processes: (a) Assign default values to string variables value1 and value2, for example: value1 =“threeFour“ value2=“Five Six “

(b)

Copy at most five characters of the value2 into the value1 and assign them into a new variable named newValue3. Write a printf() statement to display value of the newValue3 on the screen.

(c)

Calculate the length of string value in value3tmp and write a printf() statement to display the result on the screen.

(d)

What is the result for newValue3 and value3tmp when this program is executed?

[15 marks]

---End of Questions---

Page 14 of 14
Semester II 2010/2011 Principles of Programming