cs 61b midterm review

Post on 05-Feb-2016

69 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

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

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”

Option A: Error

Option B: i= 0

Option C: i= 5

Option D: i = 25

Option A: Error

Option B: i= 0

Option C: i= 5

Option D: i = 25

Option A: Error

Option B: s =

Option C: s = A

Option D: s = AA

Option A: Error

Option B: s =

Option C: s = A

Option D: s = AA

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Option A: Error

Option B: true

Option C: false

Option D: Buh?

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Option A: Error

Option B: 5

Option C: 7

Option D: 10

Option A: Error

Option B: True

Option C: False

Option D: ???

Option A: Error

Option B: True

Option C: False

Option D: ???

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;

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;

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;

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;

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;

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;

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

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

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

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

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;

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;

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;

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;

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;

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;

}

}

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;

}

}

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;

}

}

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;

}

}

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;

}

}

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

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;

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

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

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

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;

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

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

top related