Unformatted text preview:

University of Maryland College Park Dept of Computer Science CMSC131 Fall 2018 Exam 2 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 each page 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 You don t need to use meaningful variable names however we expect good indentation Grader Use Only 1 2 3 Total Problem 1 Miscellaneous Problem 2 Memory Map Problem 3 Class Implementation Total 30 30 140 200 DirectoryId 1 Problem 1 Miscellaneous 1 3 pts A method should be defined as static if circle all that apply It does not require access to instance variables a b We would like to call the method by using the class name e g JOptionPane showInputDialog c The method makes a reference to special value this d e None of the above It calls a static method 2 3 pts How many objects exist in the following code fragment 3 3 pts How many objects exist in the following code fragment 3 pts Which of the following actually creates an object 4 Answer a b String m Answer 0 String s Taco Answer 1 a new b constructor c private d None of the above 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 6 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 false 2 Answer a 3 pts When we call a doSomething b which value does the special value this present inside of the doSomething method 5 7 8 3 pts In the previous Values class is there a default constructor associated with the class Answer Yes 3 pts Name one immutable Java class mentioned in lecture One Possible Answer String 3 pts Where in memory objects reside in Java Circle all that apply 9 a Heap b Stack c main method d None of the above 10 3 pts What happens if new is called and there is no space available in the heap a The program will crash b The program will continue executing using the stack as a heap c The main method will be used as a the heap area d None of the above Answer a Answer a 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 Answer public class Driver public static void process Road b double x double value x 1 b increaseCost x x HERE public static void main String args String alias Uno double cc 10 50 Road a new Road alias 4 process a cc public class Road private String name private double cost public Road String nameIn double costIn name nameIn cost costIn public void increaseCost double delta cost delta public double getCost return cost public String getName return name 11 50 10 50 14 50 value name alias 9 50 Uno args cost x b cc a 3 Problem 3 Class Implementation Implement a class named Printer according to the specifications below The class allow us to define a printer that can hold a maximum number pages and that keeps track of the content that has been printed Below we have provided a driver that illustrates the functionality associated with the Printer class Feel free to ignore it if you know what to implement For this problem keep in mind that the StringBuffer append method allows you to append a String to the StringBuffer object and the toString method returns a string with the StringBuffer content 1 Provide a class definition that includes the following private instance variables a brand String variable representing a printer s brand b maxPages Maximum number of pages the printer can print c numberOfPrintedPages Number of printed pages d printedContent StringBuffer that stores the content that has been printed and the following private static variable totalPrinters Keeps track of how many Printer objects have been created public class Printer private String brand private int maxPages numberPrintedPages private StringBuffer printedContent private static int totalPrinters 0 Answer public class Printer 2 Define a constructor that takes a string brand and maximum number of pages maxPages as parameters The current object is initialized based on these parameters In addition a StringBuffer object will be created and the number of printed pages will be set to 0 Add any other initialization you understand is necessary in order to keep track of the number of objects that have been created Answer 3 Define a constructor that takes a string brand as parameter It initializes the printer with the specified brand and with 100 as the public Printer String brand int maxPages this brand brand this maxPages maxPages this printedContent new StringBuffer totalPrinters maximum number of pages You must call the previous constructor in order to implement this constructor public Printer String brand this brand 100 4 Define a static method getTotalPrinters that returns the total number of Printer objects created Answer Answer public static int getTotalPrinters return totalPrinters 4 5 Define a method called printPage that has a string as a parameter representing the content to print The method will append the string parameter to the printedContent StringBuffer object if the number of printed pages has not exceeded the maximum number of pages and if the parameter is not null otherwise the message Printer error using System out println will be printed The method will return a reference to the current object in all cases Answer 6 Define a toString method that returns a string with the following format if numberPrintedPages maxPages content null else return this public Printer printPage String content numberPrintedPages printedContent append content System out println Printer error Brand BRAND MaxPages MAXPAGES NumberPrintedPages NUMBER PRINTED PAGES PrintedContent PRINTED CONTENT where BRAND MAXPAGES NUMBER PRINTED PAGES PRINTED CONTENT correspond to the brand the maximum number of pages the number of


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?