cs 61b midterm review

49
CS 61B Midterm Review HKN, Fall 2010

Upload: bozica

Post on 05-Feb-2016

69 views

Category:

Documents


0 download

DESCRIPTION

CS 61B Midterm Review. HKN, Fall 2010. Predict the output. 1. What is the output of the following code: System.out.println ("ABCDEF".substring(0, 4).substring (1, 3)); Option A: Error Option B: BCD Option C: CD Option D: BC. Predict the output. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 61B Midterm Review

CS 61B Midterm Review

HKN, Fall 2010

Page 2: CS 61B Midterm Review

Predict the output

1. What is the output of the following code:System.out.println ("ABCDEF".substring(0, 4).substring (1, 3));

Option A: Error

Option B: BCD

Option C: CD

Option D: BC

Page 3: CS 61B Midterm Review

Predict the output

1. What is the output of the following code:System.out.println ("ABCDEF".substring(0, 4).substring (1, 3));

Option A: Error

Option B: BCD

Option C: CD

Option D: BC

• “ABCDEF”.substring(0, 4) is the string “ABCD”

• “ABCD”.substring(1, 3) is the string “BC”

Page 4: CS 61B Midterm Review

Option A: Error

Option B: i= 0

Option C: i= 5

Option D: i = 25

Page 5: CS 61B Midterm Review

Option A: Error

Option B: i= 0

Option C: i= 5

Option D: i = 25

Page 6: CS 61B Midterm Review

Option A: Error

Option B: s =

Option C: s = A

Option D: s = AA

Page 7: CS 61B Midterm Review

Option A: Error

Option B: s =

Option C: s = A

Option D: s = AA

Page 8: CS 61B Midterm Review

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Page 9: CS 61B Midterm Review

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Page 10: CS 61B Midterm Review

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Page 11: CS 61B Midterm Review

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Page 12: CS 61B Midterm Review

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Page 13: CS 61B Midterm Review

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Page 14: CS 61B Midterm Review

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Page 15: CS 61B Midterm Review

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Page 16: CS 61B Midterm Review

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Page 17: CS 61B Midterm Review

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Page 18: CS 61B Midterm Review

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Page 19: CS 61B Midterm Review

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Page 20: CS 61B Midterm Review

Option A: Error

Option B: True

Option C: False

Option D: ???

Page 21: CS 61B Midterm Review

Option A: Error

Option B: True

Option C: False

Option D: ???

Page 22: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal a = new Animal();

Dog d = (Dog) a;

Page 23: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal a = new Animal();

Dog d = (Dog) a;

Page 24: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal a = new Dog();

Dog d = (Animal) a;

Page 25: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal a = new Dog();

Dog d = (Animal) a;

Page 26: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal a = new Dog();

Dog d = (Dog) a;

Page 27: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal a = new Dog();

Dog d = (Dog) a;

Page 28: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal[] aa = new Dog[2];

Page 29: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of Animal

Animal[] aa = new Dog[2];

Page 30: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Calls Dog’s eat method

Option D: Calls Animal’s eat method

Dog is a subclass of Animal BOTH implement :eat(String food)

Dog d = new Dog();

((Animal) d).eat(“kibble”);

Page 31: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Calls Dog’s eat method

Option D: Calls Animal’s eat method

Dog is a subclass of Animal BOTH implement :eat(String food)

Dog d = new Dog();

((Animal) d).eat(“kibble”);

Page 32: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of AnimalAnimal[] aa = new Animal[2];

aa[0] = new Dog();

aa[1] = new Dog();

Dog[] da = (Dog []) aa;

Page 33: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of AnimalAnimal[] aa = new Animal[2];

aa[0] = new Dog();

aa[1] = new Dog();

Dog[] da = (Dog []) aa;

Page 34: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of AnimalAnimal[] aa = new Dog[2];Dog [] da = (Dog []) aa;

Page 35: CS 61B Midterm Review

Option A: Does not compile

Option B: Run time error

Option C: Works

