Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

KABARAK UNIVERSITY

UNIVERSITY EXAMINATIONS
MAIN CAMPUS
SECOND SEMESTER 2020/2021 ACADEMIC YEAR
EXAMINATION FOR THE DEGREE OF BACHELOR OF SCIENCE IN
CS/CSF/ACTS/TLCM

COMP 120 STRUCTURED PROGRAMMING WITH C

STREAM: YIS1 TIME: 2:00-4:00PM


EXAMINATION SESSION: JAN.-APRIL DATE: 20/04/2021

INSTRUCTIONS TO CANDIDATES
1. Answer Question 1 and any other two questions in the answer booklet provided.
2. Do not write on your question papers. All rough work should be done in your
answer booklet.
3. Clearly indicate which question you are answering.
4. Write neatly and legibly.
5. Edit your work for language and grammar errors.
6. Follow all the instructions in the answer booklet

As members of Kabarak University family, we purpose at all times and in all places, to set apart in one’s heart,
Jesus as Lord. (1 Peter 3:15)
Kabarak University is ISO 9001:2015 Certified
Page 1 of 6
QUESTION ONE (30 MARKS)
a) Explain why C language is still a powerful language by giving three examples of its
usage (3 Marks)
b) Give the differences between the following terms: (6 Marks)
i) Call by valueand Call by reference
ii) Actualparametersand Formal parameters
c) Write acomplete program to demonstrate the use of call by value with a return value.
(6 Marks)
d) Write a program in C language to compute and display the area of a circle based on the
radius given by the user. (4 Marks)
e) i) Explain the differences between a whileloop and a do-while loop(4Marks)
ii) Write a program in C to display the following output using a while loop (5 Marks)
Value of a is 0
Value of a is 1
Value of a is 2
Value of a is 3
Value of a is 4
f) Explain why Array is useful in C Programming. (2 Marks)

As members of Kabarak University family, we purpose at all times and in all places, to set apart in one’s heart,
Jesus as Lord. (1 Peter 3:15)
Kabarak University is ISO 9001:2015 Certified
Page 2 of 6
QUESTION TWO (20 MARKS)
a) What would be the exact output of the following code shown below? (4 Marks)
#include<stdio.h>
int main()
{
int array [3] = {26, 10, 8};
array[2] = 30;
printf("First Element contains %d\n", array[0]);
printf("Second Element contains %d\n", array[1]);
printf("Third Element contains %d\n", array[2]);
return 0;
}

b) Use a for loop to write a code in C that would display exactly the following output:
1, 2, 3, 4, 5, 6, (4 Marks)
c) Write an If....Else statement to display the following student Marks and grades
MARKS GRADE (6 Marks)
70 -100 A
60 - 69 B
50 - 59 C
40 - 49 D
0 - 39 F

d) Explain THREE major types of errors in C programs (6 Marks)

As members of Kabarak University family, we purpose at all times and in all places, to set apart in one’s heart,
Jesus as Lord. (1 Peter 3:15)
Kabarak University is ISO 9001:2015 Certified
Page 3 of 6
QUESTION THREE (20 MARKS)
a) Explain any 4 types of tokens available in C programming language (4 Marks)
b) Write a program in C to test if a given number is odd or even (4 Marks)
c) Study the following code and determine the output after execution (4 Marks)
#include<stdio.h>
int main()
{ int j=0;
do
{ printf("Value of variable j is: %d\n", j);
j++;
}
while (j<=8);
return 0;
}
d) Explain four categories of user defined functions. (4 Marks)
e)Show the exact output after execution of the following code: (4 Marks)
#include<stdio.h>
void swap(int *a, int *b);
int main()
{
int a = 22, b = 44;
printf("values before swap a = %d \n and b = %d",a,b);
swap(&a, &b);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;

As members of Kabarak University family, we purpose at all times and in all places, to set apart in one’s heart,
Jesus as Lord. (1 Peter 3:15)
Kabarak University is ISO 9001:2015 Certified
Page 4 of 6
printf("\n values after swap a = %d \nand b = %d", *a, *b);
}
QUESTION FOUR (20 MARKS)
i. What is apointer in C language? (2 Marks)
ii. Show the output of the following code below: (4 Marks)
#include <stdio.h>
int main()
{
int num = 10;
int *ptr;
ptr = &num;
printf("Value of variable num is: %d\n", *ptr);
return 0;
}
iii) Show with one line of code how you woulddisplay the address of the variable num
(3 Marks)
a) What would be the exact output of the following code written in C language? (4 Marks)
#include <stdio.h>
void display(int a, int b)
{
printf("%d\n", a);
printf("%d\n", b);
}

int main()
{
int Array[] = {2, 8, 4, 12};
display(Array[1], Array[2]);
return 0;
}
b) Write a C program to find the length of the following string e. g “Kabarak Campus”

As members of Kabarak University family, we purpose at all times and in all places, to set apart in one’s heart,
Jesus as Lord. (1 Peter 3:15)
Kabarak University is ISO 9001:2015 Certified
Page 5 of 6
(7 Marks)
QUESTION 5 (20 MARKS)
a) Given the following declarations: char array1 [] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'},
write a program in C where main()function will pass these elements one by one to
another function called display()whose job is to display them one by one. (6 Marks)
b) Write a code to compute a square root of a number given by the user (4 Marks)
c) Using nested if statements, write a code to produce the following output: (4 Marks)
4 is less than 1
and A is equal to A
But 1 is not equal to 0
d) Show the exact output of the following given code: (4 Marks)
#include <stdio.h>
int main()
{ int i=2;
switch (i)
{
case 1:
printf("Case1 ");
case 2:
printf("Case2 ");
case 3:
printf("Case3 ");
case 4:
printf("Case4 ");
default:
printf("Default ");
}
return 0;
}
ii) Explain why your output looks like that (2 Marks)

As members of Kabarak University family, we purpose at all times and in all places, to set apart in one’s heart,
Jesus as Lord. (1 Peter 3:15)
Kabarak University is ISO 9001:2015 Certified
Page 6 of 6

You might also like