



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive set of exam questions covering fundamental concepts in object-oriented programming, including inheritance, generics, recursion, exception handling, data structures, and linked lists. The questions are designed to assess students' understanding of these concepts and their ability to apply them in practical scenarios. A valuable resource for students preparing for exams or seeking to deepen their understanding of these core programming principles.
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




CS180 Spring 2011 Exam 2, 6 April, 2011 Prof. Chris Clifton
Turn Off Your Cell Phone. Use of any electronic device during the test is prohibited. Time will be tight. If you spend more than the recommended time on any question, go on to the next one. If you can’t answer it in the recommended time, you are either going in to too much detail or the question is material you don’t know well. You can skip one or two parts and still demonstrate what I believe to be an A-level understanding of the material. For multiple choice questions, please circle the letter next to your choice. I would hope to see an A student get at least 28 on the exam, a B student at least 21, and a C student at least 14.
Given the following classes:
class Person { public Person() { System.out.println("Person constructor"); }
public void showClassName() { System.out.println("Person"); }
public void printSomething() { System.out.println("In printSomething"); } }
class Employee extends Person { public Employee() { this("Calling this"); System.out.println("Employee constructor"); }
public Employee(String s) { System.out.println(s); }
public void showClassName() { System.out.println("Employee"); } }
For each of the following statements, show the output. Assume the statements are executed in the order given (note that you should be able to figure out the later ones even if you aren’t sure about the earlier ones.) Scoring: 1 point each
Person p1 = new Person();
Person constructor
p1.showClassName();
Person
Employee e1 = new Employee();
Person constructor Calling this Employee constructor
e1.showClassName();
Employee
e1.printSomething();
In printSomething
p1 = e1;
Nothing (one point for stating “nothing” or for leaving it blank.)
p1.showClassName();
Employee
Person e2 = new Employee("e2");
Person constructor e
e2.showClassName();
Employee
Given a Set class (partially shown below):
public class Set
Using the Person and Employee classes from question 1, we do the following:
Set
Is the last statement ( ps.insert(e1); ) legal? (that is, will it compile and run?) Briefly explain your answer. You may assume that Set is implemented correctly and that the first two statements compile and run. Yes, it will. since e1 is an Employee, and Employee extends Person, e1 can be used anyplace a Person object is needed. Since ps is a set of Person, e1 can be inserted into the set. Scoring: 1 point for “yes”, 1 for showing understanding of inheritance.
Scoring: -1 if the method signature does not contain “throws”. -2 if the “conditional + throw” is incorrect. If previous section was incorrect, combined partial credit was given based on overall validity of the answer.
Suppose you are writing a program which implements a Gradebook class whose primary role is to hold a collection of Student objects.
An instructor using this class should have the ability to add students (when they sign up for the class) and remove students (if they drop the course) as necessary. Should the gradebook hold the collection of students using an array-based implementation or a linked list implementation? Justify your choice. Either is acceptable. If you assume all students are added to the book at once and there is minimal students dropping the course, then an array-based implementation (e.g., java.util.Vector) may be preferred for easy indexing and fast iteration. If you assume the size of the gradebook is very fluid, then a linked list implementation may be preferred for fast addition/subtraction from the gradebook. It is not correct to say that the linked list implementation is resizable and the array is not (it is only easier to be resized). Scoring: 1 point for making a choice or saying either, 1 point for a justification that correctly supports the answer given.
All students must be either an UndergradStudent or GradStudent; we would never actually create an object that was just a Student. However, we do need a Student class, since this is what the gradebook holds; UndergradStudent and GradStudent will be subclasses of Student. Note that while undergrads and grad students have much in common, there are some differences. For example, a GradStudent should have a “thesisTitle” field; and UnderGrad should have a “classYear”. How- ever, both have common attributes and methods such as “transcript”, “addCourseGrade()”, and “com- puteGPA()”. Should the Student class be (choose one):
A an Interface,
B an Abstract class, or
C a regular class.
Briefly justify your answer Student should be an abstract superclass. There’s no point in a Student object being made, so it should be abstract. Student should be a class instead of an interface in order to declare similar fields and accessors/mutators which can be inherited. Scoring: 0 points for interface, 1 point for regular class and noting that this allows you to share common methods, 3 points for saying abstract class and noting that this allows you share common methods (we assumed by selecting abstract class that you understand an instantiation won’t be possible.
class OrderedList { private int value; private OrderedList next; // Invariant: value < next’s value. That is, !next.lessThan(value);
public OrderedList(int data, OrderedList next) {
value = data; this.next = next; }
private boolean lessThan (int val) { // True if val < my value // Note that another object of the same class may use an object’s private method. if (val < value) return true; else return false; }
public void insert(int val) { // Put val at it’s proper place in the list /* Line A (for parts 3 and 4) / if ( next.lessThan(val) ) { / Line B (for parts 3 and 4) / OrderedList temp = new OrderedList(val, next); next = temp; } else { / Line C (for parts 3 and 4) / next.insert(val); / Line D (for parts 3 and 4) / } / Line E (for parts 3 and 4) */ } }
Assume OrderedList ol; is the following list:
Show what the above list will look like after running the statement: ol.insert(4); Solution:
Scoring: Minus one point for each of not having a new node, not having 4, not correctly pointing to the next node, not correctly pointing the previous to the next, not having ol pointing to the right place, and not null-terminating the list. But at least one point for having one of these things right.
If you do an ol.insert(78); the code will fail to do what it should. This is because:
A There is no base case for val > value.