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

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

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

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

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

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

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

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

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

Using Java Classes Fall 2018 Margaret Reid-Miller

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

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

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

Object-Oriented Programming

"Hello" " This " + "is String " + "concatenation"

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Midterms Save the Dates!

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

AP CS Unit 3: Control Structures Notes

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Midterm Examination

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Preview from Notesale.co.uk Page 2 of 79

Java By Abstraction - Test-B (Chapters 1-6)

Chapter 4 Mathematical Functions, Characters, and Strings

More non-primitive types Lesson 06

CHAPTER 6 MOST COMMONLY USED LIBRARIES

Introduction to the Java Basics: Control Flow Statements

CSE 114 Midterm 1. Please leave one seat between yourself and each of your neighbors.

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

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Selection and Repetition Revisited

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

Methods with Parameters and Return Values

CS111: PROGRAMMING LANGUAGE II

Creating Strings. String Length

Chapter 3. Selections

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

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

We now start exploring some key elements of the Java programming language and ways of performing I/O

Computer Science 145 Midterm 1 Fall 2016

Faculty of Science COMP-202A - Foundations of Computing (Fall 2015) - All Sections Midterm Examination

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Lab 14 & 15: String Handling

Using Free Functions

Mr. Monroe s Guide to Mastering Java Syntax

Introduction to Computer Science Unit 2. Notes

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

A variable is a name for a location in memory A variable must be declared

Java: Learning to Program with Robots

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

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

Pace University. Fundamental Concepts of CS121 1

Chapter 3: Program Statements

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Term 1 Unit 1 Week 1 Worksheet: Output Solution

CS1004: Intro to CS in Java, Spring 2005

Chapter 2: Objects and Primitive Data

Java I/O and Control Structures

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Computer Programming, I. Laboratory Manual. Final Exam Solution

Flow of Control. Chapter 3

More on variables and methods

Midterms Save the Dates!

York University Fall 2001 / Test #1 Department of Computer Science

Selection and Repetition

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

Methods and Data (Savitch, Chapter 5)

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Full file at

Using Classes and Objects

QUIZ: What value is stored in a after this

CSC 1051 Data Structures and Algorithms I

STUDENT LESSON A12 Iterations

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

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

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CS110: PROGRAMMING LANGUAGE I

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Practice Midterm 1 Answer Key

Computer Science 145

Important Java terminology

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

Chapter 1: Introduction to Computers, Programs, and Java

Transcription:

CSC 1051 Algorithms and Data Structures I Midterm Examination March 1, 2018 Name: KEY A Question Value Score 1 20 2 20 3 20 4 20 5 20 TOTAL 100 Please answer questions in the spaces provided. If you make a mistake or for some other reason need more space, please use the back of pages and clearly indicate where the answer can be found. Good luck!

1. ( / 20) What gets printed? Please show output as it will appear, or indicate NO OUTPUT. If there is an infinite loop, be sure to show some lines of the output followed by INFINITE LOOP. int a = 1; while (a < 7) if ((a%2)==0) System.out.println(a); a++; Output 2 4 6 int a = 1; while (a <= 20) System.out.println(a); a += 5; 1 6 11 16 int a = 8; do System.out.println(a); a--; 8 while (a < 5); size = 14; do System.out.print(size + " => "); int category = size / 4; switch(category) case 3: System.out.print ("X"); // note: no break here case 2: System.out.println ("L"); break; case 1: System.out.println ("M"); break; case 0: System.out.print ("S"); size = size - 2; while (size >= 2); for (int a = 3; a > 8; a++) System.out.print(a); 14 => XL 12 => XL 10 => L 8 => L 6 => M 4 => M 2 => S NO output

