Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam (120 minutes)



 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: December 15, 2006



 


No questions will be answered during this examination.  If you do not understand a question, read it again.  If you still do not understand it, make reasonable assumptions and write them down. (Points will be deducted if unreasonable assumptions are made.)

DO NOT CHEAT: Cheating includes not only copying from another person but also allowing someone to copy from you.  Anyone who copies or allows someone to copy will receive a score of zero.  So be defensive and protect your work.

You may not use any electronic devices, book, notes or other references during this examination.

 

This examination contains 15 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages. The number of points for a question is roughly proportional to the amount of time you may need for it. Don’t spend too much time on any one question.

Do not forget to sign the attendance list and to write your signature on the line below:

 

_______________________________________________________________________
(If your exam is misplaced and you did not sign the attendance list then you will receive a zero score for the exam.)

 

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Deduction

Grader

1

18

 

 

2

12

 

 

3

20

 

 

4

12

 

 

5

12

 

 

6

12

 

 

7

16

 

 

8

16

 

 

9

18

 

 

10

14

 

 

11

18

 

 

12

12

 

 

13

12

 

 

14

20

 

 

15

20

 

 

16

18

 

 

Total

250

 

 

 

 

Exam Scoreà

 

A

1.            The following programs compile and run without errors. Fill in the blanks with the correct output from each program.

 

a) Program 1

 

#include <stdio.h>

 

void swap(int x, int y)

{

   int temp = x;

   x = y;

   y = temp;

}

 

void main(void)

{

   int x = 1;

   int y = 2;

   swap(x, y);

   printf("x = %i, y = %i\n", x, y);

}

 

x = _______1_________________ , y = ________2_____________________

 

 

b) Program 2

 

#include <stdio.h>

 

int x;

int y;

 

void swap(int x, int y)

{

   int temp = x;

   x = y;

   y = temp;

}

 

void main(void)

{

   x = 1;

   y = 2;

   swap(x, y);

   printf("x = %i, y = %i\n", x, y);

}

 

x = ________1__________________ , y = ___________2___________________

 

 

 

 

 

c) Program 3

 

#include <stdio.h>

 

int x;

int y;

 

void swap(void)

{

   int temp = x;

   x = y;

   y = temp;

}

 

void main(void)

{

   x = 1;

   y = 2;

   swap();

   printf("x = %i, y = %i\n", x, y);

}

 

x = ___________2_______________ , y = __________1____________________

 

 

 

2.            The following program compiles and runs without errors. Write the output of this program.

 

#include <stdio.h>

 

int modify(int x, int* y, int z)

{

  x = x+2;

  *y = *y-3;

  z = x;

   return z;

}

 

void main(void)

{

  int* a;

  int b, c;

  b = 6;

   c = 6;

  a = &b;

  c = modify(*a, &b, c);

  printf("%i %i %i\n", *a, b, c);

}

 

 

____________3_______3________8_______________________________________________

3.            Complete the following program by filling in the blanks. This program declares an array named list of five students, and sorts them alphabetically in ascending order according to the students’ last name ( ‘Jones’ before ‘Smith’). If two students have the same last name then sort the students in ascending order according to the students’ first name (‘Bob’ before ‘John’).

 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct {

  char fname[100]; /* fist name */
char lname[100]; /* last name */

  int   uid;

} Student;

 

int comp_names(Student* ptr1, Student* ptr2)

{  

  int result = strcmp( ____ptr1->lname OR (*ptr1).lname__ , __ ptr2->lname OR (*ptr2).lname _);

 

 

   if ( result != 0)

      return result;

   else

      return __( ____ptr1->fname OR (*ptr1).fname__ , __ ptr2->fname OR (*ptr2).fname _)_ ;

 

}

 

 

void main (void)

{

   int i;

   Student list[5] = {

     {"John", "Smith", 278749},

     {"Bob", "Smith", 346773},

     {"Tom", "Jones", 337462},

     {"Paul", "Newmann", 373637},

     {"Mia", "Turner", 283940}};

   

    /*  fill in the blanks to sort using comp_names    */

 

    qsort(___list_,____5__,_sizeof(Student) OR sizeof(list[0]__,__ comp_names __);
   

    /* print fname , lname and uid for each student */

    for(i=0; i < 5; ++i)

     printf("%s %s %i\n", _list[i].fname__, _list[i].lname_, _list[i].uid___);

}

 

 

4.            Which of the following choices, when filled in the blank below, correctly reads the following input:

 

The perfect storm, Wolfgang Petersen, 2000-9



scanf(" ___________________", title, author, &year, &month);

Do NOT fill in the above blank. Just circle the correct answer below.

You may assume that the variables have been correctly declared. Choose only ONE of the following:

 

a)  " %[^;], %s, %i-%i"



 

 

