Unformatted text preview:

1 University of Maryland College Park Department of Computer Science CMSC131 Fall 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 (e.g., 111222333)) at the bottom of pages with DirectoryId. • Provide answers in the rectangular areas. • 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. • Your code must be efficient and as short as possible. • 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) 22 #3 Problem #3 (Class Implementation) 130 Total Total 2002 Problem #1 (Miscellaneous) 1. (3 pts) How many objects exist in the following code fragment? String s1 = "today"; String s2 = s1; StringBuffer s3 = new StringBuffer(); Answer: 2 2. (3 pts) Would the following class compile? Yes / No public class Misc2 { public static void main(String[] args) { String m; System.out.println(m); } } Answer: No. m has no value. 3. (3 pts) Which of the following actually creates an object? a. Destructor b. Constructor c. new operator d. None of the above. Answer: c. 4. (3 pts) Circle all the areas where an object can be found. a. Stack b. Heap c. Only in the first method that has been called. d. None of the above. Answer: b. 5. (3 pts) Object a has 20 instance variables and object b has one instance variable. The amount of time it will take to pass a or b to a method is: a. Higher for object a. b. Higher for object b. c. Same. d. None of the above. Answer: c. 6. (3 pts) When we call m.sum(k) which value does the special value this (present inside of the sum method) have? a. It has the same value m has. b. It has the same value k 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 Station { private boolean rainy; private StringBuffer log; private int support; public static void main(String[] args) { Station s = new Station(); System.out.println(s.rainy); System.out.println(s.log); System.out.println(s.support); } } Answer: false null 0 8. (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. or a., b, and .c 9. (3 pts) If you define a JUnit test and don’t add any assertions: 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 10. (3 pts) The code in the finally block is executed: a. Always. b. When two or more exemptions take place. c. Before the try block. d. None of the above. Answer: a. 11. (3 pts) When we execute the following code: double x = .10; while (x != .20) { x = x + .01; } System.out.println(x); a. The program will always print 0.20 b. The loop may become an infinite loop. c. None of the above. Answer: b. (we should not compare floating point values)4 12. (3 pts) What does the new operator return? a. The object that was created. b. The address in the heap where the object was created. c. The address in the stack where the object was created. d. None of the above. Answer: b. 13. (6 pts) The method Task.completeTask() is expected to return 20. Write a student test that verifies whether the correct value is returned by the method. public class StudentTests { Answer: @Test public void test() { assertTrue(20 == Task.completeTask()); } 14. (6 pts) Assuming variable c is a character type variable and s is a string type variable, rewrite the following code using a single statement and the ternary operator so s is assigned the correct value. if (c == 'y') { s = "Yes"; } else { s = "No"; } Answer: s = c == 'y' ? "Yes" : "No";5 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. } Answer: public class MemMap { public static void filterData(String c, StringBuffer d, int e) { c += "Bytes"; e += 200; d.append("Fight"); d = null; /* HERE */ } public static void main(String[] args) { String a = new String("Jaws"); StringBuffer b = new StringBuffer("Rocky"); int salary = 10; filterData(a, b, salary); } args b a salary c e d RockyFight Jaws 10 null 210 JawsBytes 0 length6 Problem #3 (Class Implementation) Complete the implementation of a class called Sandwich that represents a sandwich. A sandwich has a name (name instance variable), a number of calories (cal instance variable), a number of ingredients (numOfIng instance variable), and ingredients (ings instance variable). The number of Sandwich objects created is represented by the static field TOTAL. Below we have provided a driver that illustrates some of the functionality associated with the class. Feel free to ignore it if you know what to implement. You MAY NOT add any methods beyond the ones specified below (not even private). Also you MAY NOT add any additional instance or static variables. All the methods below are public unless specified otherwise. public class Sandwich { private String name; private int cal, numOfIng; private StringBuffer ings; private static int TOTAL = 0; } 1. Define a constructor that takes a string parameter called name. The parameter represents the name of the sandwich. The constructor will initialize the name


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?