Multiple Choice
(1) 1. Source code is contained within: A. .class files,
B. .java files,
C. all .jar files, D. manifest files.
(1) 2. Variables whose values cannot change are declared using what keyword? A. static,
B. constant,
C. final,
D. none of the above.
(1) 3. What is the output of the following code?
if (new Person(Eric) == new Person(Eric))
System.out.println(Equal);
else
System.out.println(Not Equal);
A. Equal,
B. Not Equal.
(1) 4. Which of these classes are likely examples of entity classes? A. Book,
B. JFrame,
C. BookViewController, D. AddBookCommand, E. all of the above.
(1) 5. What is the result of the following, assuming the Person class compiles:
public static Person getPerson(){ Person p = null;
return p;
}
public static void main(String[] args){ getPerson().toString();
}
A. null,
B. compiler error,
C. runtime error,
D. cant tell,
E. none of the above.
(1) 6. What is the result of the following, assuming the Person class compiles:
public static Person getPerson(){ return this;
}
public static void main(String[] args){ getPerson().toString();
}
A. null,
B. compiler error,
C. runtime error,
D. cant tell,
E. none of the above.
(1) 7. When should super() be called?
A. Within a constructor of a subclass,
B. The first line of a method,
C. To call on constructor of a superclass, D. all of the above,
E. none of the above.
public class Foo{ Integer x;
private Integer y;
public Foo(){ x = 1;
y = 2;
}
public Foo(int x1, int y1)
x = x1;
y = y1;
public void doSomethingFoo(){ x = 5;
}
public void doSomethingElse(){ doSomethingFoo();
} }
public class Bar extends Foo{ private Integer w;
public Bar(){ w = 4;
}
public void doSomethingFoo(){ x = 6;
}
public void doSomethingBar(){
} }
(1) 8. What occurs during the second line when running the following from a main method:
Foo myVar = new Foo();
myVar.doSomethingFoo();
A. the x instance variable myVar object changes to 5, B. the data of the object does not change,
C. compiler error,
D. runtime error,
E. it depends.
(1) 9. What occurs when running the following from a main method:
Bar myVar = new Bar();
A. wissetto4,xto0,yto0,
B. wissetto4,xto1,yto2,
C. wissetto4,xtonull,ytonull, D. none of the above.
(1) 10. What happens when the following code is executed in a main method?
Foo myVar = new Foo();
myVar.doSomethingBar();
A. it works (if doSomethingBar() works), B. compiler error,
C. runtime error,
D. it depends.
(1) 11. What happens when the second line of the following code is executed in a main method?
Bar myVar = new Bar();
myVar.doSomethingFoo();
A. xissetto5, B. xissetto6, C. compiler error, D. runtime error, E. it depends.
(1) 12.
If the contents of doSomethingBar() is the following segment: System.out.println(y);
A. it works,
B. compiler error,
C. runtime error,
D. none of the above.
If the contents of doSomethingBar() is the following segment: super.doSomethingFoo();
and it gets executed, then: A. xissetto5,
B. xissetto6,
C. compiler error, D. runtime error,
E. it depends.
What happens when the following code is executed?
Foo myVar = new Bar();
A. A Foo object is created, B. A Bar object is created, C. compiler error,
D. none of the above.
What happens when the second line (if reached) of the following code is executed?
Foo myVar = new Bar();
myVar.doSomethingFoo();
A. xissetto5,
B. xissetto6,
C. compiler error,
D. none of the above.
What happens when the following method is executed?
Foo myVar = new Bar();
myVar.doSomethingElse();
A. xissetto5,
B. xissetto6,
C. compiler error,
D. none of the above.
(1) 13.
(1) 14.
(1) 15.
(1) 16.
(1) 17. Abstract classes are useful if:
A. No objects of this exact type should be created,
B. Classes with additional properties can be created, C. Multiple classes share its properties,
D. References to this type can be used,
E. all of the above.
(1) 18. The following code would be good to
Iterator
String name = iter.next();
}
A. iterate through a map m one element at a time,
B. iterate through a String m one letter at a time,
C. iterate through a set m one element at a time,
D. iterate through a map m in the order elements were added, E. all of the above.
(1) 19. What does the following do?
Map
mapToStrings.put(10,ten);
mapToStrings.put(50,fifty);
mapToStrings.put(80, eighty);
for (Integer key : mapToStrings.keySet()){ System.out.println(mapToStrings.get(key));
}
A. prints out each key-value pair, B. prints out each key,
C. prints out each value,
D. none of the above.
(1) 20. Given the following code, what should X be replaced with?
/* grades is an array of non-negative integers between 0 and 100,
then do something
*/
public static boolean doSomething(int[] grades){
for (int i = 0; i< grades.length; i++){ X}… return false;}A. try{if (grades[i]<0 || grades[i]>100)}
catch(RuntimeException e){ return false; }
B. if (grades[i]<0 || grades[i]>100) throw new RuntimeException();
C. if (grades[i]<0 || grades[i]>100) return false;
D. try{if (grades[i]<0 || grades[i]>100)} catch(RuntimeException e){ throw new RuntimeException(); }
(1) 21. To check whether s contains an integer and print out not integer otherwise, in the following code,
public static void main(String[] args){ Scanner in = new Scanner(System.in); String s = in.next();
X
then X should be replaced by:
A. if (!Integer.parseInt(s)) System.out.print(not integer);
B. Integer.parseInt(s); catch(Exception e){
System.out.print(not integer);
}
C. try{
}
catch(Exception e){
System.out.print(not integer);
}
D. none of the above.
}
Integer.parseInt(s);
(1) 22.
(1) 23.
(1) 24.
(1) 25.
(1) 26.
(1) 27.
When is it a good idea to catch an exception?
A. every method should catch exceptions if calling a method that throws them, B. every method should at least catch then throw the same exception
C. only if it to possible to do something appropriate based on type of exception, D. only the main function should catch exceptions.
The following are characteristics of good decomposition: A. high dependency between classes,
B. Intermingling controller and UI,
C. weak coupling,
D. weak cohesion.
Command Pattern involves:
A. a different class for each command,
B. the controller containing a different method for each command, C. separation of UI from controller,
D. keeping a list of commands to execute,
E. all of the above.
To create a custom window application using Swing, it is best to: A. create a listener,
B. create a Graphics2D object,
C. extend the ActionListener class, D. extend the JFrame class.
In a GUI, the controller is largely implemented within: A. listeners,
B. commands,
C. the model,
D. through notifications.
What does the following code do?
JFrame frame = new JFrame();
frame.setSize(300,400);
frame.setTitle(Title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
A. creates a new JFrame object, make it visible, set its size, and make the close button func- tional, and set its title,
B. not A,
C. all of the above,
D. none of the above.
(1) 28. Consider the following code:
(1) 29.
JPanel buttonPanel = new JPanel();
buttonPanel.add(button1); //button1, button2, and button3 are JButtons
buttonPanel.add(button2);
buttonPanel.add(button3);
A. adds the buttons to the JPanel from left to right,
B. adds the buttons to the JPanel according to the GridLayout,
C. adds the buttons to the JPanel from the top towards the bottom, D. the buttons are not yet placed in the JPanel,
E. none of the above.
What is likely occuring as part of the following model-view-controller code?
MyModel model = new MyModel();
MyView view = new MyView(model);
MyController controller = new MyController(model, view);
A. listener in view detects changes, propagates to controller and model, B. view displays information from model whenever it detects changes,
C. model detects changes, notifies view,
D. controller detects changes to view, controller updates model then view.
What is likely occuring as part of the following model-view-controller code?
MyModel model = new MyModel();
MyView view = new MyView(model);
model.addObserver(view);
MyController controller = new MyController(model, view);
A. when model changes, it tells view, view asks model for changed data, B. controller detects changes to view, controller updates model, then view, C. view detects changes, then tells model,
D. listener in view detects changes, propagates to controller and model,
What is likely occuring as part of the model-view-controller code from the previous question? A. model is extending a class,
B. view is implementing an interface,
C. model calls notifyObservers() from Observable class,
D. all of the above, E. none of the above.
(1) 30.
(1) 31.
(1) 32. What is likely occuring as part of the following model-view-controller code?
MyModel model = new MyModel();
MyView view = new MyView();
model.addObserver(view);
MyController controller = new MyController(model, view);
A. controller detects changes to view, controller updates model, then view, B. when model changes, it tells view what has changed,
C. view detects changes, then tells model,
D. listener in view detects changes, propagates to controller and model,
E. controller detects changes to model.
(1) 33. If a model class in a model-view-controller application contains the following code, what is occurring?
setChanged();
notifyObservers();
A. any observers are pushed data that changed,
B. any class extended the Observable class is notified,
C. the observer gets notified,
D. all observers have update(Observable o, Object obj) run, E. none of the above.
(1) 34. If a model class in a model-view-controller application contains the following code, what is occurring?
setChanged();
notifyObservers(book); //book is an object of type Book
A. any observers receive a variable of type Object,
B. any class extending the Observable class is notified, C. the observer gets notified,
D. all observers have update(Observable o) run, E. none of the above.
(1) 35. What is likely occuring as part of the following model-view-controller code?
MyModel model = new MyModel();
MyView view = new MyView();
model.addObserver(view);
MyController controller = new MyController(model, view);
A. model calls on notifyObservers(Object o),
B. Observable object calls on update(Observable o, Object obj) of view, C. view casts Object variable,
D. all of the above,
E. none of the above.
(1) 36. What is likely occurring as part of the following model-view-controller code?
MyModel model = new MyModel();
MyFirstView view1 = new MyFirstView();
model.addObserver(view1);
MyFirstController ctrl1 = new MyFirstController(model, view1);
MySecondView view2 = new MySecondView();
model.addObserver(view2);
MySecondController ctrl2 = new MySecondController(model, view2);
A. the two views are independent of each other,
B. only the first view will receive updates,
C. two different views update when model changes, D. none of the above.
(1) 37. What is the return type of the method public static
A. E,
B.
C. String,
D. depends on E,
E. does not compile.
(1) 38.
What is likely occurring as part of the following model-view-controller code, where the model con- tains an ArrayList of Book objects?
A. only the reference to the model object is read from a file, and not the Books, B. the reference to an ArrayList itself is read from a file, but not the Books,
C. the model, ArrayList, and all Books within are read from a file,
D. String placeholders are read in,
E. none of the above.
What is the likely format of MyModel.out? A. binary,
B. text,
C. Unicode 16,
D. sequential access, E. none of the above.
What does the MyModel class need to do for this to compile?
A. contain a method called readObject,
B. contain a method called writeObject,
C. implement the Serializable interface without any new methods, D. all of the above are needed,
E. none of the above are needed.
What happens when calling on the following method (with header): public static
Person[] arrayOfPersons = new Person[2];
arrayOfPersons[0] = new Person(Eleanor);
arrayOfPersons[1] = new Person(Eric);
doSomething(arrayOfPersons);
A. it can compile,
B. it cannot compile.
(1) 39.
(1) 40.
(1) 41.
The follow code applies to the next 3 questions.
MyModel model; try{
FileInputStream fileIn = new FileInputStream(MyModel.out);
ObjectInputStream in = new ObjectInputStream(fileIn);
model = (MyModel) in.readObject();
in.close();
fileIn.close(); }catch(Exception e){
System.out.println(didnt work);
}
(1) 42.
if (month>0 && month <13){ System.out.println(“A valid month”);}else{System.out.println(“Not a valid month”);}if (day>0 && day<32){if (month == 2 && day >28)
System.out.println(No good);
System.out.println(A valid day);
}else{
System.out.println(Not a valid day);
}
What smallest set of test cases will achieve statement coverage?
A. (month is 2, day is 31), (month is 14, day is 1), (month is 3, day is 32), B. (month is 2, day is 31), (month is 3, day is 3), (month is 14, day is 0), C. (month is 2, day is 31), (month is 14, day is 0),
D. there is no smallest,
E. none of the above.
What smallest set of test cases will achieve branch coverage?
A. (month is 2, day is 31), (month is 14, day is 1), (month is 3, day is 32), B. (month is 2, day is 31), (month is 3, day is 3), (month is 14, day is 0), C. (month is 2, day is 31), (month is 14, day is 0),
D. there is no smallest,
E. none of the above.
What set test cases will achieve path coverage?
A. (month is 2, day is 31), (month is 14, day is 1), (month is 3, day is 32), B. (month is 2, day is 31), (month is 3, day is 3), (month is 14, day is 0), C. (month is 2, day is 31), (month is 14, day is 0),
D. there is no smallest,
E. none of the above.
(1) 43.
(1) 44.
For the following 3 questions, refer to the following code:
(1) 45.
When running this JUnit class,
A. doBeforeClass is executed once before all tests,
B. doBeforeClass is run once before each test,
C. it is not possible to tell when doBeforeClass is run, D. doBeforeClass can be called on in any order,
E. doBeforeClass cannot be static.
When running this JUnit class,
A. doBefore is executed once before all tests,
B. doBefore is run once before each test,
C. it is not possible to tell when doBefore is run, D. doBefore can be called on in any order,
E. doBefore must be static.
(1) 46.
The following 6 questions refers to the following import static org.junit.Assert.*;
code:
public class BookCollection{ ArrayList
public class BookCollectionTest{ BookCollection coll;
@BeforeClass
public static void doBeforeClass(){
public BookCollection(){
myColl = new ArrayList
}
public void add(Book b){
myColl.add(b);
}
public void remove(Book b){ myColl.remove(b);
@Before
public void doBefore(){
}}
public boolean contains(Book b){ return myColl.contains(b);
} }}
@Test
public void testRemove(){
}
}
@Test
public void testAdd(){
}
(1) 47. If the testAdd method contains
coll.add(new Book(my book));
then doBeforeClass needs to contain
A. myColl = new BookCollection();
B. Book b = new Book(my book);
C. BookCollection myColl = new BookCollection();
D. it is not possible to tell when doBeforeClass is run,
E. none of the above. (1) 48. If the testAdd method contains
coll.add(new Book(my book));
then doBefore needs to contain
A. coll = new BookCollection();
B. Book b = new Book(my book);
C. BookCollection coll = new BookCollection();
D. it is not possible to tell when doBeforeClass is run,
E. none of the above. (1) 49. If the testAdd method contains
Book b = new Book(my book);
coll.add(b);
then to test whether the adding was successful, testAdd should then do A. assertTrue(coll.contains(b));
B. if (coll.contains(b) return true;
C. all of the above,
D. none of the above.
Name: NSID: Student #:
(10) 50.
Long answer/Coding
Draw a UML class diagram (with data but with no operations needed) to capture the following
specifications. You do not need to include any information not mentioned in the specifications.
Design a system to store information on a collection of students, instructors and courses. Each person can either be a student, or an instructor. All people have a name (one string), and an identifier. A student also is registered in some number of courses, and each instructor instructs some number of courses. Each course has some number of students, one instructor, and a course ID. It must be possible to store all courses and search by course ID, and it must be possible to store all people and search by ID.
(7) 51.
(a) Consider a BookStore class that stores an ArrayList of Books, and has many methods such as
(3)
(b) Write code from within another class that can create the book store if it does not exist, and then place a Book into it. Assume that the Book class has a constructor that takes in the title as parameter.
public BookStore();
public void add(Book b);
public void remove(Book b);
public Book searchWithTitle(String title);
public boolean inCollection(String title);
Make a new class called TheBookStore that imposes the singleton pattern on BookStores (it should use the BookStore class).
Convert the code above to the singleton pattern.
public class CommandStatus
{
} }
protected boolean successful = false;
protected String errorMessage;
/**
* @return the successful status from the last execution
*/
public boolean wasSuccessful() {
return successful;
}
/**
* @precond ! wasSuccessful()
* @return the errorMessage
*/
public String getErrorMessage() {
if (wasSuccessful())
throw new RuntimeException(The last execution must be
+ unsuccessful in order to retrieve its error message.);
return errorMessage;
(10) 52.
(a) Rewrite the code below to using Command Pattern. Make use of the CommandStatus class from the notes, which is found at the end of this exam. This may involve rewriting this method (to do this, feel free to cross out and add to provided code). (Code does not use the singleton pattern).
public void addBook(){
String name = ioInterface.readString(Enter book title: );
String authors = ioInterface.readString(Enter book author(s): ); if (bookCollection.inCollection(name)){
throw new RuntimeException(Book already in collection); } else{
Book b = new Book(name, authors); bookCollection.add(b);
}
}
(7) 53.
(a) Write a generic class called MapString that stores two instance variables, one is of a general type, and the other is a String. It should have a constructor that takes in a general type, set it to the first instance variable, and set the second instance variable to be the toString() representation of the first instance variable. It should have two accessor methods, and a mutator method called setFirst that changes the first instance variable, and the second instance variable to be the toString representation of the first instance variable.
(4) (b) Make a Double object, and create a MapString object of Doubles.
(3) (c) What advantages does this have over making the class MapString hold the first instance vari- able of type Object.
(10) 54. Write a class (and a main method inside it that displays it) that displays a 200200 window that contains an appropriate title bar, a button with the label +1, and a text field with a running total. The text field should be at the top of the window, and the button below. If you do not remember the names of methods in the Java API, just use an appropriate name that is representative of what it is doing. No need for import statements.
Reviews
There are no reviews yet.