Name: _____________________________________
CS 0449 – Sample First Midterm
Directions: You have 1 hour to complete this test. Remember to keep your eyes on your own paper. Make sure everything is off your desk. Good luck!
Multiple Choice – Circle ONE of the following
1.) Which of the following data types does not require being prefixed by an & in scanf?
A) char
B) int
C) double
D) string
2.) Which of the following keywords stops a loop, skipping any remaining statements in the body?
A) skip
B) continue
C) break
D) case
3.) Which of the following flags to gcc tells it to create a program with debugging support?
A) -o
B) -g
C) -d
D) -debug
4.) Assuming “s” is a pointer to a struct, which of the following is equivalent to (*s).data?
A) s->data
B) *s.data
C) *(s.data)
D) s.data
5.) Which of the following arguments to fopen says to open a file for reading and writing in text mode, provided the file already exists?
A) “w+t”
B) “r+w+t”
C) “rw+t”
D) “r+”
Short Answer
6.) Given: int a = 1; int b = 2; What is the value of:
a.) a & b
b.) a && b
7.) What is the result of the following calculation?
int a = 20;
int b = 4;
int c = ++a – 2*b;
b++;
a= ______________ b= ______________ c= ______________
8.) What is the problem with the following segment of code?
(Hints: There is only one problem, the program behaves as intended, the code compiles)
int *x;
do
{
x = (int *)malloc(sizeof(int));
printf(“Enter an integer (0 to stop):”);
scanf(“%d”, x);
printf(“You entered %d\n”, *x);
} while(*x != 0);
free(x);
9.) Fill in the following table:
|
Lifetime |
Scope |
global variable |
|
|
local variable |
|
|
malloc’ed variable |
|
|
Tracing
10.) What is output by the following program when run?
#include <stdio.h>
int main()
{
int i,j;
for(i=1; i<=5; i++)
{
for(j=i; j>0; j--)
{
printf(“%d\t”,i);
}
printf(“\n”);
}
return 0;
}
11.) What is output by the following program when run?
#include <stdio.h>
int mystery(int x, int y);
int main()
{
int a = 5;
int b = 3;
switch(mystery(a,b))
{
case 3: printf(“It was three\n”);
case 5:
printf(“Or maybe 5\n”);
break;
default:
printf(“It wasn\’t either!\n”)
}
return 0;
}
int mystery(int x, int y)
{
if(x < y)
{
return x;
}
return y;
}
Coding
Imagine a teacher has kept their class grades in a binary file with the following format:
Offset |
Size |
Description |
0 |
20 |
Last name |
20 |
1 |
Midterm grade (integer, out of 100) |
21 |
1 |
Final grade (integer, out of 100) |
Write a program that reads in the file, and computes each
student’s average and displays it on the screen. (as a real number with one
digit after the decimal place) Note: Be able to handle an arbitrary
number of students in the class.
Essay
12.) What are the two strategies for keeping track of variable-length data? List an advantage and disadvantage to both approaches.
13.) What is the difference between a binary and a text file?