Final Practice Questions

public class Node {

private Comparable data;

private Node next;

public Node(Comparable data) {

this(data, null);

}

public Node(Comparable data, Node next) {

this.data = data;

this.next = next;

}

public Comparable getData() {

return data;

}

public void setData(Comparable data) {

this.data = data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

public class LinkedList {

private Node head;

private Node tail;

public LinkedList() {

this.head = this.tail = null;

}

public Node getHead() {

return this.head;

}

public Node getTail() {

return this.tail;

}

public Node mystery(int i) {

return mystery(this.head, i);

}

private Node mystery(Node n, int i) {

if(n == null || i == 0) {

return n;

}

return mystery(n.getNext(), i-1);

}

}

Given the Node and LinkedList implementations above, implement a method insertAfter. This method will take as input a Comparable (data) and a reference to a Node (n). The method will insert a new Node containing the data after the Node n. If the insert is successful, the method will return true. In all other cases, the method will return false. The header of the method is as follows:

public boolean insertAfter(Comparable data, Node n);

What is the running time of the method you wrote for the previous question?

Given a LinkedList object ll that refers to a list with Nodes containing A, B, C, D, E (in that order), what would the following method call return:

ll.mystery(3);

Implement a method insertAllAtHead. This method will take as input a LinkedList (newlist) and will insert all elements of the newlist at the head of the list on which the method was called. Given a list ll containing A B C and a list newlist containing X Y Z, ll.insertAllAtHead(newlist) would result in ll containing X Y Z A B C. You need not worry about the contents of newlist after the method is called. For full credit, your method should run in constant time.

public void insertAllAtHead(LinkedList newlist);

Consider a method count that returns the number of nodes in a Linked List. Should the method be implemented using recursion or iteration? For full credit, explain your answer.

What is the output of the following code fragment:

String s = "Final Exam!!!";

s.replaceAll("Exam", "Project");

System.out.println(s);

What is the difference between a constructor and a method?

What is the difference between == and .equals? When should you use each?

Describe the terms overloading and overriding. How are they different?

This question refers to the class definitions below. For each of the following lines of code, do the following:

(1) Identify the code as VALID or INVALID. A line of code is INVALID if it would cause a compiler error.

(2) If the line is VALID, specify the output (if any) that the line of code would produce. Assume any previous INVALID lines of code would not prevent VALID lines of code from executing.

A a = new A();

a.method1();

a = new B();

a.method2();

a = new C();

a.method3();

B b = new A();

b.method1();

b = new B();

b.method2();

b = new C();

b.method3();

public class A {

public A() {

System.out.println("A constructor");

}

public void method1() {

System.out.println("A::method1");

}

public void method2() {

System.out.println("A::method2");

}

}

public class B extends A {

public B() {

super();

System.out.println("B constructor");

}

public void method3() {

System.out.println("B::method3");

}

}

public class C extends B {

public C() {

super();

System.out.println("C constructor");

}

public void method1() {

super.method1();

System.out.println("C::method1");

}

public void method2() {

System.out.println("C::method2");

}

}