Make your own free website on Tripod.com

Webster University

COSC 1550 – Introduction to Programming

Test Three

Name  _______________

 

Multiple Choice

Circle the letter of the statement that bests answers/completes each question.  Each question is worth three points.

 

1.                  Which one of the following statements regarding the operation of the while command is incorrect?

            a)         The associated loop instructions execute if the condition evaluates to false.
            b)         It is necessary that the control variable be modified inside the loop.
            c)         If the fourth instruction in a loop containing seven instructions changes the                                   control variable to a value that indicates that the loop should not be executed,                statements five through seven will still execute.
            d)         It is possible that the loop is never executed, depending on the value of the                                 control variable.

 

2.                  Which one of the following types of loops would you use if the body of the loop must be executed at least once?

            a)         while

            b)         do….while

            c)         for

            d)         case

 

3.                  When does the preincrement operator actually increment the value of its operand by one (e.g. ++counter)?
a)         Before the expression in which it appears is evaluated.
b)         After the expression in which it appears is evaluated.
c)         While the expression in which it appears is evaluated.
d)         At the end of the block in which it is located.

 

 

4.         A(n) _________ is information that is passed to a function, and a(n) _________ is information that is defined by a function’s header.

            a.         function call, function header

            b.         formal parameter, actual parameter

            c.         actual argument, formal parameter

            d.         prototype, header


5.         Given the following do…while loop,

 

            do

            {

                        cout << “Enter a number” << endl;      

                        cin >> Number;

            }while(Number>=50);

 

What values will prevent the loop from executing thus allowing the next statement to execute?

a)         only those values equal to 50.
b)         only those values greater than 50.
c)         all values greater than or equal to 50.

d)         all values less than 50.

 

 

6.         In a loop, both the break statement and continue statement cause the loop to terminate early.  Which one of the following statements bests describes the difference between break and continue?

      a)         A break statement causes the current iteration of the loop to terminate and                                 begins the next iteration and the continue statement causes the loop to                          terminate completely.
      b)         A break statement causes the current iteration of the loop to terminate and                                 begins the next iteration and the continue statement causes the program to                                terminate completely.
      c)         A continue statement causes the current iteration of the loop to terminate and                 begins the next iteration and the break statement causes the loop to terminate                      completely.
      d)         A continue statement causes the current iteration of the loop to terminate and                 begins the next iteration and the break statement causes the program to                               terminate completely.

7.         Which one of the following statements regarding function calls is not correct?
a)         The argument list can contain the same number of arguments as the        parameter list in the function header.
b)         The argument list must contain arguments whose data types are the same as       the parameter list data types or C++ will attempt data conversions.
c)         All data types within the argument list must be the same.
d)         A function call can pass multiple data values to the function.

 

8.         When passing data to a function and returning a single value back from the function, which one of the following statements is not true?
a)         The values passed to the function must be passed by reference.
b)         The value is returned to the calling function using the
return command.

c)         The function header of the called function must be defined with a return data type.

d)         The function call can be on the right side of an assignment statement.

 

9.         Assume the following code:

 

                        int number = 1, sum=0;

                        while (number <= 5)

                                    sum += number++;

                        cout << "The sum is " << sum << and the number is " << number << endl;

 

            Which one of the following will display on the screen?

            a)         The sum is 10 and the number is 4

            b)         The sum is 15 and the number is 6

            c)         The sum is 10 and the number is 5

            d)         The sum is 15 and the number is 5

 

10.       In the following code, what kind of loop control variable is used?

 

                  inFile >> number;

                  while (number != 9999)

                  {

                              cout << “The number in the file is “ << number << endl;

                              inFile >> number;

                  }

                             

      a)         user controlled

      b)         counter

      c)         sentinel value

      d)         system generated

 

11.       The scope of a local variable is
a)         for the duration of the entire program.
b)         from the time it is defined until the end of the program.
c)         from the time it is defined until the end of the function in which it is defined.
d)         from the time it is defined until the end of the function that calls it.

 

12.       Which one of the following statements bests describes the purpose of a function prototype?
a)         It acts as a function declaration similar to a variable declaration in that it             lets C++ know that the function has been defined for use.
b)         It defines the function and its associated code to the calling program.
c)         It is used to invoke the execution of a function.
d)         It defines a function to be a global function, not a local function.


13.       Which one of the following statements causes a function to end?

      a)         break

      b)         return

      c)         continue

      d)         release

 

14.       Which one of the following statements regarding functions is correct?

      a)         A function can have only one parameter and one returned value.

      b)         A function can have many parameters and many returned values.

      c)         A function can have only one parameter and many returned values.

      d)         A function can have many parameters and one returned value.

 

15.       Which one of the following is not required in a function header?

      a)         data type(s) of parameter(s)

      b)         ending semi-colon

      c)         function name

      d)         data type of return value

 

16.       A function is actually executed when it is

      a)         defined.

      b)         prototyped.

      c)         declared.

      d)         called.

 

