All Computer Science Resources
Example Questions
Example Question #12 : Program Analysis
public static int[][] doWork(int[][] a, int[][] b) {
int[][] ret = new int[a.length][a[0].length];
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
ret[i][j] = a[i][j] + b[i][j];
}
}
return ret;
}
In the code above, what is the potential error that will not be caught on the following line?
ret[i][j] = a[i][j] + b[i][j];
The arrays may not be initialized
The array could go out of bounds
The code is fine as it is written
The array a
may be set to null
The arrays may contain null values
The array could go out of bounds
At this point of the code, it is not possible for a
or b
to be null. Furthermore, these arrays cannot contain null values, for they are made up of primitive types (int
types). These cannot be set to null, as they are not objects. The potential error here is a little bit abstruse, but it is important to note. There is one possible error, namely that the 2D arrays are "ragged"; however we don't need to worry about this for the exam. Still, it is also possible that the array b
(presuming that it is not null) is not the same size as the array a
. Since we are using a
to set our loop counts, this could potentially mean that we overrun the array b
by using index values that are too large. (Note, it is also possible that b
could be null, causing an error; however, there are no answer choices for that possibility.)
Example Question #251 : Computer Science
Consider the following code:
public static int[] del(int[] a,int delIndex) {
if(delIndex < 0 || delIndex >= a.length) {
return null;
}
int[] ret = new int[a.length - 1];
for(int i = 0; i < a.length; i++) {
if(i != delIndex) {
ret[i] = a[i];
}
}
return ret;
}
What is the error in the code above?
There is a null pointer exception.
The use of the array index i is incorrect.
The loop is infinite.
You need to implement a swap for the values.
The intial conditional (i.e. the if statement) has incorrect logic.
The use of the array index i is incorrect.
The problematic line in the code above is the one that reads:
ret[i] = a[i];
This is going to work well until the end of the array. The variable i is going to go for the length of the array a. However, the array ret is one less in length. This means that you will overrun your array, getting an ArrayIndexOutOfBoundsException at the very end of the looping. You would need to implement two indices—one of which will not be incremented on that one time when you skip the element that is removed from the array.
Example Question #252 : Computer Science
What is wrong with this loop?
int i = 0;
int count = 20;
int[] arr = [1, 3, 4, 6, 8, 768, 78, 9]
while (i < count) {
if (i > 0) {
arr[i] = count;
}
if (i < 15) {
System.out.println("HEY");
}
}
it prints Hey too many times
The loop is infinite
if statement is incorrect
count never gets decremented
The loop is infinite
The loop is infinite because i never gets incremented to become greater than count.
The loop in this case is,
while (i < count) {
if (i > 0) {
arr[i] = count;
}
and the error occurs because there is no terminating factor in the while loop. A terminating factor would be,
while(i<count){
if(i>0, i<10, i++){
arr[i] = count;
}
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 from num to 10. (That is, it provides the factorial value of 10!.) What is the error in the code?
The i initialization is wrong.
Both the i intialization and the loop control value are incorrect.
The loop control, the i initialization, and the initialization for num are all wrong.
You cannot use *= in a loop.
The loop control is incorrect.
Both the i intialization and the loop control value are incorrect.
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.
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;
seconds = hours + min * 60 + currentSeconds;
}
}
Which of the following issues could be raised regarding the code?
There is insufficient data verification for the mutator methods.
There is a potential null pointer exception in one of the methods.
There are no issues.
The setMinutes
method should return an integer value.
There is an error in the setMinutes
calculation.
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 #1 : 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.
Variable j is not defined.
You cannot define the variable logic as a bool.
The code has no errors.
You must define the sum variable as an integer, not a double.
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 #2 : 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?
Testfile.txt shouldn't have double quotations around it.
There is nothing wrong with the code.
The ifstream library should be included.
The outputFile should be of type "ofstream", not "ifstream".
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 #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?
b
cb
d c
d e
e
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 #2 : Counting Statement Executions
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:
Certified Tutor