0% found this document useful (0 votes)
7 views

BCSC 1102 INTRODUCTION to PROGRAMMING - Department of Computing and Mathematics

Uploaded by

kintaxima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

BCSC 1102 INTRODUCTION to PROGRAMMING - Department of Computing and Mathematics

Uploaded by

kintaxima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The Co-operative University of Kenya

ORDINARY EXAMINATION DECEMBER 2022


EXAMINATION FOR THE BACHELOR OF SCIENCE COMPUTER SCIENCE IN
SOFTWARE ENGINEERING/ INFORMATION TECTHNOLOGY/ DATA SCIENCE
/ APPLIED DATA SCIENCE / STATISTICS AND INFORMATION TECHNOLOGY /
COMPUTER SCIENCE / BACHELOR OF BUSINESS INFORMATION
TECHNOLOGY
(YR I SEM I)
UNIT CODE: BCSC 1102

UNIT TITLE: INTRODUCTION TO PROGRAMMING

DATE: DECEMBER, 2022 TIME: 2HRS

INSTRUCTIONS:
 Answer question ONE (compulsory) and any other TWO questions

QUESTION ONE (30 MARKS)

a) What is the output of the following program


1. #include <stdio.h>
2. int main(void)
3. {
4. int a = 4, b = 5, c;
5. a = (a > 3) + (b <= 5);
6. b = (a == 3) + ((b-2) >= 3);
7. c = (b != 1);
8. printf("%d %d %d\n", a, b, c);
9. return 0;
10. } [6 Marks]
b) Write a program that declares two integers, assigns to them the values 40 and 60, and
displays their sum, difference, product, the precise result of their division, and the
remainder. To find the remainder use the % operator. [4 Marks]

c) Fix the errors in order to run the program and display the value of m. (Indicate the line
number where the bug and and propose a solution)

1. include stdio
2. int main(void);
3. (
4. int= m
5. b = 10
6. m = 2b+100
7. print(%f\n", M);
8. Return0;
9. ) [6 Marks]

1
d) What is the output of the following program?

1. #include <stdio.h>
2. int main(void)
3. {
4. int a = 4, b = 3, c = 5;
5. printf("%d\n",(a < b) == !(c > b));
6. return 0;
7. } [2 marks]
e) Write a program that reads an integer and displays a message to indicate whether it is
even or odd. [2 marks]

f) What is the output of the following program?


1. #include <stdio.h>
2. int main(void)
3. {
4. int a = 10, b = 20, c;
5. c = (a < 15) && (++b > 15);
6. printf("%d %d\n", c, b);
7. return 0;
8. {
[2 marks]

g) Write a program that reads a student’s grade and displays its corresponding
description, as follows:
18–20 Excellent
16–18 Very Good
13–16 Good
10–13 Dangerous Zone
0–10 Need Help [8 Marks]

QUESTION TWO (20 MARKS)


a. Write a program that reads a man’s height (in metres) and weight (in kilograms) and
calculates his body mass index (BMI) using the formula BMI=weight/height2. The
program should display the BMI and a corresponding message according to Table 1
below, and the lower and upper limit of the normal weight for the given height.
Table 1.

[10marks]
b. Define the structure type city with members: city name, country name, and
population. Write a program that uses this type to read the data of 80 cities and store
them in an array. Then, the program should read the name of a country and a number
and display the cities of that country whose population is greater than the input
number [10 Marks]

2
QUESTION THREE (20 MARKS)
a. What is the output of the following program?
1. #include <stdio.h>
2. int main(void)
3. {
4. unsigned int i = 10;
5. if((i >> 4) != 0)
6. printf("One\n");
7. else
8. printf("Two\n");
9. printf("%d\n", i);
10. i = 1;
11. if((i << 3) == 8)
12. printf("One\n");
13. else
14. printf("Two\n");
15. printf("%d\n", i);
16. return 0;
17. } [5 marks]
b. Write a function that takes as parameters two pointers to floats and swaps the values
they point to. (5 Marks)
c. Suppose you wish to give a loan to a person who is either over 21 years or has a bank
balance of Kes 90,000. The loan amount is half the bank balance. Write a program to
implement this. Do not use the IF or SWITCH STATEMENTS. (10 Marks)

QUESTION FOUR (20 MARKS)

d. What is the output of the following program?


1. #include <stdio.h>
2. int main(void)
3. {
4. int *ptr1, *ptr2, *ptr3, i = 10, j = 20, k = 30;
5. ptr1 = &i;
6. i = 100;
7. ptr2 = &j;
8. j = *ptr2 + *ptr1;
9. ptr3 = &k;
10. k = *ptr3 + *ptr2;
11. printf("%d %d %d\n", *ptr1, *ptr2);
12. return 0;
13. } [4 Marks]
e. Write a function that takes as parameters three values and returns the minimum of
them. [6 marks]

3
f. Write a program that will give the following display. Use a nested for loop.
1x0=0
1x1=1
1x2=2

2x0=0
2x1=2
2x2=3

3x0=0
3x1=3
3x2=6 [10 marks]

QUESTION FIVE (20 MARKS)

g. What is the output of the following program


1. #include <stdio.h>
2. void test(int *p, int a);
3. int main(void)
4. {
5. int i = 100, j = 200;
6. test(&i, j);
7. printf("%d %d\n", i, j);
8. return 0;
9. }
10. void test(int *p, int a)
11. {
12. *p = 300;
13. a = 400;
14. } [5 marks]

h. Write a program that reads the name of a file and displays its second line. [15 marks]

You might also like