You are on page 1of 11

UNIVERSITY OF TECHNOLOGY, JAMAICA

SCHOOL OF COMPUTING & INFORMATION TECHNOLOGY SEMESTER 2 EXAMINATION


DATE: GROUP: MODULE: APRIL 2009 DURATION: 2 HOURS

RESIT/CA1 CMP1002

MOUDLE NAME: PROGRAMMING USING THE C LANGUAGE

INSTRUCTIONS: ANSWER ALL QUESTIONS IN SECTIONS I, II AND III AND ANY TWO FROM SECTION IV Section I Compulsory Answer all questions from this section on the OMR sheet provided for a total of 25 marks. 1. Which of the following is not a keyword? int a. return b. if c. main d. 2. Which of the following is a repetition structure? if a. ifelse b. dowhile c. switch d. 3. The order in which statements are __________ is called flow of control. a. entered in a source file b. preprocessed c. compiled d. executed 4. Which statement is true? a. Unless directed otherwise, the computer automatically executes C statements one before the other. b. The sequence structure is essentially built into C. c. A flowchart is a pseudocode representation of an algorithm or a portion of an algorithm.

/2

d.

Unlike pseudocode, flowcharts are not very useful for developing and representing algorithms.

5. The __________ statement performs one of many different actions, depending on the value of an expression. a. if b. if then c. if else d. switch 6. How many times will the following program print hello?
i = 1; while ( i >= 10 ) printf( hello );

a. b. c. d.

10 8 an infinite number of times 0

7. Indefinite repetition is controlled by a a. data value b. counter c. sentinel value d. non-constant condition 8. Which statement about comments is false? a. Lengthy comments can cause poor execution-time performance b. Comments do not cause any machine language object code to be generated c. Programmers insert comments to document programs and improve program readability d. Comments begin and end with /* and */, respectively. 9. Which of these is not a valid identifier? Gnash a. too_many b. union c. _9Times d. 10. In C, the condition 4 > y > 1 a. evaluates correctly but could be replaced by ( 4 > y && y > 1 ) b. does not evaluate correctly and should be replaced by ( 4 > y && y > 1 ) c. evaluates correctly and should not be replaced by ( 4 > y && y > 1 ) d. does not evaluate correctly and should not be replaced by ( 4 > y && y > 1 ) 11. A valid reason for building programs out of functions is a. that the divide-and-conquer approach facilitates program construction b. that pre-existing functions can be used to create new programs /3

c. d.

the avoidance of code repetition within a program all of the above

12. The __________ sign is also known as the __________ operator. a. +, assignment b. =, assignment c. *, stream manipulator d. &, stream insertion 13. Which of the following for headers is not valid? a. int i; b. c. d.
for( i = 0; i < 10; i++ ) int i; for( ; i < 10; i++ )

int i; for( ; ; i++) int i; for( ; i < 10; )

14. What value does function mystery return when called with a value of 4?
int mystery ( int number ) { if ( number <= 1 ) return 1; else return number * mystery( number 1 ); }

a. b. c. d.

1 24 0 4

15. Whats wrong with this code?


int[ ] = ( 1, 2, 3, 4, 5);

a. b. c. d.

The array size must be specified in the square brackets. The parentheses should be square brackets. The square brackets should be curly braces The parentheses should be curly braces.

16. Which statement is true? a. Entire arrays are passed simulated call by reference and individual array elements are normally passed simulated call by reference. b. Entire arrays are passed call by value and individual array elements are normally

/4

c. d.

passed call by value. Entire arrays are passed call by value and individual array elements are normally passed simulated call by reference. Entire arrays are passed simulated call by reference and individual array elements are normally passed call by value.

17. Consider the following code, assuming that x is an integer variable with an initial value of 6:
if( x = 12 ) printf(%d, x);

What is the output? a. b. c. d. 6 12 nothing a syntax error is produced

