CMSC131 Final Exam Practice Questions

Similar documents
Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CSC Java Programming, Fall Java Data Types and Control Constructs

Introduction to Programming Using Java (98-388)

Index COPYRIGHTED MATERIAL

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Selected Questions from by Nageshwara Rao

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

COP 3330 Final Exam Review

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

RAIK 183H Examination 2 Solution. November 10, 2014

Computer Science II Data Structures

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Computer Science II Fall 2009

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Exam Duration: 2hrs and 30min Software Design

MIDTERM REVIEW. midterminformation.htm

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

UMBC CMSC 331 Final Exam

CS 113 MIDTERM EXAM 2 SPRING 2013

CMSC 132, Object-Oriented Programming II Summer Lecture 5:

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

University of Maryland College Park Dept of Computer Science

Programming Problems 22nd Annual Computer Science Programming Contest

CS 159 Midterm #1 Review Questions

CS 231 Data Structures and Algorithms, Fall 2016

Practice exam for CMSC131-04, Fall 2017

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CS 113 PRACTICE FINAL

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Brief Summary of Java

CO Java SE 8: Fundamentals

Class, Variable, Constructor, Object, Method Questions

CMSC131. Arrays: The Concept

CSCI 355 LAB #2 Spring 2004

CS 151 Name Final Exam December 17, 2014

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

CMSC132, Practice Questions

CSE 8B Final Exam Fall 2015

Lecture 4. Types, Memory, Exceptions

CMSC 331 Second Midterm Exam

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2013

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Java Identifiers, Data Types & Variables

Final Exam. COMP Summer I June 26, points

CH. 2 OBJECT-ORIENTED PROGRAMMING

Programming Languages and Techniques (CIS120)

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

COMPUTER APPLICATIONS

PASS4TEST IT 인증시험덤프전문사이트

Prelim 1. Solution. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer

Ohad Barzilay and Oranit Dror

About this exam review

Final Exam May 21, 2003

