Name_______________________ Login___________________SID________

 

CS 010: Introduction to Computer Science I

Quiz 6: 10 minutes

 

  1. How many asterisks will be output by the following code?

 

 

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

{    

   cout << “*“;

}

 

a.      0

b.      1

c.      n - 1

d.      n - m

e.      n + m

f.        n - m + 1

 

  1. How many asterisks will be output by the following code?

 

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

   {

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

      {

          cout << “*“;

      }

      cout << endl;

   }

 

a.      0

b.      1

c.      m * n

d.      m + n

e.      (m - 1) + (n - 1)

f.        (m + 1) * (n + 1)

 

  1. How many columns of asterisks will be output by the code in question 2?

 

a.      0

b.      1

c.      m

d.      n

e.      m - 1

f.        m * n

 

 

 

  1. What happens when you put a semicolon right after a do while loop’s conditional expression as in the following example?

 

do

{

   cout << n << “ “;

   n++;

} while (n <= 10);

 

a.      If n is less than 10, this will be an infinite loop.

b.      This is a syntax error.

c.      This will output the integers from n to 10 if n is less or equal to 10.

d.      Nothing happens. The while loop is skipped.

 

 

  1. What happens when you put a semicolon right after a while loop’s conditional expression as in the following example?

 

while (n <= 10);

{

   cout << n << “ “;

   n++;

}

 

a.      If n is less than 10, this will be an infinite loop.

b.      This is a syntax error.

c.      This will output the integers from n to 10 if n is less or equal to 10.

d.      Nothing happens. The while loop is skipped.

 

 

  1. What is output by the following code?

 

int n = 11;

do

{

   cout << n << “ “;

   n++;

} while (n < 10);

 

    1. There will be no output.
    2. 10 11
    3. 11
    4. 10 10
    5. 11 12
    6. There is a syntax error.

 


 

  1. How many elements does the vector v have after the following declaration?

 

vector<int> v(n);

 

    1. 0
    2. 1
    3. n
    4. n - 1
    5. n + 1
    6. none of the above

 

  1. What is the index number of the last element in vector v from question 7?

 

    1. 0
    2. n + 1
    3. n - 1
    4. n
    5. 1
    6. none of the above

 

  1. Which statement(s) outputs the entire contents of vector v from question 7.

 

    1. for (int i = 1; i <= n; i++) cout << v[i];
    2. for (int i = 0; i < v.size(); i++) cout << v[i];
    3. cout << v;
    4. cout << v[n];
    5. for (int i = 1; i <= v.size(); i++) cout << v[i];
    6. A, B, and C all output the entire contents of vector v.

 

 

  1. Is it possible to say there is definitely a syntax error in the following code fragment without knowing anything else about the C++ program it came from other than that v is a vector?

 

v[0] = 10;

v[1] = s;

v[2] = “s“;

 

    1. Yes, there is definitely a syntax error.

b.   No, we need to know more about the program this code came from to say it is definitely a syntax error.