联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> OS作业OS作业

日期:2024-11-01 06:37

CSE 12

Winter 2024

Midterm Exam

Concept 1 (6 pts). Java Generics

(Problem 1 - 5) Read the following generic class, Triplet, and answer the questions. The class has three generic variables, first, second, and third.

public class Triplet  implements Comparable>{//Line 1

1. Which of the following statement is true based on Line 1

A. Triplet is a child of Comparable

B. Generic type T must be a child of Comparable

C. Both A and B are correct

D. Neither A nor B is correct

private T first, second, third;

public Triplet(){

first = second = third = null;

}

public Triplet(T first, T second, T third){

this.first = first;//Line 2

2. True or False: If we remove this. in Line 2, the assignment won’t be correct.

A. True

B. False

this.second = second;

this.third = third;

}

public Triplet(Triplet p){

this.first = p.first;//Line 3

3. True or False: If we remove this. in Line 3, the assignment won’t be correct.

A. True

B. False

this.second = p.second;

this.third = p.third;

}

public int compareTo(Triplet other){

if(other.first.equals(first) && other.third.equals(third)){

return second.compareTo(other.second);

}

else{

return other.first.compareTo(first);

}

}

public static void main(String[] args){

//Line 4

}

}

4. Which of the following is the correct way to create a Triplet of Strings at the location of Line 4?

A. Triplet ref = new Triplet<>(“a”, “b”, “c”);

B. Triplet ref = new Triplet<>();

C. Triplet ref = new Triplet(ref2);//assume ref2 points to a Triplet of Strings

D. All the approaches work.

E. None of the answers is correct.

5. Based on the definition of the CompareTo method from Java, please answer the question.

compareTo compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Based on the compareTo method, which of the following Triplet object is bigger? Triplet of  (2, 4, 4) or  Triplet of  (1, 3, 5)?

A. Triplet of  (1, 3, 5)

B. Triplet of (2, 4, 4)

C. They are the same.

6. Which of the following is the correct way to create a generic array of type E with 10 elements? This problem isn't related to the problems above.

A. new E[10];

B. new Object[10];

C. (E[]) new Object[10];

D. More than once choices are correct

E. None of the answer is correct

Concept 2. (4 pts) JUnit and Testing

7. True or False: The @Test tag is used in JUnit to indicate the method is a tester method.

A. True                        B. False

(Problems 8 - 10). Suppose we need to write a tester for the following function dosth.

public class Foo{

String name;

public int dosth (){

//code to find a pattern in the name and returns the location of the first occurrence.

//this code may possibly throw a NullPointerException

} }

8. True or False: We need to change the signature of dosth to public boolean dosth (int a) throws NullPointerException to avoid a compiler error.

A. True                        B. False

9. True or False: In our tester, if we assume no exception is thrown, we only need to test if the dosth method returns the correct value. We shouldn’t test if this method changes name.

A. True                        B. False

10. True or False: If we use Junit test to test for exceptions, we can use try catch in our tester to test if the correct exception is thrown.

A. True           B. False

Concept 3 (8 pts). ArrayList

(Problems 11 - 17). In our class and PA, we created our own generic ArrayList class. A part of the generic class is shown below (slightly modified from our PA2 code). Please answer the following questions

