AP Computer Science A › Identifying & Correcting Errors
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.
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.
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.
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.
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.
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.
What is wrong with the following code?
Variable j is not defined.
You cannot define the variable logic as a bool.
You must define the sum variable as an integer, not a double.
There is an opening/closing bracket missing.
The code has no errors.
If you notice, in our for loop, the integer j is used as the iteration variable. However, no where in the code is that variable defined. To fix this issue this could have been done.
for (int j=0;j<3,j++)
Note the bold here is inserted. We need to define j here. We could have also defined j as a double.
What is wrong with the following code?
Variable j is not defined.
You cannot define the variable logic as a bool.
You must define the sum variable as an integer, not a double.
There is an opening/closing bracket missing.
The code has no errors.
If you notice, in our for loop, the integer j is used as the iteration variable. However, no where in the code is that variable defined. To fix this issue this could have been done.
for (int j=0;j<3,j++)
Note the bold here is inserted. We need to define j here. We could have also defined j as a double.
What is wrong with the following code?
Variable j is not defined.
You cannot define the variable logic as a bool.
You must define the sum variable as an integer, not a double.
There is an opening/closing bracket missing.
The code has no errors.
If you notice, in our for loop, the integer j is used as the iteration variable. However, no where in the code is that variable defined. To fix this issue this could have been done.
for (int j=0;j<3,j++)
Note the bold here is inserted. We need to define j here. We could have also defined j as a double.
Consider the following code:
int num = 1;
for(int i = 0; i < 10; i++) {
num *= i;
}
The code above is intended to provide the continuous product from num to 10. (That is, it provides the factorial value of 10!.) What is the error in the code?
Both the i intialization and the loop control value are incorrect.
The i initialization is wrong.
You cannot use *= in a loop.
The loop control is incorrect.
The loop control, the i initialization, and the initialization for num are all wrong.
This loop will indeed run 10 times. However, notice that it begins on 0 and ends on 10. Thus, it will begin by executing:
num *= 0
Which is the same as:
num = num * 0 = 0
Thus, you need to start on 1 for i. However, notice also that you need to go from 1 to 10 inclusive. Therefore, you need to change i < 10 to i <= 10.
Consider the following code:
int num = 1;
for(int i = 0; i < 10; i++) {
num *= i;
}
The code above is intended to provide the continuous product from num to 10. (That is, it provides the factorial value of 10!.) What is the error in the code?
Both the i intialization and the loop control value are incorrect.
The i initialization is wrong.
You cannot use *= in a loop.
The loop control is incorrect.
The loop control, the i initialization, and the initialization for num are all wrong.
This loop will indeed run 10 times. However, notice that it begins on 0 and ends on 10. Thus, it will begin by executing:
num *= 0
Which is the same as:
num = num * 0 = 0
Thus, you need to start on 1 for i. However, notice also that you need to go from 1 to 10 inclusive. Therefore, you need to change i < 10 to i <= 10.
Consider the following code:
int num = 1;
for(int i = 0; i < 10; i++) {
num *= i;
}
The code above is intended to provide the continuous product from num to 10. (That is, it provides the factorial value of 10!.) What is the error in the code?
Both the i intialization and the loop control value are incorrect.
The i initialization is wrong.
You cannot use *= in a loop.
The loop control is incorrect.
The loop control, the i initialization, and the initialization for num are all wrong.
This loop will indeed run 10 times. However, notice that it begins on 0 and ends on 10. Thus, it will begin by executing:
num *= 0
Which is the same as:
num = num * 0 = 0
Thus, you need to start on 1 for i. However, notice also that you need to go from 1 to 10 inclusive. Therefore, you need to change i < 10 to i <= 10.
a. public void draw() {
b. int i = 0;
c. while(i < 15){
d. system.out.println(i);
e. i++
f. }
g. }
Which lines of code have errors?
d e
d c
e
b
cb
line d's error is that system.out.println (i); is not capitalized.
line e's error is that there is a missing semicolon.