CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

Similar documents
CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

CS 455 Final Exam Spring 2017 [Bono] May 10, 2017

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

CS 455 Midterm Exam 1 Spring 2013 [Bono] Feb. 21, 2013

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

CS 455 Midterm Exam 1 Spring 2015 [Bono] Thursday, Feb. 19, 2015

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

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

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

CS 455 Midterm Exam 2 Spring 2017 [Bono] Tuesday, April 4, 2017

Midterm Exam 2 CS 455, Spring 2015

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

Midterm Exam 2 CS 455, Fall 2014

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

Midterm Exam 2 CS 455, Spring 2014

Midterm Exam 2 CS 455, Fall 2013

Midterm Exam 2 CS 455, Fall 2012

Midterm Exam 2 CS 455, Spring 2011

Midterm Exam 2 CS 455, Spring 2013

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS 455 MT 2 Sample Questions

CSCE 2014 Final Exam Spring Version A

CS 216 Exam 1 Fall SOLUTION

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

CSC 1052 Algorithms & Data Structures II: Linked Lists Revisited

EECE.2160: ECE Application Programming

CSCI 102 Fall 2010 Exam #1

Prelim One Solution. CS211 Fall Name. NetID

STUDENT LESSON A12 Iterations

Lecture 12 TREES II CS2110 Spring 2018

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

Section 1: True / False (2 points each, 30 pts total)

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Prelim CS410, Summer July 1998 Please note: This exam is closed book, closed note. Sit every-other seat. Put your answers in this exam. The or

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

PRACTICE MIDTERM EXAM #2

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

CSE 333 Midterm Exam July 24, Name UW ID#

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

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Total Score /1 /20 /41 /15 /23 Grader

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Initial Coding Guidelines

CS 111X - Fall Test 1

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

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

Problem Grade Total

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

In addition to the correct answer, you MUST show all your work in order to receive full credit.

LAB 4.1 Relational Operators and the if Statement

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CSE 333 Midterm Exam 7/25/16. Name UW ID#

Exam I KEY (corrected)

CS 101 Fall 2006 Midterm 1 Name: ID:

No Aids Allowed. Do not turn this page until you have received the signal to start.

MCS 360 Exam 2 11 November In order to get full credit, you need to show your work.

Computer Science. Section 1B

CSE413 Midterm. Question Max Points Total 100

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Java Console Input/Output The Basics

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

CS 1110 Final, December 17th, Question Points Score Total: 100

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Points off Total off Net Score. CS 314 Final Exam Fall 2016

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

UNIVERSITY REGULATIONS

Question Q1 Q2 Q3 Q4 Q5 Q6 Q7 Total

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Spring % of course grade

CS165 Practice Final Exam

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

