Anda di halaman 1dari 6

Name:

Student ID:

COM142 C Programming
Spring 2009-2010 Computer Engineering Department Near East University

Midterm Exam
April 27, 2010 [11:30A] Lecturer: Hseyin Sevay

INSTRUCTIONS
You have 100 minutes for this exam. This exam is worth a total of 105 points, and points above 100 are bonus. This question paper has 5 single-sided pages (except for this title page) and a total of 12 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 remaining page NOW ! Each question species 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 Midterm Exam (Spring 2009-2010)

Student ID:

1. Suppose you have a C source le named hw.c . Provide a GNU/Linux command that would compile (but not link) this source le: [5 points]

2. Suppose you have an object le, project.o created from the C source le named project.c that you longer have access to. Provide a GNU/Linux command to link this object le into an executable, assuming it needs no external libraries. [5 points]

3. Suppose you have a C source le named program.c. Provide a GNU/Linux command that would display on the screen the result of just preprocessing this source le. [5 points]

4. Suppose you have a C source le named hw.c that calls Math library functions. Provide a single GNU/Linux command that would create an executable from this source le. [5 points]

5. Suppose you are working on a project that involves three (3) C source les named fileA.c, fileB.c, and main.c. Provide all the GNU/Linux commands that you would need to create an executable named proj from these source les. [5 points]

6. Convert the English statement An old woman living in Nicosia, Kyrenia, or Famagusta who has at least two children. into a proper conditional statement in C. State the names, types, and meaning of each variable you use, and write the condition as an if-statement with an empty body. [5 points]

PLEASE DO NOT WRITE IN THIS SPACE !

Page 1 of 5

COM142 C Programming Midterm Exam (Spring 2009-2010)

Student ID:

7. Write down the output that will be produced by the following program. [15 points]

#include <stdio.h> int f( int i ) { printf( "%d ", i ); return i; } int g( char c ) { printf( "%c ", c ); return c; } int main( void ) { char s1[]="Hello", s2[]="Hello"; int i=10, j=20, k=30, m=40; char c1=A, c2=a, c3=B; if ((f(i) < f(j)) || (f(k) >= f(m))) {} printf( "\n" ); if ((f(i) < f(k)) && (f(j) >= f(m))) {} printf( "\n" ); if ((s1 == s2) || (f(j) >= f(k)) || (g(c1) == g(c3))) {} printf( "\n" ); if ((g(c1) != g(c3)) && (g(c1) == g(c2))) {} printf( "\n" ); if ((g(c1) > g(c2)) && (g(c1) > g(c3))) {} return 0; } /* end main */

8. What value will the following program print out? Show all your computations step by step. [10 points]

#include <stdio.h> int main( void ) { int x=2, y=17, z=11, result=5; result -= 2 * z % 13 + y / 3 + x; printf( "%d\n", result ); return( 0 ); } /* end main */

9. Write down the output of the following program. [15 points]

#include <stdio.h> int main( void ) { int i=3; printf( "%d\n", printf( "%d\n", printf( "%d\n", i += i; printf( "%d\n", printf( "%d\n", return 0; } /* end main */

(--i + 3) ); (i++ + 10) ); ++i ); i-- ); i );

PLEASE DO NOT WRITE IN THIS SPACE !

Page 2 of 5

COM142 C Programming Midterm Exam (Spring 2009-2010)

Student ID:

10. Write a C function with the prototype double series( double x, int n); that returns the sum of the following series for n terms: [10 points] x2 x3 x4 xn + + ++ 2 3 4 n

ser i es(x, n) = x +

PLEASE DO NOT WRITE IN THIS SPACE !

Page 3 of 5

COM142 C Programming Midterm Exam (Spring 2009-2010)

Student ID:

11. Write a complete C program that counts the number of even and odd integers provided from the command line. For example, assuming the executable of your program is named count, your program should work as follows: [10 points]

% ./count Number of Number of % ./count Number of Number of % ./count Number of Number of

3 5 7 9 even values: odd values : 2 2 4 4 5 even values: odd values :

0 4 4 1

even values: 0 odd values : 0

PLEASE DO NOT WRITE IN THIS SPACE !

Page 4 of 5

COM142 C Programming Midterm Exam (Spring 2009-2010)

Student ID:

12. Write a complete C program that computes the average of the oating-point values entered as argument at the command line and prints out the result as shown below. Do not assume a xed number of input values! Your program should work with any number of input values. Assuming the name of your program is is avg, your program should work as follows: [15 points] Note that input should be read from the command line. Example 1: If your program is provided the input values 4 2 3 from the command line, then your program should print out the message:

The average is 9.
Example 2: If your program is provided the input values 1 2 3 4 5 8 12 from the command line, then your program should print out the message:

The average is 5.
Example 3: If your program is provided no input values, then your program should print out the message:

*** Please enter some values.

PLEASE DO NOT WRITE IN THIS SPACE !

Page 5 of 5