CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming...

37
CS180 Review Questions
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming...

CS180

Review Questions

Administriva

• Final Exam– Friday 5/2 @ 8am MTHW 210– No GUI programming question– Format

• 35 multiple choice questions• 4 programming questions

– New topics since exam 2 stressed• Streams, file I/O• Dynamic Data Structures• Generics• Recursion• GUIs, Applets, HTML

Exceptions

Which subclass of Throwable is an exception checked at compile time?

a. ArrayIndexOutOfBoundsException

b. RuntimeException

c. IOException

d. NullPointerException

Exceptions

Which subclass of Throwable is an exception checked at compile time?

a. ArrayIndexOutOfBoundsException

b. RuntimeException

c. IOException

d. NullPointerException

ExceptionsGiven the following method and class signatures:

public class A extends Exception {...}public class B extends A {...}public class C extends B {...}

public void doStuff() throws A,B,C

The following code does not compile. Why?

try {doStuff();

} catch(A a) {a.printStackTrace();

} catch(B b) {b.printStackTrace();

} catch(C c) {c.printStackTrace();

} finally {System.out.println("I love exceptions!");

}

a. B and C are not exception classes since they do not extend class Exception and therefore cannot be caught.

b. The catch blocks for exceptions of type B and C are unreachable.c. A finally block cannot be used with multiple catch blocks.d. No one loves exceptions and therefore the finally block fails to compile.

ExceptionsGiven the following method and class signatures:

public class A extends Exception {...}public class B extends A {...}public class C extends B {...}

public void doStuff() throws A,B,C

The following code does not compile. Why?

try {doStuff();

} catch(A a) {a.printStackTrace();

} catch(B b) {b.printStackTrace();

} catch(C c) {c.printStackTrace();

} finally {System.out.println("I love exceptions!");

}

a. B and C are not exception classes since they do not extend class Exception and therefore cannot be caught.

b. The catch blocks for exceptions of type B and C are unreachable.c. A finally block cannot be used with multiple catch blocks.d. No one loves exceptions and therefore the finally block fails to compile.

Generics

Which of the following are true regarding the use of generics and parameterized types in Java?

I. Generics provide type safety by shifting more type checking responsibilities to the compiler. II. Generics and parameterized types eliminate the need for downcasts when using Java Collections.III. When designing your own collections class (say, a linked list), generics and parameterized types allow you to code the class just once as opposed to infinitely many times

Generics

Which of the following are true regarding the use of generics and parameterized types in Java?

I. Generics provide type safety by shifting more type checking responsibilities to the compiler. II. Generics and parameterized types eliminate the need for downcasts when using Java Collections.III. When designing your own collections class (say, a linked list), generics and parameterized types allow you to code the class just once as opposed to infinitely many times

Generics

Consider the following class declaration:

public class Box<T> { ... }

Which of the following statements are true regarding class Box<T>?

I. T is a parameterized type II. T is a defined class, therefore there exists a defined class T somewhereIII. T can be a String

Generics

Consider the following class declaration:

public class Box<T> { ... }

Which of the following statements are true regarding class Box<T>?

I. T is a parameterized type II. T is a defined class, therefore there exists a defined class T somewhereIII. T can be a String

Memory Managementpublic class A{

private int x;

public A(int x){

set(x);}

public int get(){

return x;}

public void set(int x){

this.x = x;}

public static void swap(A a, A b){

A temp = a;a = b;b = temp;

}}

A[] arr = new A[3];arr[0] = new A(0);arr[1] = new A(1);arr[2] = new A(2);for (int k=0; k<1000; k++){

int idx1 = (int)(Math.random()*3);int idx2 = (int)(Math.random()*3);A.swap(arr[idx1], arr[idx2]);

}for (int k=0; k<arr.length; k++)

System.out.println(arr[k].get());

Memory Managementpublic class A{

private int x;

public A(int x){

set(x);}

public int get(){

return x;}

public void set(int x){

this.x = x;}

public static void swap(A a, A b){

A temp = a;a = b;b = temp;

}}

A[] arr = new A[3];arr[0] = new A(0);arr[1] = new A(1);arr[2] = new A(2);for (int k=0; k<1000; k++){

int idx1 = (int)(Math.random()*3);int idx2 = (int)(Math.random()*3);A.swap(arr[idx1], arr[idx2]);

}for (int k=0; k<arr.length; k++)

System.out.println(arr[k].get());

012

Static Keyword

• Which of the following statements are true regarding static class methods?

I. Static class methods cannot access non static class variables.

II. Static class methods can be called by using either an object of that class type or the class name.

III. Static class methods can use non-static private class methods in their method body.

Static Keyword

• Which of the following statements are true regarding static class methods?

I. Static class methods cannot access non static class variables.

II. Static class methods can be called by using either an object of that class type or the class name.

III. Static class methods can use non-static private class methods in their method body.

Static KeywordWhat is the output of the following program?

public class A {private static int x;

public A() {x = 0;

}

public void up() {x++;

}

public void down() {--x;

}

public int get() {return x;

}

public static void main(String[] args) {A x = new A();A y = new A();x.up();x.up();x.down();y.down();System.out.println("x=" + x.get() + " y=" + y.get());

}}

a. x=0 y=0b. x=1 y=-1c. x=-1 y=1d. this code does not compile

Static KeywordWhat is the output of the following program?

public class A {private static int x;

public A() {x = 0;

}

public void up() {x++;

}

public void down() {--x;

}

public int get() {return x;

}

public static void main(String[] args) {A x = new A();A y = new A();x.up();x.up();x.down();y.down();System.out.println("x=" + x.get() + " y=" + y.get());

}}