17.       If a loop does not contain something that causes the test to eventually become false, which one of the following situations would occur?

      a)         an infinite loop

      b)         a compiler error

      c)         a run-time error

      d)         an edit error

 

18.       A loop inside of another loop is known as a(n)

      a)         infinite loop.

      b)         pre-test loop.

      c)         post-test loop.

      d)         nested loop.

 

19.       Which one of the following statements in a for statement is executed only once?

            a)         update of the control variable

            b)         initialization of the control variable

            c)         test of the control variable

            d)         validation of the control variable


20.       Which one of the following function prototypes is the correct one to use with the following function?

 

                        void DoubleNum (int& RefVar)

                        {

                                    RefVar  *= 2;

                        }

 

            a)         void DoubleNum (int);

            b)         void DoubleNum ( & );

            c)         void DoubleNum (int&);

            d)         int& DoubleNum ( );

 

21.       Using the function in the previous problem, which one of the following statements correctly invokes the function?  Assume the following additional declarations:

 

                        int Value = 4;

                        int Number;

 

            a)         Number = DoubleNum (&Value);

            b)         Number = DoubleNum (Value);

            c)         DoubleNum (&Value);

            d)         DoubleNum (Value);

 

22.       When an argument is passed to a function, which one of the following statements is correct?

            a)         If passed by value, the function can modify the value of the argument in                          the calling function and if passed by reference the function cannot modify                            the value of the argument in the calling function.

            b)         If passed by reference, the function can modify the value of the argument                                   in the calling function and if passed by value the function cannot modify                                  the value of the argument in the calling function.

            c)         If passed by value, the function cannot modify the value of the argument                                     in the calling function and if passed by reference the function cannot                           modify the value of the argument in the calling function.

            d)         If passed by reference, the function can modify the value of the argument                                   in the calling function and if passed by value the function can modify the                                 value of the argument in the calling function.


23.       Given the following for loop,

 

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

                                                cout << “i = “ << i<< endl;

 

            Which one of the following statements is incorrect regarding its operation?

            a)         The value of i when the loop body begins is 0.

            b)         The value of i is compared to 10 and if i is less than 10, the loop executes.

            c)         Before the beginning of every iteration of the loop, the value of i is                                  incremented by 1.

            d)         This loop will execute 10 times.

 

24.       Assume a double variable called average is declared as a global variable.  Which one of the following statements is not true about average?

a)         All functions in the program have access to average without passing it as a parameter.

b)         If a function declares its own variable named average, references to average in that function do not refer to the global average.

      c)         The default value for average is set to zero.

      d)         average is actually declared inside the main function.

 

25.       Which type of variable retains its value between successive calls to the function in which is declared?

      a)         pointer variable

      b)         static variable

      c)         address variable

      d)         local variable

 

26.       If a parameter is not included in a function call, it may still have a value in the function provided it has which of the following in the function prototype?

      a)         a default argument

      b)         a static argument

      c)         an initial argument

      d)         a global argument

 

27.       A stub is

      a)         a program used in testing and debugging that tests a function by simply   calling it.

      b)         a dummy function used in testing and debugging that is called instead of the actual function it represents.

      c)         a called function that calls another function.

      d)         an empty array.


28.       The exit function will

      a)         end the currently executing program when invoked.

b)         end the currently executing function when invoked, and continue the program at the statement following the function call.

c)         end the currently executing while statement when invoked, and continue the program at the statement following the while statement.

d)         end the currently executing iteration of the while statement when invoked, and continue the program at the next iteration of the while statement.

 

29.      An overloaded function is one that

a)         is able to be called from another function without the need for a function prototype.

b)         contains more parameters than C++ allows.

c)         can return multiple values to the calling function.

d)         has two or more definitions in the same program and with the same function name and different parameter lists.

 

Problems

Identify any problems you can locate in the following code segments.  You may assume all variables have been correctly declared.  You need to look for syntax errors and logic errors.  Each question is worth 3 points each.

 

30.                   count = 1;

while (count < 10);

                        {

                                    cout << “The next number is “ << count-- << endl;

                        }

 

 

 

 

 

 

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

                                    cout << “The next value of i is “ << endl;

                                    sum += i;

                        average = sum / i;

                        cout << “The average of the first 5 integers is “ << average << endl;


Note: Questions 32–34 are based on the following function:

 

void nPrint(char pChar, int n)

{

  while (n >=0)

  {

    cout << pChar;

    n--;

  }

}

 

32.       What is the printout of the call nPrint('*', 4);?

 

 

 

 

           

33.       What value of k will display after invoking nPrint() from the previous question?

 

int k = 3;

nPrint('$', k);

cout << "K=" << k << endl;

 

           

 

 

           

34.       What value of k (if any) will display after the following block executes?

 

{  

  int k = 2;

  nPrint('#', k);

}

cout << "K=" <<  k << endl;