Sample

Final Written Examination

IC210

Closed Book, 3 Hours

 

 

Precedence Table


 

  1. Which of the following is NOT a valid identifier (i.e. CANNOT be used as a name for a variable or function, etc.).

 

    1. number_of_elements
    2. int
    3. ARRAYSIZE
    4. num2
    5. All are valid

 

 

  1. A compiler will catch which of the following types of programming errors?

 

    1. syntax errors
    2. run_time errors
    3. logic errors
    4. All of the above
    5. None of the above

 

 

  1. What is the value of product after the following statements?

double x = 0, y = 9, product;

x * y = product;

 

    1. 9.0
    2. 0.0
    3. 90.0
    4. syntax error (explain): cannot have an expression (x*y) on the left hand side of an assignment statement                                                                                               .                    

 

 

  1. Which of the following choices will NOT legally get 2 values from the user and assign them to the variables num1 and num2?

 

a.       cin >> num1;

cin >> num2;

b.      cin >> num1 >> num2;

c.       cin >> num1

            >> num2;

d.      cin >> num1, num2;

 

 

 

 

 

 

 

 

 

  1. What is the output of the following code fragment?

char x, y, z;

x = ‘y’;

y = ‘z’;

z = x;

cout << x << y << z << ‘z’;

 

    1. xyzz
    2. yzxz
    3. yzyz
    4. yzzz
    5. xyyz

 

  1. What is the value of the variable result after the following code fragment?

double result, n1 = 6, n2 = 4, n3 = 2;

result = n1 / (n2 * n3);

 

    1. 0.0
    2. 0.75
    3. 2.0
    4. 3.0
    5. Garbage (result was not initialized)

 

  1. What will be output to the screen in the following code fragment?

 

int x = 20;

if (x <= 20)

{

cout << “True ”;

}

cout << “Goodbye”;

 

    1. True Goodbye
    2. True
    3. Goodbye
    4. Nothing is output to the screen
    5. Syntax error (missing the else)

 

 

  1. Which of the following is NOT a boolean comparison operator (used to compare 2 values in a Boolean expression)?

 

    1. >=
    2. !=
    3. =
    4. > 
    5. They are all boolean comparison operators

 

 

  1. For what values of temperature will the output to the screen be “Just right!”?

if (temperature <= 65 )

{

            cout << “Too cold!\n”;

}

else if (temperature <= 85)

{

            cout << “Just right!\n”;

}

else

{

            cout << “Too hot!\n”;

}

    1. All values less than or equal to 65
    2. All values less than or equal to 85
    3. All values between 65 and 85, including 65 and 85
    4. All values between 65 and 85, including 65 but not 85
    5. All values between 65 and 85, including 85 but not 65
    6. All values greater than or equal to 85

 

 

  1. What is the output of the following code fragment?

 

int i = 0;

while (i <= 10)

{

            i = i + 2;

            cout << i << “ “;

}

 

Output _______________________

 

  1. What is the output of the following code fragment?

 

int i = 30;

while (i >= 20)

{

            cout << i << “ “;

            i = i – 2;

}

 

Output __________________________

 

 

 

 

 

  1. The following loop sums positive integers read in from the user keeping track of how many were read in. Give an expression for the loop condition if we choose to end the loop with a sentinel value of a negative number read in from the user.

 

int next = 0, sum = 0, cnt = -1;

 

cout << “Enter as many positive integers as you want. “;

cout << “To stop enter a negative number\n”;

 

do

{

            cnt++;

                              sum += next;

            cin >> next;

                  }while ( ?? );

 

Answer ________________

 

 

 

  1. What is the return type of the function declared below?

