Dbit 110: Question Paper
Dbit 110:
Course:Introduction To Programming Concepts
Institution: Kenya Methodist University question papers
Exam Year:2008
KENYA METHODIST UNIVERSITY
END OF SECOND TRIMESTER 2008 EXAMINATIONS
FACULTY : BUSINESS AND MANAGEMENT STUDIES
DEPARTEMENT : BUSINESS ADMINISTRATION
COURSE CODE : DBIT 110
COURSE TITLE : INTRODUCTION TO PROGRAMMING CONCEPTS
TIME : 2 HOURS
INSTRUCTIONS:
• Answer All Questions in Section A (Compulsory) and any other TWO Questions from Section B
SECTION
Question 1
(a) Explain what is wrong with the following variable/constant declarations and correct them.
(i) integer sum; to hold an integer variable called sum
(ii) #define TRUE = 1; which defines a constant called TRUE with a value of 1
(iii) float arctan; which holds scientific notation values (+e)
(iv) #define GST 0.125; to define a constant gst with a value 0.125
(v) char grade = ‘FAIL’ to hold a string of value FAIL (5 marks)
(b) Write code in C to perform the following assignment operations
(i) Assign the value of the variable number1 to the variable total
(ii) Assign the sum of the two variables loop_count and petrol_cost to the variable sum
(iii) Divide the variable total by the value 10 and leave the result in the variable discount
(iv) Assign the character W to the char variable letter
(v) Assign the result of dividing the integer variable sum by 3 into the float variable costing. Use type casting to ensure that the remainder is also held by the float variable (5 marks)
(c) Write C statements which will perform the following scanf and printf operations
(i) Print out the text string "Welcome", followed by a newline.
(ii) Print out the character variable letter
(iii) Print out the float variable dump using two decimal places
(iv) Read a decimal value from the keyboard, into the integer variable sum
(v) Read a single character from the keyboard into the variable operator, skipping leading blanks, tabs and newline characters. (5 marks)
Question 2
(a) (i) Write a for loop C program section which will produce the following output using two nested for loops. (4 marks)
1
22
333
4444
55555
(ii) Modify the for loop above to use the while loop (2 marks)
(b) Below is a C program which checks a value of char variable letter and performs different actions depending on the value of letter.
if( letter == ''X'' )
sum = 0;
else if ( letter == ''Z'' )
valid_flag = 1;
else if( letter == ''A'' )
sum = 1;
else printf("Unknown letter -->%c\n", letter );
Rewrite this code using a CASE statement. (4 marks)
(c) Perform the following activities on arrays (5 marks)
(i) Declare a character based array called letters of ten elements
(ii) Assign the character value ''Z'' to the fourth element of the letters array above
(iii) Use a for loop to total the contents of an integer array called numbers which has five elements. Store the result in an integer called total.
(iv) Declare a multi-dimensional array of floats called balances having three rows and five columns.
(v) Write a for loop to total the contents of the multi-dimensional float array balances.
SECTION B
Question 3
(a) Below is a c program which incorporates a function. The function uses parameter passing and performs the addition of three numbers. The result is then printed in the main section of the program. Use it to answer the questions below:
#include <stdio.h>
int calc_result( int, int, int );
int calc_result( int var1, int var2, int var3 )
{
int sum;
sum = var1 + var2 + var3;
return( sum ); /* return( var1 + var2 + var3 ); */
}
main()
{
int numb1 = 2, numb2 = 3, numb3=4, answer=0;
answer = calc_result( numb1, numb2, numb3 );
printf("%d + %d + %d = %d\n", numb1, numb2, numb3, answer);
}
Using the example above define
(i) function prototype
(ii) local parameter
(iii) formal parameter
(iv) actual parameter
(v) function call (10 marks)
(b) Below is a C program incorporating a function to add all elements of a two dimensional array. The number of rows are to be passed to the function, and it passes back the total sum of all elements. It has errors; use it to answer the questions below:
#include <stdio.h>
int add2darray( int [][5], int ) /* function prototype */
int add2darray( int array[][5], int rows )
{
int total = 0, columns, row
for( columns = 0; columns < 5; columns++ )
for( row = 0; row < rows; row++ )
total = total + array[row][columns];
return total;
}
main()
{
int numbers[][] = { {1, 2, 35, 7, 10}, {6, 7, 4, 1, 0} };
int sum;
sum = add2darray( numbers, 3 );
printf("the sum of numbers is %d\n, sum” );
}
The program contains five errors in lines 2, 4, 5 & 6,14, 15. Identify these errors and correct them. (5 marks)
(c) What is the outcome of the following, assuming time=2, a=3, b=4, c=5
time -= 5;
a *= b + c; (5 marks)
Question 4
(a) Differentiate between the following in C
(i) long int and int variables (2 marks)
(ii) float and double variables (2 marks)
(iii) char and string variables (2 marks)
(b) Write down the result after the following printf operations are performed:
(i) printf("%d", sum); (1 mark)
(ii) int sum = 50;
float modulus;
modulus = sum % 10;
printf("The %% of %d by 10 is %.2f\n", sum, modulus); (2 marks)
(iii) int value1 = 12, value2 = 5;
float answer = 0;
answer = value1 / value2;
printf("The value of %d divided by %d is%f\n",value1,value2,
answer); (2 marks)
(iv) #include <stdio.h>
#define TAX_RATE 0.10
main()
{
float balance;
float tax;
balance = 72.10;
TAX_RATE = 0.15;
tax = balance * TAX_RATE;
printf("The tax on %.2f is %.2f\n", balance, tax );
}
(c) A section of code is to written to check if marks entered on keyboard obeys the following rules. Write down the Boolean expression for the rules in C;
(i) 0=marks=100 (1 mark)
(ii) 39< marks = 50 and catmarks>20 (2 marks)
(iii) 74<marks=100 and exammark=40 (2 marks)
Question 5
(a) Details of a student are kept in a record called student with fields admin_no, name, course, year and sex(M,F):
(i) Define the structure called student to hold these values (3 marks)
(ii) Using appropriate C statements assign the values “1508, TOM ONYANGO, MBA, 2, M” to the fields admin_no, name, course, year and sex respectively. (3 marks)
(iii) Redefine the student record above as an array studarray to hold the details of the students shown in the table below: ( 2 marks)
admin_no name course year sex
1202 TOM CHOMLEY BUS 2 M
0082 RECHO KAMAU BBIT 1 F
1304 BRAMUEL KARANI BDIT 3 M
0045 FLORENCE MENYA BCOM 2 F
2477 RICHARD KIPLIMO MBA 2 M
0942 GREGORY WAMALWA BIT 4 M
(iv) Initialize the student array above with the table values shown above (5 marks)
(v) Assuming the values were entered from the keyboard into the array using a for loop with an index J, write down the C code for the loop. (3 marks)
(b) What is the output of the following program segment?
int count = 10, *temp, sum = 0;
temp = &count;
*temp = 20;
temp = ∑
*temp = count;
printf("count = %d, *temp = %d, sum = %d\n", count, *temp, sum );
(4 marks)
Question 6
(a) Answer the following question on file handling in C
(i) Define an input file handle called input_file, which is a pointer to a type FILE
(1 mark)
(ii) Using input_file, open the file results.dat for read mode as a text file.
(1 mark)
(iii) Write C statements which tests to see if input_file has opened the data file successfully. If not, print an error message and exit the program. (2 marks)
(iv) Close the file associated with input_file. (1 mark)
(b) Write C code which will read a line of characters (terminated by a \n) from input_file into a character array called buffer. NULL terminate the buffer upon reading a \n. (5 marks)
(c) Explain using examples how the following can be defined in C;
(i) A record containing item_name, item_id, item_prize (2 marks)
(ii) A linked list of the record above (2 marks)
(d) Write a C function called delete_node, which accepts a pointer to a list, and a pointer to the node to be deleted from the list, assuming its header is
void delete_node( struct node *head, struct node *delnode );
(3 marks)
(e) Write a function called insert_node, which accepts a pointer to a list, a pointer to a new node to be inserted, and a pointer to the node after which the insertion takes place, assuming the header is as defined below;
void insert_node( struct node *head, struct node *newnode, struct node *prevnode ); (3 marks)
More Question Papers