MIDTERM EXAM, Fall 2010 Number of Questions: 19 Total Points: 53 NAME (printed):

mammetlizardlickΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

87 εμφανίσεις

CIS 21
68

MIDTERM

EXAM, Fall 2010


Number of Questions:

19



Total Points:

53



NAME
(printed):



Question 1 (3 points)

Write a short JAVA method that prints the values of a given JAVA LinkedList instance
'theList'. Assume that the objects in 'theList
' can print their value overriding the method
'toString()' from 'JAVA Object':


public void printAll (LinkedList theList){










}

Question 2 (1 point)


Imagine you wrote an event based program, all is written correctly, all listeners are registers and

fully implemented. Which of the following statements is
WRONG
?




All methods you wrote will always be used when the program is run, otherwise there
would be unnecessary methods


a waste of implementation time



Possibly only a few, or even none of the liste
ner methods will be used, depending on the
user's actions



The listener methods can have empty bodies
---

the minimal requirement is to declare all
interface methods and to implement them empty (that means, '{ }' )



You had to implement all methods of all ne
cessary listeners because implementing an
interface equals the promise to do so


Question 3 (2 points)


Assume a class P (for parent), and two inherited classes class C1 and C2 (C for child).

P has a method ‘print()’, which prints “PARENT”, C1, C2 overrid
e this method with their own
implementation, printing “CHILD1”, “CHILD2”, respectively.

What’s the output of the following code?


P a[] = new P[6];

a[0] = new P();

a[1] = new C1();

a[2] = (Parent)new C1();

a[3] = a[1];

a[4] = (Parent)a[1];

a[5] = new C2();

for (int i=0; i<6;i++)


System.out.println(a[i].print();


ANSWER
:



Question 4 (2 points)


A programmer adds a JButton jb to a visible JPanel jp, only implementing the lines


JButton jb = new JButton;

jp.add(jb);


He/she wonders why nothing happens
when the button is clicked, although it appears on the
screen. What is missing?








Question 5 (2 points)


Describe shortly how to achieve the following GUI layout (which layouts are used, how
are they nested?)


















Question 6. (2 points)


Select the best answer to each question below from the choices:


Double linked list, stack, array, iterator




the first element of a __________________, is removable in O(1)




A ________ is a specific data structure, where the access is limited to the f
irst element




You have random access in O(1) to every element of a ___________




In Java, the ________________ can be used to traverse LinkedLists as well as
ArrayLists

















Question 7 (1 point)

What’s correct?



Static methods come with the JAVA distribution

and can only be altered by SUN



Static methods can not contain variables, they are for ‘static’ purposes (like
System.out.println)



Static methods can not be overridden



Static methods can only directly (i.e. without instantiating objects) access static (cla
ss)
variables



Question 8 (1 point)

In JAVA, in order to be an ActionListener, a (possibly abstract) class must


-

Be inherited from a java.awt or javax.swing class

-

Implement the addActionListener() method

-

At least declare (but not necessarily implement)

all methods of the Interface
ActionListener

-

Be registered to a source that is able to fire an ActionEvent


Question 9 (2 points)

In the JAVA event model, the listener registers to the source. The source carries a list of
registered listeners. In case the

event occurs, the source calls some specific method of each
registered listener. Why can the source assume that the specific method is implemented in the
listeners?









Question 10 (4 points)

Please determine the order of magnitude of the following a
lgorithms, based on the table
below. The table describes the runtime of each algorithm if performed on 1000 data
elements, and on 2000 data elements.



1000 data elements

2000 data elements

Order of magnitude

Algorithm 1

10 seconds

80 seconds

O( )

Al
gorithm 2

7 seconds

7 seconds

O( )

Algorithm 3

122 seconds

244 seconds

O( )

Algorithm 4

1 second

4 seconds

O( )




Question 11 (2 points)

How long does algorithm 1 of the previous question need to process 10,000 data
elements?





Question
12 (1 points)


Suppose
public abstract void explore();

to be a declaration of a method in an abstract
class A.
abstract class AE

is a derived abstract class. (AE extends A).


What’s correct?



AE MUST implement ‘explore’ since it is derived from an abstrac
t class



AE can not be derived from A, since explore is abstract



AE is NOT ALLOWED to implement explore, since AE is abstract



AE might or might not implement explore, it doesn’t matter




Question 13 (parts: a: 3 points,b: 3 points ,c: 3 points,d: 2 points,

e: 1 point;
TOTAL: 12 points)

A binary tree is a linked data structure, which is defined as follows:

a) it has a start node, called 'root'

