0% found this document useful (0 votes)
16 views8 pages

MOI University COE 162 Exam Paper

This document is an examination paper for the course 'Introduction to Computer Programming' at Moi University for the academic year 2010/2011. It consists of seven questions covering various programming concepts, C language features, and practical coding tasks. Students are required to answer question one and any other four questions from the paper.

Uploaded by

frashawangeshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

MOI University COE 162 Exam Paper

This document is an examination paper for the course 'Introduction to Computer Programming' at Moi University for the academic year 2010/2011. It consists of seven questions covering various programming concepts, C language features, and practical coding tasks. Students are required to answer question one and any other four questions from the paper.

Uploaded by

frashawangeshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MOI UNIVERSITY

OFFICE OF THE CHIEF ACADEMIC OFFICER

UNIVERSITY EXAMINATIONS
2010/2011 ACADEMIC YEAR
FIRST YEAR SECOND SEMESTER EXAMINATIONS
FOR THE DEGREE OF
BACHELOR OF TECHNOLOGY
IN
COMPUTER ENGINEERING
COURSE CODE: COE 162

COURSE TITLE: INTRODUCTION TO COMPUTER PROGRAMMING


DATE: TIME:
INSTRUCTIONS TO CANDIDATES
1. THIS PAPER CONTAINS SEVEN (7) QUESTIONS
2. QUESTION ONE (1) IS COMPULSORY. ATTEMPT ANY OTHER FOUR
QUESTIONS.
THIS PAPER CONSISTS OF (8) PRINTED PAGES
PLEASE TURN OVER
COE 162: Introduction to Programming Semester II Academic year: 2010/11

Question One (Compulsory)


a) Distinguish between the following programming concepts:

i) Objects and Operations


ii) Compilers and Interpreters
iii) Procedural and Non-Procedural languages
[3 marks]
b) How do the following characteristics improve the quality of a program?

i) Modularity
ii) Localization
iii) Delayed decisions
[3 marks]
c) Draw a flowchart to depict the following examination processing scenario:

i) The input consists of students mark in a given course;


ii) Complain if the mark is below 0 or above 100;
iii) Determine the grade from the supplied mark (>=70=A; <40 = C; otherwise B)
iv) Print grade
[4 marks]
c) Explain the output when you execute the following C code.

#include<stdio.h>
void main(){
int a=5;
{
int b=10;
++b;
++a;
{
int a=20;
++a;
a=++b;
}
++a;
++b;
printf("%d %d",a,b);
}
printf(" %d",a);

[3 marks]

d) i) Accounting software has to deal with money in amounts exceeding a trillion dollars
and yet keep the arithmetic accurate to the penny. What kind of number that C
supports would be adequate?
[1 mark]
ii) Write a simple C program that requests three floating point numbers, and prints the
sum and product of their square roots to three decimal places.
[4 marks]
2
COE 162: Introduction to Programming Semester II Academic year: 2010/11

Question Two
a) List four important features of C language.
[2 marks]
b) What is wrong with the variable declaration and assignment in the following programs?

i)
#include<stdio.h>
int main(){
int goto=5;
printf("%d",goto);
return 0;
}
[1 mark]
ii)
#include<stdio.h>
int main(){
int __BIG__ = 32;
int y;
y= __BIG__ && 8;
printf("%d",y);
return 0;
}
[2 marks]
c) By means of a simple illustration, show the difference between local variable and global
variable as used in C programming.
[4 marks]
d)
i) If n is an integer variable, what is the value of the expression n % 8 when n has the
value 24?
[1 mark]
ii) What is the value of the C expression 2 * 6 - 5 + 8 % 5
[1 mark]
iii) What will be printed when the following segment of code is executed as part of a
program?

int i, total ;

for (i = 1 ; i = 15 ;i = i + 1)
{ if ( i % 3 == 0)
{
printf("%d ", i) ;
}
}
printf("\n\n");
[2 marks]

3
COE 162: Introduction to Programming Semester II Academic year: 2010/11

