Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS180 Review Questions. Administriva Final Exam –Friday 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming."— Presentation transcript:

1 CS180 Review Questions

2 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

3 Exceptions Which subclass of Throwable is an exception checked at compile time? a. ArrayIndexOutOfBoundsException b. RuntimeException c. IOException d. NullPointerException

4 Exceptions Which subclass of Throwable is an exception checked at compile time? a. ArrayIndexOutOfBoundsException b. RuntimeException c. IOException d. NullPointerException

5 Exceptions Given 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.

6 Exceptions Given 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.

7 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

8 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

9 Generics Consider the following class declaration: public class Box {... } Which of the following statements are true regarding class Box ? I. T is a parameterized type II. T is a defined class, therefore there exists a defined class T somewhere III. T can be a String

10 Generics Consider the following class declaration: public class Box {... } Which of the following statements are true regarding class Box ? I. T is a parameterized type II. T is a defined class, therefore there exists a defined class T somewhere III. T can be a String

11 Memory Management public 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());

12 Memory Management public 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()); 0 1 2

13 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.

14 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.

15 Static Keyword What 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.down(); y.down(); System.out.println("x=" + x.get() + " y=" + y.get()); } a. x=0 y=0 b. x=1 y=-1 c. x=-1 y=1 d. this code does not compile

16 Static Keyword What 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.down(); y.down(); System.out.println("x=" + x.get() + " y=" + y.get()); } a. x=0 y=0 b. x=1 y=-1 c. x=-1 y=1 d. this code does not compile

17 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, K public 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();

18 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, K public 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();

19 Inheritance public 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 0 b. AB returnStuff returns 1 c. B returnStuff returns 0 d. B returnStuff returns 1

20 Inheritance public 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 0 b. AB returnStuff returns 1 c. B returnStuff returns 0 d. B returnStuff returns 1

21 Java Features Which of the following is deomonstrated by the following code? Integer intObject = 5; int intPrimitive = intObject; a. boxing and unboxing b. compile time errors c. dynamic binding d. pass by value

22 Java Features Which of the following is deomonstrated by the following code? Integer intObject = 5; int intPrimitive = intObject; a. boxing and unboxing b. compile time errors c. dynamic binding d. pass by value

23 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. 256 b. 16 c. 8 d. this code does not compile

24 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. 256 b. 16 c. 8 d. this code does not compile

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

26 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

27 Recursion public void f(int x) { if (x > 0) { System.out.print(x % 10); f(x / 10); } f(58493) = ?

28 Recursion public void f(int x) { if (x > 0) { System.out.print(x % 10); f(x / 10); } f(58493) = 39485

29 Programming Questions

30 Linked Lists Consider 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) { … }

31 Linked Lists public 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(); }

32 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) { }

33 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; }

34 Linked Lists Now write the same method, but recursively. public Node removeEveryOther(Node l) { }

35 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; }

36 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) { }

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


Download ppt "CS180 Review Questions. Administriva Final Exam –Friday 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming."

Similar presentations


Ads by Google