ASDV 2520: Final Study Guide (Chapter 18, 19, 20, 21, 22, 23, 24, 25, 30)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

125

*Consider the following recursive method.* public static int m(int value) { if (value >= 0) return 5 * m(value - 2); else return 1; } What value is returned when invoking m(5)? + 100 + 225 + 75 + 125 + 25

n is 1

In the following method, what is the base case? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } + n is greater than 1 + no base case + n is 1 + n is less than 1

line 2

Indicate where a compiling error exists if any. public class G<T1>//line 0{ List<?extends T1> listA = new ArrayList<T1>();//line 1 List<?extends T1> listB = new ArrayList<String>();//line 2 } + line 0 + line 1 + line 2

Yes

Is ArrayList<?> same as ArrayList <?extends Object>?

Yes

Is ArrayList<Integer> a subclass of ArrayList<?>?

No

Is ArrayList<Integer> a subclass of ArrayList<Object>?

Yes

Is ArrayList<Number> a subclass of ArrayList<?extends Number>?

B

Look at fizzle() again: fizzle(1) = 1 fizzle(N) = fizzle( (N+1)/2 ) + fizzle(N/2), for N>1 *Which of the following Java methods correctly implements it?* A. int fizzle( int N ) { if ( N>1 ) { return fizzle( N ) + fizzle( N ) + 1; } else { return 1; } } B. int fizzle( int N ) { if ( N==1 ) { return 1; } else { return fizzle( (N+1)/2 ) + fizzle( N/2 ) ; } } C. int fizzle( int N ) { if ( N>=1 ) { return fizzle( N+1/2 ) + fizzle( N/2 ) ; } else { return 1; } } D. int fizzle( int N ) { if ( N==1 ) { return 1; } else { return fizzle( N/2 + 1 + N/2 ) ; } }

D

Look at mystery() again: mystery(0,N) = N mystery(P,Q) = mystery(P-1, Q+1) *Which of the following Java methods correctly implements it?* A. int mystery( int P, int Q ) { if ( Q==0 ) { return Q; } else { return mystery(P-1, Q-1); } } B. int mystery( int P, int Q ) { if ( P==Q ) { return Q; } else { return mystery(P-1, Q); } } C. int mystery( int P, int Q ) { if ( P==0 && Q==0) { return 1; } else { return mystery(P-1, Q+1); } } D. int mystery( int P, int Q ) { if ( P==0 ) { return Q; } else { return mystery(P-1, Q+1); } }

True

The <T> of class A is the same as <T extends Object> of class B. class A<T>{} class B<T extends Object>{}

stops

The base case ________ the recursion. + pauses + stops + starts + breaks

List<CharSequence> in; List<CharSequence> result;

The signature of a method in a class is as follows: public static <E extends CharSequence> List<?super E> doIt( List<E> nums ) This method is being called in the following code: result = doIt( in ); Given that String implements CharSequence interface what should be the reference type of "in" and "result" variables? + ArrayList<String> in; List<CharSequence> result; + List<String> in; List<Object> result; + List<CharSequence> in; List<CharSequence> result; + ArrayList<Object> in; List<CharSequence> result; + None of these.

<E extends Number>

To create a generic type bounded by Number, use ________. + <E extends Number> + <E extends Integer> + <E> + <E extends Object>

ArrayList<Integer> list = new ArrayList<Integer>();

To create a list to store integers, use ________. + ArrayList<Number> list = new ArrayList<Integer>(); + ArrayList<int> list = new ArrayList<int>(); + ArrayList<Object> list = new ArrayList<Integer>(); + ArrayList<Integer> list = new ArrayList<Integer>();

public class A<E> { ... }

To declare a class named A with a generic type, use ________. + public class A<E> { ... } + public class A<E, F> { ... } + public class A(E) { ... } + public class A(E, F) { ... }

public class A<E1, E2> { ... }

To declare a class named A with two generic types, use ________. + public class A<E1, E2> { ... } + public class A<E, E> { ... } + public class A(E, E) { ... } + public class A(E, F) { ... }

public interface A<E> { ... }

To declare an interface named A with a generic type, use ________. + public interface A<E, F> { ... } + public interface A(E) { ... } + public interface A<E> { ... } + public interface A { ... }

public interface A<E1, E2> { ... }

To declare an interface named A with two generic types, use ________. + public interface A<E1, E2> { ... } + public interface A(E) { ... } + public interface A(E, F) { ... } + public interface A { ... }

n <= 0

What are the base cases in the following recursive method? public static void xMethod(int n) { if (n > 0) { System.out.print(n % 10); xMethod(n / 10); } } + n > 0 + no base cases + n < 0 + n <= 0

