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

 

                        0

 

b.) a && b

 

            “true”

 

 

7.) What is the result of the following calculation?

 

      int a = 20;

      int b = 4;

      int c = ++a – 2*b;

      b++;

 

 

a= ______21______   b= _____5________   c= ____13________

 

 


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);

 

 

There is a memory leak, as the malloc occurs every time the loop executes, but the free() only frees the last allocation.

 

 

 

9.) Fill in the following table:

 

 

Lifetime

Scope

global variable

 

The lifetime of the program

 

 

The file it is declared in

local variable

 

Function call -> return

 

 

The block it is declared in

malloc’ed variable

 

From the call of malloc to the call of free or program termination

 

 

The lifetime of the pointer(s) that point to the malloc’ed region

 

 

 


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;

}

 

 

1

2          2

3          3          3

4          4          4          4

5          5          5          5          5
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;

}

 

 

It was three

Or maybe 5

 

 

 

 

 

 

 


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.

#include <stdio.h>

 

struct student {

     char name[20];

     char midterm;

     char final;

};

 

int main(int argc, char *argv[])

{

     FILE *f;

     if(argc < 2)

     {

           return -1;

     }

f = fopen(argv[1], “rb”);

while(!feof(f))

{

     struct student s;

 

/*We need to check the return value to see if we actually read a struct the last time. It’s not until we do this last read that fails that feof() will return true. fread returns how many objects it read, and since we asked for 1, it better return > 0.*/

     if( fread(&s, sizeof(struct student), 1, f) > 0)

     {

printf(“%s got a %3.1f\n, s.name, (s.midterm + s.final) / 2.0);

     }

}

fclose(f);

return 0;

}


Essay

 

 

12.) What are the two strategies for keeping track of variable-length data? List an advantage and disadvantage to both approaches.

 

 

-          Keeping a length variable

o   Advantage: saves time when wanting to know the length

o   Disadvantage: requires extra storage space

-          Using a sentinel value

o   Advantage: saves space

o   Disadvantage: requires going through entire data structure to find length

 

 

 

 

 

 

 

 

 

13.) What is the difference between a binary and a text file?

 

Text files contain only data in the limited subset of printable ASCII characters. Binary files contain arbitrary data just as it was in memory. A major difference is with numbers, where a text file would contain the string “1234” whereas the binary file would contain just the binary representation of the number. Text files are good where a human might need to interpret or understand the contents of a file, binary files are efficient, since the data is in the format the computer wants to manipulate already.