b) each node has at max two successor nodes,

called 'leftNext' and 'rightNext'

(see figure. The nodes marked 'X,
Y' are used in the questions)







a) Please write the node class for a binary tree that contains integer (primitive int) values.

The constructor should take an int
-
variable as input.










b) write the method 'addFirst(int a)' for this data structure.

The method replaces the root
with a new node containing the value 'a'. The current root becomes the left child.








r
o
o
t

x

y


c) write a method 'leftMost' that returns the LEFTMOST element of a binary tree. An
example for a leftmost element is given in the fig
ure, node 'Y'. Do NOT write a program
that solves the task only for the specific tree in the example, but for ANY binary tree.













d) Assuming the tree has a sufficient number of elements, how do you specifically address
the node marked 'X' in the
figure, given the root?





e) What is the order of magnitude of 'addFirst' in this data structure?









Question 14 (parts: a,b. part a: 3 points, b: 1 point; TOTAL: 4 points)


a) (3 points):

What is the output of the following program ?


public class

Untitled1 {


static LinkedList ll = new LinkedList();



public static void main(String[] args) {


for (int i=0;i<10;i+=3){


ll.addFirst(new Integer(i));


}



Iterator it=ll.iterator();



while(it.hasNext()){


Integer iv = (Integer)
it.next();


System.out.println(iv); // prints the value of iv


}


}

}


Answer:







b) (1 point):

Why must the field ‘
LinkedList ll’

be declared static ?





Question 15. (1 point)


Which of the following is correct:



abstract classes may not co
ntain non
-
abstract (=implemented) methods



interfaces may contain non
-
abstract (=implemented) methods



an abstract class may implement the methods of an interface



an interface may be derived from multiple abstract classes


Question 16. (3 points)

Assume a
double linked list with start and end pointer. What's the order of magnitude for
the following operations:


InsertFirst:


GetFirst:


InsertLast:


GetLast:


Iterate through list:


removeAtIndex(i):




Question 17. (6 points)


Assume you have a Node clas
s for a linked list containing values of type Object, and a
MyLinkedList class providing a public 'start' reference (of type Node, of course).

Please write a class that implements an Iterator for the MyLinkedList class.

Do so by implementing 'public boo
lean hasNext()' and 'public int next()' methods (do NOT
implement the 'remove' method which would be necessary for a JAVA Iterator)


Please complete the following template:

public class MyIterator implements Iterator{

// add field(s)





public MyIterator(
MyLinkedList mll){

// implement constructor




}




public boolean hasNext(){

// implement method







}


public int next(){

// implement method







}


public void remove(){

// LEAVE BLANK!

}


Question 18. (3 points)

Write a function ‘
public String re
verse(String s)
’ that returns a reversed String (“hello”
turns into “olleh”). You have to use a Stack and the methods
pop()

and
push
(). Pseudo code or
a short description is fine. No Java program required.













Question 19. (1 point)

What is the
difference between the following two declarations?


LinkedList ll1 = new LinkedList();

LinkedList<MyClass> ll2= new LinkedList<MyClass>




ll1 can contain Objects of any kind, while ll2 is limited to objects of class 'MyClass'



ll2 is my own implementation of

LinkedList, while ll1 is the JAVA version



there is no difference for the compiler, it's just a suggestion to the programmer to use
MyClass objects



ll2 is the version of LinkedList that was introduced in Java 5, ll1 is deprecated and should
not be used


T
hat’s it ! Good Luck !