Self Test 1

  1. What is the output from this C++ code segment?

    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  int x = 2, y = 3;
        7	  cout << x/y << "!";
        8	  
        9	  double p = 2.0, q = 3.0;
       10	  cout.setf(ios::fixed);
       11	  cout.precision(4);
       12	  cout << p/q << endl;
       13	
       14	  return 0;
       15	}
    
    1. 0!0.6666
    2. 0!0.6667
    3. 0.0000!0.6667
    4. 0.6666!0.6667
    next prev start

  2. Which of the following declares x to be of type double, results in x having the value 6.5, and has no syntax error?
    1. int x = 6.5;
      
    2. int x;
      x = 6.5;
      
    3. double x;
      x(6.5);
      
    4. double x(6.5);
      
    next prev start
  3. What is the type of the constant 0?

    1. short
    2. int
    3. long
    4. float
    5. double
    next prev start
  4. What is the type of the constant 0.0?

    1. short
    2. int
    3. long
    4. float
    5. double
    next prev start
  5. What is the output of the following statement?

    
        cout << "\"Hello\\Goodbye\"";
    
    1. Hello\\Goodbye
    2. "Hello\\Goodbye"
    3. Hello\Goodbye
    4. "Hello\Goodbye"
    next prev start
  6. What is the output of the following statement?

    
    
    	cout << "Hello\nGoodbye";
    
    1. Hello\nGoodbye
    2. HellonGoodbye
    3. Hello Goodbye
    4. Hello
      Goodbye
    next prev start
  7. What is the syntax error in the following code?

    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  const int x;
        7	
        8	  cout << "x = " << x << endl;
        9	
       10	  return 0;
       11	}
    
    1. There is no syntax error.
    2. A const declaration must include an initialization.
    next prev start
  8. The following program has no compilation errors. What is the output?

    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  int x;
        7	
        8	  cout << "x = " << x << endl;
        9	
       10	  return 0;
       11	}
    
    1. x = 0
    2. x = 4
    3. x = -858993460
    4. Not defined, will be: x = some number
    next prev start
  9. What is the output of the following code?

    
    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  int x = 8;
        7	  double y;
        8	
        9	  x++;
       10	  y = x/8;
       11	
       12	  cout.precision(4);
       13	  cout.setf(ios::showpoint);
       14	  cout << y << endl;
       15	
       16	  return 0;
       17	}
    
    1. 1.000
    2. 1.0000
    3. 1.125
    4. 1.1250
    next prev start
  10. If line 13 is removed from the code in the previous problem, what is the output?

    1. 1
    2. 1000
    3. 1.250
    4. 1250
    next prev start
  11. Line 14 has been added and line 10 has been changed. What is the output?

    
    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  int x = 8;
        7	  double y;
        8	
        9	  x++;
       10	  y = x/8.0;
       11	
       12	  cout.precision(4);
       13	  cout.setf(ios::showpoint);
       14	  cout.setf(ios::fixed);
       15	  cout << y << endl;
       16	
       17	  return 0;
       18	}
    
    1. 1.000
    2. 1.0000
    3. 1.125
    4. 1.1250
    next prev start
  12. If two operands of the division operator / are both of type int, integer division is used and any fractional part is discarded even if the result is assigned to a variable of type double.

    A type cast can be inserted by a programmer to force conversion of one of two int operands of division to be of type double.

    Assume the following declarations/with initializations of x and y:

    int x = 3, y = 2;
    double z;
    

    Which of the following will correctly assign the value 1.5 to z?

     A: z = (double) (x / y);
     B:z = (double) x / y;
     C: z = ((double) x) / y;
     D: z = static_cast<double>(x / y);
     E: z = static_cast<double>(x) / y;
    
    1. A and D only
    2. C and E only
    3. B, C, and E only
    4. E only
    next prev start
  13. The user accidentally hits the 'enter' key, then a 5, 'enter' again, then a 10 and 'enter' a third time. What is the output of the following code?

    
    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  int x, y;
        7	
        8	  cin >> x;
        9	  cin >> y;
       10	
       11	  cout << "x = " << x << ", y = " << y << endl;
       12	
       13	  return 0;
       14	}
    
    1. x = 0, y = 5
    2. x = some undefined value, y = 5
    3. x = some undefined value, y = 10
    4. x = 5, y = 10
    next prev start
  14. The variable x is declared and initialized, then y is declared and assigned an expression:

    int x = 5;
    int y;
    y = x++ + 10;
    

    What are the values of x and y after this code executes?

    1. x = 5, y = 15
    2. x = 5, y = 16
    3. x = 6, y = 15
    4. x = 6, y = 16
    next prev start
  15. If x = 345 which expression will assign the value 4 to y?

    1. y = x / 10;
    2. y = x % 10;
    3. y = (x / 10) % 10;
    4. y = (x % 10) / 10;
    next prev start
  16. Which of the following expressions will output the value of an int variable x on one line and an int variable y on the next output line?

    A: cout << x;
       cout << y;
    
    B: cout << x << endl;
       cout << y << endl;
    
    C: cout << x << y;
    
    D: cout << x << endl << y << endl;
       
    1. A, B, and D only
    2. B and D only
    3. B only
    4. A, B, C, and D
    next prev start
  17. The << operator is overloaded - it can either be the output insertion operator or the left-shift operator. If both operands are of type int, it is the left-shift.

    What is the printed output of this code?

    #include <iostream>
    using namespace std;
    int main()
    {
      int x = 5;
      int a,b;
     
      a = x << 1;
      b = x << 2;
    
      cout << a << ", " b << endl;
    
      return 0;
    }
    
    
    1. 10, 20
    2. 51, 52
    3. 6,7
    4. 5,10
    next prev start
  18. What is the output of the following C++ program?

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int x = 5;
      int y = 2;
    
      cout << x << y << " ";
      cout << (x << y) << endl;
    
      return 0;
    }
    
    1. 5 2 5 2
    2. 52 52
    3. 5 2 20
    4. 52 20
    prev start