2. [ /20] What output is produced by the following program? public class OneMoreTime public static void main ([] args) int x = 1, a = 2, b = 3, c = 4; System.out.println("Howdy, here are some numbers: "); System.out.println("x = " + x + " a = " + a + " b = " + b + " c = " + c); System.out.println ("I promise\nthis is the \n\"last time\" "); System.out.print ("you have to do this "); System.out.println ("so\nplease count the \"\\\"s carefully!"); if (a>0) if (b<0) x = x + 5; if (a>5) x = x + 4; x = x + 3; x = x + 2; System.out.println(); System.out.println("Here are the numbers after the if/: "); System.out.println("x = " + x + " a = " + a + " b = " + b + " c = " + c); System.out.println(); c = a; a = b; b = 100; System.out.println("Here are the final values: "); System.out.println("x = " + x + " a = " + a + " b = " + b + " c = " + c); Howdy, here are some numbers: x = 1 a = 2 b = 3 c = 4 I promise this is the "last time" you have to do this so please count the "\"s carefully! Here are the numbers after the if/: x = 4 a = 2 b = 3 c = 4 Here are the final values: x = 4 a = 3 b = 100 c = 2

3. [ /20] Short answer questions. a) For each of the following expressions, indicate the order in which the operations are performed by writing a number beneath each operator. a / b d * e + f 1 3 2 4 a / (b + c) / e f 2 1 3 4 b) The code below is supposed to print the numbers from 1 to 10, but it has an error. int count = 1; while (count <= 10); System.out.println (count); count++; i) Describe the error and how to correct it? Incorrect semicolon after while ii) If not corrected, what, if anything gets printed? Nothing -- infinite loop iii) Is this a syntax, runtime, or logical error? Logic c) Given a Random object named gen, what range of values are produced by the following expressions? gen.nextint(4) 0 to 3 gen.nextint(20) + 100 100 to 119 gen.nextint(4) 15-15 to -12 d) Fill in the blanks word = "plaintalkin"; int num = word.length(); num _11_ word.charat(1) l word.charat(4)_n_ word.sub(1)_laintalkin_ for (int n = 0; n < word.length(); n +=4) System.out.println(word.charAt(n) + ":"); p: n: k: e) Suppose the variable word is already initialized (similar to part (d), but not necessarily with the same value). Write a Java code fragment to prints word, BACKWARDS. for (int count = word.length() - 1; count>=0; count--) System.out.print(message.charAt(count));

4. [ /20] Short answer questions. a) Given the following declarations: int iresult, num1 = 7, num2 = 3; double fresult, val1 = 9.0; boolean status, part1 = false; What result is assigned by each of the following assignment statements? Source code Result stored fresult = (num1 + 2)/ 2; iresult = num1 % num2; fresult = val1 / 2; fresult = (double) num1 / 2; status = part1 && (num1 > num2); status = part1 (num1 > num2); 4.0 1 4.5 3.5 false true BE SURE TO INDICATE RESULTS OF TYPE double BY USING A DECIMAL POINT For example: 3.0 (for double) vs. 3 (for int). b) Consider the following code fragments to compute the GPA of a student: Version A Version B int qp = 35; int credits = 10; double gpa = (double) qp / credits; int qp = 35; int credits = 10; double gpa = (double)(qp / credits); Which version will compute the correct value for the GPA? A What is the value calculated by the other one? 3.0 c) Suppose your code has already calculated the value of num as the number of dimes to give out as change. Rewrite the following output statement using the conditional operator, so that it prints 1 Dime instead of 1 Dimes in cases where num = 1. System.out.println ("Your change is " + num + "Dimes"); System.out.println ("Your change is " + num + "Dime" + (num!= 1? "s" : ""));

5. [ / 20] Construct an algorithm that inputs a number num and then prints Hello that many times. After the Hello s are printed, print a goodbye message. Example: If num (i.e., the input) is 5, the algorithm should print something like this: Hello Hello Hello Hello Hello Goodbye Directions: Write your algorithm by rearranging and structuring elements chosen from the list below, using indentation to show structure. Do not use anything and note that not all of these are needed, but you may use one of them more than once, if necessary. input num input count count = 1 count = 0 count = count + 1 num = num + 1 if (count < num) while (count <= num) while (count!= 5) while (count <= 5) print Hello print num print Goodbye input num count = 1 while (count <=num) print Hello count = count + 1 print Goodbye NOTE: other correct solutions are possible.

