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

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Yoshi
Java Review Interface, Casting, Generics, Iterator.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
Testing and Error Handling Intro to Java. Testing We test to try and make sure our programs work correctly and have no bugs If we have access to the code,
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
1 Why do we need exceptions? In C, return variables must be used to indicate errors: if((fd = fopen(path,...)) == -1){ if(errno==a){...} else if(errno==b){...}
March 2004Object Oriented Design1 Object-Oriented Design.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Final Review Recitation – 05/01/2009 CS 180 Department of Computer Science, Purdue University.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CIS 270—Application Development II Chapter 13—Exception Handling.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
OVERRIDING/OVERLOADING Srinivas. EXAM OBJECTIVES  Given a code example, determine if a method is correctly overriding or overloading another method,
Object Oriented Programming: Java Edition By: Samuel Robinson.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CS 180 Final Exam Review 12/(11, 12)/08. Announcements Final Exam  Thursday, 18 th December, 10:20 am – 12:20 pm in PHYS 112  Format 30 multiple choice.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 1. Normal and exceptional control flow 2. Java exception types 3. Exception handling syntax 4. Inheritance.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Defining Your Own Classes II
Modern Programming Tools And Techniques-I
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Tirgul 13 Exceptions 1.
Introduction to Exceptions in Java
Exceptions, Interfaces & Generics
Introduction to Exceptions in Java
CS Week 10 Jim Williams, PhD.
null, true, and false are also reserved.
Interface.
Java Programming Language
CMSC 202 Exceptions 2nd Lecture.
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
CMSC 202 Exceptions 2nd Lecture.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Why do we need exceptions?
Java Basics Exception Handling.
Presentation transcript:

CS180 Review Questions

Administriva Final Exam –Friday 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

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.

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.

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 {... } 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Programming Questions

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

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

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