Option D: Don’t know

Dog is a subclass of AnimalAnimal[] aa = new Dog[2];Dog [] da = (Dog []) aa;

Page 36: CS 61B Midterm Review

Try this at home!

Fill in the blanks below with all possible combinations (there are 16) of Animal and Dog, and determine whether the code will compile, or whether it will produce a compile-time or a run-time error:

______ a = new _____();

______ d = (_____) a;

Page 37: CS 61B Midterm Review

The moveToEnd method moves the first node of a doubly-linked list to the end of the list. Fill in the blanks:

public class DList {

protected DListNode head;

protected DListNode tail;

public void moveToEnd() {

// Moves the first node of the list to the end

if(head != null) {

tail.next = __________

head.prev = __________

tail = __________

head = __________

tail.next = null;

head.prev = null;

}

}

Page 38: CS 61B Midterm Review

The moveToEnd method moves the first node of a doubly-linked list to the end of the list. Fill in the blanks:

public class DList {

protected DListNode head;

protected DListNode tail;

public void moveToEnd() {

// Moves the first node of the list to the end

if(head != null) {

tail.next = head;

head.prev = __________

tail = __________

head = __________

tail.next = null;

head.prev = null;

}

}

Page 39: CS 61B Midterm Review

The moveToEnd method moves the first node of a doubly-linked list to the end of the list. Fill in the blanks:

public class DList {

protected DListNode head;

protected DListNode tail;

public void moveToEnd() {

// Moves the first node of the list to the end

if(head != null) {

tail.next = head;

head.prev = tail;

tail = __________

head = __________

tail.next = null;

head.prev = null;

}

}

Page 40: CS 61B Midterm Review

The moveToEnd method moves the first node of a doubly-linked list to the end of the list. Fill in the blanks:

public class DList {

protected DListNode head;

protected DListNode tail;

public void moveToEnd() {

// Moves the first node of the list to the end

if(head != null) {

tail.next = head;

head.prev = tail;

tail = head; // equivalently: tail = tail.next;

head = __________

tail.next = null;

head.prev = null;

}

}

Page 41: CS 61B Midterm Review

The moveToEnd method moves the first node of a doubly-linked list to the end of the list. Fill in the blanks:

public class DList {

protected DListNode head;

protected DListNode tail;

public void moveToEnd() {

// Moves the first node of the list to the end

if(head != null) {

tail.next = head;

head.prev = tail;

tail = head;

head = head.next;

tail.next = null;

head.prev = null;

}

}

Page 42: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Fuzzy f; Puppy p; f = new Fuzzy(); p = (Puppy) f; System.out.println(p.cuteness());}

Page 43: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Fuzzy f; Puppy p; f = new Fuzzy(); p = (Puppy) f; System.out.println(p.cuteness());}

Run-time Error:p = (Puppy) f;

Page 44: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Fuzzy f; Puppy p; p = new Puppy(3); f = p; p = (Puppy) f; System.out.println(f.cuteness());}

Page 45: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Fuzzy f; Puppy p; p = new Puppy(3); f = p; p = (Puppy) f; System.out.println(f.cuteness());}

Works =)// output:4

Page 46: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Cute c; Fuzzy f; Puppy p; p = new Puppy(3); f = (Fuzzy) p; c = f; System.out.println(c.cuteness());}

Page 47: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Cute c; Fuzzy f; Puppy p; p = new Puppy(3); f = (Fuzzy) p; c = f; System.out.println(c.cuteness());}

Compiler Error:c = f;

Page 48: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Cute c; Fuzzy f; f = new Puppy(3); c = (Cute) f; System.out.println(c.cuteness());}

Page 49: CS 61B Midterm Review

Consider the following class definitions:

For each of the main methods below, determine whether the method will cause a compiler error, a run-time error, or if it runs fine, determine what the method will print out

public static void main(String[] argv) { Cute c; Fuzzy f; f = new Puppy(3); c = (Cute) f; System.out.println(c.cuteness());}

Works =)// output:4