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