Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 8 Question 100 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 100
Topic #: 8
[All CPP Questions]

What happens when you attempt to compile and run the following code?

#include

#include

#include

#include

#include

#include

using namespace std;

class B { int val;

public:

B(int v=0):val(v){}

int getV() const {return val;}

operator int() const { return val; };};

templatestruct Out {

ostream & out;

Out(ostream & o): out(o){}

void operator() (const T & val ) {out<

int main () {

int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

fstream f("test.out", ios::trunc|ios::out);

list l(t, t+10);

for_each(l.begin(), l.end(), Out(f));

f.close();

f.open("test.out");

for( ; f.good() ; ) {

B i;

f>>i;

cout<

}

f.close();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Louvenia
1 months ago
Ah, the joys of C++ programming. Where the syntax is like a maze, and the output is a riddle wrapped in an enigma. I wonder if the exam will have a bonus round for interpretive dance?
upvoted 0 times
Audria
30 days ago
User 1
upvoted 0 times
...
...
Virgina
1 months ago
I'm going with E. The code looks like it's writing the contents of the list to the file, and then reading them back out. Should be a piece of cake! Now, if only the exam questions were this straightforward...
upvoted 0 times
...
Melissa
1 months ago
This is a tricky one! I'm leaning towards C, since the code is opening the file for reading after writing to it. But then again, the output suggests E might be the right answer. Decisions, decisions...
upvoted 0 times
...
Lucina
1 months ago
Hmm, I'm torn between B and E. The code seems to be opening the file for writing, and then reading from it. I'll go with B, just to be safe. Wouldn't want to end up with a compile error!
upvoted 0 times
...
Makeda
1 months ago
I'm not sure, but I think it might be D) compilation error.
upvoted 0 times
...
Eleonore
2 months ago
I agree with Amira, because the code is opening the file in trunc mode.
upvoted 0 times
...
Charlene
2 months ago
The code looks correct, so I'd say the correct answer is E. The program will display the sequence 1 2 3 4 5 6 7 8 9 10. Can't wait to see if I get this one right!
upvoted 0 times
Ahmed
15 days ago
Yes, the correct answer is E. The program will display the sequence 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...
Ahmed
19 days ago
I think you're right, the program should display the sequence 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...
...
Amira
2 months ago
I think the answer is B) file test.out will be truncated.
upvoted 0 times
...