int return_grade(char score);

 

    1. int
    2. grade
    3. char
    4. score
    5. none of the above

 

 

  1. Which of the following function headers is the best choice for this function definition?

            ?(function header goes here)

            {

                        cout << “Enter the first value\n”;

                        cin >> first;

                        cout << “Enter the second value\n”;

                        cin >> second;

                        return;

            }

 

    1. void get_input(int first, int second)
    2. void get_input(int& first, int& second)
    3. int get_input(int first, int second)
    4. int get_input(int& first, int& second)

 

 

 

 

 

  1. Which one of the following statements is true?

 

a.       Void functions must contain a return statement.

b.      Void functions must not contain a return statement.

c.       Non-void functions must not contain multiple return statements.

d.      Non-void functions must contain a return statement.

e.       Non-void functions must not contain a return statement.

 

 

  1. Give a correct call to the function prototype: int something(double x, int y, char z);

 

 

 

 

  1. Which of the following is a correct call to the function declared below? You may assume the variables double_var, int_var, and char_var were declared and initialized as the correct type.

 

void doSomething(double& x, int& y, char& z);

 

a.       void doSomething(double& x, int& y, char& z);

b.      void doSomething(double_var, int_var, char_var);

c.       doSomething(double_var, int_var, char_var);

d.      doSomething(double& x, int& y, char& z);

e.       doSomething(8.5, 9, ‘a’);

f.        c and e

 

 

 

  1. What is the output of the following code fragment given this definition of the function something?

 

int something(int& a, int b)

{

            int c = a + b;

                              a = a * 2;

                              b = c + a;

                              return c;

                  }

 

                  // a fragment of main

                  int r = 1, s = 2, t = 3;

                  s = something(t, r);

                  cout << r << “ “ << s << “ “ << t << endl;

 

a.       1 4 3

b.      10 4 6

c.       1 4 6

d.      4 3 10

e.       10 4 3

f.        1 2 6

 

  1. Which of the following statements output the value 3.5 to the screen?

 

a.   cout << double(7) / 2;

b.   cout << double(7 / 2);

c.   cout << 7 / 2;

d.   cout << 7 / 2.0;

e.   a, b, and d

f.    a and d

 

20. Which of the following Boolean expressions evaluate to true?

 

    1. true || true
    2. true || false
    3. false || true
    4. false || false
    5. a, b, and c

 

 

in.txt

 
21. What is the output of the following code fragment assuming fin is an ifstream object that has been connected to the file in.txt correctly?

7 3 1 7  

1 2 4 7

 
 


int next, sum = 0;

while (fin >> next)

{

            if ((next % 2) = = 0)

{

sum += next;

                                                }

}

cout << sum;

 

Answer _____________

 

21. Which statement correctly opens the file input.txt and connects it to the ifstream object fin?

 

a.       open(fin, “input.txt”);

b.      fin = “input.txt”;

c.       fin = open(“input.txt”);

d.      connect(fin, “input.txt”);

e.       fin.open(“input.txt”);

 

 

 

 

 

 

 

Use the following structure definitions and object declarations for the next 5 questions