a. x=0 y=0b. x=1 y=-1c. x=-1 y=1d. this code does not compile

Inheritance

• Given the following classes, which of the following declarations are valid?

public interface I {…}public interface J extends I {…}public interface K {…}public abstract class A {…}public class B extends A {…} implements J, Kpublic class C extends B {…}public class D extends A {…} implements I

A a = new B();B b = new J();C c = new B();B b = new C();I i = new A();I i = new B();I i = new D();K k = new C();

Inheritance

• Given the following classes, which of the following declarations are valid?

public interface I {…}public interface J extends I {…}public interface K {…}public abstract class A {…}public class B extends A {…} implements J, Kpublic class C extends B {…}public class D extends A {…} implements I

A a = new B();B b = new J();C c = new B();B b = new C();I i = new A();I i = new B();I i = new D();K k = new C();

Inheritancepublic class A{

public A(){

System.out.print("A");}

public int returnStuff(){

return 0;}

public static void main(String[] args){

A a = new B();System.out.print(" returnStuff returns " +

a.returnStuff());}

}

public class B extends A{

public B(){

System.out.print("B");}

public int returnStuff(){

return 1;}

}

What is the output of the program if the main in class A is run?

a. AB returnStuff returns 0b. AB returnStuff returns 1c. B returnStuff returns 0d. B returnStuff returns 1

Inheritancepublic class A{

public A(){

System.out.print("A");}

public int returnStuff(){

return 0;}

public static void main(String[] args){

A a = new B();System.out.print(" returnStuff returns " +

a.returnStuff());}

}

public class B extends A{

public B(){

System.out.print("B");}

public int returnStuff(){

return 1;}

}

What is the output of the program if the main in class A is run?

a. AB returnStuff returns 0b. AB returnStuff returns 1c. B returnStuff returns 0d. B returnStuff returns 1

Java Features

Which of the following is deomonstrated by the following code?

Integer intObject = 5;int intPrimitive = intObject;

a. boxing and unboxingb. compile time errorsc. dynamic bindingd. pass by value

Java Features

Which of the following is deomonstrated by the following code?

Integer intObject = 5;int intPrimitive = intObject;

a. boxing and unboxingb. compile time errorsc. dynamic bindingd. pass by value

Scope

What is the output of the following code?

for (int k=0, x=2; k<3; k++){

x *= x;}System.out.println("x = " + x);

a. 256b. 16c. 8d. this code does not compile

Scope

What is the output of the following code?

for (int k=0, x=2; k<3; k++){

x *= x;}System.out.println("x = " + x);

a. 256b. 16c. 8d. this code does not compile

Recursion

public int f(int x, int y){

if (x == 0)return y;

return y + f(x-1, y+1);}

System.out.println(f(4, 3));

Recursion

public int f(int x, int y){

if (x == 0)return y;

return y + f(x-1, y+1);}

System.out.println(f(4, 3));

3 + 4 + 5 + 6 + 7 = 25

Recursionpublic void f(int x){

if (x > 0){

System.out.print(x % 10);f(x / 10);

}}

f(58493) = ?

Recursionpublic void f(int x){

if (x > 0){

System.out.print(x % 10);f(x / 10);

}}

f(58493) = 39485

Programming Questions

Linked ListsConsider the following Node class which can be used to make a linked list of integers:

public class Node {int x;Node next;

public Node(int x, Node next) {setX(x);setNext(next);

}

public void setX(int x) { this.x = x; }public int getX() { return x; }public void setNext(Node next) { this.next = next; }public Node getNext() { return next; }

}

Complete the method below, removeOddNumbers, which takes in a linked list and returns a linked list with all Nodes with odd numbers removed from it. Your method should NOT create new Node objects (that is, the phrase "new Node" should not exist in your code) and should keep the even numbers remaining in the list in the same order. You may assume that all numbers in the list are non-negative.

As an example, if you had a list:

1 -> 4 -> 3 -> 6 -> 4 -> 1 -> 9

removeOddNumbers would return the list:

4 -> 6 -> 4

public Node removeOddNumbers(Node head) {…

}

Linked Listspublic Node removeOddNumbers(Node head){

Node curr = head;Node prev = null;while (curr != null){

if (curr->getX() % 2 == 1){

if (prev != null)prev->setNext(curr->getNext());

curr = curr.getNext();}else{

prev = curr;curr = curr.getNext();

}}

}

Linked Lists

• Given an appropriate Node class, write a method which removes every other node from the list, starting with the second.

public Node removeEveryOther(Node l)

{

}

Linked Lists

• Given an appropriate Node class, write a method which removes every other node from the list, starting with the second.

public Node removeEveryOther(Node l)

{

Node l1 = l;

while (l1 != null && l1.next != null)

{

l1.next = l1.next.next;

l1 = l1.next;

}

return l;

}

Linked Lists

• Now write the same method, but recursively.

public Node removeEveryOther(Node l)

{

}

Linked Lists

• Now write the same method, but recursively.

public Node removeEveryOther(Node l)

{

// base case

if (l == null || l.next == null)

return l;

// recursive case

Node l1 = removeEveryOther(l.next.next);

l.next = l1;

return l;

}

Recursion

Write a function isPalindrome which takes in a String and returns true if the String is a palindrome, false otherwise.

public boolean isPalindrome(String s)

{

}

Recursion

Write a function isPalindrome which takes in a String and returns true if the String is a palindrome, false otherwise.

public boolean isPalindrome(String s)

{

if (s == null)

return false;

if (s.length() <= 1)

return true;

return s.charAt(0) == s.charAt(s.length()-1) &&

isPalindrome(s.substring(1, s.length()-1));

}