CSC 126                                 FINAL EXAMINATION                                Fall 2002A

 

Name (last, First) ____________________________   __________________________

 

Recitation/Lab Section Professor (circle one) Prof Sciove  Prof Kumar Jagadish  Prof Sri

Pentapati   Prof Nevins  Prof Domanski

 

Lecture Professor Name (circle one)  Prof Imberman    Prof Zelikovitz   Prof Domanski

Prof Fan

 

Question #

Total Possible

Total Received

1.

5

 

2.

6

 

3.

5

 

4.

4

 

5.

7

 

6.

7

 

7.

4

 

8.

10

 

9.

6

 

10.

6

 

11.

8

 

12,

9

 

13.

4

 

14.

4

 

15.

15

 

TOTAL

100

 


1. (5 points) Examine the following program and convert the if…else statement to a switch statement.  You need only rewrite the switch statement, not the entire program. 


#include <iostream>

using namespace std;

 

void main()

{

   int num;

  

   cout<<"Enter in a number: ";

   cin>>num;

 

   #include <iostream>

using namespace std;

 

void main()

{

     int num;

    

     cout<<"Enter in a number: ";

     cin>>num;

 

     if (num == 1)

           cout<<"One partridge\n";

     else if (num == 2)

           cout<<"Two turtle Doves\n";

     else if (num == 3)

           cout<<"Three French hens\n";

     else

           cout<<"Enjoy the Break\n";

}

 

  

 


     2. (6 points) What is printed by the following C++ program?

 

#include <iostream>

#include <iomanip>

using namespace std;

 

int

main()

{

     float num1, num2;

 

     num1 = 13.689;

     num2 = 1441141.98;

 

     cout << num1 << endl;

     cout << num2 << endl;

         

     cout << setiosflags(ios::fixed);

     cout << setiosflags(ios::showpoint);

 

     cout << setprecision(2) << num1 << endl;

     cout << num2 << endl;

      

     cout << '|' << setw(8) << num1 << '|' << endl;

     cout << '|' << setw(8) << num2 << '|' << endl;

         

     return 0;

}

 


3. (5 points) Suppose x and y are int variables and ch is a char variable. Assume the following input data:

14  28  D

13  E  98

B  C  56

 

What value (if any) is assigned to x, y and ch after the following statements execute?

 

 

            cin.get(ch);

     cin.ignore(50, '\n');

     cin >> x;

     cin.ignore(50, 'E');

cin >> y;

cout << x << ' ' << y << ' ' << ch;

 

 

 

 

 

 

 

 

 

 

4. (4 points) What is printed by the following program segment?

 

     x = 9;

     y = 6;

     z = 4;

 

     if ((x < y)&&(y != 7)) cout << "Georgie\n";

     else if ((z < 7)||(y == 6))cout << "Porgie\n";

     else cout << "Pudnin\n";

     cout << "Pie\n";

     cout<<"Kissed the girls and made them cry!"<<endl;
5. (7 points) Match the term in COLUMN A with the BEST phrase in COLUMN B.  Write the appropriate letter of the phrase in the blank space to the left of the term

 

COLUMN A                                      COLUMN B

 

____1. Interpreted language

 

____2. source code

 

____3.secondary storage

 

____4.ALU

 

____5. Syntax

 

____6. RAM

 

____7. Assembler

 

 

a. Directs and controls the overall operation of the computer.

 

b. The set of rules for formulating grammatically correct language statements..

 

c. Contains fundamental instructions that cannot be lost or changed..

 

d. Where your programs and data are temporarily stored while you use the computer. 

 

e. Performs arithmetic and logic functions such as addition , subtraction , comparison .

 

f. A language, which translates each statement individually and executes immediately upon translation

 

g. A permanent storage area for programs and data.

 

h. A program that translates a program written in  assembly language into equivalent program in  machine  language.

 

i. Computer instructions which have not been compiled yet

 

 

 

 

 


6. (7 points) Suppose you have strings:

 

char S1[ ] = {“rodent”};

char S2[ ] = {“dog”};

char S3[20];

 

int X, Y, Z;

 

a)      What is the value of  S2[1]? ______________________

 

 

b)      X = strlen (S1);  What value does X receive? _________________

 

 

c)       Z = strcmp (S1,   S2); What is the value of Z?_________________

 

 

d)        After strcpy (S3, S1) what is in S3?________________

 

 

e)                  For string S5 of unknown length and value write C++ to count the number of  question marks in string S5.

 

 

 

 

 

 

 

 

 

 

 

7. ( 4 points) The following  for  loop prints out all number between 0 and 100 that are divisible by n.  Write a C++  while  loop that will do exactly the same thing.

 

int i;

int n = 2;

 

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

   if (!(i % n)) cout<<i;


8. (10 points)  Each line of the following C++ program is numbered.  Use the line numbers to answer  questions  a through  f.

 

#include <iostream>                        //Line 1

using namespace std;                       //Line 2

 

int func1(int& a, int b);                  //Line 3

void func2(int& r, int& s);                     //Line 4

 

void main()                                //Line 5

{                                          //Line 6

   int num1 = 5;                                //Line 7

   int num2 = 10;                          //Line 8

  

   cout << "func1 returns "               

<< func1(num1, num2) << endl;              //Line 9

   cout << "num1 = " << num1              

      << " num2 = " << num2 << endl;       //Line 10

 

   func2(num2, num1);                      //Line 11

   cout << "num1 = " << num1              

<< " num2 = " << num2 << endl;       //Line 12

}                                         

 

int func1(int& a, int b)                   //Line 13

