Sie sind auf Seite 1von 6

KABARAK UNIVERSITY

UNIVERSITY EXAMINATIONS
MAIN CAMPUS

THIRD SEMESTER, 2015/2016 ACADEMIC YEAR

EXAMINATION FOR THE BACHELOR OF SCIENCE COMPUTER SCIENCE

COMP120: STRUCTURED PROGRAMMING

STREAM: Y1S2 TIME: 11.30-1.30 P.M

EXAMINATION SESSION : AUGUST DATE:11/08/ 2016

INSTRUCTIONS

1. Section A is compulsory, and then Attempt any other two questions from section B:
2. Do not write anything on this question paper.

SECTION A: (30 marks)


a. i) Explain the differences between the following two types of functions in C

programming: (4 marks)

1. Standard library functions


2. User-defined functions.

ii) Briefly explain three advantages of User-defined functions. (3 marks)

b. Differentiate between the following decision loops: (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)
i. while loop
ii) do – while loop

c. Show the output of the following two codes given in (i) and (ii) below. (4 marks)
i) include<stdio.h>
main ( )
{
int i = 0 ;

while (i > 2)

{
printf(“%d”, i);

i++;
}

return 0; }
ii) #include<stdio.h>
main( )
{
int i = 1 ;

do

{
printf(“value of i is %d”, i);

i++;
}

while (i > 3);

return 0;
}

d. State the output of C code given below (4 marks)


#include<stdio.h>
#define LINE “_______________”
#define TITLE “My program”
int main()
{

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)
printf (“\n\t %s \n\t %s \n”, LINE, TITLE);
printf(“\t %s \n”, LINE);
return 0;
}

e. i) Explain what a pointer is as used in C language. (2 marks)

ii) Identify three ways on how pointers are used. (3 marks)

f. i) Using C language, write a program that would test and display if an integer entered by
the user is ODD or EVEN (4 marks)

ii) As in e(i) above, use a conditional operator to write a programthat would do the

same task (4 marks)

SECTION B

Question 2 (20 marks)


a. What would be the output of the following C code? (2 marks)
#include<stdio.h>

main( )

int x = 10, y = 20 ;

if ( x == y )

printf ( "\n%d %d", x, y ) ;}

b. Write a program using nested if-else to check if a numberentered is Positive or Negative


(8 marks)
c. Explain the differences between the following two keywords: break and continue
(2 marks)
d. As an example, draw a flowchart that demonstrate the use of the keyword: continue
(4 marks)
e. Explain four differences between switch case and else if ladder in C language (4 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)
QUESTION THREE(20 marks)

a. Write a program to calculate the sum of 10 numbers entered from the keyboard. Negative
numbers will not be included in the calculation. (Use a for-loop) (8 marks)

b. Draw a switch-case Statement Flowchart based on the following switch casesyntax:


(6 marks)
switch (n)
{
case constant1:
// code to be executed if n is equal to constant1;
break;

case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}

c. Write a program just to print/display an integer entered by a user, and show the output
(4 marks)

QUESTION FOUR (20 marks)


a. What would be the output of the following C code? (2 marks)
#include<stdio.h>
#include<string.h>
char main()
{
char string_a [ ] = "Richard";
char string_b [ ] = "Philiph";
strcpy (string_a, string_b);

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)
printf("string_a is %s\n", string_a);
printf("string_b is %s", string_b);
}
b. Using conditional operator, write a programto find the biggest number between two
integer numbers entered by the user. (4 marks)
c. Based on file concepts, explain the differences between the following two terms:
(2 marks)
i) fclose(a) (ii) fcloseall()
d. Using User defined function, write a program to add two integer numbers entered by the
user. The main() function should pass those two numbers to a function called
addNumbers() that is going to add the two numbers together and return the result to the
main() function to display on the screen (6 marks)
e. Explain the concepts of the following C code and show the output (4 marks)

#include<stdio.h>
int A;
int B;
int Add()
{ return A + B;
}
int main()
{ int answer;
A = 5;
B = 7;
answer = Add();
printf("%d\n",answer);
return 0;
}

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)
f. What would be the output of the following c code? (2 marks)

#include <stdio.h>

int main () {

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};


printf("Greeting message: %s\n", greeting );
return 0;
}

QUESTION FIVE (20 marks)

a. i) Describe what an array is C programming (2 marks)


ii) Explain four ways that can be used to initialize an array (4 marks)
iii)How do elements of a given array be accessed? (3 marks)

b. Using a pointer, write a program in C that would print out the values of two declared
integer variables along with their addresses (6 marks)
c. Write a program using C language that would be used to show the net pay of an
employee based on the following information: Nhif = ks. 200.50 andTax is 30% of
the gross pay. Restrict the net pay to two places of decimal. (5 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)

Das könnte Ihnen auch gefallen