140 likes | 385 Views
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
E N D
MidtermSolutions 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 • 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 √
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
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.
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
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
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
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; • }
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
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; • }
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
Grades • This is the grade distribution for the class Midterm: