Practice exam for CMSC131-04, Fall 2017

Similar documents
CSC Java Programming, Fall Java Data Types and Control Constructs

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS159 Midterm #1 Review

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

Index COPYRIGHTED MATERIAL

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

APCS Semester #1 Final Exam Practice Problems

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

Introduction to Programming Using Java (98-388)

About this exam review

EXAMINATIONS 2016 TRIMESTER 2

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

Question 2. [5 points] State a big-o upper bound on the worst case running time of the given

Name: CS 159 Practice Final Fall 2015

Name: CS 159 Practice Final Fall 2015

Introduction to Programming (Java) 4/12

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

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

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

CS159. Nathan Sprague

public static boolean isoutside(int min, int max, int value)

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

1. Answer as shown or -2 unless question allows partial credit. No points off for differences in spacing, capitalization, commas, and braces

CS 159 Midterm #1 Review Questions

TOPICS TO COVER:-- Array declaration and use.

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

CS 307 Midterm 2 Fall 2010

Pace University. Fundamental Concepts of CS121 1

Programming Languages and Techniques (CIS120)

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

University of Maryland College Park Dept of Computer Science

Array. Prepared By - Rifat Shahriyar

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

CS163/164 Final Exam Study Session

CS159. Nathan Sprague

Lab Assignment Three

CMSC 202H. Containers and Iterators

CSE 143 Au03 Final Exam Page 1 of 15

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Full file at

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

University of Maryland College Park Dept of Computer Science

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

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Prelim One Solution. CS211 Fall Name. NetID

Computer Sciences 302 Exam 2 Information & Sample Exam

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Summer Final Exam Review Session August 5, 2009

CS 251 Intermediate Programming Java Basics

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

TeenCoder : Java Programming (ISBN )

Selected Questions from by Nageshwara Rao

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Topic 7: Algebraic Data Types

Examination Questions Midterm 1

Programming II (CS300)

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

CS 177 Week 15 Recitation Slides. Review

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Lec 7. for loops and methods

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSCI-142 Exam 2 Review November 2, 2018 Presented by the RIT Computer Science Community

CS 307 Midterm 2 Fall 2009

CS 139 Practice Midterm Questions #2

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CS 180 Final Exam Review 12/(11, 12)/08

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CSE 431S Type Checking. Washington University Spring 2013

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Java Classes and Objects

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

ESC101 : Fundamental of Computing

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CompuScholar, Inc. 9th - 12th grades

CMSC 206: Data Structures Final Exam Reference May 2018

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Java 2 Programmer Exam Cram 2

CS 61B, Spring 1999 MT3 Professor M. Clancy

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

CMSC 202. Containers

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

Brief Summary of Java

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

PIC 20A Number, Autoboxing, and Unboxing

CMSC132, Practice Questions

PhoneBook (Array List)

CS 314 Exam 2 Spring

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Transcription:

Practice exam for CMSC131-04, Fall 2017 Q1 makepalindrome - Relevant topics: arrays, loops Write a method makepalidrome that takes an int array, return a new int array that contains the values from the original array in their original order, followed by the elements from the original array in reversed order. For example, given [5, 4, 8], it will return the array [5, 4, 8, 8, 4, 5]. The original array should not be modified. 1

Q2 countpairs - Relevant topics: recursion, strings - Note: From codingbat-1 Write a recursive method countpairs that takes a String and returns the number of pairs in the string, where we ll define a "pair" in a string is two instances of a char separated by a char. So "AxA" the A's make a pair. Pair's can overlap, so "AxAxA" contains 3 pairs -- 2 for A and 1 for x. Recursively compute the number of pairs in the given string. Do not use any looping constructs or string methods other than length, charat and substring. Other example test cases: countpairs( axa ) 1 countpairs("axax") 2 countpairs("axbx") 1 Please complete the following about your implementation: Did you avoid using any loops (e.g., while loops or for loops)? yes I didn t read the instructions and am going to lose lots of points What is the base case? What are the recursive calls you make? In what way are the arguments to the recursive calls simpler? Your solution (use extra paper if needed; this is only a practice exam) 2

Q3 selectstrings - Relevant topics: List operations, iteration, String methods Write a static method selectstrings that takes two arguments: A list of Strings A string prefix The selectstrings method returns a list of Strings containing just the strings from the list provided as an argument that start with the provided prefix. The arguments should not be modified. For example, given the arguments [ CMSC132, MATH140, PSYC200, CMSC250 ] and CMSC, it should return [ CMSC132, CMSC250 ]. 3