FORM 2 (Please put your name and form # on the scantron!!!!)

CSE351 Winter 2016, Final Examination March 16, 2016

Transcription:

Name: USC NetID (e.g., ttrojan): CS 455 Final Exam Fall 2016 [Bono] December 13, 2016 There are 6 problems on the exam, with 67 points total available. There are 10 pages to the exam (5 pages double-sided), including this one; make sure you have all of them. There is also a one-page code handout that accompanies the exam. If you need additional space to write any answers, pages 8, 9, and 10 of the exam are left blank for that purpose. If you use these pages for answers you just need to direct us to look there. Do not detach any pages from this exam. Note: if you give multiple answers for a problem, we will only grade the first one. Avoid this issue by labeling and circling your final answers and crossing out any other answers you changed your mind about (though it s fine if you show your work). Put your name and USC username (a.k.a., NetID) at the top of the exam. Also, put your NetID at the top right of the front side of each page of the exam. Please read over the whole test before beginning. Good luck! 1

Problem 1 [10 points] [C++] Consider the following two C++ programs that use a Student class and answer the questions below each one (see code handout for documentation of Student class methods): // Program 1 void foo(student * s) { s->addscore(15); s = NULL; int main() { Student * joe = new Student( Joe ); foo(joe); cout << joe->getscore() << endl; Part A. In the space above, draw a box-and-pointer diagram (a.k.a., memory diagram) showing all object variables, objects, and their state as they have changed during the code sequence. This includes showing foo's parameters. Part B. What is the output of Program 1? // Program 2 void bar(student * & t) { t->addscore(25); t = NULL; int main() { Student * sal = new Student( Sal ); bar(sal); cout << sal->getscore(); Part C. In the space above, draw a box-and-pointer diagram for Program 2 like you did for Part A. Part D. What is the output of Program 2? 2

Problem 2 [8 points] USC NetID: [Java] Consider the following code to do linear search in an array (written in Java). It doesn t currently work in all cases that satisfy the precondition. // returns the location of target in nums, or -1 if not found. // PRE: nums is not null and nums has no duplicates public static int lookup(int[] nums, int target) { inti = 0; while (nums[i]!= target) { i++; if (nums[i] == target) { return i; else { return -1; Part A. Provide a test case where the code does not work correctly, and the corresponding result of that test case. (Show your answer under the headings below.) nums target result Part B. Provide a test case where the code does work correctly, and the corresponding result of that test case. (Show your answer under the headings below.) nums target result Part C. Fix the code above. Do not rewrite the whole method, but rather make minimal changes right into the code above, using arrows to show where your new code should be inserted, crossing out code that you would get rid of, etc. 3

Problem 3 [10 points] [Java] The binary search algorithm can be implemented recursively. Below is a partially complete recursive binary search implementation (consists of the two functions below). Complete the code below so it does what its comments say. The only missing parts are indicated by the boxes and underlined areas. (i.e., fill in the boxes and underlined areas provided.) // returns the location of target in words array, or -1 if not found. // PRE: words is in alphabetical order and has no duplicates public static int binsearch(string[] words, String target) { return binsearchr(words, target, 0, words.length-1); // returns the location of target in the part of words with indices [low,high] // or -1 if not found // PRE: words is in alphabetical order and has no duplicates public static int binsearchr(string[] words, String target, int low, int high) { if (low >high) { int mid = (low + high) / 2; if (target.equals(words[mid])) { else if (target.compareto(words[mid]) < 0) { // target < words[mid] return binsearchr(words, target,, ); else { // words[mid] < target return binsearchr(words, target,, ); 4

Problem 4 [4 pts] USC NetID: [Java] Recall that the Concord class computes the number of occurrences of all words from a document. Consider the two variations of our Concord class below. Version A is the one we did in lecture and lab: it takes a Scanner as a parameter to the adddata method. Version B s adddata, instead creates a local variable for the Scanner (and initializes it inside the method). To highlight the differences between the two they are shown in bold in the code below. (The other methods (not shown) of both classes are a constructor that creates an empty version of the concordance (i.e., with no words in it), and another method to print out the results.) Describe a scenario that illustrates an advantage of Version A over Version B. Limit your answer to one to two sentences. Version A. // Computes the number of occurrences of all words from a Scanner public class Concord { //... [other details of Concord left out] /** Add data from Scanner to concordance. @param in data to scan. "in" will be at the end of its data after this operation. */ public void adddata(scanner in) { while (in.hasnext()) { String word = in.next(); <... add this occurrence of word to the number of occurrences... > Version B. // Computes the number of occurrences of all words in the input. public class Concord { //... [other details of Concord left out] /** Add data from System.in to concordance (reads until end-of-file). */ public void adddata() { Scanner in = new Scanner(System.in); while (in.hasnext()) { String word = in.next(); <... add this occurrence of word to the number of occurrences... > 5

Problem 5 [20 pts] Write the Java function printreversedsentences which reverses the order of all the words in each sentence from a Scanner (it does not reverse the order of the sentences themselves), printing the reversed versions. For full credit you must use a Java Stack (see code handout for interface). A sentence has at least one word and is terminated with a period. To make the problem easier, we are not going to worry about proper capitalization. Here is an example of this function at work (pay attention to the placement of the periods): input text ahem. mary had a little lamb. its fleece was white as snow. the end. corresponding output text ahem. lamb little a had mary. snow as white was fleece its. end the. // prints out a version of the words from the scanner such that the order of // words is reversed in each sentence. Processes all the words from the // Scanner. All output will be on the one line (i.e., no newlines printed). // PRE: the scanner contains a sequence of zero or more sentences, each one // ending with a period. public static void printreversedsentences(scanner in) { 6

Problem 6 [15 points] USC NetID: [C++] A circular list, used in some list applications, is a special linked list representation where the last node in the list points back to the first node. Here are some examples of a circular list: Write the C++ function iscircularlist, which tells whether its parameter is a well-formed linked list or a circular list. Hint: in C++ you can use the equality operator (==) on pointers (analogous to comparing object references in Java for equality). The Node and ListType definitions are on the code handout. // returns true iff list is a circular list // PRE: list has at least one node and is either a well-formed linked list // or a circular list bool iscircularlist(node * list) 7

Extra space for answers or scratch work. (DO NOT detach this page.) If you put any of your answers here, please write a note on the question page directing us to look here. Also label any such answers here with the question number and part, and circle the answer. 8

USC NetID: Extra space for answers or scratch work (cont.) (DO NOT detach this page.) If you put any of your answers here, please write a note on the question page directing us to look here. Also label any such answers here with the question number and part, and circle the answer. 9

Extra space for answers or scratch work (cont.) (DO NOT detach this page.) If you put any of your answers here, please write a note on the question page directing us to look here. Also label any such answers here with the question number and part, and circle the answer. 10