Download review1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Generalized linear model wikipedia , lookup

Mathematical optimization wikipedia , lookup

Fisher–Yates shuffle wikipedia , lookup

Expectation–maximization algorithm wikipedia , lookup

Binary search algorithm wikipedia , lookup

Tail call wikipedia , lookup

Quicksort wikipedia , lookup

Algorithm characterizations wikipedia , lookup

History of the Church–Turing thesis wikipedia , lookup

Corecursion wikipedia , lookup

Recursion (computer science) wikipedia , lookup

Transcript
CS 122 – Review questions for midterm
1. In Java, what is short-circuit evaluation?
2. When using the method System.out.printf( ), what is the purpose of the %d format code?
3. What does it mean for the return type of a method to be void?
4. What Java keyword is used when invoking a constructor?
5. Suppose a is a one-dimensional array of double. Fill in the blanks in the following code so that it
finds the largest element of the array and stores this value in max.
double max = _________;
for (int i = 0; i < __________; ++i)
if (________________)
max = a[i];
6. A recursive binary search algorithm always reduces the problem size by how much at each
recursive call? For example, how many searches are needed if the array size is 128? How likely
is it that we will perform the maximum number of searches?
7. In the Eight Queens problem, how many queens can occupy one column?
8. If we define f(1) = 1 and f(2) = 1 and for all n >2, f(n) = f(n – 1) + f(n – 2), how many recursive
calls are there when we evaluate f(5)?
9. Suppose P(a, b) is defined as follows:
public int P(int a, int b)
{
if (b == 0 || a == b)
return 1;
else
return P(a – 1, b – 1) + P(a – 1, b);
}
What is the total number of recursive function calls if we evaluate P(5, 3)?
10. In Java, what is the syntax for a Javadoc comment? What makes Javadoc comments special
compared to other types of comments?
11. The following recursive function attempts to find how many values in array a from a[start]
through the end of the array are equal to a target number. But there are some mistakes in the
algorithm. How would you fix the implemenation?
public static int getNumberEqual(int a[], int start, int target)
{
int count;
if (n >= a.length)
return 0;
else
{
if (a[start] == target)
count = 1;
else
count = 0;
return getNumberEqual(a, start – 1, target);
}
}
12. How many parameters does each of the following types of constructors take?
a. Initial-value
b. Default
c. Copy
13. If w is a palindrome, then which proper substring of w is also a palindrome?
14. Fill in the blanks below to complete this recursive implementation of a function that determines
if a string is a palindrome.
public static boolean isPalindrome(String s)
{
if (__________________________________________________)
return true;
else if (___________________________________________)
return false;
else
return isPalindrome(___________________________________);
}
15. What is the name of the Java API class that helps us to split strings into pieces, such as breaking
up a sentence into individual words?
16. Write a recursive function in Java that will count how many array elements are negative, and
return this number. The parameters to this function are an array of integers, and an integer
denoting the index at which to start counting. Assume that the function will be initially called
like this: int numNegative = count(a, 0);
17. Write a recursive definition, in the form of a grammar, for the set of strings that begin with an
‘a’ and end with a ‘b’. You may assume that the alphabet is just these two letters.
18. What is the recursive definition of factorial?