COM142 – C Programming Final Exam

advertisement
Name:
Student ID:
COM142 – C Programming
Fall 2009-2010
Computer Engineering Department
Near East University
Final Exam
January 12, 2010 [11:30A]
Lecturer: Hüseyin Sevay
INSTRUCTIONS
You have 100 minutes for this exam.
This exam is worth a total of 111 points, and points above 100 are bonus.
This question paper has 9 single-sided pages (except for this title page) and a total of 19 questions. Please check
to make sure you have all the pages NOW !
Please write your name and student ID in the boxes at the top of this page and your student ID on each of the
remaining question pages NOW !
Each question specifies how many points it is worth.
Please write your answers in the allotted space/box(es) below or next to each question. Do not write your
answers anywhere else! Especially please do not write below the footer line on question pages.
Read each question carefully, and do not start answering a question before understanding what that question
is asking for.
COM142 – C Programming – Final Exam (Fall 2009-2010)
Student ID:
1. Suppose you have a C source file named account.c . Provide a GNU/Linux command that would compile
this source code into object code: [2 points]
2. Suppose you have a C source file named grades.c . Provide a GNU/Linux command that would compile and
link this code with the Math Library into an executable named grades: [2 points]
3. During which stage of building an executable are directives such as #define and #ifdef processed by a C
compiler? Name the stage. [2 points]
4. Suppose are given an object file named main.o. In order, name the stage/stages of building an executable that
this file has to be put through in order to generate an executable from it. [2 points]
5. What will the following program print out when run? [12 points]
int getSize( char *str ) { return sizeof( str ); }
int main( void ) {
char S1[30]="I love playing soccer.";
char S2[20]="We are";
printf( "%d\n", strlen( S1 ) );
printf( "%d\n", sizeof( S1 ) );
printf( "%d\n", getSize( S1 ) );
strcpy( S1+15, "basketball." );
printf( "%s\n", S1 );
printf( "%d\n", strlen( S1 ) );
printf( "%d\n", sizeof( S1 ) );
strncpy( S1, S2, strlen( S2 ) );
printf( "%s\n", S1 );
printf( "%d\n", strlen( S1 ) );
printf( "%d\n", getSize( S2 ) );
printf( "%d\n", sizeof( S2 ) );
S1[6] = ’\0’;
printf( "%s\n", S1 );
printf( "%d\n", strlen( S1 ) );
return 0;
} /* end main */
PLEASE DO NOT WRITE IN THIS SPACE !
Page 1 of 9
COM142 – C Programming – Final Exam (Fall 2009-2010)
Student ID:
6. What will the following following program print out when run? [8 points]
int main( void ) {
char s[]="Alexander Graham Bell";
char* p=(char*)0;
int x=45;
int *px=(int*)0;
int *sx=&x;
p=s;
*p = ’F’;
printf( "%s\n", s );
p += 17;
*p = ’D’;
printf( "%s\n", s );
printf( "%d\n", *sx );
printf( "%d\n", *sx + 5 );
printf( "%d\n", *sx );
px = sx;
printf( "%d\n", *px );
*px = 83;
printf( "%d\n", x );
printf( "%d\n", *sx );
return 0;
} /* end main */
7. What will the following program print out when run? Show how you computed the results of each operation.
[5 points]
int main( void ) {
int x=17;
int y=24;
int p=(x & y);
int r=(x | y);
int s=(x ^ y);
int t=(y >> 2);
int v=(x << 3);
printf( "%d\n",
printf( "%d\n",
printf( "%d\n",
printf( "%d\n",
printf( "%d\n",
return 0;
} /* end main */
p
r
s
t
v
);
);
);
);
);
PLEASE DO NOT WRITE IN THIS SPACE !
Page 2 of 9
COM142 – C Programming – Final Exam (Fall 2009-2010)
Student ID:
8. Consider the following C function named fact. Trace the call fact( 5 ). Show how you reached the return
value of this call by drawing a function call tree. [5 points]
int fact( int a ) {
if (a < 1) {
return 1;
}
else {
return( a * fact( a - 1 ) );
}
} /* end fact */
9. Write a C function named printRange that, when called, prints out all integers between two given values a
and b, assuming a <= b, where a should be the first argument to the function and b the second argument
to the function. For example, if the call printRange( 1, 10000 ) is executed, this call should print out the
sequence 1 2 3 4 5 . . . 9998 9999 10000 . [5 points]
PLEASE DO NOT WRITE IN THIS SPACE !
Page 3 of 9
COM142 – C Programming – Final Exam (Fall 2009-2010)
Student ID:
10. Write a complete C program that computes the average of the floating-point values entered as argument at the
command line and prints out the result as shown below with 2 significant decimal digits. Do not assume a fixed
number of input values. Your program should work with any number of arguments. [10 points]
• Input should be read from the command line, not from the keyboard directly!
• Example 1: If your program is provided the input values 1 2 3 4 , then your program should print out
the message:
The average is 2.50
• Example 2: If your program is provided the input values 1 2 3 4 5 6 12 , then your program should
print out the message:
The average is 5.00
• Example 3: If your program is provided no input values, then your program should print out the following
message and exit:
*** Please provide some values.
• Hint 1: atof()
• Hint 2: argv, argc
PLEASE DO NOT WRITE IN THIS SPACE !
Page 4 of 9
COM142 – C Programming – Final Exam (Fall 2009-2010)
Student ID:
11. Consider the following incomplete C program. Fill in the seven (7) blanks according to the descriptions given
in the comments in the program. [7 points]
int main( void ) {
int a=5;
char s[]="Classical Music";
char* p=(s + 4);
char c=’X’;
double d=150000;
long int i=18000000L;
printf( "_____", a ); /* Print
printf( "_____", s ); /* Print
printf( "_____", p ); /* Print
printf( "_____", c ); /* Print
printf( "_____", p ); /* Print
printf( "_____", d ); /* Print
printf( "_____", i ); /* Print
return 0;
} /* end main */
the
the
the
the
the
the
the
value of a */
content of s */
value pointed by p */
value of c */
value of p */
value of d */
value of i */
12. Consider the function named reverse that reverses a given string in the following program. Fill in the six (6)
blanks to complete the implementation of reverse. [6 points]
#include <stdio.h>
#include <string.h>
char* reverse( char* str ) { /* Reverses string str */
int n, h, j, k;
char ________;
n = strlen( ________ );
h = (n / 2);
for ( j=0, k=(n - 1); (j < h); j++, _______ ) {
tmp = ___________;
str[k] = str[j];
str[j] = ___________;
} /* end for */
return _________;
} /* end reverse */
int main( int argc, char** argv ) {
char s[512];
strcpy( s, argv[1] );
printf( "Reverse: %s\n", reverse( s ) );
return 0;
} /* end main */
PLEASE DO NOT WRITE IN THIS SPACE !
Page 5 of 9
COM142 – C Programming – Final Exam (Fall 2009-2010)
Student ID:
13. Indicate the output of the following program. [5 points]
int main( int argc, char** argv ) {
int a=2, b=5, c=7;
{
int a=0;
printf( "%d\n", a );
printf( "%d\n", b );
printf( "%d\n", c );
{
c=3;
b = a + b + c + 1;
printf( "%d\n", a );
printf( "%d\n", b );
printf( "%d\n", c );
}
c += (a - b);
{
int c=(a - ++b);
printf( "%d\n", a );
printf( "%d\n", b );
printf( "%d\n", c );
}
printf( "%d\n", a );
printf( "%d\n", b );
printf( "%d\n", c );
}
printf( "%d\n", a );
printf( "%d\n", b );
printf( "%d\n", c );
return 0;
} /* end main */
14. Write a C function that accepts a 1-dimensional floating-point array and its size as argument and returns the
average of the values in that array. [5 points]
PLEASE DO NOT WRITE IN THIS SPACE !
Page 6 of 9
Student ID:
COM142 – C Programming – Final Exam (Fall 2009-2010)
15. Write a C function that accepts a (n × 5) 2-dimensional floating-point array and its row size n as argument and
returns the average of the values in that array. [5 points]
16. Suppose you have the following integer array where the indices are shown below the array. Show how the
Bubble Sort algorithm would sort this array in ascending order. Show the result of each pass and all critical
steps within each pass. [5 points]
7
6
5
9
3
0
8
4
2
1
0
1
2
3
4
5
6
7
8
9
PLEASE DO NOT WRITE IN THIS SPACE !
Page 7 of 9
Student ID:
COM142 – C Programming – Final Exam (Fall 2009-2010)
17. Suppose you have the following integer array where the indices are shown below the array. Show how the
Bubble Sort algorithm would sort this array in descending order. Show the result of each pass and all critical
steps within each pass. [5 points]
7
6
5
9
3
0
8
4
2
1
0
1
2
3
4
5
6
7
8
9
18. In C, implement the Ackermann function whose definition is given below. Your function should have the
prototype int Ackermann( int m, int n ); [10 points]

if m = 0
 n +1
A(m − 1, 1)
if m > 0 and n = 0
A(m, n) =

A(m − 1, A(m, n − 1)) if m > 0 and n > 0
PLEASE DO NOT WRITE IN THIS SPACE !
Page 8 of 9
Student ID:
COM142 – C Programming – Final Exam (Fall 2009-2010)
19. Consider the following data structure, whose individual elements are declared as follows:
struct node {
int value;
struct node *next;
}
Assuming this declaration, write just the necessary C code in order to dynamically create the structure given in
the figure below. [10 points]
5
7
2
lst
PLEASE DO NOT WRITE IN THIS SPACE !
Page 9 of 9
Download