AP Computer Science A : Identifying & Correcting Errors

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 :Identifying & Correcting Errors

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 fromnumto 10. (That is, it provides the factorial value of 10!.) What is the error in the code?

Possible Answers:

The loop control, theiinitialization, and the initialization fornumare all wrong.

Theiinitialization is wrong.

Both theiintialization and the loop control value are incorrect.

You cannot use *= in a loop.

The loop control is incorrect.

Correct answer:

Both theiintialization and the loop control value are incorrect.

Explanation:

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 gofrom 1to10inclusive.Therefore, you need to change i < 10 to i <= 10.

Example Question #1 :Identifying & Correcting Errors

Consider the code below:

public class Clock {

private int seconds;

public Clock(int s) {

seconds = s;

}

public void setTime(int s) {

seconds = s;

}

public void setSeconds(int s) {

int hoursMinutes = seconds - seconds % 60;

seconds = hoursMinutes + s;

}

public void setMinutes(int min) {

int hours = seconds / 3600;

int currentSeconds = seconds % 60;

秒=时间+分钟* 60 + currentSeconds;

}

}

Which of the following issues could be raised regarding the code?

Possible Answers:

There are no issues.

There is an error in thesetMinutescalculation.

There is a potential null pointer exception in one of the methods.

ThesetMinutesmethod should return an integer value.

没有足够的数据验证μtator methods.

Correct answer:

没有足够的数据验证μtator methods.

Explanation:

回想一下,一个“突变”是一个方法,允许哟u to change the value of fields in a class. Now, for thesetTime,setMinutes, andsetSecondsmethods, you do not check several cases of errors. For instance, you could give negative values to all of these methods without causing any errors to be noted. Likewise, you could assign a minute or second value that is greater than 60, which could cause errors as well. Finally, you could even make the clock have a value that is greater than "23:59:59", which does not make much sense at all!

Example Question #3 :Identifying & Correcting Errors

What is wrong with the following code?

  1. int main()
  2. {
  3. bool logic;
  4. double sum=0;
  5. for(j=0;j<3;j++)
  6. {
  7. sum=sum+j;
  8. }
  9. if (sum>10)
  10. {
  11. logic=1;
  12. }
  13. else
  14. {
  15. logic=0;
  16. }
  17. }
  18. }
Possible Answers:

There is an opening/closing bracket missing.

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.

The code has no errors.

Correct answer:

Variable j is not defined.

Explanation:

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 (intj=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.

Example Question #1 :Identifying & Correcting Errors

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?

Possible Answers:

The ifstream library should be included.

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

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

There is nothing wrong with the code.

Correct answer:

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

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.

Example Question #1 :Identifying & Correcting Errors

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?

Possible Answers:

e

d c

cb

d e

b

Correct answer:

d e

Explanation:

line d's error is that system.out.println (i); is not capitalized.

line e's error is that there is a missing semicolon.

Learning Tools by Varsity Tutors