Anda di halaman 1dari 16

UNIVERSITI TENAGA NASIONAL

College of Information Technology

BACHELOR OF ENGINEERING (HONS.)


MID TERM EXAMINATION
SEMESTER I 2007/2008
PRINCIPLES OF PROGRAMMING
(CSEB114)

24 August 2007

Time allowed: 2 hours

INSTRUCTIONS TO CANDIDATES
1.

The total marks for this exam is 100 marks.

2.

There are THREE (3) SECTIONS to this paper: Section A, Section B, and Section C.

3.

Answer ALL questions in this question paper.

4.

Please fill up the following information:


Student Name:

_______________________________________

Student ID:

_____________________ Section: __________

Lecturer Name:

_______________________________________

DO NOT OPEN THIS QUESTION PAPER UNTIL YOU ARE INSTRUCTED TO DO SO


THIS QUESTION PAPER CONSISTS OF 16 PRINTED PAGES INCLUDING THIS PAGE

SECTION A: MULTIPLE-CHOICE (20 QUESTIONS, 30 MARKS)


Instruction: Please select the BEST answer from the given choices. Circle your
answer.
1.

Which unit of the CPU is responsible for executing a multiplication operation?


(a) Arithmetic logic unit
(b) Registers
(c) Control Unit
(d) All of the above

2. Which of the following is INCORRECT about an algorithm?


(a)

A good algorithm will eventually end

(b)

A good algorithm can be translated into programming languages

(c)

A good algorithm can be run by a computer directly

(d)

A good algorithm is clear and precise

3. Which of the following is a VALID escape sequence?


(a)

\i

(b)

\%

(c)

\n

(d)

\d

4. Which of the following is an INVALID declaration of identifier?


(a)

5iCount

(b)

I5cOunT

(c)

5iCount_5iCount

(d)

iCount5

Page 2 of 16
Semester 1 2007/2008

Principles of Programming

5. What will be the output of the following program?


