University of Maryland College Park Dept of Computer Science CMSC131 Fall 2017 Midterm II Key Last Name PRINT First Name PRINT University Directory ID e g umcpturtle I pledge on my honor that I have not given or received any unauthorized assistance on this examination Your signature Grader Use Only 1 2 3 4 Total Problem 1 General Questions Problem 2 Memory Map Problem 3 Class Definition Problem 4 String Manipulation Total 20 30 95 55 200 1 Problem 1 General Questions 1 3 pts If the garbage collector stops working which of the following can eventually be exhausted if we continue creating objects Circle all that apply a Stack b Heap c Area where static variable resides d None of the above 2 3 pts A method should be defined as static if circle all that apply a Makes no reference to instance variables b Makes a reference to at least one instance variable c Makes a reference to static method d None of the above 3 3 pts Which of the following represents no address Circle all that apply a 0 b null c false d None of the above StringBuffer b int x 10 Number of Objects 4 3 pts How many objects are present in the following code fragment 5 3 pts When is the code associated with a finally block executed a Only when the exception occurs b Always c Only if no exception occurs d None of the above Answer b Answer a Answer b Answer 0 Answer b 2 6 3 pts What is the actual task a constructor method performs a Creating the object in the stack b Initializing the object c Moving the object from the stack to the heap d None of the above Answer b 7 2 pts Which of the following applies to the this reference a It is a reference to the current object b Can be used by both static and non static methods c It is initialized for you d None of the above size x name new String nameIn size sizeIn private String name private int size public Fruit String nameIn int sizeIn public void increaseSize int x public String toString Answer a and c Problem 2 Memory Map One the next page draw a memory diagram showing both the stack and the heap at the moment this program reaches the point identified by HERE public class Fruit public class Driver public static void task Fruit param int delta param increaseSize delta delta 1000 Fruit other param other increaseSize 20 param null HERE public static void main String args Fruit f1 new Fruit p k task f1 m String p orange int k 10 m 5 return name size 3 Stack Heap 1005 null 5 10 name size 35 length 0 Orange Orange p f1 args delta other m k param Stack Bottom 4 Problem 3 Class Definition Implement a class named Party according to the specifications below The class allow us to define a party with a maximum number of guests and to add guests to the party Below we have provided a driver that illustrates the functionality associated with the Party class Feel free to ignore it if you know what to implement Make sure you avoid code duplication 1 The class has the following private instance variables a name String variable representing a party s name b maxGuests Maximum number of guests allowed in the party c guests StringBuffer used to store the names of guests to the party d Feel free to add any other private instance variables you understand you need 2 All the methods in the class are public and non static unless otherwise specified a Constructor Takes a string name and maximum number of guests maxGuests as parameters The current object is initialized based on these parameters The parameters must be named after the instance variables Feel free to provide any other initialization that is needed b Constructor Takes a string name as parameter It initializes the party with the specified name and with 100 as the maximum number of guests c addGuest Takes a string guestName as parameter If the parameter is different than null and the current number of guests have not reached the maximum possible then the name will be added to the StringBuffer holding the names of guests A reference to the current object will be returned d getTotalParties Static method that returns the total number of Party objects created Feel free to add any variable you might need and to modify any method so this method can return the appropriate count e toString Returns a string with the following format Name NAME NumberGuests NUMBER GUESTS Guests GUESTS Where NAME NUMBER GUESTS and GUESTS correspond to the party s name the number of guests and a string with the guests respectively See sample driver below for an example Remember that the StringBuffer append method allows you to add a String and the toString method returns a string with the StringBuffer contents Driver Party party party new Party Halloween 50 System out println party addGuest Ann addGuest John Party party2 new Party EndOfSemester System out println party2 System out println Total Party getTotalParties Output Name Halloween NumberGuests 2 Guests AnnJohn Name EndOfSemester NumberGuests 0 Guests Total 2 5 private String name private StringBuffer guests private int maxGuests currGuests private static int totalParties 0 public Party String name int maxGuests this name name this maxGuests maxGuests guests new StringBuffer totalParties public Party String name this name 100 public Party addGuest String guestName if guestName null currGuests maxGuests return this guests append guestName currGuests public static int getTotalParties return totalParties public String toString String answer Name name NumberGuests currGuests answer Guests guests toString return answer Answer public class Party 6 Problem 4 String Manipulation For this problem you will implement a static method called getStrength that evaluates how strong a password is and returns one of three strings Weak Medium Strong The following definitions are associated with this problem a Digit Character represents a digit value between 0 and 9 including 0 and 9 The Java method Character isDigit determines whether a character is a digit e g Character isDigit 2 returns true b Alphabetic Character represents a letter either uppercase or lowercase The Java method Character isAlphabetic determines whether a character is alphabetic e g Character isAlphabetic returns false The password types we will have are character a Strong The password has at least one digit at least one uppercase letter and at least one non alphabetic b Medium The password has at least one digit and at least one non alphabetic character c Week The password does not have digits uppercase letters and non alphabetic characters If the passwd parameter
View Full Document