Program Analysis

Help Questions

AP Computer Science A › Program Analysis

Questions 1 - 10
1

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

2

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

3

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

4

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

5

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

6

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

7

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

8

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

9

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

10

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

The outputFile should be of type "ofstream", not "ifstream".

There is nothing wrong with the code.

The ifstream library should be included.

Testfile.txt shouldn't have double quotations around it.

Explanation

Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.

Page 1 of 91
Return to subject