REFERENCE MATERIAL Random class double nextdouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 Math class int nextint(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. static double abs(double a) Returns the absolute value of a double value. static double cos(double a) Returns the trigonometric cosine of an angle. static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument. static double random() Returns a double value greater than or equal to 0.0 and less than 1.0. static long round(double a) Returns the closest long to the argument. static double sin(double a) Returns the trigonometric sine of an angle. static double sqrt(double a) Returns the correctly rounded positive square root of a double value. class char int int boolean int boolean charat(int index) Returns the char value at the specified index. compareto( another) Compares two strings lexicographically. indexof(int ch) Returns the index within this string of the first occurrence of the specified character. isempty() Returns true if, and only if, length() is 0. length() Returns the length of this string. replace(char oldchar, char newchar) Returns a new string resulting from replacing all occurrences of oldchar in this string with newchar. startswith( prefix) Tests if this string starts with the specified prefix. substring(int beginindex) Returns a new string that is a substring of this string. substring(int beginindex, int endindex) Returns a new string that is a substring of this string. tolowercase() Converts all of the characters in this to lower case using the rules of the default locale. trim() Returns a copy of the string, with leading and trailing whitespace omitted.

CSC 1051 Algorithms and Data Structures I Midterm Examination March 1, 2018 Name: KEY B Question Value Score 1 20 2 20 3 20 4 20 5 20 TOTAL 100 Please answer questions in the spaces provided. If you make a mistake or for some other reason need more space, please use the back of pages and clearly indicate where the answer can be found. Good luck!

1. ( / 20) What gets printed? Please show output as it will appear, or indicate NO OUTPUT. If there is an infinite loop, be sure to show some lines of the output followed by INFINITE LOOP. int a = 2; while (a < 4) System.out.println(a + 3); a++; Output 5 6 int a = 1; while (a > 10) System.out.println(a); a = a + 15; int a = 6; NO OUTPUT do System.out.println(a); a--; 6 while (a < 5); for (int a = 3; a < 6; a++) System.out.print(a); int size = 10; do System.out.print(size + " => "); int category = size / 3; switch(category) case 0: System.out.print ("X"); // note: no break here case 1: System.out.println ("S"); break; case 2: System.out.println ("M"); break; case 3: System.out.println ("L"); size = size - 2; while (size >= 0); 3 4 5 10 => L 8 => M 6 => M 4 => S 2 => XS 0 => XS

2. [ /20] What output is produced by the following program? public class OneMoreTime public static void main ([] args) int x = 10, a = 20, b = 30, c = 40; System.out.println("Howdy, here are some numbers: "); System.out.println("x = " + x + " a = " + a + " b = " + b + " c = " + c); System.out.println ("I promise\nthis is the \n\"last time\" "); System.out.print ("you have to do this "); System.out.println ("so\nplease count the \"\\\"s carefully!"); if (a>0) if (b<0) x = x + 5; if (a>5) x = x + 4; x = x + 3; x = x + 2; System.out.println(); System.out.println("Here are the numbers after the if/: "); System.out.println("x = " + x + " a = " + a + " b = " + b + " c = " + c); System.out.println(); c = a; a = b; b = 100; System.out.println("Here are the final values: "); System.out.println("x = " + x + " a = " + a + " b = " + b + " c = " + c); Howdy, here are some numbers: x = 10 a = 20 b = 30 c = 40 I promise this is the "last time" you have to do this so please count the "\"s carefully! Here are the numbers after the if/: x = 14 a = 20 b = 30 c = 40 Here are the final values:

3. [ /20] Short answer questions. a) For each of the following expressions, indicate the order in which the operations are performed by writing a number beneath each operator. a / (b d) * e + f 2 1 3 4 a / b + c / e f 1 3 2 4 b) The code below is supposed to print the numbers from 1 to 10, but it has an error. int count = 1; while (count <= 10) System.out.println (count); count++; i) Describe the error and how to correct it? _missing braces in body of while ii) If not corrected, what, if anything gets printed? 1, over and over -- infinite loop iii) Is this a syntax, runtime, or logical error? Logic c) Given a Random object named gen, what range of values are produced by the following expressions? gen.nextint(8) 0 to 7 gen.nextint(50) + 10 10 to 59 gen.nextint(8) 10-10 to -3 d) Fill in the blanks & show output word = "overdrive"; int num = word.length(); num _9_ word.charat(1) v word.charat(4)_d_ word.charat(num-1) _e_ for (int n = 0; n < word.length(); n++) if(word.charat(n)== e ) System.out.print(word.charAt(n) + "a"); eaea e) Suppose the variable word is already initialized (similar to part (d), but not necessarily with the same value). Write a Java code fragment to prints word, BACKWARDS. for (int count = word.length() - 1; count>=0; count--) System.out.print(message.charAt(count));

