Name_______________________ Login___________________SID________

 

CS 010: Introduction to Computer Science I

Quiz 6: 10 minutes

 

 

  1. The following for loop and while loop will have the exact same output.

int i = 100;

while (i > 0)

{

            cout << i * i << “ “;

            i--;

}

cout << endl;

 

 

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

{

            cout << i * i << “ “;

}

cout << endl;

 

 
 

 

 

 

 

 

 

 

 

 


a.       TRUE

b.      FALSE

 

  1. The following for loop and while loop will have the exact same output.

int i = 100;

while (i > 0)

{

i--;

            cout << i * i << “ “;

}

cout << endl;

 

 

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

{

            cout << i * i << “ “;

}

cout << endl;

 

 
 

 

 

 

 

 

 

 

 

 


a.       TRUE

b.      FALSE

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. What is output by the following code?

 

int main()

{

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

{

                        cout << i << “ “;

}

cout << i << endl;

return 0;

}

 
 

 

 

 

 

 

 

 

 

 

 

 


a.       0 1 2 3 4

b.      0 1 2 3 4 5

c.       1 2 3 4 5

d.      4

e.       5

f.        one of the cout statements has a syntax error because of the scope of i

 

  1. What is output by the following code?

int main()

{

int i;

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

{

                        cout << i << “ “;

}

cout << i << endl;

            return 0;

}

 
 

 

 

 

 

 

 

 

 

 

 


a.       0 1 2 3 4

b.      0 1 2 3 4 5

c.       1 2 3 4 5

d.      4

e.       5

f.        one of the cout statements has a syntax error because of the scope of i

 


  1. What is output by the following code?

int x = 10;

do

{

x -= 3;

            cout << x << “ “;

} while (x > 0) ;                       // do while x is greater than 0

cout << x << endl;

 

 
 

 

 

 

 

 

 

 

 

 


    1. 10 7 4 1 -2
    2. 7 4 1 -2 -2
    3. 7 4 1 1
    4. 10 7 4 1
    5. none of the above

 

  1. What is output by the following code?

int x = 10;

do

{

x -= 3;

            cout << x << “ “;

} while (x > 10) ;                       // do while x is greater than 10

cout << x << endl;

 

 
 

 

 

 

 

 

 

 

 

 


    1. 10
    2. 7  7
    3. 7 4
    4. 10 7
    5. none of the above

 

 

  1. Out of the following function headers, which is the BEST choice for a function that just reads in 2 integers from the user and “passes” them back to where the function was invoked (called)?

 

    1. void get_inputs(int& input1, int& input2)
    2. void get_inputs(int input1, int input2)
    3. int get_inputs(int input1, int input2)
    4. int get_inputs(int& input1, int& input2)
    5. int get_inputs()
    6. void get_inputs()

 

  1. A do while loop guarantees that the body of the loop will execute at least once.

 

    1. TRUE
    2. FALSE

Use the following code for the next 2 questions

 

                        void f1(int& x)

                        {

                                    x = 100;

                        }

                       

                        void f2(int a, int& b)

                        {

                                    a = a * b;

                                    b = 12;

                                    if (b < a)

                                    {

                                                f1(b);

                                    }

                                    else

                                    {

                                                f1(a);

                                    }

                        }

 

                        int main()

{

            int num1 = 2;

            int num2 = 4;

            f2(num1, num2);

            cout << num1 << “ “ << num2 << endl;            //OUTPUT 1

            num1 = 4;

            num2 = 5;

            f2(num1, num2);

            cout << num1 << “ “ << num2 << endl;            //OUTPUT 2

            return 0;

}

 

  1. What will OUTPUT 1 print?
    1. 2 4
    2. 100 4
    3. 8 12
    4. 100 12
    5. 2 12
    6. none of the above

 

  1. What will OUTPUT 2 print?

 

    1. 20 5
    2. 20 100
    3. 4 100
    4. 4 5
    5. 20 12
    6. none of the above