Download Powerpoint Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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.
b.
c.
d.
ArrayIndexOutOfBoundsException
RuntimeException
IOException
NullPointerException
Exceptions
Which subclass of Throwable is an
exception checked at compile time?
a.
b.
c.
d.
ArrayIndexOutOfBoundsException
RuntimeException
IOException
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<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 somewhere
III. 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 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.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.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
public
public
public
public
public
public
A
B
C
B
I
I
I
K
a
b
c
b
i
i
i
k
=
=
=
=
=
=
=
=
interface I {…}
interface J extends I {…}
interface K {…}
abstract class A {…}
class B extends A {…} implements J, K
class C extends B {…}
class D extends A {…} implements I
new
new
new
new
new
new
new
new
B();
J();
B();
C();
A();
B();
D();
C();
Inheritance
• Given the following classes, which of the following
declarations are valid?
public
public
public
public
public
public
public
A
B
C
B
I
I
I
K
a
b
c
b
i
i
i
k
=
=
=
=
=
=
=
=
interface I {…}
interface J extends I {…}
interface K {…}
abstract class A {…}
class B extends A {…} implements J, K
class C extends B {…}
class D extends A {…} implements I
new
new
new
new
new
new
new
new
B();
J();
B();
C();
A();
B();
D();
C();
public class A
{
public A()
{
System.out.print("A");
}
Inheritance
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.
b.
c.
d.
AB returnStuff returns 0
AB returnStuff returns 1
B returnStuff returns 0
B returnStuff returns 1
public class A
{
public A()
{
System.out.print("A");
}
Inheritance
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.
b.
c.
d.
AB returnStuff returns 0
AB returnStuff returns 1
B returnStuff returns 0
B returnStuff returns 1
Java Features
Which of the following is deomonstrated by the
following code?
Integer intObject = 5;
int intPrimitive = intObject;
a.
b.
c.
d.
boxing and unboxing
compile time errors
dynamic binding
pass by value
Java Features
Which of the following is deomonstrated by the
following code?
Integer intObject = 5;
int intPrimitive = intObject;
a.
b.
c.
d.
boxing and unboxing
compile time errors
dynamic binding
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));
3 + 4 + 5 + 6 + 7 = 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));
}
Related documents