Вы находитесь на странице: 1из 5

CIS 209 C Programming For Engineers Name: __________________

Fall 2012
Exam 1 Lab Section:___________________

True/False: (3 pts each) Place a T or an F in the blank next to each number indicating if you think the
statement is true or false.

1. __F___ We use the #include statement to give us access to commands such as switch and if

2. __T___ An if/else statement can always be used in place of a switch-case statement

3. __T___ 3.0 / 4 is 0.75 in C.

4. __T___ x = 6;
y = 5;
( (x < y + 3) || (3 * x >= 4 * y 3) ) (is it true or false?)

5. __T___ x = 4;
y = 3;
( ((x>y) && (4 * y != 3 * x)) || y <= 4 ) (is it true or false?)

6. __F___ Using a while loop guarantees that the code inside the loop is performed at least one
time.

7. __F___ The keyword endcase is used inside each case of a switch statement to indicate the end
of the code that applies to that case.

8. __T___ There is a maximum value for integer variables

Multiple Choice: (3 pts each) circle the letter corresponding to the best choice among those given.

9. What is the value of x when the following lines of code have completed?
int x;
int y=9;
x = y / 2;

a. 5
b. 4
c. 3
d. 0

10. What keyword indicates the exit point of a function to the compiler?
a. return
b. getch( )
c. void
d. goodbye
e. exit
11. Given the code below, how many asterisks will be displayed on the screen?
int A, B;
A = 3;
while(A < 9)
{
B = 10;
while(B>=A)
{
printf("*");
B=B-2;
}
printf("*\n");
A = A+3;
}
a. 0
b. 7
c. 8
d. 9
e. 16

12. Given the data file shown to the right, in what order will the values be read in using fscanf?
a. 1, 12, 5, 7, 3, 21, 2, 14, 3
b. 1, 7, 2, 12, 3, 14, 5, 21, 3 1 12 5
c. the access happens when a given value is needed
(the order can't be predicted) 7 3 21
d. this is an invalid data file 2 14 3

13. (3 pts) Show the output from the following code:


int x = 3;
while( x < 15)
{ 3711
if(x % 4 == 3)
{
printf(%i\n, x);
}
x = x + 2;
}

14. (4 pts) list 4 data types we have discussed in class

int
float
double
char
15. (8 pts) For each condition below, CLEARLY show on a number line what values of x will make
the condition evaluate to true.

(x < 10)

open circle at 10, arrow to left

(x != 5)

open circle at 5, arrows both directions

(x <= 10 && x > 12)

no values

(x > 10 || x < 12)

all values

16. (3 pts) In one sentence, explain what the compiler does.

Translates source code to machine language

17. (3 pts) In one sentence, explain what casting or type casting does.

Tells compiler to promote a variable of one type, such as an int, to another type for a
single operation

18. (3 pts) what is the difference in how do-while loops and while loops work?

Do while loop is guaranteed to go inside 1 time (post test loop vs pre test loop)

19. (5 pts) Write a for loop that displays the values 8, 12, 16, 20....44 on the screen, 1 value per line.

Int x;
for(x=8; x <= 44; x=x+4)
{
printf(%:\n, x);
}
20. (10 pts) Write a complete program that will ask the user for two numbers, read in the values,
and then print out the sum of the two values.

Enter a value: 14
Enter another value: 5
The total is: 19
Thank you and have a nice day!

#include <stdio.h>
int main()
{
int val1, val2;
printf(enter a value: );
scanf(%i, &val1);
printf(enter another value: );
scanf(%i, &val2);
printf(the total is: %i\n, val1 + val2);
printf(thank you and have a nice day!);
return 0;
}

21. (10 pts) Given the following function prototypes, and the variables declared within main below,
show how to call each function from main. You must use the variables given, and if a function
returns a value, you must store that value in one of the variables.

void function1( );
void function2(int inum);
void function3( double fval, int cval);
double function4(int ival, int inum);
int function5( )

void main( )
{
int ival1 = 3;
int ival2 = 8;
double fnum = 1.234;

___function1( );______________________________________

___function2(ival1);___________________________________

___function3(fnum, ival1);_____________________________

___fnum = function4(ival1, ival2);________________________

___ival1 = function5( );______________________________________


}
22. (10 pts) Given the code below, use a while loop to limit this number to 1, 2, or 3 only. Then,
using a switch case statement, tell the user if they entered one, two or three. (print out
the word corresponding to the number they entered).

int value;
printf(enter a 1, 2 or 3: );
scanf(%i, &value);

while( value < 1 || value > 3 )


{
printf( invalid, try again: );
scanf(%i, &value);
}
switch(value)
{
case 1:
printf(one);
break;
case 2:
printf(two);
break;
case 3:
printf(three);
break;
}

23. (5 pts) Show the code needed to open a data file named savegame.dat. The first value in the
data file is a number telling what level the character is. Read this value in and display it on the
screen.

int x;
FILE * inputFile;
inputFile = fopen(savegame.dat, r);
fscanf(inputFile, %i, &x);
printf(%i, x);

Вам также может понравиться