CS 201 Advanced Object-Oriented Programming Midterm Exam March 14, 2013
Question
Topic
Points
1
Terminology
15
2
Method signatures
15
3
Bug
5
4
Testing
15
5
Subtyping
10
6
True/false
20
Total
Score
80
There are 6 questions on this examination. The point values associated with the questions are shown in the table above. You have 75 minutes to complete the examination. The test is closed book and closed notes. Show all answers on the pages provided. For the questions that ask you to write pieces of code, it is NOT necessary to write comments or to declare constants. Please sign the following honor code statement. I have neither given nor received help on this exam.
High grade - 77 Median - 62 Questions most people nailed: Terminology, Method signatures Most challenging questions: Bug, Testing
1 of 9
1. (15 points) Answer the questions below about this class. public class Skier { ! private static final int MAXIMUM_POINTS = 999; ! private String name; ! private int slalomPoints; ! private int giantSlalomPoints; ! private int superGPoints; ! private String ussaNumber; ! ! public Skier (String name, String ussaNumber) { ! ! this.name = name; ! ! this.ussaNumber = ussaNumber; ! } ! ! public String getBestEvent() { ! ! if (slalomPoints < giantSlalomPoints) { ! ! ! if (slalomPoints < superGPoints) { ! ! ! ! return “Slalom”; ! ! ! } ! ! ! return “Super G”; ! ! } ! ! ! ! if (giantSlalomPoints < superGPoints) { ! ! ! return “Giant Slalom”; ! ! } ! ! }
! }
return “Super G”;! !
!
a. What are the names of the instance variables declared in this class?
name, slalomPoints, giantSlalomPoints, superGPoints, ussaNumber b. What are the signatures of the constructors declared in this class?
public Skier (String name, String ussaNumber) c. What are the names of the parameters declared in this class?
name, ussaNumber d. What are the signatures of the methods declared in this class?
public String getBestEvent() e. What are the names of the constants declared in this class?
MAXIMUM_POINTS
2 of 9
2. (15 points) In this question, I show you the signatures of some constructors and methods that could be defined for a Restaurant class, a class intended to store information about a restaurant. I would like you to show me how you would call the methods. If the method or constructor returns a value, I would like you to put the call on the right hand side of an assignment statement so that the returned result is saved in a variable. For the purposes of this question, you can assume that the following variables have been declared and initialized, if necessary: Restaurant restaurant; String restName; String restAddress; String menu;
Person restOwner; String ownerName; String[] dessertList;
Do not provide an implementation for these methods. a. public Restaurant (String name)
restaurant = new Restaurant (restName); b. public String getAddress()
restAddress = restaurant.getAddress(); c. public void printMenu()
restaurant.printMenu(); d. public void setOwner (Person owner)
restaurant.setOwner(restOwner); e. public String[] getDessertList ()
dessertList = restaurant.getDessertList();
3 of 9
3. (5 points) Please find and fix the bug described below. 1. import java.io.File; 2. import java.io.FileNotFoundException; 3. import java.util.Scanner; 4. 5. 6. public class BuggyFileReader { 7. !
private String filename;
8. ! 9. !
public BuggyFileReader(String filename) {
10.!
!
11.!
}
this.filename = filename;
12. 13.!
private void readFile() throws FileNotFoundException {
14.!
!
Scanner scanner = new Scanner(new File(filename));
15.!
!
while (scanner.hasNext()) {
! 16.!
String nextAnimal = scanner.next(); !
!
!
if (scanner.next().equals("Dog")) {
!
nextAnimal
17.!
!
!
!
18.!
!
!
}
19.!
!
!
else if (scanner.next().equals("Cat")) {
!
!
System.out.println("Woof");
!
nextAnimal
20.!
!
!
!
21.!
!
!
}
22.!
!
!
else if (scanner.next().equals("Bird")) {
!
!
System.out.println("Meow");
!
23.!
!
!
!
24.!
!
!
}
25.!
!
}
26.!
}
nextAnimal
System.out.println("Tweet");
4 of 9
27.! 28.!
public static void main(String[] args) {
29.!
!
BuggyFileReader reader = new BuggyFileReader("animals.txt");
30.!
!
try {
31.!
!
!
32.!
!
} catch (FileNotFoundException e) {
33.!
!
!
34.!
!
}
35.! 36. 37.}
}
reader.readFile();
System.out.println("Can't find the file animals.txt!");
The user has a file named “animals.txt” that contains: Dog Cat Bird When she runs the following program, she expects to see the output: Woof Meow Tweet The actual output that she gets is: Woof Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1347) at BuggyFileReader.readFile(BuggyFileReader.java:22) at BuggyFileReader.main(BuggyFileReader.java:31)
5 of 9
4. (15 points) Develop white box test cases that provide statement, branch and loop coverage for the following methods. You do not need to write JUnit code. Instead, fill in the table provided to show the input to the test case, the expected result and the reason you included this test case. a. Test cases for isPrime // Precondition: num must be > 1. public boolean isPrime (int num) { // Math.sqrt is the square root function ! for (int i = 2; i = 0. public void nprimes (int num) { ! int numFound = 0; ! int next = 2; ! ! ! ! ! ! ! }
while (numFound < num) { if (isPrime(next)) { ! numFound++; ! System.out.print(next + " "); } next++; }
num
expected output
0
Reason for the test case
0 iterations
1
2
1 iteration of loop, if-condition true
5
2,3,5,7,11
multiple iterations of loop, if condition false
7 of 9
4. (10 points) For each of the following code snippets, please indicate if the code shown would a. fail to compile, b. compile, but result in an error at runtime, or c. execute without an error. In this case, indicate what output you would see. public abstract class Plant { ! public void smell() { ! ! System.out.println(“No smell”); ! } } public class Flower extends Plant { public void smell() { ! ! System.out.println(“I smell good!”); ! } } public class Fruit extends Plant { public void smell() { ! ! System.out.println(“I smell yummy!”); ! } }
a. Flower rose = new Flower(); rose.smell();
I smell good! b. Fruit orange = new Fruit(); orange.smell();
I smell yummy! c. Plant oakTree = new Plant(); oakTree.smell();
Does not compile d. Plant banana = new Fruit(); banana.smell();
I smell yummy! e. Plant violet = new Flower(); Flower myViolet = violet; myViolet.smell();
Does not compile
8 of 9
5. (2 points each) Please answer the following true/false questions. If your answer is false, give a brief explanation of why it is false. a. Class and object mean the same thing.
False, an object is created by calling the constructor of a class. b. A method signature includes information about the number and types of parameters that a method has.
True c. An interface can be used as the type of a variable.
True d. To be notified when a user clicks on a button, the program must add a listener to the button.
True e. A subclass can add behavior that is not present in the superclass.
True f. When stopped at a breakpoint in the debugger, it is possible to see the values of the variables.
True g. Testing allows us to prove that our programs are correct.
False, it only tells us how the program behaves on the test cases. h. If FileNotFoundException occurs when a program is executed, it means that the program contains a bug.
False, it means that a file the program expects to find is missing. It is probably not a programming error, but something missing in the environment. i. Instance variables should always be declared to be public.
False, they should always be declared to be private. j. Variables should be declared to be local, rather than instance variables, whenever possible. (If the program would not work if they were local variables, consider that to be “not possible”.)
True
9 of 9