Unformatted text preview:

1 University of Maryland College Park Department of Computer Science CMSC131 Spring 2019 Exam #2 Key FIRSTNAME, LASTNAME (PRINT IN UPPERCASE): STUDENT ID (e.g., 123456789): Instructions - Please print your answers and use a pencil. - This exam is a closed-book, closed-notes exam with a duration of 50 minutes and 200 total points. - Do not remove the exam’s staple. Removing it will interfere with the scanning process (even if you staple the exam again). - Write your directory id (e.g., terps1, not UID) at the bottom of pages with the text DirectoryId, provide answers in the rectangular areas, and do not remove any exam pages. Even if you don’t use the extra pages for scratch work, return them with the rest of the exam. - If you continue a problem on the extra page(s) provided, make a note on the particular problem. - For multiple choice questions you can assume only one answer is expected, unless stated otherwise. - You don’t need to use meaningful variable names; however, we expect good indentation. - You must write your name and id at this point (we will not wait for you after time is up). - You must stop writing once time is up. Grader Use Only #1 Problem #1 (Miscellaneous) 48 #2 Problem #2 (Memory Map) 50 #3 Problem #3 (Class Implementation) 102 Total Total 2002 Problem #1 (Miscellaneous) 1. (3 pts) A method should be defined as static if (circle all that apply): a. It does not access instance variables. b. If it is a public method. c. The method makes a reference to special value this. d. None of the above. Answer: a. 2. (3 pts) How many objects exist in the following code fragment? StringBuffer m; Answer: 0 3. (3 pts) Instance variables are created when: a. An instance of the class is created. b. When the constructor is called. c. When a set method is called. d. None of the above. Answer: a. 4. (3 pts) Which of the following actually creates an object? a. new b. constructor c. private d. None of the above. Answer: a. 5. (3 pts) We can call a non-static method of a class (circle all that apply): a. Only if an object of the class has already been created. b. From a static method of the class. c. By using the class name (e.g., ClassName.method) d. None of the above. Answer: a. or b. (or both). 6. (3 pts) When we call a.completeTask(b) which value does the special value this (present inside of the completeTask method) have? a. It has the same value a has b. It has the same value b has c. It has the value null d. None of the above. Answer: a.3 7. (3 pts) What is the output of the following program? public class Values { private String distance; private double cost; private boolean completed; public static void main(String[] args) { Values values = new Values(); System.out.println(values.distance); System.out.println(values.cost); System.out.println(values.completed); } } Answer: null 0.0 or 0 False 8. (3 pts) In the previous Values class, is there a default constructor associated with the class? Yes / No Answer: Yes. 9. (3 pts) Where can primitive type variables reside in Java? Circle all that apply. a. Heap b. Stack c. main method d. None of the above. Answer: a. and b. (if they mark c. do not penalize) 10. (3 pts) If you define a JUnit test and don’t add any assertions (e.g., assertTrue) then: a. The test will fail when it is run. b. The test will succeed when it is run. c. The test will not compile. d. None of the above. Answer: b. 11. (3 pts) We have an array of StringBuffer objects and we would like to make a copy of the array so changes to the new array will not affect the original. To make the copy: a. We need to make a deep copy, otherwise changes in the new array will affect the original. b. A shallow copy where we only duplicate the array object and not the StringBuffer objects is enough. c. A reference copy is enough. d. None of the above. Answer: a. 12. (3 pts) The code in the finally block is executed: a. After a catch clause has been executed. b. When two or more exemptions take place. c. Before the try block. d. None of the above. Answer: d.4 13. (3 pts) When we execute the following code: System.out.println((14.90 - 14.80) == .10 ? "Hello" : "Bye"); a. We will always get “Hello”. b. Getting “Bye” is possible as some numbers cannot be represented precisely in binary. c. The expression within the System.out.println does not compile. d. None of the above. Answer: b. 14. (3 pts) The class Toy needs to implement the equals method. Which of the following is the correct prototype (first line of the method) for the equals method needed by this class? a. public boolean equals(Object obj) b. public boolean equals(Toy obj) c. public boolean equals(String obj) d. None of the above. Answer: a. 15. (6 pts) Math.random() returns a value between 0 (inclusive) and less than 1. Complete the following assignment so x is assigned a random integer value between 2 (inclusive) and 50 (inclusive). The only function you can use is Math.random(). int x = Answer: (int)(49 * Math.random()) + 25 Problem #2 (Memory Map) Draw a memory map for the following program at the point in the program execution indicated by the comment /*HERE */. Remember to draw the stack and the heap. If an entry has a value of null write NULL. If an entry has a value of 0 do not leave it blank; write 0. public static void process(StringBuffer[] data, int size, int delta) { data[0] = new StringBuffer("Tom"); data[1].append("131"); size = 20; delta += 1000; /* HERE */ } public static void main(String[] args) { StringBuffer[] all = new StringBuffer[2]; int length = 10; int[] col = {4, 500, 7}; all[0] = new StringBuffer("Dora"); all[1] = new StringBuffer("Elsa"); process(all, length, col[1]); } delta 20 1500 size 10 data col Elsa131 all args length 4 3 length 0 length 2 length Tom 500 76 Problem #3 (Class Implementation) Complete the implementation of a class called Account that represents a bank account. A bank account has an id, a password, a balance, and an array of integers (deposits) representing deposits done to the account. The maximum size of the array is MAX_DEPOSITS and the current number of deposits made is represented by numDeposits. Below we have provided a driver that illustrates the


View Full Document

UMD CMSC 131 - Exam #2 Key

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Exam #2 Key
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exam #2 Key and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exam #2 Key 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?