BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering Cohort: BCNS/07/FT- BIS/07/FT- BIS/06/FT -BM/07/FT BSE/07/FT/PT- BSE/06/FT BSE/05/PT Examinations for 2007-2008 / Semester 1 Resit Examinations for 2006-2007 / Semester 1 MODULE: PROGRAMMING ESSENTIALS MODULE CODE: PRG101/PROG1101/BISE011 Duration: 2 Hours 15 minutes Instructions to Candidates: 1. Answer all questions using C as programming language. 2. Questions may be answered in any order but your answers must show the question number clearly. 3. Always start a new question on a fresh page. 4. All questions carry equal marks. 5. Total marks 100. This question paper contains 4 questions and 7 pages. Page 1 of 7
ANSWER ALL QUESTIONS QUESTION 1: (25 MARKS) (a) (i) Name and describe the four basic data types in C. (ii) Name and describe the four basic types of constants in C. (iii) What is an escape sequence in C? What is its purpose in C programs? (6 marks) (b) (i) Briefly describe the five arithmetic operators in C. (ii) Describe the two equality operators included in C. How do they differ from relational operators? (iii) What are arguments and how are they written? How is a call to a library function written if there are no arguments? (9 marks) (c) A C program contains the following declarations and initial assignments: int i = 7, j = 6; float x = 0.009, y = -0.03; char c = c, d = d ; Now, determine the value of each of the following expressions. (i) (3 * i 2 * j) % (2 * d c) (ii) 2 * ((i / 5) + (4 * (j 3)) % (i + j -2)) (iii) (i 3 * j) % (c + 2 * d) / (x y) (iv) 2 * x + y = = 0 (v) (x > y) && (i > 0) && (j < 5) [Note:- ASCII value of c =99, d =100] (10 marks) Page 2 of 7
QUESTION 2: (25 MARKS) (a) (i) What is the purpose of the scanf function? How is it used within a C program? (ii) What is the purpose of the printf function? How is it used within a C program? (iii) In what ways does the control string within a printf function differ from the control string within a scanf function? (b) A C program contains the following statements: #include <stdio.h> int i, j, k; Write an appropriate scanf function to enter numerical values for i, j, and k assuming the following scenarios:- (i) The values for i, j and k will be decimal integers not exceeding seven characters each. (ii) The value for i will be a decimal integer, j an octal integer and k an hexadecimal integer, with each quantity not exceeding eight characters. (iii) The values for i and j will be hexadecimal integers and k will be an octal integer. Each quantity will be seven or fewer characters. (c) A C program contains the following statements: #include <stdio.h> int i, j; long ix; short s; unsigned u; float x; double dx; char c; Write an appropriate scanf function to accommodate each of the following situations, assuming that integers will be read in as decimal quantities. (Continued) Page 3 of 7
Question 2(c) : (continued) (i) Enter values for i, j, x and dx, assuming that each integer quantity does not exceed four characters, the floating-point quantity does not exceed eight characters, and the double-precision quantity does not exceed 15 characters. (ii) Enter values for i, ix, j, x and u, assuming that each integer quantity does not exceed five characters, the long integer does not exceed 12 characters, and the floating-point quantity does not exceed ten characters. (iii) Enter values for i, u and c, assuming that each integer quantity does not exceed six characters. (9 marks) (d) A C program contains the following variable declarations: float a = 1.5, b = 0.0006, c = 2000.; Write down the output resulting from each of the following printf statements: a) printf("%f %f %f", a, b, c); b) printf( "%3f %3f %3f", a, b, c); c) printf( "%8f %8f %8f", a, b, c); d) printf("%8.4f %8.4f %8.4f", a, b, c); e) printf( "%8.3f %8.3f %8.3f", a, b, c); f) printf("%e %e %e", a, b, c); g) printf("%3e %3e %3e", a, b, c); h) printf("%12e %12e %12e", a, b, c); i) printf("%12.4e %12.4e %12.4e", a, b, c); j) printf("%8.2e %8.2e %8.2e", a, b, c); (10 marks) Page 4 of 7
QUESTION 3: (25 MARKS) (a) What is the purpose of the if-else statement? Describe the two different forms of the if-else statement. How do they differ? (b) (i) What is the purpose of the return statement? (ii) What are the 2 principal components of a function definition? (c) Write the first line of the function definition, including the formal argument declarations, for each of the situations described below:- (i) A function called sample that generates and returns an integer quantity. (ii) A function root accepts two integer arguments and returns a floating point result. (iii) A function convert accepts a character and returns a character. (iv) A function called transfer accepts a long integer and returns a character. (v) A function inverse accepts a character and returns a long integer. (5 marks) (d) (i) What is the purpose of the switch statement? (ii) Write a simple C program using the switch to prompt a user to enter any one of the three characters r or w or b. The program displays red if r is entered, it displays white if w is entered or blue if b is entered. If one of the three characters is not entered then the default case executes to print Error. (6 marks) (Continued) Page 5 of 7
Question 3 : (continued) (e) What will be the outputs generated in the following programs. Explain fully all your answers. (i) #include <stdio.h> main( ){ int i=0,x=0; while (i < 20){ if(i%5 == 0){ x += i; printf( %d, x); ++i; printf( \n x= %d, x); (4 marks) (ii) #include <stdio.h> main( ){ int i, j, k, x = 0; for(i=0; i<5; ++i) for(j=0; j<i; ++j){ k = (i + j 1); if (k % 2 == 0) x += k; else if (k % 3 == 0) x += k 2; printf( %d, x); printf( \n x = %d, x); (4 marks) Page 6 of 7
QUESTION 4: (25 MARKS) (a) (i) What are array subscripts? How are they written? What restrictions apply to the values that can be assigned to subscripts? (ii) When passing an array to a function, how must the array argument be written? How is the corresponding formal argument written? (iii) If an array is passed to a function and several of its elements are altered within the function, are these changes recognized in the calling portion of the program? Explain fully. (8 marks) (b) (c) Describe the array that is defined in each of the following. Indicate what values are assigned to the individual array elements. (i) float c[8] = {2., 5., 3., -4., 12., 12., 0., 8.; (ii) float c[8] = {2., 5., 3., -4.; (iii) int z[12] = {0, 0, 8, 0, 0, 6; (6 marks) Explain the meaning of each of the following declarations. (i) int *p; (ii) int *p[10]; (iii) int (*p)[10]; (iv) int *p(void); (v) int p(char *a); (5 marks) (d) A C program contains the following declaration. static int x[8] = {10, 20, 30, 40, 50, 60, 70, 80; (i) What is the meaning of x? (ii) What is the meaning of (x + 2)? (iii) What is the value of *x? (iv) What is the value of (*x + 2)? (6 marks) ***END OF QUESTION PAPER*** Page 7 of 7