public int calculatedamage() { // Generate and return the damage inflicted on ship }

COE318 Lecture Notes Week 8 (Oct 24, 2011)

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

CS 12 Fall 2003 Solutions for mid-term exam #2

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

CSCI 355 Lab #2 Spring 2007

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

COE318 Lecture Notes Week 10 (Nov 7, 2011)

H212 Introduction to Software Systems Honors

EXAM Computer Science 1 Part 1

Declarations and Access Control SCJP tips

Generics method and class definitions which involve type parameters.

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

Informatik II (D-ITET) Tutorial 6

Part 3: GridWorld Classes and Interfaces

Introduction to Java

IT 313 Advanced Application Development

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

In Java there are three types of data values:

COMP 401 Spring 2013 Midterm 1

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

Use the supplementary boxes on the last sheet if you want to change any answer.

Prelim 1. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Transcription:

CMSC131 Final Exam Practice Questions Disclaimer: The following are questions that try to provide you with some practice material for the final exam. By no means do they represent the only material you should know for the final exam. We will not be providing answers to this set of questions, however, feel free to see the instructors or TAs for assistance during office hours. Some of the questions can be considered challenging. A. Miscellaneous 1. What is the Java Virtual Machine? 2. What is byte code? 3. How can you generate bytecode for a program called Test.java? 4. What is the difference between a syntax error and a run time error? 5. Can every class in Java have a main method? 6. Write an output statement that prints the following string: the path to use is "home\games". The quotes and backslash must be printed. 7. Given the following code: String name = scanner.next(); if (name!= null && name.length() >= 5) { System.out.println(name); scanner.close(); a. Does it makes a difference if the expression name!= null appears after name.length() >= 5? b. Which Java feature associated with the && and operators is the above example relying on? 8. What is the default value for a local variable? 9. What is the default value for an instance variable? 10. Rewrite the following cascaded if statement using a switch statement. int age = scanner.nextint(); if (age == 21) { System.out.println("Legal age"); else if (age == 25) { System.out.println("rental possible"); else if (age == 15) { System.out.println("Party"); else { System.out.println("Unknown"); scanner.close(); 11. Are the numeric comparisons in the following example valid? int a = scanner.nextint(), b = scanner.nextint(); double c = scanner.nextdouble(), d = scanner.nextdouble(); if (a == b) { System.out.println("Same integers"); if (c == d) { System.out.println("Same doubles");

12. Why the following swapwrong method does not swap the values? Would it make a difference if instead of integer parameters we were using Integer parameters? public static void wrongswap(int x, int y) { int temp = x; x = y; y = temp; public static void main(string[] args) { int a = 10, b = 20; wrongswap(a, b); System.out.println(a + " " + b); 13. Write pseudocode for a program that determines whether any two consecutive values in a sequence of values differ by at most a particular value. For example, for the sequence 9, 10, 18, 16 two consecutive values differ by at most two. 14. How do the String and StringBuffer classes differ? 15. What is the difference between method overloading and method overriding? Provide examples. 16. Provide an expression using the Random class that generates a random value in the range [4, 10]. 17. What is the difference between final int x and final static int x? 18. What is the difference between a shallow copy and a deep copy? 19. Which two methods of the Object class do we often override? B. Classes 1. What is the role the constructor plays in a class? 2. What is a copy constructor? 3. What is the current object (this reference)? 4. What is the heap? 5. What is the difference between a class and an object? 6. What is the value stored by a reference variable? 7. What is the difference between a static method and a non-static method? 8. When should you define a method as static? 9. Define an interface called Road with the following methods: a. String getname() b. double getdistance() 10. Define a class called Highway that has specifications below. The class implements the above Road interface. a. Instance variables i. number (integer) ii. name (String) iii. distance (double) b. Non-static methods i. Constructor Takes a number, name and distance as parameters. ii. Default constructor Initializes the object with the values 0, NO_NAME, and 0 as the values for number, name and distance, respectively. iii. get and set methods for all instance variables. iv. equals method Two roads are considered equal if they have the same number. v. tostring Returns a string with object information (number, name, distance). c. Static methods i. getcount Returns the number of Road objects that have been created. d. This class implements the Comparable interface. Objects will be compared based on the number, with highways having a lower number appearing first, if we were to sort an ArrayList of them. Feel free to add any other variables/methods you understand are needed.

11. Define a class called Roadtrip with the following specifications: a. Instance variables i. name (String) ii. route (ArrayList<Road>) b. Static methods i. addroad Static method that has as parameters a Roadtrip object and a Road. The Road will be added to the specified Roadtrip object. c. Non-static methods i. Constructor - Takes a name parameter representing the roadtrip s name. ii. addroad Appends a Road object to the ArrayList. You must implement this method using the addroad static method above. The method returns a reference to the current object. iii. removeroad Removes the road from the ArrayList associated with a particular name. iv. tripdistance Returns the total distance associated with the roadtrip. v. equals method Two roadtrips are the same if they have the same name. vi. tostring Returns a string with the roadtrip name, followed by information about each road in the route. Feel free to add any other variables/methods you understand are needed. 12. Assume you also have a class named LocalRoad that implements the Road interface. Implement the method printinfo that has the following signature: void printinfo(arraylist<road> roads) The method will print the road number of highways that are part of roads ArrayList. Remember that not all roads are highways (hint: use instanceof). C. Arrays 1. Define a method called rotateleft that will rotate elements in an integer array by a particular number of positions. For example, given an array with the values 10, 4, 5, 70, rotating the array by 2 will generate the array 5, 70, 10, 4. 2. Define a method called hasthreeofakind that returns true if an array of length 5 has three values that are the same and false otherwise. 3. Define a method called drawhourclass that draws an hour glass with a specific number of lines. For example, calling drawhourclass(4) will draw the following diagram: **** ** ** **** 4. Define a method called filter that has the prototype below and that relies on the Highway class you defined above. The method returns a new array with copies of Highway objects that have a number in the specified range. The will throw an IllegalArgumentException if the list is null or startrange is greater than endrange. public static Highway[] filter(highway[] list, int startrange, int endrange) 5. Define a method called includesall that has the prototype below and relies on the above Highway class. The method determines whether all the highways in list1 and part of list2. public static boolean includesall(highway[] list1, Highway[] list2)

6. Define a method called removeconsecutiveduplicates that has the prototype below and relies on the above Highway class. The method removes duplicate highways that appear one after another in the list. The method will return the number of highways removed. public static int removeconsecutiveduplicates(highway[] list) 7. Define a static method called firstrow that given a 2-dimensional array of doubles, finds the first row that consists of an increasing sequence of values, that is, it finds the smallets i such that: array[i][0] < array[i][1] < array[i][2] <... If such a row exists, a copy of the contents of that row are returned as a one dimensional array; otherwise null is returned. 8. The Cell class is defined as follows: public class Cell { private char color; public Cell(char color) { setcolor(color); public char getcolor() { return color; public void setcolor(char color) { if (color == 'R' color == 'G' color == 'B') { this.color = color; else { this.color = 'Y'; public String tostring() { return Character.toString(color); D. Recursion a. Is the above class mutable or immutable? b. Does the above class has a default constructor? c. Define a recursive static method named colorrow that will color a row with random colors. public static void colorrow(cell[][] cells, int rowindex) d. Define a recursive static method named flip that will flip cells that surround the one associated with rowindex and colindex. Flipping a cell requires changing an R cell to a B cell; otherwise no change takes place. public static int flip(cell[][] cells, int rowindex, int colindex) Note: For these problems feel free to add at most one auxiliary method (if needed). 1. See previous problem. 2. Define a recursive static method called readinteger() that reads a string representing an integer value using JOptionPane.showInputDialog, and converts the value to an integer using Integer.parseInt(). If the provided string represented an integer value, the method will return the integer value; otherwise the method will print the message Invalid value and call itself to read another value. Hint: You must handle the NumberFormatException while implementing this method. 3. Given a string, define a recursive method that computes the number of vowels in the string.

4. Given a string, define a recursive method that returns a string where vowels have been replaced by the @ symbol. 5. Given an array, define a recursive method that computes the product of all the elements in the array. E. Design 1. Write a Java program that simulates a guessing number game. The program will generate a random value the user needs to guess and keep asking the user for a guess until the user provides the expected value. If a value has already provided, the program will print the message Value already provided. If the value is higher than the value to guess, the program will print Too high ; if it is less, the program will print the message Too low. Once the user enters the expected value, the program will print the number of attempts. 2. Define which classes / classes you might need in order to implement software that keeps track of students submissions (similar to the submit server). You don t need to implement any code; just describe the classes / interfaces you might need and their relationship. For each student, the submit server keeps track of every submission and classifies them on-time, late or very-late. A submission is associated with a student and a project. Several projects can be associated with a class. F. Memory Maps Draw a memory diagram showing both the stack and the heap at the moment this program reaches the point marked /* HERE */. public class MemMap { public static void proc1(apple passed, int x, Apple[] container) { String newcolor = "Green"; passed.update(x, newcolor); x += 7; passed = null; container[1] = container[0]; container[0] = null; /* HERE */ public static void main(string[] args) { String color1 = "Red"; int size1 = 10, size2 = 20; Apple a1 = new Apple(size1, color1); Apple[] basket = new Apple[2]; basket[1] = new Apple(1, "Yellow"); basket[0] = a1; proc1(a1, size2, basket); public class Cell { private char color; public Cell(char color) { setcolor(color); public char getcolor() { return color; public void setcolor(char color) { if (color == 'R' color == 'G' color == 'B') { this.color = color; else { this.color = 'Y'; public String tostring() { return Character.toString(color);