18. Which of the following does not initialize all of its elements to 5? a. b. c.
int b[ 2 ][ 2 ]; b[ 0 ][ 0 ] = b[ 0 ][ 1 ] = b[ 1 ][ 0 ] = b[ 1 ][ 1 ]=5; int b[ 2 ][ 2 ] = { 5 }; int b[ 2 ][ 2 ]; for ( int i = 0; i < 2; i++ ) for (int j = 0; j < 2; j++ ) b[ i ][ j ] = 5;

d.

all of the above initialize all of their elements to 5.

19. Which of the following can have a pointer as an operand? a. *= b. % c. -d. / 20. If bPtr is assigned b (the name of an array), then array element b[ 4 ] can alternatively be referenced with the pointer expression __________. a. b. c. d.
*( bPtr + 4) *b [ bPtr + 4 ] b[ bPtr + 4 ] bPtr + 4

21. Structures may be passed to functions by ________. a. passing an entire structure b. passing individual structure members

/5

c. d.

passing a pointer to the structure all of the above

22. Which of the following file opening modes can you use to create a file that does not exist? a. r+ b. w+ c. rb d. all of the above 23. typedef is used to a. b. c. d. create a name that is an alias for another name. create new data types cast one struct to another type initialize struct members.

24. The statement


while ( -- counter >= 1 ) printf( %s\n, counter % 2 ? even : odd );

can not be rewritten as a.


while ( -- counter >= 1 ) if ( counter % 2 ) printf( even ); else printf( odd ); while ( counter >= 1 ) if (counter % 2) printf( even ); else printf( odd ); --counter; while ( counter >= 1 ) { if ( counter % 2 ) printf( even ); else printf( odd ); --counter; } do { printf( %s\n, counter % 2 ? odd : even ); --counter;

b.

c.

d.

/6

}while ( counter >= 2 );

25. Which of the following functions does not contain any errors? a.
double circumference ( int r ); { return ( 5.14 * 2 * r ); } void printnum ( int x ) { print( %d, x ); return x; } double triple ( float n ) return ( 3 * n ); int cube( int s ) { int s; return ( s * s * s ); }

b.

c. d.

Section II Compulsory Answer all question from this section on the OMR sheet provided for a total of 10 marks.
Shade A for True and B for False for each of the following questions.

26. Structures can have two fields with the same name. 27. The address of a variable is accessed by using the & operator. 28. To access the variable pointed to by a pointer the redirection operator is used. 29. A field in a structure cannot be a structure. 30. void functions cannot return a value. 31. When closing a file in C the fclose function is used in the format: fclose(fileName). 32. The array declaration specifies how many values the array must know. 33. Bottom-up programming starts with the lowest level modules. 34. All functions must perform some sort of input or output. 35. A loop is a section of a program which is repeated zero or more times. Section III Compulsory Answer all question from this section in the booklet provided for a total of 30 marks. /7

36. If originally, ii) x = 2, y = 3 and z = 0, what is the value of x, y and z after executing the following code?
if((z < x || y >= z) && z == 1) if(z && y) y = 4; else x += 4;

[3 marks] iii) x = 1, y = 2 and z = 1, what is the value of x, y and z after executing the following code?
switch(x) { case 0:

x y case 1: x default: y x = 3;

= 2; = 3; *=z; = 6;

[3 marks] 37. What would be printed by each of the following program segments? i) y = 7;
while(y = 7) { printf(%d, , y); y--; }

[2 marks]
ii) for(x = 9; x > 14; x--) printf(%d, , x);

[2 marks]
iii) do{ printf(%d, , ++x); }while(x < 14); x = 9;

[2 marks] 38. At Martys Car Mart the manager wants to store the data of the vehicles on the lot; the data to be stored on each vehicle is as follows:Year, make (e.g. Toyota, Honda, etc.), model (civic, accord, yaris, etc.), body type (sedan, hatch back, etc.) and seating capacity.
Write the C code segment to do the following:-

/8

i. Create a structure to represent a vehicle on the lot.

[3 marks]

ii. Use a diagram to show the pictorial representation of a vehicle called VH1 declared and initilazed with the following values year: 2008, make: Mazda, model: 6, body type: sedan, and seating capacity: 4. [2 marks]

