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 = ________________________ , y = _____________________________

 

 

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 = __________________________ , y = ______________________________

 

 

 

 

 

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 = __________________________ , y = ______________________________

 

 

 

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.            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( __________________ , __________________);

 

 

   if ( result != 0)

      return result;

   else

      return _______________________________________________ ;

 

}

 

 

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(_____________,______________,______________,_______________);
   

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

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

     printf("%s %s %i\n", _____________, _____________, _____________);

}

 

 

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

}

 

 

__________________________________________________________________________