b)  " %lf, %s, %i-%i; "




 

c)  " %[^,], %[^,], %i-%i"




 

d)  " %[^;]; %[^;]; %i-%i"





e)  " %s, %s, %i"

 

 

 

 

 

 

 

 

 

 

 

 

 

5.            The following program compiles and runs without any error. Write the output of this program.

 

#include <stdio.h>

 

void func(int* n1, int* n2)

{

  int temp;

  *n1 = *n2;

  temp = *n1;

  *n2 = temp;

}

void main (void)

{

  int i;

  int chain[6] = {1, 2, 3, 4, 5, 6};

 

   func(&chain[1], &chain[3]);

   func(&chain[0], &chain[5]);

 

   for (i=0; i<6; i++)

       printf("%i ", chain[i]);

   printf("\n");

}

 

 

_______6_____4______3________4_________5________6_______________________________

 

 

 

 

 

 

6.            For each of the following questions, fill in the blank with TRUE or FALSE.

 

 

a)  ___FALSE________  The following statement correctly opens a file named  output.dat

                and enables writing to it using the variable named  fileptr :
                     
FILE fileptr = fopen("output.dat", "w");

 

 

b)  _____TRUE_______  The C function named rewind causes next file operation to work on the

   beginning of the file.



c)                 ___FALSE__________  The following statement correctly closes a file named  output.dat :

                       fclose("output.dat");

 

 

 

7.            The following program compiles and runs without any errors. Write the output of this program.

 

 

#include <stdio.h>

#include <string.h>

 

typedef struct {

        int id;

        char name[100];

} Employee;

 

Employee func1(Employee emp)

{

        emp.id = emp.id + 1000;

        strcpy(emp.name, "Bob Smith");

        return emp;

}

 

void func2(Employee * emp2)

{

        if (strstr((*emp2).name, "Smith") != NULL)

            emp2->id = emp2->id - 2000;

      else

            emp2->id = emp2->id - 1000;

 

}

 

void main (void)

{

        Employee emp1 = {3774, "Robert Smith"};

        Employee emp2;

 

        emp2 = func1(emp1);

        printf("%i, %s\n", emp2.id, emp2.name);

 

        func2(&emp1);

        printf("%i, %s\n", emp1.id, emp1.name);

}

 

 

_______4774, Bob Smith__________________________________________________

 

 

 

 

_______1774, Robert Smith__________________________________________________

 

 

 

 

8.            Fill in the blanks with the output that the following program generates.

#include <stdio.h>

 

 

void swap1(int *a , int *b)

{

        int tmp;

        tmp = *a;       

        *a = *b;        

        *b = tmp;

}

 

void swap2(int * a)

{

        int tmp;

        tmp = a[0];       

        a[0] = a[1];       

        a[1] = tmp;

}

 

 

 

 

void main (void)

{

        int a[2] = {0,1};

        int b[2] = {0,1};

        swap1(a, &a[1]);

        printf("1st: (%i, %i)\n",a[0],a[1]);

        swap2(b);

        printf("2nd: (%i, %i)\n",b[0],b[1]);

          

}

 

Output:


1st: ( __________1____________ , ___________0_________________ )



2nd: (__________1___________ , ____________0_______________ )



 

 

 

 

 

 

 

9.            Complete the following program by filling in the blanks in agreement with the instructions found immediately above the blanks.

 

#include <stdio.h>

 

typedef struct

{

        char name[20];

        int  ssn;

} Student;

 

void print_student(Student* s)

{

        printf("Name: %s,   SSN: %i\n", s->name, s->ssn);

}

 

void main(void)

{

        char stu_name[] = "Bob Smith";

        int stu_ssn = 123456789;

        Student bob;

 

/* copy the value of stu_name to the name field of Student bob */

        ______strcpy(bob.name, stu_name);__________________

 

/* assign the value of stu_ssn to the ssn field of Student bob */

        _____bob.ssn = stu_ssn;____________________________

/* call print_student to print the values of Student bob */

        print_student(___&bob_________);

   

}

 

The output from this program should be:

 

Name: Bob Smith,   SSN: 123456789

 

 

 

 

 

 

 

 

 

 

 

10.        You are given the variable ptr declared below. Using either of the C functions calloc or  malloc, write a single C statement to allocate memory for an array with the name ptr that holds 100 integer values.

 
  
int *ptr;

 

 

 

      _________ptr = calloc(100 , sizeof(int));   OR_______ ptr = malloc(100 * sizeof(int));   ________________________

 

 

 

 