(#22 - # 26)

 

struct Book

{

   string name;

   string author;

   int num_of_pages;

   char type;                 

};

 

struct Child

{

  string name;

  double height;

  double weight;               

   int age;

  Book favorite_book;

};

 

 

Child next_child, prev_child;

Book new_book;

 

22. Which statement correctly initializes the name of a child’s favorite book?

 

a.   next_child.favorite_book.name = “Goodnight Moon”;

b.   next_child.name = “Goodnight Moon”;

c.   next_child.favorite_book = “Goodnight Moon”;

d.   Child.Book.name = “Goodnight Moon”;

e.   favorite_book.name = “Goodnight Moon”;

 

23. Which statement correctly initializes a child’s age?

 

a.   Child.age = 2;

b.      next_child = 2;

c.       next_child.age = 2;

d.      age = 2;

 

24. Which of the following is NOT a legal C++ statement?

 

                                     a.                                                      prev_child = next_child;

                                    b.      Book old_book = new_book;

                                     c.      Child new_child = prev_child.name;

                                    d.      next_child.age = 3;

                                     e.      next_child.weight = prev_child.weight;

 

25. What is the type of next_child.favorite_book.num_of_pages?

 

                        Answer _____________

 

26. What is the type of new_book?

 

Answer ______________

 

 

27. What would be the output of the following code fragment?

 

for (int i = 9; i > 0; i = i - 2)

{

            cout << i << “ “;

}

 

a.       9 8 7 6 5 4 3 2 1

b.      9 8 7 6 5 4 3 2 1 0

c.       9 7 5 3 1

d.      9 7 5 3 1 -1

e.       infinite loop

 

28. For the arrays declared below, explain which of the following statements should not be used:

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

char grade[5] = {‘F’, ‘D’, ‘C’, ‘B’, ‘A’};

 

a.       if (stu_score == score[i])

cout << grade[i];

b.      cout << score[0];

c.       cin >> score[5];

d.      while (grade[i] > ‘F’)

cin >> grade[i];

Explain why _________ _____________

 

 

29. Assuming the array below had been initialized to some values, which statement would correctly move each element of the array up one spot. Don’t worry about the one lost value, but do not access values “out of bounds”?

     

       int array[10];

 

a.    for (int i = 0; i < 9; i = i + 1)

                                         array[i] = array[i + 1];

 

b.    for (int i = 0; i < 10; i = i + 1)

                                         array[i] = array[i + 1];

 

c.    for (int i = 1; i < 9; i = i + 1)

           array[i - 1] =  array[i];

 

d.    for (int i = 1; i < 10; i = i + 1)

           array[i - 1] = array[i];

 

e.    a and d

 

 

 

 

 

30. What is the output of the following code?

 

int array[5];

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

{

            array[i] = i * 3;

}

for (int i = 4; i >= 0; --i)

{

            cout << array[i] << “ “;

}

 

a.       4 3 2 1

b.      4 3 2 1 0

c.   12 9 6 3

d.   12 9 6 3 0

e.    9 6 3 0

 

31. Why should you use a named constant for the size of an array?

 

a. Readability of code

b. Makes changes to the program easier

c. Helps reduce logic errors

d. All of the above

 

 

 

32. Which is a correct call to this function?

           

int out_of_order(double array[], int size)

            {

                        for (int i = 0; i < size – 1; i = i + 1)

                        {

                                    if (array[i] > array[i + 1])

                                    {

                                                return (i +1);

                                    }

                        }

                        return -1;

            }

 

a.       cout<< out_of_order(double array[], int size)

b.      cout << out_of_order(arr[], 5)

c.       cout << out_of_order(arr, 5)

d.      cout << out_of_order(double array[size])

 

 

 

 

33. Explain why which of the following is NOT a legal statement in C++?

 

a.       string name;

b.      string name = “”;

c.       string name = “Justin”;

d.      They are all legal statements.

 

Explain why ______________________________

 

 

34. The syntactically correct way in C++ to declare and initialize, in a single step, a pointer to a previously declared variable s, of type double, is

a.       double* p = &s;

b.      double &p = *s;

c.       double* p = s;

d.      double p = *s;

e.       double* p = *s;

 

35. After the following statements are executed, what can be said about variables x and p?


char x = ‘M’;
char* p = &x;
*p = ‘W’;

a.       p holds the value ‘M’.

b.      p holds the value ‘W’.

c.       p points to the variable x, which has the value ‘M’

d.      p points to the variable x, which has the value ‘W’

e.       p holds the value ‘W’, x holds the value ‘M’

f.        The statements cannot be executed because of a compiler error.

 

36. If p1 and p2 are pointers to two different variables v1 and v2 of the same type, with p1 pointing to v1 and p2 pointing to v2, then which of these statements is correct?

a.       *p1 = *p2; makes the value of p1 equal to the value of p2.

b.      *p1 = *p2; makes the value of v2 equal to the value of v1.

c.       *p1 = *p2; makes the value of p2 equal to the value of p1.

d.      *p1 = *p2; makes the value of v1 equal to the value of v2.

e.       None of the above.

 

37. Given that int* p; declares a pointer to an integer, which of the following is not correct:

a.       *p = 7; is a valid C++ statement.

b.       p = new int(7); is a valid C++ statement.

c.       Following the declaration, p points to an integer variable allocated in the heap area.

d.       p = &x; is a valid C++ statement, provided that x is declared as an integer variable in the same scope of p and declared prior to the statement.

 

 

38. Suppose you have a pointer variable p and you execute the statement delete p;. What happens?

a.       p is deleted from the heap.

b.       p is deleted from the stack.

c.       The variable which p points to is deleted but p still remains.

d.       Only the value of the variable which p points to is deleted; the variable itself remains.

e.       Both the variable which p points to, as well as p itself, are deleted.

 

39. What happens when the statement bool *p = new bool(true); is executed?

a.       p is set to the value true.

b.       A nameless variable of type bool is allocated in the heap and given the value true.

c.       A nameless variable of type bool is allocated in the stack and given the value true.

d.       The statement cannot be executed because it does not compile under any standard implementation of the C++ language.

e.       The argument true is passed to the function named bool and the result is assigned to *p.

 

40. Suppose that p1 and p2 are both pointers to the same integer variable x; y is another integer variable. After the execution of which of the statements below is the pointer variable p1 considered a dangling pointer?

a.       x = 0;

b.       p1 = &y;

c.       p2 = new int(7);

d.       delete x;

e.       delete p2;

 

41. After the following statements are executed, what is the value of *p?

 char x = ‘M’;

 char * p = NULL;

 p == &x;

 p = ‘W’;

a.       ‘M’.

b.       ‘W’.

c.       The statements cannot be executed because the compiler complains about the statement p == &x;.

d.       The statements cannot be executed because the compiler complains about the statement p = ‘W’;.

e.       None of the above.

 

42. All the code between
            #ifndef  MYSTRUCT_H

                        and

           #endif

            is ____________ if  MYSTRUCT_H is defined.

a.       skipped

b.       executed

c.       compiled

d.       debugged

 

43. What (if anything) is wrong with the following recursive function? It should print out the contents of the array backwards.

void print(int* array, int start, int size)

{

     if(start == size)

           return;

     else

     {

           print(array, start+1,size);

           cout << array[start] << endl;

     }

}

a.       infinite recursion

b.      the stopping condition is wrong

c.       the recursive call is wrong

d.      nothing, this works fine as defined

 

44.     Given the below struct Node, and assuming that LIST is a Node* that points to the beginning of the linked list shown below, give C++ source code that sums the data fields of the list and prints the answer to the console. Be sure that you do not lose the head of the list!  

struct Node
{
    int data;
    Node* next;
};

 

 

 

 

 

 

 

 


45.     Fibonacci numbers are a fascinating number sequence that crops up in the most unexpected places – from galaxies to snails to the stock market (show me the money!). The sequence is defined as:

       F( 0 ) = 0; F( 1 ) = 1; (termination)
       F( n ) = F( n-1 ) + F( n-2 )

so the first few numbers in the sequence are:

F(0) = 0, F(1)=1, F(2) = 1, F(3) = 2, F(4) = 3, F(5) = 5, F(6) = 8, F(7) = 13, F(8) = 21, F(9) = 34 ….

Define a recursive function to calculate the nth Fibonacci number. The function prototype is:

int fib( int n );                 //returns F(n)

 

 

 

 

 

 

 

 

 

46.     Now define an iterative version of the fibonacci function. Use the same function prototype as above.

Remember that each term is built up from the values of the two terms before it, meaning that inside the loop body you will need to hold the values of two previous terms. (It might ((or might not)) help you to think of the “swap” function).