1 Which of the following are advantages of using arrays to i
1. Which of the following are advantages of using arrays to implement bags and lists?
A. arrays can become full and need to be expanded
B. arrays reuire shifting under certain circumstances
C. arrays allow direct access to any element
D. arrays make it easy to add an element to the middle of a list
2. Which property of recursion does the following method violate?
public int callIt (int n) {
if (n>0)
return callIt (n-1);
else
return callIt (n-2) }
Select one: A. the method does not call itself; B) There is no base case (escape clause); C) There base case exists but will never be executed; D) The method shouldn\'t return a value
3. What gets printed by the following statements? The method recMethod2 is described in pseudocode.
char arr [] = {\'m\', \'p\', \'q\'};
recMethod2 (arr,2);
public void recMethod2 (char a[], int k) {
char c1 = a[1], c2 =a[2];
if (k is greater than zero) {
a[k] = \'a\';
recMethod2 (a,k-1);
print c1
print c2}}}
4. Which of the following are valid values for big-oh? Select all that apply.
Sect one or more
A.3n^2 B.1 C. n D. 2n E. log n F. n log n G. n^2 H. 2^n I. 2 J. n^2+n
5. Which of the following has the largest order of growth?
n ; n^2 ; 2^n; log n ; n^4
Solution
1> option C : because in array we can directly access an element using the index position for eg. a[0]=10, a[5]=20.
option D : since we can directly access an element in an array, we don\'t need to find the index position of the middle element. we can directly access the middle element as arr[(arr.length/2)-1]. Although, you need to right shift all the elements on right side of middle by one.
2> option B : The given recursion function doesn\'t have any base case to limit the program flow. Since there is no condition to stop the execution of the function. It will cause buffer overflow.
3> output : p a p q
initial arrar [m p q] was sent with k=2;
case1 :c1=p c2=q
now array [m p a] is sent with k=1;
case 2:c1=p c2=a
now array [m a a] is sent with k=0;
function will return as k is not greater than 0.
now the value of case 2 is printed-----> p a
now the value of case 1 is printed------> p q
4> B C E F G H (big-oh stands for upper bound of time or space complexity, so the upper(bigger)value is writen inside)
5> 2^n has the largest order of growth as it is exponential. Others are polynomial.
----------------------------------------------------------------------------------------------------
feel free to reach out if you have any doubt. keep learning :)