+ List<?> list = new ArrayList<>(Arrays.asList(names)); + List<String> list = new ArrayList<>(Arrays.asList(names));

What can be inserted at line 2 of the code snippet below? String[] names = {"Alex", "Bob", "Charlie"}; //inset code at line 2 System.out.println( list.get( 0 ) ); + List<?> list = new ArrayList<>(Arrays.asList(names)); + List<String> list = new ArrayList<>(Arrays.asList(names)); + List<?> list = new ArrayList<?>(Arrays.asList(names)); + List<> list = new ArrayList<String>(Arrays.asList(names)); + List<> list = new ArrayList<String>(Arrays.asList(names))

3

What is the output for the call m( 3)? public static int m(int x) { if ( x < 0 ) return 1; m(--x); return 1 + m(--x); } + 1 + 2 + 3 + 4 + 5

4 3 2 1

What is the printout of invoking xfunction(1234)? public static void xfunction(int n) { if (n > 0) { System.out.print(n % 10 + " "); xfunction(n / 10); } } + 1234 + c + 4321 + 1 2 3 4

Nothing

What is the printout of invoking xfunction(1234)? public static void xfunction(n) { if (n <= 0) { System.out.print(n % 10 + " "); xfunction(n / 10); } } + Nothing + 1 2 3 4 + 4321 + 4 3 2 1 + 1234

13

What is the printout of invoking xfunction(6)? public static int xfunction(int n) { if (n <= 1) return 1; else return n + xfunction(n - 2); } + 12 + 14 + 11 + 13

1

What is the printout of invoking xfunction(6)? public static int xfunction(int n) { if (n >= 1) return 1; else return n + xfunction(n - 2); } + 2 + 4 + 3 + 1

5

What is the printout? public class Recursion{ static int count = 0; public static int fib(int n){ count++; if (n == 0){ return 0; } else if (n == 1){ return 1; } else // Reduction and recursive calls{ return fib(n - 1) + fib(n - 2); } } public static void main(String[] args){ fib( 3 ); System.out.println( count); } } + 5 + 4 + 3 + 6

10

What is the return value for xMethod(4) after calling the following method? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } + 11 + 10 + 9 + 12

+ List<String> list = new ArrayList<>(); + List<String> list = new ArrayList<>(10);

Which of the following is valid? + List<String> list = new List<>(); + No answer text provided. + ArrayList<> list = new ArrayList<>(); + List<String> list = new ArrayList<>(); + List<String> list = new ArrayList<>(10);

The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

*Analyze the following code:* public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; xMethod(x, 5); } public static void xMethod(int[] x, int length) { System.out.print(" " + x[length - 1]); xMethod(x, length - 1); } } + The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException. + The program displays 1 2 3 4 6. + The program displays 5 4 3 2 1. + The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.

f2 is tail recursion, but f1 is not

*Analyze the following functions:* public class Test1 { public static void main(String[] args) { System.out.println(f1(3)); System.out.println(f2(3, 0)); } public static int f1(int n) { if (n == 0) return 0; else { return n + f1(n - 1); } } public static int f2(int n, int result) { if (n == 0) return result; else return f2(n - 1, n + result); } } + f1 is tail recursion, but f2 is not + f1 and f2 are both tail recursive + Neither f1 nor f2 is tail recursive + f2 is tail recursion, but f1 is not

Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.

*Analyze the following two programs:* A: public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int length) { if (length > 1) { System.out.print((length - 1) + " "); xMethod(length - 1); } } } B: public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int length) { while (length > 1) { System.out.print((length - 1) + " "); xMethod(length - 1); } } } + The two programs produce the same output 4 3 2 1. + The two programs produce the same output 1 2 3 4. + The two programs produce the same output 5 4 3 2 1. + The two programs produce the same output 1 2 3 4 5. + Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.

+ line 1 + line 2 + line 5

