Name_______________________ Login___________________SID________

 

CS 010: Introduction to Computer Science I

Quiz 5: 10 minutes

 

  1. What is output by the following program?

 

#include <iostream>

using namespace std;

 

void do_something(int& x)

{

   x = x * 2;

}

 

int main()

{

   int a = 1;

   while (a < 10)

   {

      cout << a << “ “;

      do_something(a);

   }

   return 0;

}

 

a.      1 2 3 4 5 6 7 8 9

b.      1 2 4 8

c.      8

d.      16

e.      Infinite loop, program never ends

f.        Program will not compile

 

  1. Which is the BEST choice for a header for the following function?

 

        ??????

{

   int product = 1;

   for (int i = 1; i < n; i++)

   {

      product = product * i;

   }

   return product;

}

 

a.      void factorial()

b.      int factorial()

c.      int factorial(int& n)

d.      int factorial(int n)

  1. Which is the BEST choice for a header for the following function?

 

        ??????

{

   a = n;

   b = n * n;

  

   return;

}

 

a.      void do_something()

b.      void do_something(int a, int b, int n)

c.      void do_something(int& a, int& b, int& n)

d.      int do_something(int& a, int& b, int& n)

e.      void do_something(int& a, int& b, int n)

f.        int do_something(int a, int b, int& n)

 

 

Use the following function prototypes (declarations) for the next 2 questions.

 

                                    void func1(int n, double m, string s);

               int func2(int& n);

               bool func3(int n);

 

  1. Which statement has an error given n1 is an integer variable, d1 is a double variable, and s1 is a string variable?

 

a.      func1(10, 20.0, “abc”);

b.      func1(func2(n1), d1, s1);

c.      if (func3(n1)) cout << s1;

d.      n1 = func2(n1);

e.      cout << func2(func1(n1, d1, s1));

f.        B and E

 

  1. Which statement does NOT have an error given n1 is an integer variable, d1 is a double variable, and s1 is a string variable?

 

    1. n1 = func1(10, 20.0, “abc”);
    2. cout << func2(10)
    3. s1 = func3(10);
    4. while (func2(n1) < 100) func1(n1, d1, s1);
    5. bool b1 = func3(10);
    6. D and E

 

  1. A function may be defined within another function.

 

    1. TRUE
    2. FALSE

 


  1. Which expression is equivalent to the following expression?

 

!(m < n && o == p)

 

    1. !(m < n) || !(o == p)
    2. m >= n && o != p
    3. m >= n || o != p
    4. A and C

 

  1. Which expression is equivalent to the following expression?

 

m > n || !(o <= p && q > r)

 

    1. m <= n && o > p || q < r
    2. !(m <= n && o <= p && q > r)
    3. m > n || o > p || q <= r
    4. m > n || o > p && q <= r
    5. A and B
    6. B and C

 

  1. What is output by the following code fragment?

 

int i;

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

{

   cout << i << “ “;

}

cout << i << endl;

 

    1. 0 1 2 3 4 5
    2. 1 2 3 4 5
    3. 0 2 4 6
    4. 0 2 4
    5. 2 4 6 8
    6. this is an infinite loop

 

  1. Which for loop would produce the output: 5 4 3 2 1

 

    1. for (int i = 5; i > 1; i--) cout << i << “ “;
    2. for (int i = 1; i < 5; i--) cout << i << “ “;
    3. for (int i = 5; i > 0; i--) cout << i << “ “;
    4. for (int i = 0; i < 5; i--) cout << i << “ “;