490 likes | 650 Views
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.
E N D
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”
What is the printed value of i after the following code is executed ? • public static void main (String [] a) {inti = 5; f (i);System.out.println("i=" + i);} • static void f (inti) {i = i * i; } Option A: Error Option B: i= 0 Option C: i= 5 Option D: i = 25
What is the printed value of i after the following code is executed ? • public static void main (String [] a) {inti = 5; f (i);System.out.println("i=" + i);} • static void f (inti) {i = i * i; } Option A: Error Option B: i= 0 Option C: i= 5 Option D: i = 25
What is the value of s after the following code is executed ?public static void main (String [] a) { String s = "A"; double (s);System.out.println("s = " + s);} • void static double (String s) { s = s + s;} Option A: Error Option B: s = Option C: s = A Option D: s = AA
What is the value of s after the following code is executed ?public static void main (String [] a) { String s = "A"; double (s);System.out.println("s = " + s);} • void static double (String s) • { s = s + s;} Option A: Error Option B: s = Option C: s = A Option D: s = AA
Predict the output • String s0 = null;String s1 = "A";String s2 = new String ("A");String s3 = s1; • System.out.println(s1 == s1); Option A: Error Option B: true Option C: false Option D: Buh?
Predict the output • String s0 = null;String s1 = "A";String s2 = new String ("A");String s3 = s1; • System.out.println(s1 == s1); Option A: Error Option B: true Option C: false Option D: Buh?
Predict the output • String s0 = null;String s1 = "A";String s2 = new String ("A");String s3 = s1; • System.out.println(s1 == s2); Option A: Error Option B: true Option C: false Option D: Buh?
Predict the output • String s0 = null;String s1 = "A";String s2 = new String ("A");String s3 = s1; • System.out.println(s1 == s2); Option A: Error Option B: true Option C: false Option D: Buh?
Predict the output • String s0 = null;String s1 = "A";String s2 = new String ("A");String s3 = s1; • System.out.println(s1 == s3); Option A: Error Option B: true Option C: false Option D: Buh?
Predict the output • String s0 = null;String s1 = "A";String s2 = new String ("A");String s3 = s1; • System.out.println(s1 == s3); Option A: Error Option B: true Option C: false Option D: Buh?
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(j); Option A: Error Option B: 5 Option C: 7 Option D: 10
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(j); Option A: Error Option B: 5 Option C: 7 Option D: 10
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(i); Option A: Error Option B: 5 Option C: 7 Option D: 10
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(i); Option A: Error Option B: 5 Option C: 7 Option D: 10
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(k); Option A: Error Option B: 5 Option C: 7 Option D: 10
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(k); Option A: Error Option B: 5 Option C: 7 Option D: 10
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(i.equals(k)); Option A: Error Option B: True Option C: False Option D: ???
Predict the output • inti = 5;int j = i;int k = i;i = 7;j = 10; • System.out.println(i.equals(k)); Option A: Error Option B: True Option C: False Option D: ???
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: Works Option D: Don’t know
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: Calls Dog’s eat method Option D: Calls Animal’s eat method
Dog is a subclass of Animal • Animal[] 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 Animal • Animal[] 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 Animal • Animal[] 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 Animal • Animal[] 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
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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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: interface Cute { public int cuteness(); } class Fuzzy { public int fuzziness; public int cuteness() { return 0; } } class Puppy extends Fuzzy implements Cute{ public Puppy(inti) { fuzziness = i; } public int cuteness() { return fuzziness + 1; } } 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