11.        The Fibonacci sequence is defined as follows: the first and second terms are 0 and 1 respectively, and each successive term is the sum of the two immediately previous terms. The first few terms in the Fibonacci sequence are thus 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

 

Complete the code for the function named fib by filling in the blanks. When the complete program runs it should correctly output the first N terms of the Fibonacci sequence. Your program must work correctly for any non-negative integer value assigned to N (found in main), not just 10. Your function must use recursion.

 

 

#include <stdio.h>



int fib(int n) {

 

  /* base case (Voom!!!) is where n = 0 or 1  */

 

     if ( ______(n==0)_____|| (n==1)_______________________ )



                    return ____________n_________________________ ;

 

           else

 

                return _____________fib(n-1)+fib(n-2)_______ ;

 

}

 

 

 

void main(void) {

  int N = 10;

  int i;

  for (i=0; i < N; i++)

    printf("fib(%i) = %i \n", i, fib(i));

}

12.        Assume the data type complex has been defined,

 

typedef struct {

  double real;

  double imaginary;

} complex;

 

Assume that the variables c , ptr and x have been declared as follows:

 

complex c = { 3.0, 4.0};

complex * ptr = &c;

double x;

 

Circle each valid statement (a statement that does not produce compiler error). There may be more than one statement that is valid.

 

a)  x = ptr.real;

 




b)  x = ptr->real;

 




c)  x = c.real;

 




d)  x = (*ptr).real;

 




e)  x = c->real;

 

 

 

 

f)  x = real.imaginary;

 

 

13.        The program below compiles without errors and executes correctly.  Circle the correct output the program outputs. Circle ONE answer.

 

#include <stdio.h>

 

int func(int x, int y) {

    if (x == 0)

         return 1;

    else

       return y * func(x-1, y);

}

 

void main(void){

    printf("%i \n", func(3,2));

}

 

a.   0

 

 

b.  1

 

 

c.   7

 

 

 

d.  8

 

 

 

e.   9

 

 

 

f.   a blank space

 

 

14.        From MP 2, we defined a structure named Album as follows:

 

typedef struct {
   int     cdno;       /* cd number */
   char    title[30]; 
   char    artist[20]; 
   int     year;
   int     num_tracks; /* number of tracks */
   int     quantity;
   float   price;
   float   sales[MONTHS];  /* cd sales ($US) over the 12 months in 2005 */
   Track   tracks[20]; /* info for each track */
} Album;

 

Write the complete code for the function named findCDNO. The first line of the function is given to you below. As a reminder, the MP 2 instructions state:

Input parameters: an array of albums, the number of albums in the array, and the target cdno
Return: the array index of the album with the given cdno or -1 if there is no such album

This function is going to find an album having the given cdno number. If it finds one, it returns the array index for that album. If it doesn't find one, it returns -1. You don't need to print any error message if the album is not found; you just return -1. Note that the cdno is unique for each album, so there is at most one album containing the given cdno number.

Do NOT write code for the main function or any other function except findCDNO.

Do NOT include comments.

You will receive ZERO points for this problem if you use a printf or scanf function in       your code.

 

int findCDNO( Album albums[], int num_albums, int cdno )

{

 

int i;

 

for (i=0; i < num_albums ; ++i)

     if ( albums[i].cdno == cdno)

           return i;

 

return -1;

 

 

}

15.  From MP 2, write the complete code for the function named compareCDNOAsc. The first line of the function is given to you below. As a reminder, from MP 2 the compareCDNOAsc would be called by the qsort function to sort the array named albums in ascending order by cdno ( 1001 before 1003).

 

Do NOT write code for the qsort or the main function or any other function except compareCDNOAsc.

Do NOT include comments.

You will receive ZERO points for this problem if you use a printf or scanf function in your code.

 

int compareCDNOAsc( Album *a, Album *b )

{

 

 

if ( a->cdno > b->cdno)

     return 1;

else if ( a->cdno < b->cdno)

          return -1;

     else

          return 0;

 

}

 

 

 

 

 

 

 

16.                          Given the structures, 

typedef struct {

  int month;

  int day;

} date;





typedef struct {

  int hour; /* using military time */

  int minute;

} time;



typedef struct {

  date  d;

  time  t;

} event;

complete the following C code by filling in the blanks to assign to the variable named graduation the date 5/14 (in month/day format) and time 10:30 am (in hour/minute format).
The value for month has already been assigned for you.

 

/* declare the variable "graduation" of data type "event" */

event graduation;
 
/* assign the month */

graduation.d.month = 5;                  



____
graduation.d.day = 14;______________________________    /* assign the day */



____
graduation.t.hour = 10; ______________________________   /* assign the hour */



____
graduation.t.minute = 30;______________________________   /* assign the minute */