Top Banner
CS180 Review Questions
37

CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

CS180

Review Questions

Page 2: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 3: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

Exceptions

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

a. ArrayIndexOutOfBoundsException

b. RuntimeException

c. IOException

d. NullPointerException

Page 4: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

Exceptions

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

a. ArrayIndexOutOfBoundsException

b. RuntimeException

c. IOException

d. NullPointerException

Page 5: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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.

Page 6: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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.

Page 7: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 8: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 9: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 10: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 11: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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());

Page 12: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 13: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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.

Page 14: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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.

Page 15: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 16: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 17: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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();

Page 18: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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();

Page 19: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 20: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 21: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 22: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 23: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 24: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 25: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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));

Page 26: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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

Page 27: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

Recursionpublic void f(int x){

if (x > 0){

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

}}

f(58493) = ?

Page 28: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

Recursionpublic void f(int x){

if (x > 0){

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

}}

f(58493) = 39485

Page 29: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

Programming Questions

Page 30: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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) {…

}

Page 31: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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();

}}

}

Page 32: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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)

{

}

Page 33: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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;

}

Page 34: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

Linked Lists

• Now write the same method, but recursively.

public Node removeEveryOther(Node l)

{

}

Page 35: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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;

}

Page 36: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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)

{

}

Page 37: CS180 Review Questions. Administriva Final Exam –Friday 5/2 @ 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.

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));

}