Date: 18-Feb-2002 Time: 7:00 pm - 9:00 pm Permitted Aids: None CS 132 Midterm Exam Winter 2002 Printed Last Name: Initials: ID: Signature: Please check off your PRActicum section from the list below: Byron Weber Becker Steven Furino Arnie Dyck Wed @ 10:30 Thurs @ 10:30 unsure Wed @ 12:30 Wed @ 2:30 unsure Wed @ 8:30 Thurs @ 8:30 unsure Instructions: (Read carefully before the exam begins): 1. Before the exam begins, make certain that you have 11 pages in this booklet and 1 yellow paper. 2. Before the exam begins, fill in information about yourself on this page. 3. The yellow paper contains a case study which forms the basis for almost every question in the exam. When the exam begins, turn the yellow page over and read it carefully. 4. Answer all questions in this booklet. If additional space is required, use page 10. Label work clearly, both on page 10 and where the question is asked. 5. Additional paper is available upon request. 6. The marks assigned to each question are shown at the beginning of the question. Use this information to organize your time effectively. There are a total of 100 marks available. 7. Some multiple choice questions ask you to circle all of the correct answers. These questions have a penalty for wrong answers. Other multiple choice questions ask you to circle one answer. These questions do not have a penalty for wrong answers. 8. Questions will not be interpreted. Proctors will confirm or deny errors only. If you consider the wording of a question to be ambiguous, state your assumptions clearly and proceed to answer the question to the best of your ability. Your assumptions may not trivialize the question. 9. Documentation is always appreciated, but is not required unless explicitly requested. 10. Cheating is an academic offense. Your signature on this exam indicates that you understand and agree to the University s policies regarding cheating on exams. Marks: 1-4. / 14 5. / 10 6-7. / 13 8. / 32 9. / 20 10. / 11 / 100 Page 1 of 11
1. [03] Suppose a method in the TextUI class has a student ID stored in the local variable idnum. Circle all of the following that could be used to print the average for the specified student. a. Student s = this.model.find(idnum); System.out.println("Avg = " + s.calcavg()); b. System.out.println("Avg = " + this.calcavg()); c. Student s = (Student)this.students.elementAt(idNum); System.out.println(("Avg = " + s.calcavg()); d. double avg = this.model.find(idnum).calcavg(); System.out.println("Avg = " + avg); e. None of a, b, c, or d. 2. [05] A student is in trouble if any of the following are true: more than one assignment has not been submitted more than one assignment has been excused the average is less than 60% Assume that the following Boolean expressions are in the Student class. Circle all of the expressions that return true if a student is in trouble and false otherwise. a. this.numdns() > 1 && this.numexc() > 1 && this.calcavg() < 0.60 b. this.numdns() > 1 this.numexc() > 1 this.calcavg() < 0.60 c.!(this.numdns() <= 1 && this.numexc() <= 1 && this.calcavg() >= 0.60) d.!(this.numdns() <= 1 this.numexc() <= 1 this.calcavg() >= 0.60) e. (this.numdns() + this.numexc()) > 1 this.calcavg() < 0.60 3. [03] Given the declaration Student[] students = new Student[10]; which one of the following is the most likely explanation of an ArrayOutOfBoundsError? a. The program attempted to access students[10]. b. The program did not allocate memory for the students array. c. The program did not fill the students array with data. d. The program did not declare the students array. e. None of the above are possible explanations for an ArrayOutOfBoundsError. 4. [03] Given the declaration Student[] students = new Student[10]; which one of the following is the most likely explanation of a NullPointerException? a. The program attempted to access students[10]. b. The program did not allocate memory for the students array. c. The program did not fill the students array with data. d. The program did not declare the students array. e. None of the above are possible explanations for a NullPointerException. Page 2 of 11
5. [10] Implement the calcavg method in the Student class. Assume that every element of the array, marks, has a value and that at least one is not EXC. Recall that a mark of DNS (did not submit) is recorded in the array as 3001 and an excused mark, EXC, is recorded as 3002. DNS contributes 0 to the average; EXC is not included in the average at all. public class Student extends Object { private int id; private String givenname; private String surname; private int[] marks; private final static int DNS = 3001; private final static int EXC = 3002; public Student(int nummarks,...) {... public double calcavg() Examples: Marks Avg 70 70 90 90 80 3002 3002 80 90 85 80 3001 80 80 60 Page 3 of 11
6. [05] Professor Furino wants to list of all the students in his section (104) that have at least one DNS or EXC mark. Given a Student object, s, the test to select these students could be written as s.getsection() == 104 && (s.numdns() > 0 s.numexc() > 0) Use DeMorgan s Laws to find all the students who are not in this list. Show your work. 7. [08] A method signature is the first line of the method. It includes the visibility modifier (e.g.: public), the return type, the name of the method and the parameters. Write a signature for a method to solve each of the following tasks: a. [02] In ClassList, a method returning a single string containing all of the names of students in one specified section. b. [02] In ClassList, a method returning the ID numbers of students in one specified section. c. [02] In ClassList, a method returning the student objects for students in one or more specified sections. d. [02] In ClassList, a method returning the student objects for students in one or more specified sections that have a mark higher than a specified minimum. Page 4 of 11
8. [32] Using the design presented in the UML, implement the ClassList and Student constructors to read a data file using the format shown in the program specification. You must read the file only once, due to the inefficiency of file operations. Put the code for ClassList on this page and the code for Student on the next page. package markprogram; public class ClassList extends Object { private String coursenum; private String coursename; private Student[] students; private int numstudents; public ClassList(String filename) { // ClassList Page 5 of 11
8. (Continued) package markprogram; public class Student extends Object { private int id; private String lastname; private String givenname; private int sectionnum; private int[] marks; private final static int DNS = 3001; private final static int EXC = 3002; public Student(int nummarks, ) //Student Page 6 of 11
9. [20] Listed below are five attempts to implement the find method in the ClassList class. The find method is designed to return the Student object corresponding to the given ID or null if it isn t found. Each attempt contains an error. Find the error and fix it. In all cases your changes must be the smallest change that will fix the problem. In one case, that is 3 lines added or changed; in the remaining cases only 1 line must be added or changed. public Student find(int anid) public Student find4(int anid) { Student s = null; int i = 0; while (s == null) { if(this.students[i].getid() == anid) { s = this.students[i]; i ++; return s; public Student find2(int anid) { int i = 0; while (i >= this.numstudents this.students[i].getid() == anid) { i++; if (i > this.numstudents) { return null; else { return this.students[i]; public Student find(int anid) { boolean found = false; int i = 0; while (i < this.numstudents &&!found) { int i = 0; while (true) { if(i == this.numstudents ) { return null; if(this.students[i].getid()!= anid) { return this.students[i]; i++; public Student find5(int anid) { boolean found = false; for(int i=0; i<this.numstudents; i++) { if (this.students[i].getid() == anid) { found = (this.students[i].getid() == anid); { found = true; if (found) if (found) { return this.students[i]; { return this.students[i]; else else Page 7 of 11
{ return null; { return null; Page 8 of 11
10. [11] The users of the marks program would like to have more detail for each mark. The program will be changed to store each mark in its own object. Design this new class. It must have services to return the mark in a string so it can be easily displayed; for example, 70, DNS, or EXC. return the numeric equivalent of this mark. return a comment for the mark. determine if the mark should be included in average calculations. determine if the mark is a DNS. determine if the mark is an EXC. add a comment to an existing mark. replace a mark with a new value, keeping track of the fact that it was modified. Provide an appropriate constructor. Do the following: Complete your design of the Mark class in the UML class diagram on the right. Make the one change to the Student class that is required to use the Mark class. Show the relationship between the classes. Student -int id -String lastname -String givenname -int sectionnum -int[] marks -int nummarks Mark +Student(...) +double calcavg() +int getid() +String getname() +int getsection() +int numdns() +int numexc() +String tostring() Page 9 of 11
Page 10 of 11
Page 11 of 11