{                                          //Line 14

    int f;                               //Line 15

     f = a + b;                           //Line 16

     a++;                                 //Line 17

     b++;                                 //Line 18

return f;                            //Line 20

}                                          //Line 21

 

void func2(int& r, int& s)                 //Line 22

{                                         

     int k;                               //Line 23

    

     k = r;                               //Line 24

     r = s;                               //Line 25

     s = r + k;                           //Line 26

      }

 


a)                  Which lines contain function headers?

 

 

b)                  Which line contains the function prototype for function func2?

 

 

c)                  List variable(s) that is/are local to function func1.

 

 

d)                  List all reference parameters .

 

 

e)                  List all value parameter(s).

 

 

f)                    What is printed when the program is executed?

 

 

 

 

9. (6 points) Given the following variable declarations:

int y;

float x;

 

What is assigned to x or y for each of the following C++ assignment statements:

 

a)                  y = 14 % 5 * 2 - 3 / 2;

 

 

 

 

 

 

b)                  y = (25 / 2.0 + 2.5 )* 3 / 6;

 

 

 

 

 

 

 

 

 


10. (6 points) A C++  program contains the following statements:

 

     float a,*pa;

     float b, *pb;

     float c;

    

     a = 3.0;

     b = 1.9;

 

     pa = &a;

     *pa = 3*a;

 

     pb = &b;

     *pb += 0.5;

 

     c = *pa +*pb;

 

Suppose each floating-point number occupies 4 bytes of memory.  Assume further that the address of variable a is  128800, the address of variable b is  128804, and the address of variable c is  128808.

 

Draw a layout of memory, its variables and its pointers.  Then answer the following questions. 

 

 i) What value is assigned to pa?_______________________

 

ii) What value is represented by *(&a)? ________________________ 

 

iii) What value is represented by *pa? __________________________

 

iv) What value is represented by &(*pb)? _______________________

 

v)  What value is assigned to c?    ____________________________

 

vi) What value is assigned to pb?    ___________________________

   

 


11.  The following C++ program contains a function that sorts the elements in an array.

 

#include<iostream>

#include<cstdlib>

using namespace std;

 

void sortThem(int alpha[], int num);

 

int main()

{

     int a[] = {2, 6, 9, 3, 5};

     sortThem(a,5);

     return 0;

}

 

void sortThem(int alpha[], int num)

{

     int i, j, z, temp;

     bool exchange = 1;

     while (exchange && i < num - 1)

     {

          exchange = 0;

 

          for (j=1; j<num; j++)

          {

              if (alpha[j] < alpha[j-1])

              {

                   temp = alpha[j];

                   alpha[j] = alpha[j-1];

                   alpha[j-1] = temp;

                   exchange = 1;

              }

          }

 

          i++;

 

          for (z=0; z<num; z++)

              cout << alpha[z] <<" ";

 

          cout << endl;

     }

}

 


a)                  (6 points) What exactly is printed to the screen?

 

 

 

 

 

 

 

 

 

 

b)                  (2 points) Which sorting algorithm does the program implement?

 

 

 

 

 

 

12. Consider the following declaration:

 

            int nums [3] [4];

 

a) (5 points) Draw a diagram of nums showing the number of rows and columns.  What is stored in nums after each of the following statements executes?

 

 

for (int i=0; i<3; i++)

          for (int j=0; j< 4; j++)

              nums [i] [j] = i + j;

 

 

 

 

 

b) ( 4 points) Determine and print the sum of all elements of  nums  that are greater than 95.95 (DO NOT WRITE A COMPLETE PROGRAM).

 

 

 

 

 


13.   (4 points) What is printed by the following program segment?

 

            int i, j, counter = 0;

     for (j = 15; j < 25; j= j + 2)

     {

          if (j < 20)

               cout << "small value" << endl;

          else

          {

            cout << "j is " << j << endl;

            counter++;

          }

          cout << counter << endl;

     }

 

 

 

 

 

 

 

 

 

 

 

         14.  (4 points) Write C++ code, using (nested) for loops, that will print out the following pattern.

 

 

0

# 0

# # 0

# # # 0

# # # # 0

# # # # # 0 

# # # # # # 0

 

 

 

 


15. (15 points) Cheap Chirps is a pet store specializing in pet birds.  The store needs a new inventory program and has hired you to develop the program.  Write a C++ program that will:

 

1.      Read an inventory list of birds from a data file located on the company's hard drive (C drive) called BIRD.txt.    The file contains information on each bird species in the store.  Each line of the file has type of bird (no larger than 20 characters), price of the bird, and number pounds of food consumed in a month.  YOU DO NOT KNOW how many bird types there are in the file. 

 

Sample Input file BIRD.txt  (remember the "real" file has an unknown number of lines)

 

MACAW               2,500.00          20

AMAZON 550.75             15

COCKATIEL        123.50             2

PARAKEET          45.50               1

 

2.      Your program will call a function that will return a character 'L if the bird is a large hookbill or an 'S' if the bird is a small hookbill.  Large hookbills eat 15 or more pounds of food a month. 

 

3.      Your program will also calculate the average price of a bird and the average amount of food eaten in one month. 

 

4.      Print the output so that it is organized as below.  Note the values for the averages are printed to two decimal places.  All printing is done in the main function. 

 

 

BIRD           PRICE          FOOD           HOOKBILL

-----------------------------------------------------------

MACAW          2500.00        20             L

AMAZON         550.75         15             L

COCKATIEL      123.50         2              S

PARAKEET       45.50          1              S

The average price is:  804.94

The average food is: 9.50