CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name:

Similar documents
CSC 1051 Algorithms and Data Structures I. Final Examination December 20, Name: KEY. Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Happy Cinco de Mayo!!!!

Happy Cinco de Mayo!!!!

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

CSC 1051 Data Structures and Algorithms I

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Chapter 3. Selections

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

SCOPE. public class ABC { public static void main( String args [ ] ) { Not accessible outside the block. Local to this block; accessible within it

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

CSC 1051 Arrays - Review questions

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

Principles of Computer Science I

Recitation: Loop Jul 7, 2008

Selection Statements and operators

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

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

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

CS212 Midterm. 1. Read the following code fragments and answer the questions.

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

Lab 11. A sample of the class is:

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

AP COMPUTER SCIENCE A

COMP 202 Java in one week

Chapter 4: Loops and Files

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

Controls Structure for Repetition

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

H212 Introduction to Software Systems Honors

Chapter 6 Lab Classes and Objects

CS 101 Exam 2 Spring Id Name

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Chapter 4: Loops and Files

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

Tutorial 06. Conditional statement: if then, if else, switch

CSE 1223: Exam II Autumn 2016

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

Algorithms and Conditionals

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

Java Coding 3. Over & over again!

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

COMP 202. Java in one week

CS1083 Week 2: Arrays, ArrayList

Handout 4 Conditionals. Boolean Expressions.

BBM 102 Introduction to Programming II Spring Exceptions

Loops. CSE 114, Computer Science 1 Stony Brook University

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Text User Interfaces. Keyboard IO plus

CS 211: Existing Classes in the Java Library

Chapter 6 Lab Classes and Objects

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Midterm Exam 2 CS 455, Spring 2013

BSc. (Hons.) Software Engineering. Examinations for / Semester 2

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

CSE 142 Sample Midterm Exam #3

Selection Statements and operators

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Questions Answer Key Questions Answer Key Questions Answer Key

CH. 2 OBJECT-ORIENTED PROGRAMMING

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

COMP-202 Unit 9: Exceptions

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

CSCI 355 LAB #2 Spring 2004

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

CS 101 Spring 2007 Midterm 2 Name: ID:

1. An operation in which an overall value is computed incrementally, often using a loop.

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Midterm Examination (MTA)

CONDITIONAL EXECUTION

CS111: PROGRAMMING LANGUAGE II

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

Practice Midterm 1. Problem Points Score TOTAL 50

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter 12 Exception Handling

COMP102: Test July, 2006

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Problem Grade Total

CIS November 14, 2017

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017

Transcription:

CSC 1051 Algorithms and Data Structures I Final Examination December 18, 2015 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided. Please be legible. If you make a mistake or need more space, use backs of pages - clearly indicate where the answer can be found. Good luck and best wishes for the holidays!. /.\./..\. /.oxo.\./.*..x.\. /.oo..oo.\./.oxo..***.\. /.*.oo..*.oo.\.

1. ( / 10) What gets printed? Please show output as it will appear, or indicate NO OUTPUT, or show some of the output followed by INFINITE LOOP. int a = 5; Output: do System.out.println(a); a--; while (a < 5) Output: for (int a = 4; a <= 6; a++) System.out.println(a * 2); String[] notes = "do", "re", "mi"; for (String s1: notes) for (String s2: notes) System.out.println(s1 + s2); Output: int a = 5; while (a < 5) System.out.println(a); a--; Output:

2. ( / 10) a) Suppose you implemented a graphical object representing an emoji using an instance variable happiness to control the smile/frown, using an if/else statement, as follows: if (happiness) else page.drawarc (x+15, y+30, 20, 10, 180, 180); page.drawarc (x+15, y+30, 20, 10, 0, 180); Rewrite the above code using the conditional operator. (i.e., in a single line of code, without using the if/else), b) Suppose you would like to generate a random inspirational message. You start by creating an array containing a list of possible messages: String[] mycliche = "Life is beautiful", "Keep calm and carry on", "Live, laugh, love", "Today is the first day of the rest of your life" ; // add your own, if you feel like it! Write a code fragment that uses a a randomly generated index into this array to print one of the above messages. Your choice whether to use an object from the Random class or the random() method from the Math class. Be sure your code works fine if you add more messages to the array (for example, if you add a fifth message in the array above).

3. ( / 10) Consider the following program: import java.util.scanner; import java.io.*; public class FileOutputFinalF15 public static void main(string[] args) throws IOException Scanner infile; PrintWriter outfile; infile = new Scanner (new File("data-in.txt")); outfile = new PrintWriter("data-out.txt"); while (infile.hasnext()) String token = infile.next(); outfile.print(token.length() + " "); outfile.close(); data-out.txt data-in.txt Down came the rain and washed the spider out. a) Suppose the file data-in.txt is used as the input file. Show the contents of the file data-out.txt after execution of the program. b) List two examples of situations that could cause IOExceptions in the above code. c) Suppose you want to catch and handle the IOExceptions using the following catch clause: catch (IOException e) System.out.println("Problem with file IO. Running interactively."); infile = new Scanner(System.in); outfile = new PrintWriter(System.out); (i.e., keep running, but issue a warning and do interactive I/O instead) Show how to incorporate this in the above program: 1) Show the statements in the above code that need to be included in the try block 2) mark the position where you would insert the catch code.