Q4 Exception test from Lab on Dec 11th - Relevant topics: Exceptions, try-catch-finally Consider the following code: public static void main ( String args []) { try { int [] a = {- 3, 5, 10 }; System. out. println ( checkpositive ( a, 0 )); System. out. println ( checkpositive ( a, 0 )); System. out. println ( checkpositive ( a, 0 )); System. out. println ( checkpositive ( a, 0 )); System. out. println ( checkpositive ( a, 0 )); } catch ( NullPointerException e ) { System. out. println ( "Null pointer exception" ); } catch ( ArrayIndexOutOfBoundsException e ) { System. out. println ( "Array index out of bounds exception" ); } finally { System. out. println ( "finished" ); } } private static boolean checkpositive ( int [] a, int i ) { try { return a [ i ] > 0; } catch ( ArrayIndexOutOfBoundsException e ) { System. out. println ( "index " + i + " is out of bounds" ); return false ; } finally { System. out. println ( "done" ); } } 4

Changed arguments for the 5 calls to checkpositive so that the output of this program is as shown. Expected output Which call Arguments to checkpositive call done true done false index 5 is out of bounds done false done Null pointer exception finished 1st 2nd 3rd 4th 5th 5

Q5 ints/double - Relevant topics: integer vs floating point math, conversion between int and double What does this code print? double z = 3.9; System. out. println ( z ); int q = ( int ) z; System. out. println ( q ); System. out. println ( q / 2 * z ); double w = 505 / 10 + 0.1; System. out. println ( w ); Print statement Output 1st println 2nd println 3rd println 4th println 6

Q6 Drawing Triangle - Relevant topics: Loops, Nested loops, Manually throwing an exception Write a method drawtriangle that will print out a right triangle whose length (height and width) is represented by the integer parameter. Look at the examples for what your code should do. If the length is less than zero, throw an IllegalArgumentException. drawtriangle(4); * ** *** **** drawtriangle(1); * drawtriangle(5); * ** *** **** ***** drawtriangle(-1); IllegalArgumentException. 7

Q7 Design of class for Course See description of Q7 on page 11 Provide just the constructors for course here; other methods on next page 8

Q7 Provide your implementations of addsection, equals and allids() here 9

Q8 recursion mystery Consider the following function. static int mystery ( int x ) { if ( x == 0 ) return 0; return x % 10 + mystery ( x / 10 ); } Show the calls to mystery in the order they are made. For each call, show the argument to mystery and the value returned. Note: there are more rows than you will need. Calls Values returned mystery(567) 10

Q7 Design of class for Course (tear this page off if desired) - Relevant topics: equals method, copy constructor, deep copy, ArrayLists This is a simplified version of the course class from project 10. For purposes of this question, the Course class has just three instance variables: public class Course { private String courseid; private String name; private ArrayList < Section > sections ; } You can assume that appropriate getters are provided for those three instance variables. You need to write the following: A constructor that takes two arguments, a courseid and a name, and initializes sections to be empty. A copy constructor that performs a deep copy. Assume that the class Section has a copy constructor that performs a deep copy. An instance method addsection that takes one Section as an argument and adds it to the sections for the course. An appropriate equals method such that two Course s are equal if and only if their courseid s, name s and section s are equal. You do not need to implement hashcode A static method allids() that takes no arguments and returns a TreeSet<String> containing all of the courseids that were ever created. Define any static variables you need to support this. 11

Reference for practice final, CMSC 131-04 The following includes all of the methods you will need to use for these questions, as well as some you won't need. String methods int length (); boolean startswith ( String s ); boolean substring ( int beginindex, int endindex ); char charat ( int i ); List < T > methods int size (); boolean add ( T t ); boolean contains ( T t ); boolean remove ( T t ); boolean removeall ( Collection<T> t ); T get ( int i ); T set ( int i, T value ); Set < T > methods int size (); boolean add ( T t ); boolean contains ( T t ); boolean remove ( T t ); boolean removeall ( Collection<T> t ); Map < K, V > methods int size (); V get ( K key ); // get the value with this key if it exists, if it doesn't return the defaultvalue V getordefault ( K key, V defaultvalue ); boolean containskey ( K k ); V put ( K key, V value ); V remove ( K key ); // return a set of the keys in the map Set < K > keyset (); You can iterate through anything iterable, including an array, a List or a Set, using a "for each" loop. For example, if names is declared as a Set<String>, you can use the following code to print out all the Strings in that set: for ( String name : names ) { System. out. println ( name ); } 12