CSE 1223: Exam II Autumn 2016

Similar documents
Question: Total Points: Score:

Controls Structure for Repetition

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

Question: Total Points: Score:

Place your name tag here

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

CS 101 Fall 2006 Midterm 1 Name: ID:

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

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

Loops. CSE 114, Computer Science 1 Stony Brook University

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

CS170 Introduction to Computer Science Midterm 2

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

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

You must bring your ID to the exam.

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Arrays. Eng. Mohammed Abdualal

Java Coding 3. Over & over again!

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

Practice Midterm 1. Problem Points Score TOTAL 50

Question: Total Points: Score:

This exam is open book. Each question is worth 3 points.

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Final Exam. COMP Summer I June 26, points

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

CS 101 Exam 2 Spring Id Name

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

Example Program. public class ComputeArea {

1 Short Answer (10 Points Each)

CS 101 Exam 1 Spring 200 Id Name

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

CSE Fall 2015 Section 002 Exam 2, Time: 80 mins

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CS 101 Fall 2006 Midterm 3 Name: ID:

1 Short Answer (10 Points Each)

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Assignment 2.4: Loops

Final Exam Practice. Partial credit will be awarded.

4. If the following Java statements are executed, what will be displayed?

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Full file at

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

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

Conditional Execution

Exam Percentage: / 55 = %

Question: Total Points: Score:

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

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

Midterm Examination (MTA)

NATIONAL UNIVERSITY OF SINGAPORE

CSE 114 Computer Science I

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Flow of Control. Chapter 3

CS111: PROGRAMMING LANGUAGE II

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

b. Suppose you enter input from the console, when you run the program. What is the output?

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

COS 126 Midterm 1 Written Exam Fall 2011

CS 101 Spring 2007 Midterm 2 Name: ID:

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

COMP 202 Java in one week

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

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Oct Decision Structures cont d

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CIS 110 Introduction to Computer Programming Summer 2016 Midterm. Recitation # (e.g., 201):

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Full file at

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm. Answer Key

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

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

COMP 202. Java in one week

CIS 110 Introduction to Computer Programming Summer 2018 Midterm. Recitation ROOM :

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Fundamentals of Programming Data Types & Methods

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Examination Questions Midterm 1

COMP 401 Spring 2013 Midterm 1

CIS Introduction to Computer Programming Spring Exam 1

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

STUDENT LESSON A12 Iterations

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Name CIS 201 Midterm II: Chapters 1-8

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Transcription:

CSE 1223: Exam II Autumn 2016 Name: Instructions: Do not open the exam before you are told to begin. This exam is closed book, closed notes. You may not use any calculators or any other kind of computing device on this exam. Direct all questions to the instructor and no one else You have 55 minutes to complete this exam. When time is called you must lay down your pencil and submit the exam. There are three parts to this exam containing questions worth a total of 100 points. Not all questions are worth the same number of points. You should scan the exam before starting. The space allotted for each questions should provide more than sufficient space to answer the question. If you find you are running out of space, you are probably on the wrong track. Part Score I /40 II /34 III /26 TOTAL /100 In accordance with The Ohio State University Code of Student Conduct, I certify that: I have received no aid on this exam from any other person I have not given anyone aid on this exam I shall NOT discuss the contents of this exam with anyone who has not already taken this exam Signed:

Part I Coding (40 points) 1. (10 points) Complete the skeleton below for the method named longest that takes an array of Strings as input and returns the longest String in the array to the calling program. For example, if the array has the values {"The", "Brown", "Fox" then the method will return the value "Brown". In the case of ties your code can return ANY String with the longest length. For example if the array has the values {"The", "Quick", "Brown", "Fox" you may return either "Brown" or "Quick". You may assume that the arrays passed to the method will have a length of at least 1. private static String longest(string[] array) { // your code goes here CSE 1223 EXAM II Page: 2 of 13

2. (15 points) Fill in the skeleton below for a program that prompts the user for an integer. The program should then display a triangle of the numbers as shown below. The program should continue prompting for input until the user enters a negative number. The program should end with a Goodbye! message. See below for an example of the program in action. Make sure your code produces the same output as that given in the transcript below. Please note that a correct solution to this problem will NOT make use of any arrays. See the last page of the exam for some Scanner methods you might find useful. Here is a sample transcript of how the program should work. Input typed by the user is indicated by bold text: Enter an integer: 3 3 33 333 Enter an integer: 2 2 22 Enter an integer: -1 Goodbye! import java.util.scanner; public class Exam2B { public static void main(string[] args) { Scanner in = new Scanner(System.in); // your code goes here CSE 1223 EXAM II Page: 3 of 13

3. (15 points) Fill in the skeleton below for a method named zeroodds that takes an array of integers as a parameter. The method should return a new array that has only the even values of the original array, with the odd values replaced by zeroes. For example, if the array arr1 = {2,7,-1,8,5, then the call zeroodds(arr1) should return the array {2,0,0,8,0. Note that your code should work for arrays of length zero and that a zero length array with the odds zeroed out is another zero length array. private static int[] zeroodds(int[] array) { CSE 1223 EXAM II Page: 4 of 13

Part II Multiple Choice (34 points) For each of the following questions, circle the single answer that BEST answers the question. (2 points each) 1. Consider the method header public static String foo(int i). Which of the following could be a correct method call? (Note: in all of the examples below i is of type int, and s is of type String). a. String foo(int i) b. String foo(i) c. s = foo(i) d. foo(string s) 2. For the loop given below, what is the final value of i when the loop is finished? int i = 1; int arr = {2, 3, 5; while (i < arr.length) { i = i + 1; a. 1 b. 2 c. 3 d. 4 e. You cannot tell just from the code segment provided f. The loop will never exit it is an infinite loop 3. Suppose you have a problem where you are trying to find the larger of two numbers. Which ONE of the following Java programming concepts would be the best fit to use to solve this problem? a. while loop b. if-else c. String length d. None of the above in the space below explain how you can choose the larger of two values without using any of the above concepts CSE 1223 EXAM II Page: 5 of 13

4. A Java program that is supposed to compute the average of an array of numbers tries to access a number at an index larger than the length of the array. This is an example of what kind of error: a. Runtime error b. Syntax error c. Array Too Small error d. None of the above 5. What value does the variable msg have at the end of this segment of code? int value = 31; String msg = ; if (value < 50) { msg = Under 50! ; else if (value > 30) { msg = Over 30! ; if (value < 25) { msg = Under 25! ; a. b. Under 25! c. Over 30! d. Under 50! e. You cannot tell from the code provided 6. For the loop given below, how many times will the boolean expression (i < 2) be evaluated? int i = 0; while (i < 2) { i = i + 1; a. 0 b. 1 c. 2 d. 3 e. 4 f. The loop will never exit it is an infinite loop 7. If you declare an array as char[] cs = { 4, 3, 2, 1, then the array element cs[3] contains what value? CSE 1223 EXAM II Page: 6 of 13

a. 1 b. 2 c. 3 d. 4 e. It is impossible to tell 8. What will be printed by the println statement at the end of this code segment? int i = 2; while ( i < 3 ) { String msg = "Value: "+i; i = i + 1; System.out.println(msg); a. Value: 0 b. Value: 1 c. Value: 2 d. Value: 3 e. None of the above the above segment has an error in it. In the space below give a brief explanation of what the error is: Each of the following questions may have multiple answers. Circle ALL responses that are answers to these questions (3 points each) 9. The method declared with the header public static double onethirdof(int i) returns: a. An integer value b. A double value c. The value of the parameter i divided by 3 d. No value e. You cannot tell from the information provided 10. If x, y and z are boolean variables, for what values of x, y and z does the expression ((!x && y) z) evaluate to true? (Mark ALL that apply): a. x=false, y=true, z=false CSE 1223 EXAM II Page: 7 of 13

b. x=true, y=false, z=false c. x=false, y=false, z=true d. x=true, y=true, z=true e. The expression can be true, but not for any of the assigned values listed above f. The expression cannot be true, no matter how you assign values to x, y and z 11. If i, j and k are int variables, for what values of i,j and k does the expression ((( i < j) (i < k)) && (j * k > 10)) evaluate to true? (Mark ALL that apply): a. i=8, j=9, k=1 b. i=4, j=3, k=5 c. i=8, j=10, k=1 d. i=11, j=11, k=9 e. None of the above assignments of i, j and k make the expression true 12. The following segment of code provides examples of which of the following (mark ALL that apply): i = 1024; if (i % 2 == 0) { i = i / 4; a. boolean expression b. integer expression c. looping d. branching e. None of the above 13. Which of the following method headers are examples of procedures? (Mark ALL that apply) a. private static void barfoo(int x, String s) b. private static String foobar() c. private static void foo(double x, char c) d. private static int bar(string x) e. None of the above are examples of functions 14. Which of the following facts about arrays and ArrayLists are false? (Mark ALL that apply) a. Items in an array must all have the same type b. The size of an array does not need to be known when it is created c. The size of an ArrayList must be known when it is created CSE 1223 EXAM II Page: 8 of 13

d. ArrayLists can never be empty e. None of the above are false CSE 1223 EXAM II Page: 9 of 13

Part III Short Answer (26 points) Consider the following code segments: public static void main(string[] args) {...(1) int enigma = 12;...(2) int mystery = 15;...(3) int riddle = 30;...(4) while(riddle>mystery) {...(5) int result = mymethod(mystery, riddle);...(6) System.out.println(result);...(7) riddle--;...(8) mystery++;...(9)...(10) System.out.println( DONE! );...(11)...(12) private static int mymethod(int enigma, int mystery) {...(13) int riddle = enigma * mystery;...(14) return riddle;...(15)...(16) 1. What are the line numbers of the lines that are in the scope of the variable result declared at line 6? (2 points) 2. What are the line numbers of the lines that are in the scope of the variable riddle declared at line 4? (2 points) 3. What are the line numbers of the lines that are in the scope of the variable riddle declared at line 14? ( 2points) 4. What are the line numbers of the lines that are in the scope of the variable enigma declared at line 2? ( 2points) CSE 1223 EXAM II Page: 10 of 13

Trace the effect of the call to the method enigma below by tracing the execution of the method body of enigma for this call. (6 points). Code State (Variable Values) i = 5 j = 6 k = enigma(i, j); i = j = k = Tracing table for enigma: Code private static int enigma(int x, int y){ State (Variable Values) x = y = y = x / 2; x = y = x = y + y*2; x = y = int j = x - y; x = y = j = return j; CSE 1223 EXAM II Page: 11 of 13

Consider the following code segment and answer the questions below. HINT: Doing question 9 first might help you with questions 6 8 if you are stuck. int i; int j; int[] arr = {6, 4, 7, 2, 1; for (i = arr.length - 1; i > 1; i--) { System.out.println("Hello World!"); j = arr[i]; 6. How many times will the above code print the sentence Hello World!? (3 points) 7. What is the value of the integer variable i after executing the for loop? (3 points) 8. What is the value of the integer variable j after executing the for loop? (3 points) 9. Convert the for loop above to a while loop. You need to ensure that your code prints the sentence Hello World! the same number of times and ends with the same value for the integer value for the variables i and j. (3 points) int i; int j; int[] arr = {6, 4, 7, 2, 1; CSE 1223 EXAM II Page: 12 of 13

Summary of Possibly Useful Methods String methods int s.length(); // returns the length of the String s char s.charat(int pos); // returns the character at position pos in the String s int s.indexof(string s2); // returns the index of the first occurrence of the String s2 in the String s String s.substring(int start,int end); // returns the substring of the String s starting at start and ending at end -1 boolean s.equals(string s2); // returns true if s is the same sequence of characters as s2, otherwise false Scanner methods String s.nextline(); // returns the next line from the Scanner s int s.nextint(); // returns the next integer from the Scanner s double s.nextdouble(); // returns the next double value from the Scanner s The rest of this page may be used as additional workspace as needed CSE 1223 EXAM II Page: 13 of 13