4. ( / 10) Consider the following program: import java.util.scanner; import java.io.*; public class FinalF15 public static void main(string[] args) throws IOException Scanner infile = new Scanner (new File("data.txt")); String[][] table = new String[3][4]; String[] labelthis = new String[4]; String[] labelthat = new String[3]; String line = infile.nextline(); Scanner scan = new Scanner(line); scan.usedelimiter(","); scan.next(); // skips something for (int i = 0; i < table[0].length; i++) labelthis[i] = scan.next(); for (int i = 0; i < table.length; i++) line = infile.nextline(); scan = new Scanner(line); scan.usedelimiter(","); labelthat[i] = scan.next(); for (int j = 0; j < table[i].length; j++) table[i][j] = scan.next(); data.txt ignore this,this is good,this is bad,this is ok,this is excellent My Documents,apple,orange,asian pear,avocado My Downloads,eenie,meenie,miney,mo My Pictures,Betty Ann,Socrates,Vijay,Olivia (continued on next page)

(continued from previous page) // printing array contents: System.out.println("\n\nlabelThis"); for (String x: labelthis) System.out.print(x + " "); System.out.println("\n\nlabelThat"); for (String x: labelthat) System.out.print(x + "&&&"); System.out.println("\n\ntable"); for (int i = 0; i < table.length; i++) for (int j = 0; j < table[i].length; j++) System.out.print(table[i][j] + "$"); System.out.println("*****"); System.out.println("Bye Bye"); Show the output produced using the input file data.txt as given on the previous page. Output

5. ( / 10) Write a Java method palindrome() with one parameter, a String that returns a boolean true if the String is a palindrome (i.e., it reads the same forward and backward) and false if it is not a palindrome. For example, palindrome("anna") should return true, whereas palindrome("mamamia") should return false. Note that the method should not print anything.

6. ( / 10). Trace through the following code and show what gets printed. int[] a = 100, 200, 300, 400; int[] b = 1000, 2000, 3000, 4000; int[] c = b; for (int i=0; i<a.length; i++) b[i] = a[i]; Output: a[1] = 44; b[2] = 55; c[3] = 66; for (int x: a) System.out.print(x + " "); System.out.println(); for (int x: b) System.out.print(x + " "); System.out.println(); for (int x: c) System.out.print(x + " "); System.out.println(); (c) Suppose an int array ratings contains values in the range 0-3. Write a code fragment that creates an array count that contains the frequency of occurrences of 0 s, 1 s, 2 s and 3 s in the ratings array, and then prints this information. Example: if the array ratings has these contents: ratings 0 1 2 3 4 5 6 2 3 2 1 0 2 2 Your code should create the array count with the following contents: count 0 1 2 3 1 1 4 1 and the output would be: Count for 0: 1 Count for 1: 1 Count for 2: 4 Count for 3: 1 Write your code fragment below. Assume the array ratings is already initialized to some values. Your code should work for any size array.

7. ( / 10) We studied the problem of repeatedly obtaining input and performing a calculation, for example, computing the circumference of a circle given its radius, using the following algorithm: Variables: radius, circ Rewrite this algorithm, modifying it so that it uses a while structure to repeat the processing of each input in two different ways. a) Keep computing circumferences and ask each time whether to keep going. Variables: Algorithm: Algorithm: input radius circ = 2 * radius* PI print circ b) Keep computing circumferences until user inputs -1 for the radius (sentinel value) Variables: Algorithm: c) Compute the circumference of 5 circles (exact count). Variables: Algorithm:

8. ( / 10) Fill in code for an Employee class, following guidelines in comments. public class Employee // instance variables String name; String position; double hourly; // hourly wages int yearhired // constructor: Construct object with w, x, y, and z as // name, position, hourly pay rate, and // year hired, respectively. // tostring(): Returns a String corresponding to object. // getyearhired(): Accessor for yearhired // wages(): Given the number of hours worked (a value of type double, // returns the wages of this employee, calculated based on // hourly rate, for up to 40 hours and 1.5 overtime of // hourly rate for hours over 40.

9. ( / 10) Using the Employee class from the previous question: a) Draw a UML diagram for the Employee class. b) Write client code that uses the Employee class: Instantiate an Employee object with name Lucia Rodriguez, with position software engineer, hourly rate $42.50, hired in 2013. Assign it to a variable named coderboss c) Write client code that uses the Employee class: Suppose you have three Employee objects e1, e2 and e3 and that e2 worked 44.5 hours last week, whereas e1 and e3 both worked 40 hours. Write some client code to calculate and print: 1) the wages for each employee and 2) the average of their wages. (Note: it is NOT necessary to format as currency). d) Write client code that uses the Employee class: Suppose you have two Employee objects e1, e2. Write some client code that uses the getyearhired() and tostring() methods of the Employee class to print the information for the Employee who has been with the company the longest (i.e., hired earlier).

10. ( / 10) Suppose you look up a class in the Java API and find something that looks like the following (NOTE: this is a made-up class.): java.exam Class Mystery java.lang.object java.exam.mystery public class Mystery extends Object This is a made-up class. It does not matter what it actually does, I am just trying to see if you know how to use it. Constructor Summary Mystery(double x) Creates a new Mystery object. Method Summary boolean decider(int x, String y) Mystery method 1. void updator(double x) Mystery method 2. a) What import statement do you include in your program in order to use this class? b) Write some code to declare variables for two objects of this class, named thing1 and thing2 (use any values of the appropriate type in the constructor). c) Consider the following client code: Valid Java? Yes No thing1.updator(5.3); thing1.updator(double x); Mystery.updator(7.2); thing2.decider(int x, String y); System.out.println("answer= " + thing2.decider(5, "d")); System.out.println("answer= " + thing2.updator(7.2)); if (thing2.updator(0) == 2.0) System.out.println("ok"); if (thing2.decider(5, "d")) System.out.println("ok");