39. Evaluate and show the final output for the following recursive function with a call of 7. Be sure to use diagrams to aid in your explanation.
int fn(int r) { if(r==1 || r==0) return 1; if(r%2==0) return fn(r/2)+2; else return fn(r-1)+3; }

[5 marks]

40. What values would be printed by the following program?


#include <stdio.h> int main(void) { int list[10]={ printf("%d\n, printf("%d\n, printf("%d\n, return 0; }

2, 5, 3, 7, 12, 8, 4, 2, 9, 6}; list[1]+list[9]); list[3 + 5]); list[list[6]]);

[3 marks] Section IV Answer any two (2) questions (20 marks each) QUESTION 41 20 MARKS i. Write a function which compares two strings for equality. If they are equal, zero is returned, otherwise the difference in value between the first two non-matching characters is returned. [10 marks] /9

ii.

What will be printed after execution of a following program block? [10 marks]
int x[] = {1, 2, 3, 4, 7}; //x[0] //x[1] //x[2] //x[3] //x[4] address:1245040, address:1245044, address:1245048, address:1245052, address:1245056, value:1 value:2 value:3 value:4 value:7

int *p = &x[2]; int *q = &x[1]; int *r = ++q; printf("(p + 1) = printf("(p - 1) = printf("(r + 2) = printf("q = %p *q printf("r = %p *r

//p address:1245036 //q address:1245032 //r address:1245028 //q address:1245032 %p *(p + 1) = %d\n", (p + 1), *(p + 1)); %p *(p - 1) = %d\n", (p - 1), *(p - 1)); %p *r+2 = %d\n", (r + 2), *r+2); = %d\n", q, *q); = %d\n", r, *r);

QUESTION 42 20 MARKS i. Write C code to accomplish the following a. Define a global symbolic constant called size with a value of 10. b. Using the symbolic constant defined above, initialize all the elements in a 10element array LIST with the values 1, 2, 5, 7, 4, 7, 2, 6, 2, 3. c. Write a function encode that accepts a one-dimensional integer array and a variable representing the size of the array. The function will traverse the array, modify and print the values of the elements according to the rules outlined below. The function returns nothing. Rules All negative numbers should be replaced by its square Every positive number in the array that is divisible by 3 should be replaced with the number 5 Every positive number in the array that is not divisible by 3 should be replaced with the number 9 d. Write an appropriate call for the function defined in part (c) above. [10 marks]

/10

ii. JamCar Auto Parts makes a large number of electrical parts each year. Some of these parts are returned to them because of defects. An input record is prepared for each defective part containing a code. The code is either 1 or 2, indicating which branch (Branch 1 or Branch 2) made the defective part. JamCar needs a program to process the codes entered and total the number of defective parts manufactured by each branch. A trailer record containing a branch code of 9 is to signal end of input. Write a C program to accomplish the above, including all necessary validations. [10 marks] QUESTION 43 20 MARKS i. With the aid of a diagram(s) explain the relationship between FILE pointers, FILE structures and FCBs (File Control Blocks). Be sure to highlight the relationship in regards to events such as file opening, when an I/O is issued, file close etc. [8 marks]

ii. For 2 marks each indicate whether the following statements are TRUE or FALSE by circling T for True and F for False then detach this page and place in your answer booklet. [12 marks] a.

Each open file must have a separately declared pointer of type FILE that is used to refer to the file. The line if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) names the file"clients.dat"to be used by the program and establishes a "line of communication" with the file. The following line of code inputs data from account, name and balance contained in the file pointed to by cfPtr. fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); Data in a sequential access file can be modified without any dangerous side effects. Individual records of a sequential access file are normally fixed in length and may be accessed directly (and thus quickly) without searching through other records. fread and fwrite read and write data such as integers in fixed-size rather than variable-size format; the data they handle is processed in human-readable format. The following code instructs the program to keep reading from a file until the end of the file is reached.

b.

c. d.

T T

F F

e.

f.

/11

while ( feof( filePtr ) ) { fread( &myDataType, sizeof( myDataType ), 1, filePtr );

**~**~* END OF EXAM*~**~**

/12

You might also like