Computer Sciences 200 Midterm Exam 2 Thursday, November 15th, 2018 100 points (15% of final grade) Instructors: Jim Williams and Marc Renault (Family) Last Name: (Given) First Name: CS Login Name: NetID (email): @wisc.edu Circle your Lecture: Lec001 TR 0800 (Jim) Lec002 TR 0930 (Jim) Lec003 MWF 1100 (Marc) Lec004 MWF 1320 (Marc) Fill in these fields (left to right) on the scantron form using a #2 pencil. 1. LAST NAME (family name) and FIRST NAME (given name), as much as there is space to enter. 2. IDENTIFICATION NUMBER is your Campus ID number. 3. SPECIAL CODES ABC: Enter your three-digit lecture number: 001, 002, 003, or 004. 4. SPECIAL CODES F: Write the letter P for Primary and fill in the bubble for (1). FILL IN THE BUBBLES CORRESPONDING TO ALL OF THESE ANSWERS! The exam has two parts and is worth a total of 100 points. Part I has 20 Simple Choice questions worth 2 points each for 40 points possible for Part I. Part II has 20 Multiple Choice questions worth 3 points each for 60 points possible for Part II. You will have 120 minutes to complete the exam. Be sure to read through every question completely. I certify that I will keep my answers covered so that they may not be viewed by another student during the exam or prior to completion of their exam. I also certify that I will not view or in any way use another s work or any unauthorized devices. I understand that I may not make any type of copy of any portion of this exam without express permission from my instructor. I understand that being caught allowing another to view my work or being caught viewing another s work are both violations of this agreement and either may result in automatic failure of the course. Any penalty may also be reported to the Deans Office for all involved. Signature: 1. Be sure to review the reference pages as needed during the exam. 2. Turn off and put away your cell phone, calculator, Inspector Gadget (watches, glasses, pencils, etc.) now and wait for the proctor to signal the start of the exam. c 2018 Williams, Renault CS200 Midterm Exam 2 (P) Page 1 of 7
Disclaimer: the following are provided for your reference only, and the inclusion of information here does not guarantee it will be used on the exam. Operator Precedence Table: Level Operator Description Associativity higher ( <expression> ) grouping with parentheses left to right [ ] ( ). array index, method call, member access (dot operator) left to right ++ -- post-increment, post-decrement left to right ++ -- + -! pre-increment, unary plus/minus, logical negation right to left (type) new casting and creating object right to left * / % multiplication, division, modulus left to right + - + addition, subtraction, concatenation left to right < <= > >= instanceof relational and Java s instanceof operator left to right ==!= equality left to right && conditional AND (short-circuits) left to right conditional OR (short-circuits) left to right lower? : ternary conditional right to left = += -= *= /= %= assignment, arithmetic (compound) assignment right to left Methods from the java.lang.string class: int length() char charat(int index) boolean contains(string s) String tolowercase() String touppercase() int indexof(string s) int lastindexof(string s) boolean equals(string s) boolean equalsignorecase(string s) String substring(int begin) String substring(int begin, int end) boolean startswith(string prefix) boolean startswith(string pre, int off) string trim() Returns number of characters in the String Returns the character at the specified index of the String Returns true iff string s is in this string, otherwise false Returns a new string that is the lowercase version of this string. Returns a new string that is the UPPERCASE version of this string. Returns the index within this string of the first character of the first occurrence of the specified string s or -1 if not found. Returns the index within this string of the first character of the last occurrence of the specified string s or -1 if not found. Returns true if the contents of this String is the same as the contents of String s. Returns true iff the contents of the this string is the same as that of the string s, ignoring differences in case. Returns a new string that is a substring of this string starting at begin to the end of this string. Returns a new string that is a substring of this string starting at index begin up to but not including end. Returns true iff this string starts with the specified prefix prefix, false otherwise. Returns true iff this string starts at the specified offset off with the specified prefix pre, false otherwise. Returns a new string of this with any leading and trailing whitespace removed. c 2018 Williams, Renault CS200 Midterm Exam 2 (P) Page 2 of 7
Class (static) Constant(s) and Methods from the java.lang.math class: static double Math.PI static double random() static double pow(double x, double n) static double sqrt(double n) static double abs(double n) static double ceil(double n) static double sin(double theta) Field that represents the constant π Returns a random value between 0 (inclusive) and 1 (exclusive) Returns x n Returns n Returns the absolute value of n Returns the value of n rounded up to the nearest whole number. Returns the sine of the angle θ (θ is in radians) Other trig methods also available. Methods from the java.util.random class: Random() Creates a new pseudorandom number generator instance. Random(long s) Creates a new pseudorandom number generator seeded with s. int nextint() Returns the next pseudorandom integer value. int nextint(int n) Returns the next pseudorandom integer value between 0 (inclusive) and n (exclusive). double nextdouble() Returns the next pseudorandom double value between 0.0 (inclusive) and 1.0 (exclusive). Methods from the java.io.printstream class: void print(string s) void print(char c) void print(char[] c) void print(int i) void print(double d) void print(boolean b) void println(string s) void println(char c) void println(int i) void println(double d) void println(boolean b) Prints a String. Prints a character. Prints all the characters from the array in order. Prints an int. Prints a double. Prints a boolean. Prints a String followed by a newline. Prints a character followed by a newline. Prints an int followed by a newline. Prints a double followed by a newline. Prints a boolean followed by a newline. Methods from the java.util.scanner class: Scanner(String s) Scanner(System.in) boolean hasnext() boolean hasnextint() boolean hasnextdouble() boolean hasnextline() String next() int nextint() double nextdouble() String nextline() Creates a Scanner to read the String s Creates a Scanner that reads from the keyboard. Returns true if there s another token of input. Returns true if the next token is an int value. Returns true if the next token is a double value. Returns true if there s another line of input. Finds and returns the next complete token from this scanner. Scans the next token of the input as an int. Scans the next token of the input as a double. Advances this scanner past the current line and returns the input that was skipped. c 2018 Williams, Renault CS200 Midterm Exam 2 (P) Page 3 of 7
Methods from the java.util.arraylist class: Note the E s below are replaced with the particular ArrayList s element type. ArrayList() ArrayList<E>() ArrayList(int initcap) ArrayList<E>(int initcap) int size() E[] toarray() boolean contains(e item) int indexof(e item) E get(int index) void add(e item) void add(int index, E item) Constructs an empty array list where elements are type Object with an initial capacity of 10. Constructs an empty array list of the specified element type E with an initial capacity of 10. Constructs an empty array list where elements are type Object with an initial capacity of initcap. Constructs an empty array list of the specified element type E with an initial capacity of initcap. Returns the number of used elements in this list. Returns an array of the specified type of this ArrayList Returns true iff the specified item is in this list, otherwise false. Returns the index of the specified item if it is in this list, otherwise -1. Returns the item at the specified index in this list, throws IndexOutBoundsException if invalid index. Adds the specified item to the end of this list. Adds the specified item by inserting it into this list at the specified index. boolean addall(collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection s Iterator. E remove(int index) boolean remove(e item) Removes and returns the item from the specified index. Returns true iff the specified item was removed from this list, otherwise false. Method from the java.util.arrays class: Note the E s below are replaced with the particular array s element type. static String tostring(e[] array) Returns a String representation of the array, consisting of a list of the array s elements as strings separated by commas and enclosed in square brackets ("[]"). static void sort(e[] array) sorts the specified array in memory type E must be Comparable or Comparable<E> static int[] copyof(int[] orig, int newlength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. static <E> E[] copyof(e[] orig, int newlength) Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. static <E> List<E> aslist(e... a) Returns a fixed-size list backed by the specified array. c 2018 Williams, Renault CS200 Midterm Exam 2 (P) Page 4 of 7
Exception Class Inheritance Hierarchy: public class Object public class Throwable extends Object public class Exception extends Throwable public class RuntimeException extends Exception public class ArithmeticException extends RuntimeException public class IndexOutOfBoundsException extends RuntimeException public class ArrayIndexOutOfBoundsException extends IndexOutOfBounds public class StringIndexOutOfBoundsException extends IndexOutOfBounds public class ClassCastException extends RuntimeException public class NullPointerException extends RuntimeException public class NoSuchElementException extends RuntimeException public class InputMismatchException extends NoSuchElementException public class IOException extends Exception public class FileNotFoundException extends IOException public class EOFException extends IOException c 2018 Williams, Renault CS200 Midterm Exam 2 (P) Page 5 of 7
Part 1: Simple Choice (Questions 1 1, 2 points each) 1. The return type of the expression Math.pow(5, 6) * 2 is. A. double B. int c 2018 Williams, Renault CS200 Midterm Exam 2 (P) Page 6 of 7
Part 2: Multiple Choice (Questions 2 2, 3 points each) 2. What will the following code print out when it is run? Scanner scnr = new Scanner("Hello \n\n World!"); String c = scnr.next(); String b = scnr.nextline(); String a = scnr.next(); System.out.println(":" + a + ":" + b + ":"); A. :World!: : B. : : World!: C. :Hello:World!: D. :Hello: : c 2018 Williams, Renault CS200 Midterm Exam 2 (P) Page 7 of 7