4. [ /20] Short answer questions. a) Given the following declarations: int iresult, num1 = 7, num2 = 3; double fresult, val1 = 9.0; boolean status, part1 = false; What result is assigned by each of the following assignment statements? Source code Result stored fresult = (num1 + 2)/ 2; iresult = num1 % num2; fresult = val1 / 2; fresult = (double) num1 / 2; status = part1 && (num1 > num2); status = part1 (num1 > num2); 4.0 1 4.5 3.5 false true BE SURE TO INDICATE RESULTS OF TYPE double BY USING A DECIMAL POINT For example: 3.0 (for double) vs. 3 (for int). b) Rewrite the following code (in the box) to use the conditional operator to compute the value of the variable outcome (instead of if/): int a = rand.nextint(100); int b = rand.nextint(100); int number = scan.nextint(); int outcome; if (number == a + b) outcome = 1; outcome = 0; int outcome = (number == a + b? 1: 0); c) Consider the following code fragments to compute the GPA of a student: Version A Version B int qp = 28; int credits = 10; double gpa = (double) (qp / credits); int qp = 28; int credits = 10; double gpa = (double) qp / credits; Which version will compute the correct value for the GPA? B What is the value calculated by the other one? 2.0

3. [ / 20] Construct an algorithm that inputs three integers n, m, and d. The algorithm should count up by increments of d, starting from n and stopping at m. After it is done printing the numbers, it should print a goodbye message. You can assume that the numbers entered will be positive integers with n<m, so you do NOT need to check for mistakes in the input. Example 1: If the inputs are 7, 25, 4, then the algorithm should print: 7 11 15 19 23 Goodbye Example 2: If the inputs are 12, 32, 5, then the algorithm should print: 12 17 22 27 32 Goodbye Directions: Write your algorithm by rearranging and structuring elements chosen from the list below, using indentation to show structure. Do not use anything and note that not all of these are needed, but you may use one of them more than once, if necessary. input n input m input d input n + d n = 0 m= 0 d = 0 n = n+ d n = m m = n d = d + 1 n = n +1 if (n > 0) if (n < m) while (n < m) while (n <= m) while (n > m) while (d > 0) while (d >= 0) while (d < m) print n print m print d print Goodbye input n input m input d while (n <= m) print n n = n + d print Goodbye NOTE: other correct solutions are possible.

REFERENCE MATERIAL Random class double nextdouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 Math class int nextint(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. static double abs(double a) Returns the absolute value of a double value. static double cos(double a) Returns the trigonometric cosine of an angle. static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument. static double random() Returns a double value greater than or equal to 0.0 and less than 1.0. static long round(double a) Returns the closest long to the argument. static double sin(double a) Returns the trigonometric sine of an angle. static double sqrt(double a) Returns the correctly rounded positive square root of a double value. class char int int boolean int boolean charat(int index) Returns the char value at the specified index. compareto( another) Compares two strings lexicographically. indexof(int ch) Returns the index within this string of the first occurrence of the specified character. isempty() Returns true if, and only if, length() is 0. length() Returns the length of this string. replace(char oldchar, char newchar) Returns a new string resulting from replacing all occurrences of oldchar in this string with newchar. startswith( prefix) Tests if this string starts with the specified prefix. substring(int beginindex) Returns a new string that is a substring of this string. substring(int beginindex, int endindex) Returns a new string that is a substring of this string. tolowercase() Converts all of the characters in this to lower case using the rules of the default locale. trim() Returns a copy of the string, with leading and trailing whitespace omitted.