Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 171

Practice Midterm Exam

Problem 1 (10 points)

1. Which abstract datatype is used to implement a breadth-first search

2. In Java, what is auto-boxing?

3. If a class implements Iterable interface, what can you do with the class objects?

4. Which of the following are not correct?

(a)  An interface type species all methods required by the interface

(b)  An interface type can also have instance variables

(c)  Classes can be instantiated (into objects using new) while interfaces cannot. (d)  All methods in an interface type cannot have method implementations         (e)  A class can implement more than one interface type.

5. In Java, what key word is used to create a subclass from another class?

Problem 2 (15 points) Below is an example Java method which uses a stack of integers with typical push and pop operations.

public  static  int  foo(int  x,  int  y)   {

int  sum=0;

while (x   >=   0){

if (x   %   3  ==   0)

sum  +=  stack .pop();

stack .push(y-- );

x-- ;

}

return  sum;

}

 

1.  Assuming the stack is initially empty, draw a snapshot of the stack after every push and pop state-

ment for each iteration for foo(5,6). Label each stack snapshot with the values of x and y, and the push or pop statement.  For example, the following shows the rst snapshot of the stack after the push statement for the call foo(5,6)

6

---------------

x=5,  y=6  -  push

2. What does foo(5,6) evaluate to?

Problem 3 (20 points)

1.  Consider interface Measurable and class BankAccount that implements interface Measurable. Which of he following statements would generate an error?

public  interface  Measurable   {

double  getMeasure();

}

public  class  BankAccount  implements  Measurable{

private  double  balance;

public  BankAccount()   {

balance  =  0;

}

public  double  getMeasure()   {

return  balance;

}

}

 

(a) Measurable  m  =  new  BankAccount();

(b) Measurable  m  =  new  Measurable();

(c) Measurable  m  =  new  BankAccount(); BankAccount  b  =   (BankAccount)  m;

(d)  BankAccount  b  =  new  BankAccount   ();

2.  Consider the classes Person and Student. Which of the following would generate an error?

public  class  Person   {

public  String  name;

public  Person(String  n){

name  =  n;

}

}

public  class  Student  extends  Person   {

public  double  gpa;

public  void  printGPA(){

System .out .println(gpa);

}

}

 

(a)  Person  p  =  new  Student();

(b)  Student  s  =  new  Person();

(c)  Student  s  =  new  Person("Alice");

(d)  Person  p  =  new  Student(); Student  s  =   (Student)  p;

(e)  Student  s  =  new  Person("Bob");

Person  p  =   (Person)  s;

(f)  Person  p  =  new  Student("Steve");

3. What values will be returned during the following sequence of queue operations in the order given, if the queue is initially empty?

enqueue(3), enqueue(1),  dequeue(), enqueue(4), enqueue(5),  dequeue(),  dequeue(), enqueue(9), en- queue(2), dequeue(), enqueue(6), enqueue(8), dequeue(), dequeue(), enqueue(7), dequeue(), dequeue() dequeue()

4. What values will be returned during the following sequence of stack operations in the order given, if the stack is initially empty?

push(3), push(1), pop(), push(4), push(5), pop(), pop(), push(9), push(2), pop(), push(6), push(8), pop(), pop(), push(7), pop(), pop() pop()

Problem 4.  (30 points) Consider the program below and answer questions

class  Node   {

public  int  iData;

public  Node  next;

public  Node(int  id){

iData  =  id;

}

}     //  end  class  Node

while   (current   !=  null  &&  current .iData   !=  key)

current  =  current.next ;  //  go  to  next  node

return  current;

}

public  void  displayList()   {  //  display  the  list

for   (Node  current  =  first;  current   !=  null;  current  =  current.next ) System .out .println(current .iData);  //  print  data

}

public  void  insertFirst(int  key)   {

//insert    a  node  at  the  front  of  the  list

//  fill  in  your  implementation

}

public  Node  delete(int  key)   {

//  delete  the  node  with  a  given  key

//  fill  in  your  implementation

}

}    //  end  class  LinkList

class  LinkListApp

{

public  static  void  main(String[]  args)

{

LinkList  theList  =  new  LinkList();  //  create  a  list

theList .insertFirst(22);

theList .insertFirst(44);

theList .insertFirst(66);

Node  d  =  theList .delete(44);

d  =  theList .delete(88);

theList .displayList();

}     //  end  main ()

}    //  end  class  LinkListApp

 

(a) Implement the insertFirst method that inserts a new node with the given key at the front of the

linked list. Assume the list is not empty.

public  void  insertFirst   (int  key)

(b) Implement the delete method that deletes a node with the given data key.  Assume the list is not

empty. If the key does not exist in the list, the method should return null and should not change the list. public  Node  delete   (int  key)

(c)  Predict the printed results of the main method in LinkListApp

Problem 5.  (25 points) Given an array of positive integers, and an integer count, write a method (called copyEvens) that returns a new array of length count containing the rst count even numbers from the

original array. The original array will contain at least count even numbers.  Some example calls and what should be returned are:

copyEvens({3,2,4,5,8}, 2) returns {2,4}

copyEvens({3,2,4,5,8}, 3) returns {2,4,8}

copyEvens({6,1,2,4,5,8}, 3) returns {6,2,4}

Remember x  %  y will give you the remainder when x is divided by y.