Question Three
a) What is the difference between the expressions x++ and ++x?
[2 marks]
b) Modify the program to achieve the same effect with a conditional expression instead of
if….else statement.

#include <stdio.h>

main()
{
int n;

printf("Enter an integer: ");


scanf("%i", &n);
printf("\nYou entered %i", n);
if (n >= 0)
printf(", which isn't negative.\n");
else
printf(", which is negative.\n");
}
[4 marks]
c) A C program contains the following statements:

#include<stdio.h>
float a,b,c;

Write printf function for each of the following groups of variables or expressions, using
f-type conversion for each floating-point quantity.

i) a, b and c with a minimum field width of 5 characters per quantity, with no more than
three decimal places.
ii) Sqrt (a+b), abs(a-c), with a minimum field width of 8 characters for the first quantity
and 6 characters for the second. Display a maximum of four decimal places for each
quantity.
iii) (a+b)+ (a-c), with a minimum field width of 7characters for the first quantity and 6
characters for the second.
[7 marks]

Question Four
a) Write a for loop that will compute the sum of every third integer beginning with i=5 (i.e,
calculate the sum 5+8+11+…) for all values of i less than 40.
[4 marks]
e) i) Clearly explain the difference between the while loop and do while loop.
[2 marks]

4
COE 162: Introduction to Programming Semester II Academic year: 2010/11

ii) Rewrite the following while loop as a do while loop.


#include <stdio.h>
main()
{
int m = 0, n;
puts("Enter a small integer (e.g. 10): ");
scanf("%i", &n);

while (m < n)
{
putchar('*');
m++;
if (m % 20 == 0) /* newline after every 20 */
putchar('\n');
}

putchar('\n');
}

[2 marks]

c) Write a program to print out all the prime numbers up to 50. Reminder: prime numbers
are integers greater than one with no factors other than themselves and one.

HINT: use the % operator to determine whether one number is a factor of another.

[5 marks]
Question Five
a) Explain the relationship between the data item represented by a variable v and the
corresponding variable pv.
[2 marks]
b) An implementation of pointers in C is shown below.
Void funct(int *p)

main()
{
Static int a[5]= (20,30,40,50,60);
.....
funct(a);
.....

}
Void funct(int *p)
{
int i, sum=0;
for(i=0; i<5;++i)
sum+=*(p+1);
printf(“sum=%d”,sum);
}

5
COE 162: Introduction to Programming Semester II Academic year: 2010/11

Determine:
i) The kind of argument passed to funct
[1 mark]
ii) The kind of information returned by funct
[1 mark]
iii) The value displayed by printf statement within funct
[2 marks]

c) i) Define the following array concepts: subscript, dimensionality of an array and passing
an array to a function by reference.
[3 marks]
ii) Suppose y is an array of integers, and we have just executed this code:

for(i=0;i<5;i++)
y[i] = i*i;

Suppose that y[0] is stored at address 3500. What is the value of each of the following
expressions?
 &y[0]
 y[1]
 &y[1]
 *(&y[2] +1)

[4 marks]
Question Six
a) Below is a sketchy outline of a C program.
#include<stdio.h>
main()
{
file *pt1, *pt2;

int x;
float y;
char z;
fpt= fopen(“[Link]”,”r”);
fpt=fopen(‘[Link]”,”w”);
.........
fclose (pt1);
fclose(pt2);
}

Write C statements to do the following:


i) Read the values of x,y and z from the file [Link]
[1 mark]
ii) Display each value on the screen and enter an updated value.
[2 marks]
iii) Write the new values to the data file [Link]
[1 mark]

6
COE 162: Introduction to Programming Semester II Academic year: 2010/11

b) Correct the following code, if necessary, to remove run-time or compile-time errors.


Change the code as little as possible. If no change is needed, just say so.
void main(void)
{ char x[3];
strcpy(x,"dog");
}
[2 marks]

c) i) Explain the difference between predefined functions and user defined functions.
[2 marks]
ii) Suppose we have a user defined function, long square (long), which returns the
square of a number, show how you would go about its declaration, calling and
definition.

Question Seven
a) With the aid of an example, give a comprehensive definition of a structure and clearly
show its necessity and use in C programs.
[3 marks]
b) Explain why the following code, when executed, prints 420.

#include<stdio.h>

#define init_employee(X,Y) {(X),(Y),wage_emp}


typedef struct Employee Em;
struct Employee {int hours,salary;int (*wage)(Em*);};
int wage_emp(Em *ths) {return ths->hours*ths->salary;}
#define init_manager(X,Y,Z) {(X),(Y),wage_man,(Z)}

typedef struct Manager Mn;


struct Manager {int hours,salary;int (*wage)(Mn*);int bonus;};
int wage_man(Mn *ths) {return ths->hours*ths->salary+ths->bonus;}

int main(void) {
Mn m = init_manager(40,10,20);
Em *e= (Em *) &m;
printf("%d\n",e->wage(e));
return 0;
}
[4 marks]
c) i) Explain how a union differs from a structure.
[2 marks]
ii) Consider the following C program:
#include <stdio.h>
main ()
{
union id {
char name[40];
int number;
};
struct {
int salary;
union id description;
7
COE 162: Introduction to Programming Semester II Academic year: 2010/11

} student,faculty;

printf("%d\n", sizeof(union id));


[Link] ="Sam";
printf("%s %d\n",
[Link],[Link]);
[Link] = 12;
printf("%s %d\n",
[Link],[Link]);

Determine the output displayed by the first, second and third printf() statements.
[4 marks]

Common questions

Powered by AI

The expression x++ is a post-increment; it returns the value before incrementing. In contrast, ++x is a pre-increment; it increments the value first and then returns it. This distinction affects how expressions are evaluated, particularly in loops or complex assignments .

The output 420 results from calling the 'wage' function pointer for an 'Employee' struct type, which was incorrectly cast from a 'Manager' struct. The 'wage' function calculates based on 'hours' and 'salary', ignoring the 'bonus'. The program misconstrues structure memory layout, ultimately performing unintended operations which output 420 .

Objects refer to instances of classes in object-oriented programming and encapsulate data and behavior, while operations are the methods or functions that act on these data. Objects represent real-world entities, whereas operations define the actions that can be performed on these entities .

A pointer variable, such as 'pv', holds the memory address of another variable, 'v'. This allows indirect manipulation of 'v' through 'pv'. Changes via 'pv' affect the actual data stored in 'v', enabling powerful operations like dynamic memory handling or passing function arguments by reference .

Modularity enhances program quality by dividing it into interchangeable modules, improving maintainability and reusability. Localization reduces dependency among parts, allowing changes to one section without affecting others, leading to easier maintenance. Delayed decisions support flexibility by allowing developers to keep options open, postponing decisions until more information is available, ultimately leading to more informed and adaptable designs .

For handling large amounts with high precision, such as money, the 'long double' type in C is suitable. It provides extended precision compared to 'float' and 'double', allowing for large numbers with more significant digits .

The output of the given C code is '7 12 7'. This is because inside the nested block, 'a' is redeclared as 20 but is then assigned the value of 'b' (which is 11 after being incremented twice), so 'a' becomes 11. After exiting the inner block, the outer 'a' (initially 5) is incremented twice, resulting in 7, and 'b' remains 12 due to its independent increment .

A 'while' loop checks its condition before executing the loop body, meaning if the condition is false initially, the loop body may never execute. In contrast, a 'do-while' loop executes the loop body once before checking the condition, ensuring that the loop is run at least once regardless of the condition .

In the first program, 'goto' is a reserved keyword in C and cannot be used as a variable name. In the second example, '__BIG__ && 8' uses an inappropriate logical operand for assignment, returning either 1 or 0 rather than the integer value intended for 'y' .

The value of the expression 2 * 6 - 5 + 8 % 5 is 8. The calculation follows operator precedence: (2 * 6) - 5 + (8 % 5) = 12 - 5 + 3 = 8 .

You might also like