inta=2;
switch(a)
{
case1:
printf("case1!");
case2:
printf("case2!");
//continue;
case3:
printf("case3!");
break;
default:
printf("defaultcase!");

6.

7.

(a)

case 1!case 2!default case!

(b)

case 2!case 3!

(c)

case 2!case 3!default case!

(d)

Compilation error

Which of the following is not a basic data types in C?


(a)

char

(b)

double

(c)

string

(d)

int

What will be the output of the following program?


int i = 2,j = 10;
if ((3 * i) < j){
i = i + 2;
i = i --;
}
printf("i is %d\n",i);
(a)

i is 3

(b)

i is 5

(c)

i is 9

(d)

i is 10

Page 3 of 16
Semester 1 2007/2008

Principles of Programming

8.

Choose the CORRECT sequence of steps in software life cycle.


i.
ii.
iii.
iv.
v.
vi.

9.

Program Documentation
Implementation
Requirements Specification
Analysis
Design of algorithm
Program Testing and verification

(a)

iii, iv, v, ii, vi, i

(b)

iii, iv, v, ii, i, vi

(c)

iv, iii, v, ii, vi, i

(d)

iv, iii, v, ii, i, vi

How many times will the C program fragment below display the text Hello?
inti=3;
do{
printf("Hello");
i++;
}while(i<3);

10.

(a)

(b)

(c)

(d)

Infinite

What will the C program fragment below display on screen?


inti;

11.

(a)

for(i=0;i<5;i++)
printf("%i,",i);
i++;
printf("%i,",i);
0, 1, 2, 3, 4

(b)

0, 1, 2, 3, 4, 5,

(c)

0, 1, 2, 3, 4, 6,

(d)

0, 1, 2, 3, 4, 5, 6,

A peripheral device that enables a computer to pass information to humans or


Page 4 of 16

Semester 1 2007/2008

Principles of Programming

other machine is
(a) Input device
(b) Output device

12.

(c)

Memory

(d)

CPU

Which statement is TRUE?


(a) In declaring a variable, the initial value of the variable has to be specified.
(b) Address operator (&) is required in using scanf() to read and store data into
a variable.
(c) The value of a constant in C program can be changed using assignment
operator (=).
(d) The value of a variable is stored in secondary memory during program
execution.

13.

14.

Every statement in C program must end with a


(a)

period (.)

(b)

backslash (/)

(c)

colon (:)

(d)

semicolon (;)

Choose among the operator of C language below that has the LOWEST
precedence level.

15.

(a)

(b)

++

(c)

(d)

Which of the following keywords of C language is relevant to SELECTIVE


CONTROL structure?
(i) ifelse (ii) switchcase

(iii) while

(iv) dowhile

(v) for

Page 5 of 16
Semester 1 2007/2008

Principles of Programming

16.

(a)

(b)

i and ii

(c)

iii and iv

(d)

iii, iv and iv

How many times will the C program fragment below display the text loop?
x=0;
while(x<=5)
printf("loop");

17.

18.

19.

(a)

(b)

(c)

(d)

Infinite

Microsoft Word would be best categorized as a/an


(a)

System Software

(b)

Operating Systems

(c)

Application Software

(d)

Compiler

Which of the following statement is VALID?


(a)

C is a procedural language

(b)

C is a third generation language

(c)

C is a case sensitive language

(d)

All of the above

Choose the CORRECT sequence of steps in developing a C program.


i. Compile

ii. Execute

iii. Link

iv. Edit

Page 6 of 16
Semester 1 2007/2008

Principles of Programming

20.

(a)

i, ii, iii, iv

(b)

i, iii, ii, iv

(c)

vi, i, ii, iii

(d)

vi, i, iii, ii

What will the C program fragment below display on screen?


floata=4.0,b=5.0,c=4.0;
printf("%.2f",(++a+b*c));
(a)

25

(b)

25.00

(c)

24

(d)

24.00

SECTION B: SHORT ANSWERS (4 QUESTIONS, 40 MARKS)


Instruction: Please answer ALL the questions. DO NOT show any working
unless stated by the question. Answer in the spaces provided.
Page 7 of 16
Semester 1 2007/2008

Principles of Programming

Question 1
(a)

Rewrite the following C program fragment using ifelse statements without


changing its underlying logic:
switch(color)
{
case'r':
printf("RED");
break;
case'g':
printf("GREEN");
break;
case'b':
printf("BLUE");
break;
default:
printf("INVALIDINPUT!");
}

[4 marks]

Page 8 of 16
Semester 1 2007/2008

Principles of Programming

(b)

Trace the output of the following C program.


#include<stdio.h>
int main()
{
int a=5,b=6;
b=b++ - --a;
printf("a is %d\n",a);
printf("b is %d\n",b);
return 0;
}
[2 marks]

(c)

Briefly describe ONE major difference between dowhile and while


statement.
[4 marks]

Page 9 of 16
Semester 1 2007/2008

Principles of Programming

Question 2
(a)

Trace the output of the C program fragment below.


x=0;
do{
printf("%d, ", x);
x=x+3;
}while(x <= 20);
[4 marks]

(b)

Rewrite the C program fragment in (a) using for statement without changing
its original logic.
[4 marks]

(c)

Rewrite the C program fragment in (a) using while statement without changing
the original logic.
[2 marks]

Page 10 of 16
Semester 1 2007/2008

Principles of Programming

Question 3
You are given the following C program.
#include <stdio.h>
void main()
{
int k, sum=1;
for(k = 1; k < 7; k++)
{
sum = sum * k;
if(sum == 24)
break;
}
printf("%i \n", sum);
}
(a)

Trace the output of the above C program?


[3 marks]

(b)

What is the output if the keyword break is changed to continue?


[3 marks]

(c) Briefly describe ONE major difference between the break and continue statement
in looping.
[4 marks]

Page 11 of 16
Semester 1 2007/2008

Principles of Programming

Question 4
(a)

Convert this expression into a simple if-else statement.


Reciprocal = ( ( x == 0 ) ?

0 : 1 / x );
[5 marks]

(b)

Write a C code fragment using only ONE for and ONE printf statement to
print the following series of number
36, 33, 30, 27, 24,
[5 marks]

Page 12 of 16
Semester 1 2007/2008

Principles of Programming

SECTION C: DESIGNING/ PROGRAMMING (2 QUESTIONS, 30 MARKS)


Instruction: Please answer ALL the questions. Answer in the spaces provided.
Question 1
Write a COMPLETE C PROGRAM to compute the result of n factorial (n!) defined
as:
n! = 1 x 2 x 3 x 4 x n
The program must read the value n from user and validate that it is an integer range
from 1 to 20. The program must continue to prompt user for the value of n as long as
it is not within the valid range. Refer to the example of input-output below as a guide.
Example:
Enter n: 0
Invalid n! n must be within 1 to 20
Enter n: 21
Invalid n! n must be within 1 to 20
Enter n: 4
The result of 4 factorial is 24

[15 marks]
[15 marks]

Page 13 of 16
Semester 1 2007/2008

Principles of Programming

Page 14 of 16
Semester 1 2007/2008

Principles of Programming

Question 2
DESIGN an algorithm using PSEUDOCODE to compute the average of a list of
scores entered by user. User would enter a negative score to indicate the end of list.
The scores must be within the range of 0 to 100 or else, the program should display a
message Invalid score, please enter again! You may assume that the user would
enter a valid score during the first entry. Refer to the example of input-output below
as guide.
Example:
Enter score [enter negative score to exit]:20
Enter score [enter negative score to exit]:101
Invalid score, please enter again!
Enter score [enter negative score to exit]:10
Enter score [enter negative score to exit]:30
Enter score [enter negative score to exit]:-1
The average score is 20.0
[15 marks]

Page 15 of 16
Semester 1 2007/2008

Principles of Programming

---End of Questions--Page 16 of 16
Semester 1 2007/2008

Principles of Programming