*Check all lines that DO compile:* import java.util.ArrayList; import java.util.List; public class G{ public static <E extends CharSequence> List<?super E> doIt(List<E> numbers){ return null; } public static void main(){ ArrayList<String> in1 = null; ArrayList<CharSequence> in2 = null; List<CharSequence> out1 = null; List<?> out2 = null; doIt(in2);//line 1 doIt(in1); //line 2 out1 = doIt(in2);//line 3 out1 = doIt(in1);//line 4 out2 = doIt(in1);//line 5 } } + line 1 + line 2 + line 3 + line 4 + line 5

+ ArrayList<?> ar = new ArrayList<String>(); + ArrayList<?> ar = new ArrayList<Date>();

*Check all that are correct.* + ArrayList<?> ar = new ArrayList<String>(); + ArrayList<Number> ar = new ArrayList<Integer>(); + ArrayList<?> ar = new ArrayList<Date>(); + ArrayList<Object> ar = new ArrayList<Date>();

+ compiling error at line 2 + There will be no compiling errors if we remove the constructor at line 2

*Check all that are correct.* import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Q7<T>{ List<?extends T> list; public Q7(ArrayList<?extends T> list) //line 1{ this.list = list; System.out.println(this.list); } public Q7( ArrayList<?super T> list ) //line 2{ this.list = list; System.out.println(this.list); } public static void main(String[] args){ Q7<CharSequence> q7 = new Q7(new ArrayList<CharSequence>(Arrays.asList("abc"))); //line 3 } } + No compiling errors, output is [abc] + No compiling errors, output is null + compiling error at line 1 + compiling error at line 2 + There will be no compiling errors if we remove the constructor at line 1 + compiling error at line 3 + There will be no compiling errors if we remove the constructor at line 2

square(3) = square(2) + 2*3 -1

*Consider square numbers defined as follows (for positive integers):* square(1) = 1 square(N) = square(N-1) + 2N -1 According to this definition, what is square(3)? + square(3) = square(2) - 2*3 + 1 + square(3) = square(2) + square(1) + square(3) = square(2) + 2*3 - 1 + square(3) = square(3) + 2*3 - 1

recursiveBinarySearch(list, key, low, high)

*Fill in the code to complete the following method for binary search.* public static int recursiveBinarySearch(int[] list, int key) { int low = 0;int high = list.length - 1; return ________; } public static int recursiveBinarySearch(int[] list, int key,int low, int high) { if (low > high) // The list has been exhausted without a match return -low - 1; // Return -insertion point - 1 int mid = (low + high) / 2; if (key < list[mid]) return recursiveBinarySearch(list, key, low, mid - 1); else if (key == list[mid]) return mid; else return recursiveBinarySearch(list, key, mid + 1, high); } + recursiveBinarySearch(list, key, low, high) + recursiveBinarySearch(list, key, low + 1, high - 1) + recursiveBinarySearch(list, key) + recursiveBinarySearch(list, key, low - 1, high + 1)

(s.charAt(0) != s.charAt(s.length() - 1)) // Base case

*Fill in the code to complete the following method for checking whether a string is a palindrome.* palindrome ( dad ) is true palindrome ( abc ) is false public static boolean isPalindrome(String s) { if (s.length() <= 1) // Base case return true; else if ________ return false; else return isPalindrome(s.substring(1, s.length() - 1)); } + (s.charAt(0) != s.charAt(s.length() - 1)) // Base case + (s.charAt(1) != s.charAt(s.length() - 1)) // Base case + (s.charAt(0) != s.charAt(s.length())) // Base case + (s.charAt(1) != s.charAt(s.length())) // Base case

isPalindrome(s, low + 1, high - 1)

*Fill in the code to complete the following method for checking whether a string is a palindrome.* public static boolean isPalindrome(String s) { return isPalindrome(s, 0, s.length() - 1); } public static boolean isPalindrome(String s, int low, int high) { if (high <= low) // Base case return true; else if (s.charAt(low) != s.charAt(high)) // Base case return false; else return ________; } + isPalindrome(s, low + 1, high - 1) + isPalindrome(s, low + 1, high) + isPalindrome(s, low, high - 1) + isPalindrome(s, low, high) + isPalindrome(s)

+ n * factorial(n - 1) + factorial(n - 1) * n

*Fill in the code to complete the following method for computing factorial of n.* /** Return the factorial for a specified index **/ public static long factorial(int n) { if (n == 0) // Base case return 1; else return ________; // Recursive call } + n-1 * factorial(n ) + n * factorial(n - 1) + factorial(n - 1) * n + n-1 * factorial(n-1 )

sort(list, list.length - 1)

*Fill in the code to complete the following method for sorting a list.* public static void sort(double[] list) {________;} public static void sort(double[] list, int high){ if (high > 1){// Find the largest number and its index int indexOfMax = 0; double max = list[0]; for (int i = 1; i <= high; i++){ if (list[i] > max){ max = list[i]; indexOfMax = i;}}// Swap the largest with the last number in the list list[indexOfMax] = list[high]; list[high] = max;// Sort the remaining list sort(list, high - 1); } } + sort(list) + sort(list, list.length - 2) + sort(list, list.length) + sort(list, list.length - 1)

Comparable<MyInt> / MyInt

*Fill in the most appropriate code in the place 1 and 2 in the MyInt class?* public class MyInt implements _____1___ { int id; public MyInt(int id) { this.id = id; } public String toString() { return String.valueOf(id); } public int compareTo(____2____ arg0) { if (id > arg0.id) return 1; else if (id < arg0.id) return -1; else return 0; } } + Comparable<MyInt> / Object + Comparable / MyInt + Comparable<MyInt> / MyInt + Comparable / Object

4

*How many times is line3 executed?* public static int f( int n ){ if ( n==0 ) //line 1 return 1; //line2 return n * f( n -1); //line3 } public static void main(String[] args){ f( 4 ); } + 4 + 0 + 5 + 3

False *Explanation*: No Inheritance in the diamond without wild card.

*Line 4 generates a compiling error.* 1. public static void f1( ArrayList<Number> list ){} 2. public static void main(String[] args) 3. { 4. f1( new ArrayList<Integer>()); 5. }

True

*Line 4 generates a compiling error.* 1. public static void f1( ArrayList<Number> list ){} 2. public static void main(String[] args) 3. { 4. f1( new ArrayList<Integer>()); 5. }

List<Number> list1 = new ArrayList<Number>();

*Select all that DO NOT generate compiling errors.* + ArrayList<Object> list2 = new ArrayList<Integer>(); + List<Number> list1 = new ArrayList<Number>(); + ArrayList<Number> list2 = new ArrayList<Integer>() + ArrayList<Object> list2 = new ArrayList<String>();

+ line 1 + line 3 + line 4 + line 5

*Select lines that compile without errors.* class XYZ<T1>{ ArrayList<?extends T1> list;//line 1 public static <T2> void f ( ArrayList<?extends T2> list1) { System.out.println( list1.size()); list1.add( new Integer());//line 2 } public static void main(String[] args) { f( new ArrayList<Integer>() );//line 3 f( new ArrayList<Object>() );//line 4 f( new ArrayList<A>() ); //line 5 } } class A{} + line 1 + line 2 + line 3 + line 4 + line 5

4

*Select the line(s) that DO generate compiling error(s).* public class AA{ public static void x( List<?super C> list){} public static void main(String[] args){ ArrayList<A> list1 = new ArrayList<A>(); list1.add( new A()); 1. x( list1 ); System.out.println( list1.get(0)); ArrayList<Object> list2 = new ArrayList(); 2. x( list2); 3. ArrayList<Integer> list3 = new ArrayList(); 4. x( list3); } } class A{} class B extends A{} class C extends B{} + 1 + 2 + 3 + 4

3 *Explanation*: Line 3 creates the compiling error. The wild card of line 1 fixes inheritance of lines 4 and 5 so the parameter in line 1 can be anything. Line 3 creates a compiling error since the compiler doesn't know the type of the ArrayList and we are trying to add an Integer.

*Select the lines that DO CREATE compiling error(s).* public class X{ 1. public static void f1(ArrayList<?> list){ 2. System.out.println( list.size()); 3. list.add(new Integer( 5)); } public static void main(String[] args){ 4. f1(new ArrayList<Integer>()); 5. f1(new ArrayList<Double>()); } } + 1 + 4, 5 + 2, 3 + 3

+ list3.add("abc"); + list1.add(5); + A1<Integer> a1 = new B1<Number>(); + A1<Number> a2 = new B1<Integer>();

*Select the lines that DO generate a compiling error if were put at the bottom of main method.* public class X{ public static void main(String[] args) { List<?> list1 = new ArrayList<Integer>(); ArrayList<?> list3 = new ArrayList<String>(); } } class A1<T>{} class B1<T> extends A1<T>{} class C1<T> extends B1<T>{} + list3.add("abc"); + list1.add(5); + A1<Integer> a1 = new B1<Number>(); + A1<Number> a2 = new B1<Integer>(); + A1<Number> a3 = new B1<Number>(); + list3.size();

4

*Select the lines that DO generate a compiling error(s).* public class AA{ public static void x( List<?super C> list){} public static void main(String[] args){ ArrayList<A> list1 = new ArrayList<A>(); list1.add( new A()); *1. x( list1 );* System.out.println( list1.get(0)); ArrayList<Object> list2 = new ArrayList(); *2. x( list2);* *3. ArrayList<Integer> list3 = new ArrayList();* *4. x( list3);* } } class A{} class B extends A{} class C extends B{} + 1 + 2 + 3 + 4

0

*Show the output of the following code:* public class Test1{ public static void main(String[] args) { System.out.println(f2(2, 0)); } public static int f2(int n, int result) { if (n == 0) { return 0; } else { return f2(n - 1, n + result); } } } + 0 + 1 + 2 + 3

False *Explanation*: We cannot use wildcard in Type of class declaration. class B<T extends Object>{} would have been correct but not class B<?extends Object>{}

*The <T> of class A is the same as <T extends Object> of class B.* class A<T>{} class B<?extends Object>{}

+ public static <E> void print(E[] list) + public static void print(Object[] list)

*The method header is left blank in the following code. Fill in the header.* public class GenericMethodDemo { public static void main(String[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York", "Austin"}; print(integers); print(strings); } ________ { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } } + public static void print(Integer[] list) + public static <E> void print(E[] list) + public static void print(int[] list) + public static void print(Object[] list)

False

*There is no compiling error in line 1.* public class GenericsRule8<T1>{ 1. private List<?extends T1> list = new ArrayList<String>(); }

interface Harnivore<E extends Animal> extends Hungry<E>{}

*What answer makes the following code to compile without errors?* package generics.questions; import java.util.ArrayList; abstract class Animal{} abstract class Plant{} class Grass extends Plant{} interface Hungry<E> { void eats( E x); } interface Carnivore<E extends Animal> extends Hungry<E>{} interface Harnivore<E extends Plant> extends Hungry<E>{} class Wolf extends Animal implements Carnivore<Sheep>{ public void eats(Sheep x){} } class Sheep extends Animal implements Harnivore<Sheep>{ public void eats(Sheep x){} } + interface Carnivore<E extends Plant> extends Hungry<E>{} + interface Harnivore<E extends Animal> extends Hungry<E>{} + class Sheep extends Animal implements Harnivore<Plant>{ public void eats(Grass x){} } + class Sheep extends Plant implements Harnivore<Wolf>{ public void eats(Wolf x){} } * class Wolf extends Animal implements Harnivore<Grass>{ public void eats(Grass x){} }

-1

*What does the method return for n = 5 ?* public static int f1( int n ){ if ( n == 1 || n == 2) return 1; int f = f1(n-1) - f1( n-2); return f; } + -2 + -1 + 0 + 1

dcba

*What is the output?* public class Recursion { public static void x( String s ){ if ( s.length() == 0 ) return; System.out.print(s.charAt( s.length()-1 )); x(s.substring(0, s.length()-1) ); } public static void main(String[] args){ x("abcd"); } } + abcd + dbca + aaaa + dddd

3 3 2 2

*What is the output?* public class Recursion { static int i = 0, j = 0, k = 0; public static void f4(int a[][], int b[][], int c[][]){ if (i < a.length){ if (j < b[0].length){ if (k < a[0].length){ c[i][j] += a[i][k] * b[k][j]; k++; f4(a, b, c); } k = 0; j++; f4(a, b, c); } j = 0; i++; f4(a, b, c); } } public static void main(String[] args){int[][]a = { {1,2}, {1,1} }; int[][]b = { {1,1}, {1,1} }; int[][]c = { {0,0}, {0,0} }; f4( a, b, c); for ( int i=0; i < 2; ++i) for ( int j=0; j < 2; ++j) System.out.print(c[i][j] + " "); } } + 3 3 2 2 + 2 2 3 3 + 1 1 1 1 + 0 0 0 0

18 3 20 3

*What is the output?* public class Recursion { static int i = 0, j = 0, k = 0; public static void f4(int a[][], int b[][], int c[][]){ if (i < a.length){ if (j < b[0].length){ if (k < a[0].length){ c[i][j] += a[i][k] * b[k][j]; k++; f4(a, b, c); } k = 0; j++; f4(a, b, c); } j = 0; i++; f4(a, b, c); } } public static void main(String[] args){ int[][]a = { {2,1}, {1,2},}; int[][]b = { {3,1}, {4,1} }; int[][]c = { {8,0}, {9,0} }; f4( a, b, c); for ( int i=0; i < 2; ++i) for ( int j=0; j < 2; ++j) System.out.print(c[i][j] + " "); } } + 0000 + 26 9 28 11 + 28 1 30 1 + 18 3 20 3

7

*What is the output?* public class Recursion{ public static int f3(int x){ if ( x == 1 ) return 1; return f3( (x+1)/2 ) + f3(x/2); } public static void main(String[] args){ System.out.println( f3(7) ); } } + 5 + 6 + 7 + 8 + 9

5

*What is the output?* public class Recursion{ public static int f3(int x){ if (x == 1 || x == 2) return 1; return f3((x + 2) / 2) + f3(x / 2); } public static void main(String[] args){ System.out.println( f3(7) ); } } + 3 + 4 + 5 + 6 + 7

6

*What is the output?* public class Recursion{ public static int f4(int n, int k){ if (k == 0 || n == 0) return 1; else return k * f4(n-1, k-1); } public static void main(String[] args){ System.out.println(f4(2, 3) ); } } + 5 + 6 + 7 + 8

-11

*What is the output?* public class Recursion{ public static int f1 ( int x ){ if ( x == 1 ) return 1; return f1( x-1) - 2 * x -1; } public static void main(String[] args){ System.out.println( f1( 3 ) ); } } + -9 + -10 + -11 + -12 + -13

9

*What is the output?* public class Recursion{ public static int f1 ( int x ){if ( x == 1 ) return 1; return f1( x-1) + 2 * x -1; } public static void main(String[] args){ System.out.println( f1( 3 ) ); } } + 4 + 7 + 9 + 16

+ merit + Compiling error

*What is the output?* public class Test1{ public static void main(String args[]){ G<Integer> m = new G<Integer>(); m.set("merit"); System.out.println(m.get()); } } class G<T>{ T var; void set(T var){ this.var = var; } T get() { return var; } } + merit + 0 + Compiling error + Runtime error

x( new ArrayList<List<A>>() );

*Which one of the choices DOES NOT generate a compiling error when your choice is placed inside the main method?* public static void x( List<List<A>> l ){} class A{} class B extends A{} class C extends B{} + x( new ArrayList<List<A>>() ); + x( new List<A<A>>() ); + x( new ArrayList<ArrayList<A>>() ); + x( new List<List<A>>() );

Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B

*What is the output?* public static void moveDisks(int n, char fromTower,char toTower, char auxTower){ if (n == 1) // Stopping condition System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); else{ moveDisks(n - 1, fromTower, auxTower, toTower); System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); moveDisks(n - 1, auxTower, toTower, fromTower); } } public static void main(String[] args){ moveDisks(2, 'A', 'B', 'C'); } + Move disk 1 from A to B Move disk 2 from B to A Move disk 1 from C to B + Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from C to B + Move disk 1 from A to C Move disk 2 from A to C Move disk 1 from C to B + Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B

2

*What lines GENERATE COMPILING ERROR(S)?* public class AA { public static void x( List<?extends A> list){} public static void main(String[] args){ 1. ArrayList<B> list2 = new ArrayList(); 2. list1.add(new A()); 3. x( list2 ); } } class A{} class B extends A{} class C extends B{} + 1 + 2 + 3 + no compiling error

+ return new ArrayList<Object>(); + return new ArrayList<A>(); + return new ArrayList<B>();

*What should method x return?* class A{} class B extends A{} class C{ public static <E extends B> //new type declared List<?super E>//return type x(List<E> nums) //method name and params { return ____________________ } } + return new ArrayList<Object>(); + return new ArrayList<A>(); + return new ArrayList<B>(); + return new ArrayList<C>();

line 4

*Which line(s) generate a compiling error, if any?* package generics.questions; public class Q6_1{ } abstract class Animal1{} abstract class Plant1{} class Grass1 extends Plant1{} interface Hungry1<E> { void eats( E x); } interface Harnivore1<E extends Plant1> extends Hungry1<E>{} class Goat1 extends Animal1 implements Harnivore1<Plant1>{ @Override public void eats(Plant1 x) { System.out.println("eats"); } public static void main(String[] args){ Goat1 goat1 = new Goat1(); //line 1 goat1.eats( new Grass1()); //line 2 goat1.eats( new Plant1() { } ); //line 3 //the braces in bold in line 3 means it has implemented all abstract methods goat1.eats( new Harnivore1<Plant1>()) ;//line 4 Harnivore1 h1 = new Harnivore1<Plant1>() //line 5{ @Override public void eats(Plant1 x){}}; } } + line 1 + line 2 + line 3 + line 4 + line 5

line 3

*Which line(s) generates a compiling error?* import java.util.ArrayList; import java.util.List; public class AA{ public static void x(List<?super A> list){} public static void main(String[] args){ ArrayList<B> list1 = new ArrayList<B>(); //line 1 ArrayList<A> list2 = new ArrayList<A>(); //line 2 x(list1);//line 3 x(list2);//line 4 } } class A{} class B extends A{} class C extends B{} + line 1 + line 2 + line 3 + line 4 + no compiling error

+ f( new ArrayList<List<Integer>>() ); + f( new ArrayList<ArrayList<Integer>>() );

*Which lines DO NOT generate compiling error?* public static void f( List<?extends List< Integer>> listOfList ) { } + f( new ArrayList<List<Integer>>() ); + f( new ArrayList<ArrayList<Integer>>() ); + f( new List<List<Integer>>() ); + f( new List<ArrayList<Integer>>() );

? super T

*Which of the following can be used to replace YYYYYYYY in the following code?* public class WildCardDemo3 { public static void main(String[] args) { GenericStack<String> stack1 = new GenericStack<String>(); GenericStack<Object> stack2 = new GenericStack<Object>(); stack2.push("Java"); stack2.push(2); stack1.push("Sun"); add(stack1, stack2); } public static <T> void add(GenericStack<T> stack1,GenericStack<YYYYYYYY> stack2) { while (!stack1.isEmpty()) stack2.push(stack1.pop()); } } + ? extends T + ? super Object + ? extends Object + ? super T

+ remove(o: Object): boolean + removeAll(<c: Collection>): boolean

*Which of the following methods are in the Collection interface?* + deleteAll(<c: Collection>): boolean + remove(o: Object): boolean + delete(o: E): boolean + removeAll(<c: Collection>): boolean

+ A recursive method can always be replaced by a non-recursive method. + Recursive methods usually take more memory space than non-recursive methods. + In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve.

*Which of the following statements are true?* + A recursive method can always be replaced by a non-recursive method. + Recursive methods run faster than non-recursive methods. + Recursive methods usually take more memory space than non-recursive methods. + In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve.

+ Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. + Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case. + Every recursive method must have a base case or a stopping condition.

*Which of the following statements are true?* + Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. + Every recursive method must have a return value. + Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case. + Every recursive method must have a base case or a stopping condition.

+ You cannot create an instance using a generic class type parameter. + Generic type information is not present at runtime. + You cannot create an array using a generic class type parameter. + Generic type information is present at compile time.

*Which of the following statements are true?* + You cannot create an instance using a generic class type parameter. + Generic type information is not present at runtime. + You cannot create an array using a generic class type parameter. + Generic type information is present at compile time.

+ Comparable<String> c = new String("abc"); + Comparable<String> c = "abc";

*Which of the following statements is correct?* + Comparable<String> c = new String("abc"); + Comparable<String> c = new Date(); + Comparable<Object> c = new Date(); + Comparable<String> c = "abc";

+ Generics can help detect type errors at compile time, thus make programs more robust. + Generics can make programs easy to read. + Generics can avoid cumbersome castings.

*Which of the following statements is correct?* + Generics can make programs run faster. + Generics can help detect type errors at compile time, thus make programs more robust. + Generics can make programs easy to read. + Generics can avoid cumbersome castings.

x( new ArrayList<List<A>>() );

*Which one of the choices DOES NOT generate a compiling error?* public static void x( List<List<A>> l ){} class A{} class B extends A{} class C extends B{} + x( new ArrayList<List<A>>() ); + x( new List<A<A>>() ); + x( new ArrayList<ArrayList<A>>() ); + x( new List<List<A>>() );

True

A recursive method can always be converted into a nonrecursive method using iterations.

the Serializable interface

All the concrete classes in the Java Collections Framework implement ________. + the Serializable interface + the Comparator interface + the Cloneable interface + the Comparable interface

No

ArrayList<String> and ArrayList<Integer> are two types. Does the JVM load two classes of two different types named ArrayList<String> and ArrayList<Integer>?

// line 2

At what line will there be a compiling error? public class Generics{ public static void main(String[] args){ B b = new B();//line 1 C c = b.process(new C());//line 2 B<C> b2 = new B<C>(); //line 3 C c2 = b2.process(new C()); //line 4 } } interface A{int count(); void show(); } class B<T extends A>{ T process(T t){ t.count(); t.show(); return t; } } class C implements A{public int count(){ return 25; } public void show(){ System.out.print("Class C"); } } + //line 1 + // line 2 + // line 3 + // line 4

8

Consider a definition of fizzle(): fizzle(1) = 1 fizzle(N) = fizzle( (N+1)/2 ) + fizzle(N/2), for N>1 *According to this definition, what is fizzle(8)? Assume integer division.* + 64 + 1 + 8 + 4

6

Consider a definition of mystery(): mystery(0,N) = N mystery(P,Q) = mystery(P-1, Q+1) *According to this definition, what is mystery(2,4)?* + 6 + 4 + 2 + 0

Yes

Does <?super Number> represent a superclass of Number?

+ <Date> + <?>

Fill in the code in Comparable________ c = new Date(); + <Date> + <?> + <E> + <String>

return index

Fill in the code to complete the following function for computing a Fibonacci number. 0, 1, 1, 2, 3, 5, 8, 13..... public static int fib(int index) { if (index == 0 || index == 1) // Base case ________ else // Reduction and recursive calls return fib(index - 1) + fib(index - 2); } + return 1 + return 2 + return index + return 0

c.iterator()

For an instance of Collection, you can obtain its iterator using ________. + c.iterators() + c.iterator() + c.iterable() + c.getIterator()

class Bowler implements Player<Guitar>{ public void play(Guitar o){ } }

Given: class Game{ } class Cricket extends Game{ } class Instrument{ } class Guitar extends Instrument{ } interface Player<E>{ void play(E e); } interface GamePlayer<E extends Game> extends Player<E>{ } interface MusicPlayer<E extends Instrument> extends Player{ } *Identify the valid declaration.* + class Batsman implements GamePlayer<Cricket>{ public void play(Game o){ } + class Bowler implements GamePlayer<Guitar>{ public void play(Guitar o){ } } + class Bowler implements Player<Guitar>{ public void play(Guitar o){ } } + class MidiPlayer implements MusicPlayer { public void play(Guitar g){ } } + class MidiPlayer implements MusicPlayer<Instrument> { public void play(Guitar g){ } }

B

Look at square numbers again: square(1) = 1 square(N) = square(N-1) + 2N -1 *Which Java method below successfully implements this definition?* A. int square( int N ) { if ( N<1 ) { return 1; } else { return N*N; } B. int square( int N ) { if ( N==1 ) { return 1; } else { return square(N-1) + 2*N - 1; } } C. int square( int N ) { if ( N=1 ) { return 1; } else { return square(N-1) + 2*N - 1; } } D. int square( int N ) { if ( N==1 ) { return 1; } else { return square(N); } }

List<Number> list1 = new ArrayList<Number>();

Select all that DO NOT generate compiling errors. + ArrayList<Object> list2 = new ArrayList<Integer>(); + List<Number> list1 = new ArrayList<Number>(); + ArrayList<Number> list2 = new ArrayList<Integer>() + ArrayList<Object> list2 = new ArrayList<String>();

no compiling errors

Select the line(s) that generate compiling error(s). public class X{ 1. public static void f1(ArrayList<?> list){ 2. System.out.println( list.size()); 3. list.size(); } public static void main(String[] args){ 4. f1(new ArrayList<Integer>()); 5. f1(new ArrayList<Double>()); } } + 1 + 2 + 3 + 4 + 5 + no compiling errors

+ list.add(new Integer(100)); + list.add(new ArrayList()); + list.add("Red"); + list.add(new java.util.Date());

Suppose List list = new ArrayList(). *Which of the following operations are correct?* + list.add(new Integer(100)); + list.add(new ArrayList()); + list.add("Red"); + list.add(new java.util.Date());

+ list.add("Red");

Suppose List<String> list = new ArrayList<String>. *Which of the following operations are correct?* + list.add(new java.util.Date()); + list.add("Red"); + list.add(new ArrayList()); + list.add(new Integer(100));

It will throw an exception at run time due to code at //1.

https://solacc.instructure.com/courses/172745/files/46464069/preview + Compilation failure at //1. + It will throw an exception at run time due to code at //1. + It will print isbns of the books but NOT in sorted fashion.

KIHCO

package generics.questions.examples; class ClassA<U> { U u; public ClassA(U str) { u = str; } public U getU() { return u; } } class ClassB<T, V> extends ClassA<V>// LINE A{ public ClassB(V v) { super(v); } public String myMethod(){ V input = getU(); String inputString = input.toString(); String result = ""; for (int i = inputString.toCharArray().length; i > 0; i--){ int ch = inputString.toLowerCase().toCharArray()[i - 1]; int newNumber = 0; while (ch > 0){ newNumber = newNumber * 10 + ch % 10; ch = ch / 10; } result += ((char) ((newNumber / 65) + 66)); } return result; } } class ClassC{ public static void main(String[] args){ ClassB<Integer, StringBuilder> obj =new ClassB<Integer, StringBuilder>( new StringBuilder("MERIT") ); System.out.println(obj.myMethod()); } } + 7371706577 + KIHCO + Compiling error at LINE A + Compiling error at some other place + Runtime error


Ensembles d'études connexes

Embryology Test 2 Practive from BRS

View Set

Online Homework 5- Organic Molecules

View Set

Skill in Context Journeys Unit 1 Week 3 My Librarian is a Camel

View Set

Unit 3: English Civil War Timeline

View Set

Hemostasis, Wound Healing, & Wound Closure, Ch 11, ST for the ST

View Set

National Exam Practice questions****

View Set