You are on page 1of 5

KABARAK UNIVERSITY

UNIVERSITY EXAMINATIONS
2015 / 2016 ACADEMIC YEAR
FOR THE DEGREE OF BACHELOR OF COMPUTER SCIENCE
COMP 120: STRUCTURED PROGRAMMING

DAY: THURSDAY DATE: 10/12/2015


TIME: 09:00 – 11:00AM STREAM: Y1S2
INSTRUCTIONS:

1. Section A: is compulsory, and attempt any other two questions


from section B:
2. Do not write anything on this question paper.

SECTION A:
QUESTION ONE [30 marks]
a. Identify any four keywords used in C language (4 marks)

b. i) Explain the use of the following format specifiers as used in C: (2 marks)

(1) %d (2) %f (3) %s (4) %c

c. ii) Look at the code below and show how you would display the output with only two
places of decimal? (2 marks)

int main ( )

{ float a = 10.250000;
printf(“the value of a is %f”, a);
}

Page 1 of 5
d . Explain each of the following data types as used in C programming language: (2 marks)

i) int (ii) char (iii) float (iv) double

e. Explain the differences between a variable and a constant as used in C language (2 marks)

f. Explain the differences between Formal parameters and Actual parameters (4 marks)

g. Identify the differences between the following two terms in relation to functions: (2 marks)

Predefined standard library functions and User Defined functions

h. Write a program that would determine if a character entered is a vowel or not by entering
any one character and showing some output (6 marks)

i) what is an array? (2 marks)

j. The following program has some errors, make the corrections and show the output. (4 marks)

int add(int d, int e);


main( )
{
int a, b, c;
c = add(a, b);
printf ( "value of c is %d", c ) ;
}
int add(int x, int y)
{ int p;
p = e*d;
return p;}

SECTION B:
QUESTION TWO (20 marks)
a) i) Outline six benefits of pointers as used in C language (3 marks)
ii) Show how you would declare and initialize a pointer in C- language (3 marks)

b. i) Show how you would declare and initialize an integer array of four elements. (2 marks)

Page 2 of 5
ii) Write a C code to display the output of the above mentioned elements in b(ii) (4 marks)

iii) What would happen if the size of an array is omitted during declaration? (2 marks)

c. Explain the rules for writing variable names in C language (4 marks)

d. Differentiate between a keyword and an identifier as used in C language (2 marks)

QUESTION THREE (20marks)

a. Sketch a simple code to show how each of the following operators are used in C
language: (6 marks)

i) Arithmetic operators
ii) Assignment operators
iii) Relational operators

b. Study the following two codes written in C and answer the questions that follow:
i) int main()
{
int a = 0;
while(a < 3)
{
printf("loop iteration is %d\n", a);
if(a == 2) break;
a++;
printf(" go to the next round\n");
}
return 0;
}
ii) int main()
{
int a = 0;

while(a < 6)
{
a++;

Page 3 of 5
if(a == 4) continue;
printf("loop iteration is %d\n", a);

printf(" go to the next round\n");


}
return 0;
}

i) Show the output of each code (6 marks)


ii) Explain the differences between them (4 marks)

c. Show the output of the following C code: (4 marks)


int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}
while (j<=8);
return 0;
}

QUESTION FOUR (20 marks)

a. Identify and explain three types of decision making in relation to if control statements in C

language. (6 marks)

b. Explain the nature of the following code, and state the output. (6 marks)

#include <stdio.h>
void count(int x);
int main()
{
int num;

Page 4 of 5
printf("Enter a positive integer:");
scanf("%d",&num);
count(num);
}
void count(int x)
{
printf("%d\n", x);
--x;
if(x < 0)
return;
else
count(x);
}
c. Write a C code to show how to find the memory size of a given integer variable (6 marks)
d. Explain the importance of using comments in C programming (2 marks)

QUESTION FIVE (20 marks)

a. Write a program in C whereby the main() function would call another function to print/display
exactly the

following output: (6 marks)

Hi!
My name is Tom
How are you?

b. Construct a C code to show how to swap two integer numbers entered from the keyboard
(6 marks)
c. Using a conditional operator, write a program in C to find if a number entered from the

keyboard is odd or even. (8 marks)

Page 5 of 5