All AP Computer Science A Resources
Example Questions
Example Question #2 : 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;
seconds = hours + min * 60 + currentSeconds;
}
}
Which of the following issues could be raised regarding the code?
There are no issues.
There is insufficient data verification for the mutator methods.
There is an error in the setMinutes
calculation.
There is a potential null pointer exception in one of the methods.
The setMinutes
method should return an integer value.
There is insufficient data verification for the mutator methods.
Recall that a "mutator" is a method that allows you to change the value of fields in a class. Now, for the setTime
, setMinutes
, and setSeconds
methods, 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 #2 : Identifying & Correcting Errors
What is wrong with the following code?
- int main()
- {
- bool logic;
- double sum=0;
- for(j=0;j<3;j++)
- {
- sum=sum+j;
- }
- if (sum>10)
- {
- logic=1;
- }
- else
- {
- logic=0;
- }
- }
- }
There is an opening/closing bracket missing.
You must define the sum variable as an integer, not a double.
You cannot define the variable logic as a bool.
The code has no errors.
Variable j is not defined.
Variable j is not defined.
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.
Example Question #1 : Identifying & Correcting Errors
FILE INPUT/OUTPUT
Consider the following C++ code:
1. #include <iostream>
2. #include <fstream>
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".
The ifstream library should be included.
There is nothing wrong with the code.
Testfile.txt shouldn't have double quotations around it.
The outputFile should be of type "ofstream", not "ifstream".
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 #5 : 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?
d c
b
e
d e
cb
d e
line d's error is that system.out.println (i); is not capitalized.
line e's error is that there is a missing semicolon.
Example Question #1 : Counting Statement Executions
public void countStatements() {
int a = 10, b = 15, c = 7;
// Start
for(int i = 0; i < a; i += 2) {
b--;
System.out.print("*");
}
for(int i = b; i >=0; i--) {
for(int j = 0; j < c; j++) {
System.out.print("*");
}
}
// End
}
In the code above, how many times will System.out.print
be called between the comments "// Start
" and "// End
"?
Start by counting the number of times the first loop will cycle. Notice that the loop control variable (namely, i
) is increased by 2 for each looping. This means that you will loop for i = 0, 2, 4, 6, 8. It will not loop for 10, for then, i
will equal a
. That will terminate the loop. Thus, you have five loopings thus far. Note, also that this means that b
is decremented 5 times. Thus, at the end of this loop, b
now equals 10.
For the second loop, you must be careful, for it has two loops. The outer loop will run from i = 10
to i = 0
. This is a total of 11 loopings. (This is important to note because it runs not only for 10 through 1 but also for the final number, 0. The loop within that loop will run from j
= 0 to j
= 6 (i.e. one less than c
, which is 7). This is a total of 7 loopings. Therefore, these nested loops will run a total of or 77 times. The total calls to System.out.print
will be , or 82.
Example Question #1 : Algorithm Analysis
public void countStatements() {
int[] array = new int[54];
for(int i = 0; i < array.length; i++) {
array[i] = 3 * i + 1;
System.out.println("Yay!");
}
for(int i = 0; i < array[5]; i++) {
array[i]--;
System.out.println("Yay!");
}
for(int i = array[3]; i > 0; i--) {
System.out.println("Yay!");
}
}
How many calls will be made to System.out.println
in the code above?
Let us count for each loop, paying attention to variables as needed. The first loop will run a total of 54 times. Thus it will produce 54 total executions. Notice also how it initializes the array. This will be used in the second loop. Each element is equal to 3i + 1
. Thus, we will have:
a[0] = 1; a[1] = 4; a[2] = 7; a[3] = 10; a[4] = 13; a[5] = 16; etc.
Now, loop two will use a[5]
in controlling its execution. Notice that it runs from i = 0
to i = 15
. For each execution, it reduces the value of a[i]
by one. Thus, be careful. It will eventually run for a[5]
, making it to be 15. This will then be used for the loop control. This means that it actually runs only from i = 0
to i = 14
. Thus, the loop runs a total of 15 times.
The last loop runs from i = 9
to i = 1
. (Note: This is because of the decrement in the second loop.) This is a total of 9 executions.
Thus, the total count of executions is:
Example Question #3 : Algorithm Analysis
How many statements are executed in the code snippet below?
for (int i = 0; i < arr.length; i++) {
if (i == 2) {
i++;
}
}
5
3
6
2
5
The statements executed in the code snippet are in order:
- int i = 0
- i < arr.length
- i++ (which is equivalent to i = i + 1)
- i == 2
- i++
These statements are all executions that happen in order each time the for loop is run. Sometimes there may only be 4 executions if the if statement is false.
Example Question #1 : Algorithm Analysis
Consider the following code:
What is the run-time of the code (in Big-O notation?)
The run-time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). The "for" loop that controls this constant-time operation is initialized at i = 1, and i is iterated by multiplying by 2 until it is equal to or greater than n. Thus, this loop will run times, and the run-time will be O(log(n))
Example Question #2 : Algorithm Analysis
Consider the following code:
What is the run time of the code (in Big-O notation?)
The run time of this code is best analyzed by plugging in numbers. Take the value of bar(2) for example. bar(2) leads to two calls to bar(1), which lead to four calls to bar(0). A call to bar(4) leads to two calls to bar(3), four calls to bar(2), eight calls to bar(1), and sixteen calls to bar(0). Thus, it can be shown that the number of calls is proportional to 2^n, and that that run time is O(2^n).
Example Question #3 : Algorithm Analysis
Consider the following code:
What is the expected runtime of the code (in Big-O notation?)
The run-time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). This constant time operation is performed n times in the for loop, leading to a run time that is proportional to n, or O(n).