Name_______________________ Login___________________SID________

 

CS 010: Introduction to Computer Science I

Quiz 7: 10 minutes

 

  1. In the following if structure, what is the least number of statements that could be executed?

if ( … )

            stmt;

                  else if ( … )

                              stmt;

                  else if ( … )

                              stmt;

 

a.       0

b.      1

c.       2

d.      3

e.       can not be determined

 

  1. Which Boolean expression is equivalent to the following condition

x is not equal to the strings “a” and “b”, both lower and uppercase

 

a.       (x != “a” || x != “A” || x != “b” || x != “B”)

b.      ((x != “a” || “A”) && (x != “b” || “B”))

c.       !(x == “a” || x == “A”) || !(x == “b” || x == “B”)

d.      (x != “a” && x != “A” && x != “b” && x != “B”)

e.       None of the above

 

  1. The following two sets of code produce the same results:

if (x==1 && y==2)                                          if (x==1)

            cout << "HELLO\n";                            {

else                                                                              if (y == 2)

cout << "GOODBYE\n";                                              cout << "HELLO\n";

else

                                                                                                cout << "GOODBYE\n";

                                                                              }

    1. TRUE
    2. FALSE

 

  1. Which of the following for loops will output all integers between AND INCLUDING the values stored in the integer variables first and second? You may assume first is less than or equal to second.

 

a.       for (int i = 0; i < second; i++) cout << first;

b.      for (int i = first; i <= second; i++) cout << first;

c.       for (int i = 0; i <= second; i++) cout << i;

d.      for (int i = first; i <= second; i++) cout << i;

e.       for (int i = second; i > first; i--) cout << second;

  1. Any do-while loops can be re-written to use a while loop.

a.       TRUE

b.      FALSE

 

  1. What does the following loop print?

double a = 1;

double b = 1;

int i = 0;

      do {

                  b = b / 2;

                  a = a + b;

                  i = i + 2;

      }while (a < 1.8);

      cout << i;

 

a.       2

b.      3

c.       4

d.      6

e.       None of the above

 

7.      How many “*” will be output when the following code fragment is executed?

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

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

                              cout << “*”;

a.       m

b.      n + m

c.       n * m

d.       (n – 1) * (m – 1)

e.       None of the above

 

  1. Which for loop sets the value of all elements of the integer vector v to random numbers?

 

a.       for (int i = 0; i < v.size() - 1; i++) v[i] = rand()%100;

b.      for (int i = 0; i < v.size(); i++) v[i] = rand()%100;

c.       for (int i = 1; i < v.size(); i++) v[i] = rand()%100;

d.      for (int i = 1; i < v.size() + 1; i++) v[i] = rand()%100;

e.       for (int i = 0; i <= v.size(); i++) v[i] = rand()%100;

 

  1. Which statement about vectors is TRUE.

a.       A function cannot return a vector.

b.      Vector parameters should never be passed by reference.

c.       All elements of a vector must be of the same type.

d.      Vectors cannot contain strings.

 

  1. Using an index out of bounds of the vector size is a syntax error?

a.       TRUE

b.      FALSE