0% found this document useful (0 votes)
14 views

Module 8

The document contains 20 code snippets with questions about the output or behavior. Most involve I/O stream manipulators, data types, or file I/O operations. Accurately summarizing each snippet would require running/testing the code, which is beyond my abilities as an AI system. I can only reason about the code based on the documentation provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Module 8

The document contains 20 code snippets with questions about the output or behavior. Most involve I/O stream manipulators, data types, or file I/O operations. Accurately summarizing each snippet would require running/testing the code, which is beyond my abilities as an AI system. I can only reason about the code based on the documentation provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1, the program outputs 9,9

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string s;
cin >> s; //line i
cout << s <<", " << s <<", " <<endl; //line ii
return 0;
}

2, 127.45, 127.45, 127.45

#include <iostream>
#include <string>

using namespace std;

int main ()
{
cout << 127.45 <<", " ;
cout.setf (ios::hex, ios::basefield);
cout.setf (ios::showbase); //linei
cout << 127.45 <<", " ;
cout.unsetf (ios::showbase); //lineii
cout << 127.45 <<", " ;

return 0;
}

3, the program outputs 9,9

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string s;
cin >> s; //line i
cout << s <<", " << s <<", " <<endl; //line ii
return 0;
}

4, without input

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>

using namespace std;


void printer (int i)
{
cout << setw (2) << i <<", ";
}

int main ()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7);
fstream outfile ("output.txt", ios::trunc | ios::out);
int i ;
while (outfile.good ()); //line i
{
outfile >> i; //line ii
v1.push_back (i);
}
outfile.close ();
for_each (v1.begin (), v1.end (), printer);
outfile.close();
outfile.open ("output.txt");

return 0;
}

5, program outputs 127,127,127

#include <iostream>
#include <string>

using namespace std;

int main ()
{
cout << 127 <<", " ;
cout.setf (ios::hex); //linei
cout << 127 <<", " ;
cout.setf (ios::showbase); //lineii
cout <<
127 <<", " ;

return 0;
}

6, compilation error in line I

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string s;
getline (s); //line i
cout << s <<", " << s <<", " <<endl; //line ii
return 0;
}

7, the program outputs 3.1415, 3.2, 3.14, 3.2,

#include <iostream>
#include <iomanip>

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;
}

8, false, false, false but not in the choices. (c3 value is undefined)
true true false (CORRECT ANSWER)

#include <iostream>
#include <string>

using namespace std;

int main ()
{
bool c1,c2,c3;
cin >> boolalpha >> c1 >> c2 >> c3;
cout << boolalpha << c3 <<", " << c1 <<", " << c2 <<", " <<endl ; //line i
return 0;
}

9, program outputs 9,8,a,9,8,a

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string s;
getline (cin,s); //line i
cout << s <<", " << s <<", " <<endl; //line ii
return 0;
}

10, program outputs 127,0177,177,

#include <iostream>
#include <string>

using namespace std;


int main ()
{
cout << 127 <<", " ;
cout.setf (ios::oct, ios::basefield);
cout.setf (ios::showbase); //linei
cout << 127 <<", " ;
cout.unsetf (ios::showbase); //lineii
cout << 127 <<", " ;

return 0;
}

11, the program outputs true, true, false,

#include <iostream>
#include <string>

using namespace std;

int main ()
{
bool c1,c2,c3;
cin >> c1 >> c2 >> c3;
cout << boolalpha << c3 <<", " << c1 <<", " << c2 <<", " <<endl ; //line i
return 0;
}

12, the program outputs 9 8 a, 9 8 a,

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string s;
getline (cin,s); //line i
cout << s <<", " << s <<", " <<endl; //line ii
return 0;
}

13, the program outputs 127, 7f, 0x7f (INCORRECT)

127,127,127
runtime error in line 2

#include <iostream>
#include <string>

using namespace std;

int main ()
{
cout << 127 <<", " ;
cout.setf (ios::hex, ios::basefield); //linei
cout << 127 <<", " ;
cout.setf (ios::showbase); //lineii
cout << 127 <<", " ;

return 0;
}

14, the answer is -2, not in the choices


8,9,7 IS INCORRECT
7, 9 , 8 RIGHT ANSWER
#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;
}

15, the program outputs 3,9,0,2,1,4,5


THE FILE OUTPUT.TXT WILL BE OPENED FOR WRITING
#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>
#include <iomanip>
#include <fstream>
using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{
}
operator int () const
{
return getValue();
} //linei

int getValue ()const


{
return value;
}

bool operator < (const Pocket & _Right) const


{
return value < _Right.value;
}

};
ostream & operator << (ostream & stream, const Pocket & pocket)
{
stream << pocket.getValue ();
return stream;
}

void printer (int i)


{
cout << i <<", ";
}

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
fstream outfile ("output.txt", ios::trunc | ios::out);
for_each (v1.begin (), v1.end (), printer);
outfile.close ();
outfile.open ("output.txt");
while (outfile.good () ) //linei
{
int i;
outfile >> i;
}
outfile.close ();

return 0;
}

16, the program outputs 7,8,9,9,

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <vector>

using namespace std;

void printer (int i)


{
cout << setw (4) << i <<", ";
}

int main ()
{
string s;
getline (cin, s);
stringstream input (s); //line i
vector <int> v1 ;
int i;
do
{
input >> hex >>i;
v1.push_back (i); //lineii
}

while (!input.fail () );
for_each (v1.begin (), v1.end (), printer);
return 0;
}

17, '

runs forever is incorrect


runtime error in line i is incorrect

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void printer (char c)


{
cout << setw (2) << c <<", ";
}

int main ()
{
ifstream inputfile ("input.txt");
vector <char>v1 ;
char c;
do
{
inputfile >> c; //linei
v1.push_back (c);
}

while (inputfile.good ()); //lineii


inputfile.close ();
for_each (v1.begin (), v1.end (), printer);
return 0;
}

18, the program outputs 0 , , true,

#include <iostream>

using namespace std;


int main ()
{
cout <<false<<", "; //line i
cout <<boolalpha<<", ";//lineii
cout<<true<<", ";

return 0;
}

19, runs forever is incorrect


runtime error in line i is incorrect

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void printer (char c)


{
cout << setw (2) << c <<", ";
}

int main ()
{
ifstream inputfile ("input.txt");
vector <char>v1 ;
char c;
do
{
inputfile >> c; //linei
v1.push_back (c);
}

while (inputfile.good ()); //lineii


inputfile.close ();
for_each (v1.begin (), v1.end (), printer);
return 0;
}

20, the program outputs 127, 7f, 0x7f, is incorrect


runtime error in line i is incorrect

#include <iostream>

using namespace std;

int main ()
{
cout << 127 <<", " ;
cout.setf (ios::hex, ios::basefield); //linei
cout << 127 <<", " ;
cout.setf (ios::showbase); //lineii
cout << 127 <<", " ;

return 0;
}

75%

!!!!!!!!!!!!

You might also like