C++ Mid Term

47 Questions | Total Attempts: 204

SettingsSettingsSettings
Please wait...
C Plus Plus Quizzes & Trivia

Questions and Answers
  • 1. 
    If a function needs to modify more than one variable, it must
    • A. 

      Be pass by value

    • B. 

      Be a void function

    • C. 

      Return all values needed

    • D. 

      Be a call by reference function

  • 2. 
    When a void function is called, it is known as
    • A. 

      An output function.

    • B. 

      A returned value.

    • C. 

      An executable statement.

    • D. 

      A comment

  • 3. 
    What is the output of the following code fragments? int trial( int& a, int b) {             if(b > a)             {                         a=b;                         return –a;             }             else             {                         return 0;             } }   float x=0, y=10,z; z=trial(y,x); cout << z << " " << x <<" " << y << endl;
    • A. 

      –10 0 0

    • B. 

      0 1 0 0

    • C. 

      1 0 0 0

    • D. 

      0 0 1 0

  • 4. 
    Which of the following comments would be the best post-condition for this swap function void swap( int& left, int&right);
    • A. 

      //Postcondition: None

    • B. 

      //Postcondition: the values of left and right are exchanged.

    • C. 

      //Postcondition: left has the value of right

    • D. 

      //Postcondition: left and right are unchanged

  • 5. 
    Call-by-reference should be used
    • A. 

      For all variables

    • B. 

      When the function needs to change the value of one or more arguments

    • C. 

      Never

    • D. 

      Only in void functions

  • 6. 
    A simplified main program used to test functions is called
    • A. 

      A stub

    • B. 

      Abstraction

    • C. 

      Polymorphism

    • D. 

      A driver

  • 7. 
    When a variable is local to a function, we say that it has ___ of the function
    • A. 

      Value

    • B. 

      Constance

    • C. 

      Scope

    • D. 

      Locality

  • 8. 
    Testing your program should be done
    • A. 

      As each function is developed

    • B. 

      At the end of the coding

    • C. 

      Only if there appear to be problems

    • D. 

      Only if your instructor requires it.

  • 9. 
    If you write a function that should use call-by-reference, but forget to include the ampersand,
    • A. 

      The program will not compile

    • B. 

      The program will not link

    • C. 

      The program will not run without a run-time error

    • D. 

      The program will run but probably not give you the correct information.

  • 10. 
    What is wrong with the following code? int f1( int x, int y) {             x = y * y;             return x;             int f2( float a, float& b) {             if(a < b)                         b = a;             else                         a=b;             return 0.0;        } }
    • A. 

      Neither function should return a value

    • B. 

      Function definitions may not be nested

    • C. 

      Both parameters to f2 should be pass-by reference

    • D. 

      In f2, a can not be assigned b.

    • E. 

      Nothing is wrong

  • 11. 
    In the following function, what is passed to the first parameter? void f1( int& value1, int value2);   int x,y; f1(x,y);
    • A. 

      The value of x

    • B. 

      Nothing, it is a void function

    • C. 

      The value of y

    • D. 

      The variable x (or its memory location)

  • 12. 
    the fabs(double num) function
    • A. 

      Returns the most fabulous number

    • B. 

      Returns the largest whole number

    • C. 

      Returns the negative value of num

    • D. 

      Returns the absolute value of num

  • 13. 
    What is the output of the following program fragment? cout << static_cast<int>(3/4) << endl;
    • A. 

      3

    • B. 

      0.5

    • C. 

      0

    • D. 

      0.75

  • 14. 
    If you need to write a function that will compute the cost of some candy, where each piece costs 25 cents, which would be an appropriate function declaration?
    • A. 

      Int calculateCost(char name);

    • B. 

      Char calculateCost(int count);

    • C. 

      Int calculateCost int count;

    • D. 

      Int calculateCost(int count);

  • 15. 
    What is the value returned by the following function? int function() {       int value = 35;       return value + 5;       value += 10; }
    • A. 

      35

    • B. 

      40

    • C. 

      50

    • D. 

      10

  • 16. 
    When overloading a function, what must be true?
    • A. 

      The names should be different with the same number and/or types of parameters.

    • B. 

      The names should be the same with different number and/or types of parameters.

    • C. 

      The names should be different with different number and/or types of parameters.

    • D. 

      The names should be the same with the same number and/or types of parameters.

  • 17. 
    When parameters are passed between the calling code and the called function, parameters and arguments are matched by:
    • A. 

      Their data types

    • B. 

      Their relative positions in the parameter and argument lists

    • C. 

      Their names

    • D. 

      They are not matched up at all.

  • 18. 
    The expression static_cast<int>(3) is called a
    • A. 

      Type cast

    • B. 

      Multiplier

    • C. 

      Doubler

    • D. 

      Polymorphism

  • 19. 
    If the variable x has the original value of 3.4, what is the value in x after the following? cout << static_cast<int>(x);
    • A. 

      3.4

    • B. 

      4

    • C. 

      3

    • D. 

      Unknown

  • 20. 
    What is the value of the following? sqrt(sqrt(pow(2,4)));
    • A. 

      1

    • B. 

      2

    • C. 

      4

    • D. 

      16

  • 21. 
    What is the value of i after the following function call? //function definition int doSomething(int value) {             value = 35;             return value;             value = 13 }   //fragment of main program int i=0; cout << doSomething(i);
    • A. 

      13

    • B. 

      35

    • C. 

      48

    • D. 

      0

  • 22. 
    What is the output of the following code fragement?   double size, volume=16.0; size = sqrt(sqrt(volume)) / 3; cout << fixed << showpoint <precision(2);
    • A. 

      0.67

    • B. 

      0.6666667

    • C. 

      0.00

    • D. 

      0

  • 23. 
    Information Hiding is analogous to using
    • A. 

      An algorithmic design

    • B. 

      A black-box methodology

    • C. 

      Actual parameters

  • 24. 
    What is the output of the following function call? //function body int factorial(int n) {       int product=0;       while(n > 0)       {                   product = product * n;                   n—;       }       return product; }   //function call cout << factorial(4);
    • A. 

      4

    • B. 

      0

    • C. 

      24

    • D. 

      48

  • 25. 
    Which of the following are equivalent to (!(x<15 && y>=3))?
    • A. 

      (x>15 && y

    • B. 

      (x>=15 && y < 3)

    • C. 

      (x>=15 || y < 3)

    • D. 

      (x>15 || y < 3)

Back to Top Back to top