Last Updated on May 20, 2022 by Admin 3

Advanced Input and Output Operations M8 Test

  1. What happens if you try to compile and run this program?

    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      double goodpi = 3.1415;
      double badpi = 3.2;
      cout << goodpi <<", ";
      cout << scientific; //LINE I
      cout << setprecision(3); //LINE II
      cout << goodpi <<", ";
      cout.unsetf(ios::floatfield);
      cout<<badpi <<", ";
    
      return 0;
    }
    • the program outputs 3.1415, 3.142e+00, 3.2,
    • the program outputs 3.1415, 3.200000, 3.14, 3.200, 
    • the program outputs 3.1415, 3.1415, 3.2, 
    • compilation error in LINE II
    • runtime error at LINE II
    • compilation error in LINE I
    • the program outputs 3.1415, 3.142e+00, 3.20, 
  2.  What will happen when you attempt to compile and run the following code, assuming you enter following sequence: 9 8 7 <enter> ?

    #include <stdio.h>
    #define X 1
    #define Y 2
    
    int main(void){
      int i =
    
    #if X >> y > 0
      -X
      -X
    #else
      -Y
    #endif
      ;
    
      printf("%d", i);
    
      return 0;
    }
    • the program outputs 7, 8, 9, 
    • the program outputs 8, 7, 9, 
    • the program outputs 8, 9, 7, 
    • the program outputs 7, 9, 8, 
  3. What happens if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    
    int
    main()
    {
      cout << 127.45 <<", ";
      cout.setf(ios::hex, ios::basefield);
      cout.setf(ios::showbase); //LINE I
      cout << 127.45<<", ";
      cout.unsetf(ios::showbase); //LINE II
      cout <<127.45 <<", ";
      return 0;
    }
    • the program outputs 127.45, 127.45, 127.45, 
    • the program outputs 127.45, 0x7f.2d, 7f.2d, 
    • the program outputs 127.45, 0x7f.2d, 0x7f.2d, 
    • compilation error in LINE I
    • the program outputs 127.45, 0x7f, 7f, 
    • compilation error in LINE II
    • runtime error at LINE II
  4. What happens if you try to compile and run this program?

    #include <iomanip>
    #include <iostream>
    using namespace std;
    
    int
    main()
    {
      double goodpi = 3.1415;
      double badpi = 3.2;
      cout << goodpi <<", ";
      cout << badpi <<", ";
      cout <<setprecision(3); //LINE I
      cout << goodpi <<", "; //LINE II
      cout << badpi <<", ";
      return 0;
    }
    • runtime error in LINE II
    • the program outputs 3.1415, 3.2, 3.1, 3.2, 
    • the program outputs 3.1415, 3.2, 3.14, 3.2, 
    • the program outputs 3.1415, 3.2, 3.1415, 3.20, 
    • compilation error in LINE I
    • compilation error in LINE II
    • the program outputs 3.1415, 3.2, 3.14, 3.20, 
  5. What happens if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    
    int
    main()
    {
      cout << 127 <<", ";
      cout.setf(ios::hex); //LINE I
      cout << 127 <<", ";
      cout.setf(ios::showbase, ios::basefield); //LINE II
      cout << 127 <<", ";
      return 0;
    }
    • runtime error in LINE II
    • the program outputs 127, 0x7f, 0x7f
    • the program outputs 127, 127, 127, (but it is unpredictable result)
    • the program outputs 127, 7f, 0x7f
    • compilation error in LINE I
    • the program outputs 127, 7f, 7f
    • compilation error in LINE II
  6. What happens if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    
    int
    main()
    {
      cout << 127 <<", ";
      cout.setf(ios::hex, ios::basefield); //LINE I
      cout << 127 <<", ";
      cout.setf(ios::showbase); //LINE II
      cout << 127 <<", ";
      return 0;
    }
    • the program outputs 127, 0x7f, 0x7f
    • compilation error in LINE I
    • compilation error in LINE I
    • the program outputs 127, 127, 127,
    • runtime error at LINE II
    • the program outputs 127, 7f, 7f
    • the program outputs 127, 7f, 0x7f
  7.  What happens if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    int
    main()
    {
      cout << 127 <<", ";
      cout.setf(ios::hex, ios::basefield); //LINE I
      cout << 127 <<", ";
      cout.setf(ios::showbase); //LINE II
      cout << 127 <<", ";
      return 0;
    }
    • the program outputs 127, 127, 127,
    • the program outputs 127, 7f, 0x7f
    • the program outputs 127, 0x7f, 0x7f
    • runtime error at LINE II
    • compilation error in LINE II
    • compilation error in LINE I
    • the program outputs 127, 0x7f, 7f
  8. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 9 8 a<enter> ?

    #include <iostream>
    #include <string>
    
    using namespace std;
    int
    main()
    {
      string s;
      getline (cin, s); //LINE I
      cout << s <<", "<< s <<", " <<endl; //LINE II
      return 0;
    }
    • the program outputs 9 8 a, 9 8 a,
    • the program outputs 9,
    • compilation error in LINE II
    • compilation error in LINE I
    • the program outputs 9, 8, 
    • runtime error at LINE II
    • the program outputs 9, 9,
  9. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 9.9 8.8 7.7 <enter> ?

    #include <iostream>
    using namespace std;
    
    int
    main()
    {
      double c1, c2, c3;
      cin >> c1 >> c2 >> c3;
      cout << c3 <<", "<< c1 <<", " << c3 <<", " <<endl; //LINE II
      return 0;
    }
    • the program outputs 7.7, 8.8, 9.9, 
    • the program outputs 7.7, 9.9, 8.8, 
    • the program outputs 7, 9, 8, 
    • the program outputs 7, 8, 9, 
  10.  What happens if you try to compile and run this program?

    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    int
    main()
    {
      double goodpi = 3.1415;
      double badpi = 3.2;
      cout << goodpi <<", ";
      cout << badpi <<", ";
      cout << setprecision (); //LINE I
      cout << goodpi <<", "; //lINE II
      cout << badpi <<", ";
      return 0;
    }
    • compilation error in LINE I
    • the program outputs 7, 8, 9,
    • runtime error at LINE II
    • the program outputs 3.1415, 3.2, 3.1, 3.2,
    • the program outputs 3.1415, 3.2, 3.1415, 3.20,
    • the program outputs 3.1415, 3.2, 3.14, 3.20,
    • compilation error in LINE II
  11. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 7 8 9<enter> ?

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    void
    printer (int i)
    {
      cout << i <<", ";
    }
    
    int
    main()
    {
      vector <int> v1;
      int i;
      do
      {
        cin >> i;
       v1.push_back(i); //LINE I
      }
      while(i != 9 && !cin.bad()); //LINE II
      for_each(v1.begin(), v1.end(), printer);
      return 0;
    }
    • the program outputs 7, 8
    • runtime error at LINE I
    • the program outputs 7, 8, 9,
    • compilation error in LINE II
    • the program outputs 7, 8, 9, 9,
    • program runs forever without outputs
    • compilation error in LINE I
  12. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 7 8 9<enter> ?

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    void
    printer (int i)
    {
      cout << i <<", ";
    }
    
    int
    main()
    {
      vector <int> v1;
      int i;
      do
      {
        cin >> i;
        v1.push_back(i); //LINE I
      }
      while(i != 9 && !cin.bad()); //LINE II
      for_each(v1.begin(), v1.end(), printer);
      return 0;
    }
    • the program outputs 7, 8,
    • the program outputs 7, 8, 9, 9,
    • runtime error at LINE I
    • the program outputs 7, 8, 9,
    • compilation error in LINE I
    • compilation error in LINE II
    • program runs forever without output
  13.  What happens if you try to compile and run this program?

    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      cout << 127 <<", ";
      cout.setf(ios::hex, ios::basefield); //LINE I
      cout << 127 <<", ";
      cout.setf(ios::showbase); //LINE II
      cout << 127 <<", ";
      return 0;
    }
    • runtime error at LINE II
    • the program outputs 127, 127, 127,
    • the program outputs 127, 7f, 0x7f,
    • the program outputs 127, 0x7f, 0x7f,
    • the program outputs 127, 7f, 7f,
    • compile error in LINE II
    • compile error in LINE I
  14.  What happens if you try to compile and run this program?

    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      cout << 127 <<", ";
      cout.setf(ios::oct, ios::basefield); 
      cout.setf(ios::showbase); //LINE I
      cout << 127 <<", ";
      cout.unsetf(ios::showbase); //LINE II
      cout << 127 <<", ";
    
      return 0;
    }
    • the program outputs 127, 0x177, 177,
    • compile error in LINE II
    • the program outputs 127, 127, 127,
    • compile error in LINE I
    • the program outputs 127, 0177, 177,
    • runtime error at LINE II
    • the program outputs 127, 0177, 0177,
  15.  What happens if you try to compile and run this program?

    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      double goodpi = 3.1415;
      double badpi = 3.2;
      cout << goodpi <<", ";
      cout << badpi <<", ";
      cout.setf(ios_base::showbase); //LINE O
      cout << goodpi <<", "; //LINE II
      cout << badpi <<", ";
    
      return 0;
    }
    • the program outputs 3.1415, 3.2, 3.14150, 3.20000,
    • the program outputs 3.1415, 3.2, 3.14, 3.2,
    • compile error in LINE I
    • the program outputs 3, 3, 3.1415, 3.2
    • the program outputs 3, 3, 3.14, 3.20
    • runtime error at LINE II
    • compile error in LINE II
  16. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: r e w qw q<enter> ?

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    void
    printer(string i)
    {
    cout << i <<", ";
    }
    int
    main()
    {
      vector <string> v1;
      string s;
      do
      {
         cin >> s;
         v1.push_back(s); //LINE I
      }
      while(s != "q" && cin.good()); //LINE II
      for_each(v1.begin(), v1.end(), printer);
    
      return 0;
    }
    • the program outputs r, e, w, w
    • compile error in LINE II
    • the program outputs r, e, w, qw, q,
    • the program outputs r, e, w, w, q,
    • compile error in LINE II
    • runtime error at LINE I
    • the program outputs r, e, w, q,
  17. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 9.9 8.8 7.7<enter> ?

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int
    main()
    {
      double c1, c2, c3;
      cin >> c1 >> c2 >> c3;
      cout << c3 <<", "<< c1 <<", "<< c2 <<", " <<endl;
    
      return 0;
    }
    • the program outputs 7, 8, 9,
    • the program outputs 7.7, 9.9, 8.8,
    • the program outputs 7, 9, 8,
    • the program outputs 7.7, 8.8, 9.9,
  18.  What will happen when you attempt to compile and run following code?

    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      cout << 127 <<", ";
      cout.setf(ios::hex); //LINE I
      cout << 127 <<", ";
      cout.setf(ios::showbase); //LINE II
      cout << 127 <<", ";
      return 0;
    }
    • compilation error in LINE II
    • the program outputs 127, 7f, 7f
    • runtime error at LINE II
    • the program outputs 127, 0x7f, 0x7f
    • the program outputs 127, 127, 127 (but it is unpredictable result)
    • the program outputs 127, 7f, 0x7f
    • compilation error in LINE I
  19.  What happen if you try to compile and run this program?

    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      double goodpi = 3.1415;
      double badpi = 3.2;
      cout << goodpi << ", ";
      cout << badpi << ", ";
      cout.setf (ios_base::showbase); //LINE I
      cout << goodpi << ", "; //LINE II
      cout << badpi <<", ";
      return 0;
    }
    • compilation error in LINE II
    • the program outputs 3, 3, 3.14, 3.2
    • the program outputs 3, 3, 3.14, 3.20
    • the program outputs 3.1415, 3.2, 3.14, 3.2,
    • compilation error in LINE I
    • runtime error at LINE II
    • the program outputs 3.1415, 3.2, 3.14150, 3.20000,
  20.  What happen if you try to compile and run this program?

    #include <iostream>
    using namespace std;
    
    int
    main()
    {
      cout << false <<", "; //LINE I
      cout << boolalpha <<", "; //LINE II
      cout << true << ", ";
    
      return 0;
    }
    • the program outputs 0, 0, true,
    • the program outputs 0, , true,
    • compilation error in LINE II
    • runtime error at LINE II
    • compilation error in LINE I
    • the program outputs 1, 0, 0,
    • the program outputs 0, 0, 1,