All Computer Science Resources
Example Questions
Example Question #1 : Recursion
public static int foo(int a, int b) {
if(b <= 1 || b <= a) {
return 1;
}
return (b - a) * foo(a,b-1);
}
Based on the code above, what is the value of the following function call:
foo(5,9);
32
24
36
18
16
24
The function foo is recursive, meaning that it calls itself. Therefore, you should start by looking for its termination point. Based on the definition above, it terminates when either b <= 1 or b <= a. In these two cases, it will return 1.
Now, let us do our trace of the calls:
foo(5,9) = (9-5) * foo(5,8) = 4 * foo(5,8)
foo(5,8) = (8-5) * foo(5,7) = 3 * foo(5,7)
foo(5,7) = (7-5) * foo(5,6) = 2 * foo(5,6)
foo(5,6) = (6-5) * foo(5,5) = 1 * foo(5,5)
foo(5,5) = 1
Therefore, carefully tracing back up, we get:
foo(5,9) = 4 * 3 * 2 * 1 * 1 = 24
Example Question #1 : Recursion
public static int foo(int a) {
if(a <= 3) {
return 1;
}
return a * foo(a - 3) * foo(a - 4);
}
What is the value of the following call to the function defined above:
foo(8)
240
140
144
224
160
160
The foo method is recursive, so you will need to find the stopping case first. This happens when a <= 3. At that time, the method returns 1. Now, based on this fact, we can begin to trace the method:
foo(8) = 8 * foo(5) * foo(4)
foo(5) = 5 * foo(3) * foo(1)
foo(4) = 4 * foo(1) * foo(0)
foo(0) = foo(1) = foo(3) = 1
Thus, we know:
foo(8) = 8 * 5 * 1 * 1 * 4 * 1 * 1 = 160
Example Question #1 : Recursion
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
Error, infinite loop.
11 9 7 5 3 1 -1
9 7 5 3 1 -1
11 9 7 5 3 1
9 7 5 3 1
Error, infinite loop.
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
Example Question #2 : Recursion
public void draw() {
recurs(10);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What is the output of the code?
8 6 4 2
8 6 4 2 0
10 8 6 4 2 0
error, infinite loop
10 8 6 4 2
10 8 6 4 2
The number 10 is inputed into recurs and prints count,subtracts two from count, then rentered into recurs.
Thus, the first iteration gives, 10.
The second iteration gives as, 10, 8.
It is written this way because it places 8 after the value from the first iteration.
Continuing in this fashion we get,
10 8 6 4 2.
When count reaches 0, nothing is printed.
Example Question #1 : Recursion
What does this factorial code snippet return after calling run()?
public void run() {
int n = 2;
n = factorial(n);
n = n + factorial(n);
System.out.println(n);
}
public int factorial(int n) {
if (n==1) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
2 will be printed out
6 will be printed out
3 will be printed out
4 will be printed out
4 will be printed out
4 will be printed out because factorial(2) = 2. Adding factorial(2) + 2 = 4. This can also be written as 2! + 2! = 4.
n is 2 when first assigned
n is then assigned to factorial(2) which is 2
n is then assigned n (which is 2) plus factorial(2) which is 2 so 2+2 = 4
n is 4 when it is printed out
Example Question #71 : Program Implementation
The Fibonacci sequence is a classic example of: _________
Choose the best answer.
Recursion
Selection
Iteration
None of the above
Recursion
The Fibonacci sequence is known as a recurrence relation in mathematical terms. In programming, we represent recurrence relations using recursive methods or simply recursion. The Fibonacci sequence is a great example of recursion.
Example Question #72 : Program Implementation
public int foo(int n)
{
if (n < 0)
return 1;
else
return foo(n-2) + foo(n-1)
}
What is the value returned by foo(3)?
5
6
12
3
8
8
The answer is 8.
This question tests knowledge of recursion. The correct strategy is to build a tree of function calls. Since the base case of foo returns 1, we can just add up all the function calls that return the base case. In the tree below, the green functions represent the base cases, with an argument less than 0.
Example Question #142 : Computer Science
Consider the following code:
int[] vals = {6,1,41,5,1};
int[][] newVals = new int[vals.length][];
for(int i = 0; i < vals.length; i++) {
newVals[i] = new int[vals[i]];
for(int j = 0; j < vals[i];j++) {
newVals[i][j] = vals[i] * (j+1);
}
}
What does the code above do?
It sums the values in vals, placing the outcome in newVals.
The code creates a 2D array that contains all multiples from 1 to 5 for the members of vals.
It creates a 2D matrix with the vals array for every row.
The code creates a 2D array that contains the multiples for the members of vals, using each of those values to determine the number of multiples to be computed.
It performs a matrix multiplication on vals and newVals, storing the final result in newVals.
The code creates a 2D array that contains the multiples for the members of vals, using each of those values to determine the number of multiples to be computed.
Let's look at the main loop in this program:
for(int i = 0; i < vals.length; i++) {
newVals[i] = new int[vals[i]];
for(int j = 0; j < vals[i];j++) {
newVals[i][j] = vals[i] * (j+1);
}
}
The first loop clearly runs through the vals array for the number of items in that array. After this, it creates in the newVals 2D array a second dimension that is as long as the value in the input array vals. Thus, for 41, it creates a 3rd row in newVals that is 41 columns wide.
Then, we go to the second loop. This one iterates from 0 to the value stored in the current location in vals. It places in the given 2D array location the multiple of the value. Think of j + 1. This will go from 1 to 41 for the case of 41 (and likewise for all the values). Thus, you create a 2D array with all the multiples of the initial vals array.
Example Question #221 : Computer Science
Consider the following C++ pseudocode:
Class Car {
const int wheels = 4;
float milesPerGallon;
string make;
string model;
}
Car sportscar = new Car;
What is the difference between the class car
, and the object sportscar
?
sportscar
is instantiated from Car.
They are both instantiated from something else.
Car
is instantiated from sportscar
.
They have no relation.
They are the same.
sportscar
is instantiated from Car.
When programming within the Object-Oriented paradigm, think of the class as a blueprint, and the object as a house built from that blueprint.
The class Car
is a abstract specification that does not refer to any one particular instance. It is merely a protocol that all objects wishing to be cars should follow. The sportscar object is a realization of the car blueprint. It is a specific instance. In programming jargon, "sportscar
is instantiated from Car
."
Example Question #1 : Implementation Techniques
JAVA EXPLICIT TYPE CASTING
Consider the following JAVA code:
public static void main(String[] args)
{
double number1 = 99.05;
int number2 = (int) number1;
System.out.println("number1 = " + number1);
System.out.println("number2 = " + number2);
}
What would be the console output?
number1 = 99.05
number2 = 99.05
number1 = 99.05
number2 = 99.00
number1 = 99.05
number2 = 99
number1 = 99
number2 = 99
number1 = 99.05
number2 = 99
Type casting deals with assigning the value of a variable to another variable that is of a different type. In this case we have two variables one that is a double, and another that is an integer. number1 is a double that has a value of 99.05. However, when number2 is assigned the value of number1, there is some explicit type casting going on. When an integer is assigned the value of a double, it drops off the decimal places. This means that number2 has a value of 99.