public class ArrayList < Problem 11 >{

Object[] arr; int size;

public ArrayList(E[] arr, int size){ this.arr = arr;

this.size = size; }

public E median (){

//Method to complete

}

//other methods

public static void main(String args[]){ Integer [] arr = new Integer[3];

arr[0] = 20;   arr[1] = 10;   arr[2] = 90;

ArrayList ref = new ArrayList<>(arr, 3); System.out.println(ref.median ());

System.out.println(arr[0]); //Problem 17 }

}

The median method will return the median of the array arr. It should throw a NullPointerException ifarr is null. It should throw an IllegalArgumentException if size is even. Please note that this method should   not change arr. For example, ifarr is {1, 4, 2, 3, 1}, the median method should return 2. Ifarr is {“a”, “c”, “b”}, the method should return “b”. In other words, it should always return the element in the middle   after sorting.

public E median(){

if( Problem 12 ){

throw new NullPointerException(); }

if( Problem 13 ){

throw new IllegalArgumentException(); }

Object[] tmp = Problem 14 ; for(int i = 0; i < size; ++i){

Problem 15 ;

}

Arrays.sort(tmp);

return (E) tmp[ Problem 16 ]; }

11. True or False. For the generic type for the ArrayList class, we should let it extend Comparable interface so we can guarantee the median method to work.

A. True           B. False

12. Select the correct statement for problem 12

A. arr == null      B. arr.length == 0  C. size == 0  D. More than one choice is correct.  E. No

choice is correct

13. Select the correct statement for problem 13

A. size%2==0 B. arr.length%2 == 0 C. Both A and B are correct   D. No choice is correct

14. Select the correct statement for problem 14. Select the best choice and pay attention to the for loop beneath problem 14 blank.

A. arr B. new Object[size]  C. Both A and B are correct   D. No choice is correct

15. Select the correct choice for problem 15

A. tmp[i] = arr[i] B. tmp[i] = this.arr[i]  C. Both A and B are correct   D. No choice is correct

16. Select the correct choice for problem 16

A. arr.length/2          B. size/2    C. Both A and B are correct   D. No choice is correct

17. What will be printed out by the line for problem 17 in the main method? Assume we have implemented all methods correctly.

A. 20               B. 10                C. 90

18. If the arr in the ArrayList looks like the following and the array has a capacity of 5. If we call append(2). what will be the capacity of the array after expand capacity based on PA2?

9

12

3

7

8

A. 6     B. 7     C. 8     D. 10   E. 12

Concept 4 (9 pts). LinkedList

Problems 19-25. Look at the following Node class and answer questions. We assume that the constructors will properly connect a node with the node after it.

class Node {

E data;

Node next; Node prev;

public Node(E data) { this.data = data;

next = null;

prev = null; }

public Node(E data, Node after) { this.data = data;

this.next = Problem 20 ;

this.prev = Problem 21 ;

if ( Problem 22 != null) {

after.prev.next = Problem 23 ; }

after.prev = Problem 24 ; }

}

public class Main {

public static void main(String[] args) {

Node n0 = new Node<>("cse12");

Node n1 = new Node<>("hello", n0); Node n2 = new Node<>("bye", n0);

} }

19. True or False: This linked list using this Node class should be a doubly linked list.

A. True              B. False

20. What should be the correct statement for problem 20?

A. after.prev B. after C. after.next D. None is correct    E. More than 1 is correct

21. What should be the correct statement for problem 21?

A. after.prev B. after C. after.next D. None is correct    E. More than 1 is correct

22. What should be the correct statement for problem 22?

A. after.prev B. after C. after.next D. None is correct    E. More than 1 is correct

23. What should be the correct statement for problem 23?

A. this B. this.next C. after.next D. after.prev    E. None is correct

24. What should be the correct statement for problem 24?

A. this B. this.next C. after.next D. after.prev    E. None is correct

25. Which of the following is "hello"?

A. n0.prev.data B.n2.next.data C. n2.prev.data    D. More than 1 is correct.

E. None is correct

26. True or False: For our linked list assignment PA3, our contains method from the linked list class throws an exception if the starting location is negative.

A. True                 B. False

27. What are the instance variables of our MyLinkedList class for PA3? Select all that apply

A. int size   B. Node head   C. Node tail   D. int capacity   E. int index

Concept 5 (3 pts): Iterators in Linked List

Problems 28 - 30 are about the following iterator. None of the questions are sequential to each other.

28. True or False: Given the current state of the iterator, the mostrecent call to move the iterator must be it.next() instead of it.previous()

A. True              B. False

29. True or False: Given the current state of the iterator, when we call it.previous() now, 10 will be returned.

A. True              B. False

30. True or False: Given the current state of the iterator, if we call it.remove() the node with value 20 will be deleted.

A. True              B. False

Concept 5 (7 pts): Runtime

31. Select the correct tight O notation for the f(n) = 2n + 5

A. O(1)            B. O(n)      C. O(n2)     D. O(n3)     E. None of the answer is correct

32. Select the correct tight O notation for the f(n) = 2n2   + 2n + 5

A. O(1)            B. O(n)      C. O(n2)     D. O(n3)     E. None of the answer is correct

33. True or false: Iff(n) and g(n) both∈ O(h(n)), then f(n)∈θ(g(n)). We are not judging based on the tight bound for this problem.

A. True              B. False

34. What’s the runtime of the following code? We assume the tight bound here. for (int i = 0; i < n/2; i++){

arr[i] = 0; }

for (int i = n-1; i >=0; i--){

for (int j = i+1; j < n/2; j++){

arr[j]++; }

A. O(1)            B. O(n)      C. O(n2)     D. O(n3)     E. None of the answer is correct

(Problems 33 - 35). Given the following code and answer the questions. Assume that the size of the linkedlist is n.

//Block 1

LinkedList myL = new LinkedList(); // Add n elements to myL (code not shown)

ListIterator it = myL.listIterator(); while (it.hasNext()) {

System.out.println(it.next()); }

//Block 2

LinkedList myL = new LinkedList(); // Add n elements to myL (code not shown)

for (int i = 0; i

}

35. What is the runtime of block 1 code to traverse the list? We are looking for the tightest bound.

A. O(n)      B. O(n2)     C. O(nlogn)  D. O(logn)   E. None of the answers is correct

36. What is the runtime of block 2 code to traverse the list? We are looking for the tightest bound.

A. O(n)      B. O(n2)     C. O(nlogn)  D. O(logn)   E. None of the answers is correct

37. True or False. For blocks 2, there is no difference between the best case and worst case scenarios give the same list size n.

A. True B. False




版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp