1 / 14

Midterm Solutions

Midterm Solutions. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013. Question 1. Work out the clues: Asked to identify expressions , not Statements Cannot use the – operator with Strings Casting (int), (double) are value expression operators

barb
Download Presentation

Midterm Solutions

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. MidtermSolutions Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

  2. Question 1 • Work out the clues: • Asked to identify expressions, not Statements • Cannot use the – operator with Strings • Casting (int), (double) are value expression operators • Conditional Expressions are valid expressions • The first one is a boolean expression with the logical OR operator √ x x √ 1 Mark if checked something that should be checked 1 Mark if left unchecked, something that should not be checked √

  3. Question 2 • Work out the logic one step at a time: • True when only one is true • So if A is true, B or C must be false => A && ( B==FALSE && C==FALSE) => A && (!B && !C) => A && !B && !C • Similarly, if B is the one that is true… => B && !A && !C • Similarly, if C is the one that is true => C && !A && !B • True if and only if one of these is true • Combine the three expressions using OR => A && !B && !C || B && !A && !C || C && !A && !B Check precedence! Need to add parenthesis? No! But will add them to make the logic clearer (A && !B && !C) || (!A && B && !C) || (!A && !B && C) 1 Mark 1 Mark 1 Mark 1 Mark 1 Mark Validate using a Truth Table

  4. Question 3 • 3 Marks – Polymorphism means “of many forms” • 2 Marks – The method invoked is determined using late binding based on the object being referenced, rather than the class of the reference itself.

  5. Question 4 – Multiple Choice • 2 Marks if got the one right answer • 1 Mark if got the right answer but also circled a wrong answer • 0 Marks if did not get the right answer, or circled more than 2

  6. Question 4 – Multiple Choice • 2 Marks if got the one right answer • 1 Mark if got the right answer but also circled a wrong answer • 0 Marks if did not get the right answer, or circled more than 2

  7. Question 4 – Multiple Choice

  8. Question 5 – Flow of Control 5 Marks • Java ignores the indenting, so should you! • Match up the if-then-else statements • No {} around second if as implied by indenting => first Else matches up with second if, not the first! • Third if-then-else is part of the 2ndif too • Since (a < b) is false, every if-then-else gets skipped => Print1, Print2 and Print3 do not get executed • Print4 is not part of ANY if statement ({} missing) => Print4 must get printed • Print4 is not doing addition, so not 111 • Print4 doing String Concatenation => will print “100”+”10” +”1”=> “100101” 2 x 5 √ x 0 x 4 x 3 x 0

  9. Question 6 - Create a String conCat Method • public static String conCat(String a, String b) { • int aLen = a.length(); • // Only need check for double-chars if both have 1 or more chars • if ((aLen >0) && (b.length()>0)) • // Check if last char in a equals first char in b • if (a.substring(aLen-1,aLen).equals(b.substring(0,1))) • // To eliminate the double-char, remove last char of a • a = a.substring(0,aLen-1); • return a+b; • }

  10. Question 7 – What is the output? • public class Test • { • public static void main (String[] args){ • int a = 0; • int[] b = {0, 1, 2}; • int[] c = {5, 6, 7}; • helper(a,b,c); • System.out.println(" a = " + a ); • System.out.println(" b[2] = " + b[2]); • System.out.println(" c[2] = " + c[2]); • } • private static void helper (int x, int[]y, int z[]) { • x = 100; • y[2] = 201; • z = new int[3]; • z[2] = 301; • } • } a = 0 b[2] = 201 c[2] = 7 5 Marks for each correct output line

  11. Question 8 – Working with 2-Dimensional Arrays

  12. Question 8 – Working with 2-Dimensional Arrays • // mat is an object reference, so caller will see any • // changes made here. Therefore we should return void. • public static void addToFirstCol (int intVal, int[][] mat) • { • for (int i=0; i<mat.length; i++) • if ((mat[i] != null) && (mat[i].length>0)) • mat[i][0] += intVal; • }

  13. Question 9 – What is the output? • public class ZooKeeper { • public static void main(String args[]) { • Animal bear = new Animal("Bear", "Grrrr!"); • Animal[] animals = {new Bee(), new Lion(), new MountainLion()}; • bear.speak(); • for(int i=0; i<animals.length; i++) • animals[i].speak(); }} • public class Animal { • protected String animalType; • private String sound; • public Animal(String animalType, String sound) { • this.animalType = animalType; • this.sound = sound; } • public void speak() { • System.out.println(animalType + "s say " + sound);}} • public class Bee extends Animal { • public Bee() { super("Bee", "Buzz Buzz"); } } • public class Lion extends Animal { • public Lion () { super("Lion", "Meow"); } • public void speak() { System.out.println("Big Cats ROAR!"); }} • public class MountainLion extends Lion { • public MountainLion() {super();} • public void speak() {System.out.println(animalType + "s purr");}} Bears say Grrrr! Bees say Buzz Buzz Big Cats ROAR! Lions purr 5 Marks for each correct output line

  14. Grades • This is the grade distribution for the class Midterm:

More Related