Unformatted text preview:

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 2 3 Total Problem 1 Miscellaneous Problem 2 Memory Map Problem 3 Class Implementation Total 48 22 130 200 1 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 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 a Destructor b Constructor c new operator d None of the above Answer c Answer b to a method is a Higher for object a b Higher for object b c Same d None of the above Answer c It has the same value m has It has the same value k has It has the value null a b c d None of the above Answer a 2 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 6 3 pts When we call m sum k which value does the special value this present inside of the sum method have 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 3 pts If you define a JUnit test and don t add any assertions 9 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 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 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 Answer b Answer a 3 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 returned by the method public class StudentTests Answer Test public void test assertTrue 20 Task completeTask if c y s Yes else s No Answer s c y Yes No 13 6 pts The method Task completeTask is expected to return 20 Write a student test that verifies whether the correct value is 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 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 write null If an entry has a value of 0 do not leave it blank write 0 e d 210 null 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 Answer length salary args 10 b a c 0 5 JawsBytes RockyFight Jaws 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 instance variable with the parameter and the ings instance variable with a StringBuffer object If the name parameter is null the IllegalArgumentException with the message Invalid name will be thrown Make sure you adjust the total number of objects created so far throw new IllegalArgumentException Invalid name public Sandwich String name if name null this name name ings new StringBuffer TOTAL 2 Define a default constructor that initializes a sandwich with the name NONAME You must call the previous constructor in order to implement this constructor otherwise you will not get any credit public Sandwich this NONAME 3 Define a copy constructor method for the class Changes to 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 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?