Unformatted text preview:

1 University of Maryland College Park Dept of Computer Science CMSC131 Fall 2018 Exam #3 Key FIRSTNAME, LASTNAME (PRINT IN UPPERCASE): STUDENT ID (e.g. 123456789): Instructions - Please print your answers and use a pencil. - Do not remove the staple from the exam. Removing it will interfere with the Gradescope scanning process. - To make sure Gradescope can recognize your exam, print your name, write your directory id at the bottom of pages with the text DirectoryId, provide answers in the rectangular areas provided, and do not remove any exam pages. Even if you use the provided extra pages for scratch work, they must be returned with the rest of the exam. - This exam is a closed-book, closed-notes exam, with a duration of 50 minutes and 200 total points. - Your code must be efficient. - You don’t need to use meaningful variable names; however, we expect good indentation. Grader Use Only #1 Problem #1 (Miscellaneous) 42 #2 Problem #2 (Memory Map) 42 #3 Problem #3 (Arrays) 64 #4 Problem #4 (String Manipulation) 52 Total Total 2002 Problem #1 (Miscellaneous) 1. (3 pts) What is the output for the following code fragment? int k = 1, m = 1; while (k <= 1) { while(m <= 1) { if (m == 1 ) { break; } m++; } System.out.println("Hi"); k++; } System.out.println("Done"); Answer: Hi Done 2. (3 pts) Appending (concatenating) strings is: a. An expensive operation and an alternative (e.g., StringBuffer) should be used. b. A cheap operation. c. An operation that have the same performance as using a StringBuffer. d. None of the above. Answer: a. 3. (3 pts) When we execute the following code: System.out.println(3.9 - 3.8 == .10); a. We will always get true. b. Getting false is possible as some numbers cannot be represented precisely in binary. c. An exemption will be thrown. d. None of the above. Answer: b. 4. (3 pts) When an exception occurs, Java pops back up the call stack to each of the calling methods to see whether the exception is being handled in a catch block of the method. True or False. Answer: True 5. (3 pts) In a JUnit test method if you don’t provide an assertion the test is considered to have succeeded. True or False. Answer: True 6. (3 pts) Creating an array of size 0 is not allowed in Java. True or False. Answer: False3 7. (3 pts) The code in the finally block is executed: a. Only when no exemption takes place. b. Always c. Only when the exemption takes place. d. None of the above. Answer: b 8. (5 pts) The following class has a privacy leak. Fix the code to remove it (feel free to edit and cross out the code provided). public class Example { private StringBuffer data; public Example(String dataIn) { data = new StringBuffer(); data.append(dataIn); } public StringBuffer getData() { return data; } } Answer: getData method will return a copy of data 9. (3 pts) We have an array of string 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 string objects is enough. c. A reference copy is enough. d. None of the above. Answer: b. 10. (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 1(inclusive) and 100 (inclusive). The only function you can use is Math.random(). int x = One Possible Answer: (int)(Math.random() * 100) + 1; 11. (7 pts) Rewrite the following code fragment by replacing the if statement with the ternary (? : ) operator. /* Original Code */ String name; int y = scanner.nextInt(); if (y > 0) { name = "Pos"; } else { name = "Neg"; } /* Rewritten Code */ String name; int y = scanner.nextInt(); name = Answer: name = y > 0 ? "Pos" : "Neg";4 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 value write NULL. If an entry has a value of 0 do not leave it blank; write 0. public static void process(StringBuffer[] a, double b) { a[0].append("bert"); StringBuffer m = a[0]; a[0] = null; b = 9; /* HERE */ } public static void main(String[] args) { StringBuffer[] all = new StringBuffer[3]; double[] tem = new double[2]; all[0] = new StringBuffer(); all[0].append("Al"); all[1] = new StringBuffer(); all[1].append("Bud"); tem[0] = 23.4; process(all, tem[0]); } Solution 3 length NULL NULL Bud Albert length 2 23.4 0 args 9 all tem a b m5 Problem #3 (Arrays) Implement a method named getIndicesArray that has the prototype below. The method returns an array with the INDICES of elements in the data array that have a value less than or equal to limit. For this problem: - You may only perform a single pass over the array. - The array to return must have a length that corresponds to the length of the data array plus one. - The last index added to the result array will be followed by -1 in order to mark the end of data. - If the data parameter is null, the method will throw the exemption IllegalArgumentException with the message “Invalid” and no further computation will take place. - Below we included a driver and the corresponding output that relies on the method getIndicesArray. Feel free to ignore if you know what to implement. One Possible Answer: public static int[] getIndicesArray(int[] data, int limit) { if (data == null) { throw new IllegalArgumentException("Invalid"); } int[] answer = new int[data.length + 1]; int k = 0; for (int i = 0; i < data.length; i++) { if (data[i] <= limit) { answer[k++] = i; } } answer[k] = -1; return answer; }6 Problem #4 (String Manipulation) Implement a method named getSerialNumber that has the prototype below. The method will process a string and extract an integer that is part of a valid string. A valid string is one that starts with the uppercase letter A and is followed by a 5 digit number. Any number of spaces can precede A and follow the last digit. For


View Full Document

UMD CMSC 131 - Exam #3 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 #3 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 #3 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 #3 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?