1 / 49

CS 61B Midterm Review

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.

bozica
Download Presentation

CS 61B Midterm Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 61B Midterm Review HKN, Fall 2010

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

  3. 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”

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

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

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

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

  8. 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?

  9. 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?

  10. 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?

  11. 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?

  12. 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?

  13. 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?

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

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

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

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

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

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

  20. 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: ???

  21. 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: ???

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  36. 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;

  37. 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; } }

  38. 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; } }

  39. 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; } }

  40. 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; } }

  41. 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; } }

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

  43. 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;

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

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

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

  47. 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;

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

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

More Related