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

Similar documents
Loops. CSE 114, Computer Science 1 Stony Brook University

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Chapter 4: Control Structures I

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Arrays. Eng. Mohammed Abdualal

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Course Outline. Introduction to java

CSE 20 Intro to Computing I. Coding

Chapter 2: Basic Elements of Java

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

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Building Java Programs

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

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

Question: Total Points: Score:

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

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

Over and Over Again GEEN163

St. Edmund Preparatory High School Brooklyn, NY

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

Repetition, Looping. While Loop

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

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

Darrell Bethea May 25, 2011

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

More on control structures

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Flow of Control: Loops. Chapter 4

Loops and Expression Types

The action of the program depends on the input We can create this program using an if statement

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Chapter 3. Selections

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

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

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Controls Structure for Repetition

CSE 114 Computer Science I

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Full file at

Last Class. More on loops break continue A bit on arrays

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Intro to Programming in Java Practice Midterm

REPETITION CONTROL STRUCTURE LOGO

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Introduction to Java & Fundamental Data Types

Lecture 1 Java SE Programming

CPSC 219 Extra review and solutions

Lecture 3 Tao Wang 1

Handout 4 Conditionals. Boolean Expressions.

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Date: Dr. Essam Halim

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Problem Solving With Loops

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

CS 101 Spring 2007 Midterm 2 Name: ID:

TESTING AND DEBUGGING

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

Computer Programming I - Unit 5 Lecture page 1 of 14

Programming in the Small II: Control

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

CS Week 5. Jim Williams, PhD

n Group of statements that are executed repeatedly while some condition remains true

Give one example where you might wish to use a three dimensional array

Final Exam. COMP Summer I June 26, points

1 Short Answer (10 Points Each)

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

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

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

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

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

Repetition with for loops

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

AP Computer Science Unit 1. Programs

Expressions & Flow Control

COMPUTER APPLICATIONS

Selections. CSE 114, Computer Science 1 Stony Brook University

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Lesson 7 Part 2 Flags

ECE 122. Engineering Problem Solving with Java

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Full file at

Midterm Examination (MTA)

CSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

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

Chapter 8 Multi-Dimensional Arrays

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

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

Transcription:

CSE 20 SAMPLE FINAL Version A Time: 180 minutes Name The following precedence table is provided for your use: Precedence of Operators ( ) - (unary),!, ++, -- *, /, % +, - (binary) <, <=, >, >= = =,!= && =, +=, -=, *=, /=, %= Otherwise left to right Total Possible Points = 1

1. Match the following statements (i-x) to the outputs (A-J) they produce. Note: that there are no duplicate answers ( G has the value 71) int i=3, j=2; double d=5.0; char c = 'G'; // G is 71 i. System.out.println("OUTPUT is " + ((int)c + j) ); ii.system.out.println("output is " + ((char)c - j) ); iii.system.out.println("output is " + (char)(c - j )); iv.system.out.println("output is " + (char)c + j ); v.system.out.println("output is " + (char)(c - d) ); vi.system.out.println("output is " + ((char)c - d) ); vii.system.out.println("output is " + (double)c + j ); viii.system.out.println("output is " + (double)c / j * d); ix.system.out.println("output is " + c / (char)i * d); x.system.out.println("output is " + c / j * (int)d); a) OUTPUT is B b) OUTPUT is E c) OUTPUT is G2 d) OUTPUT is 66.0 e) OUTPUT is 69 f) OUTPUT is 71.02 g) OUTPUT is 73 h) OUTPUT is 115.0 i) OUTPUT is 175 j) OUTPUT is 177.5 2

2. (a) In the following Java code, there are 4 syntax errors and 3 logic errors. Identify each error and explain how to correct it. For example, if a statement was missing a semicolon, circle where the semicolon would be and label the circle "missing ;" or something similar. public class TestIf { public static void main(string[] args) { Int i = 1; if (i <= 0); { System.out.println(i + is negative ); else ( i == 0) { System.out.println("i" + is zero ); else if (i >= 0) System.out.println(i + is positive ); else System.out.println( Should never get here! ); System.out.println( Done ); (b) State the 4 errors in the following code. Assume no additional declarations. Circle the errors and describe then with words. For example, if a ; was missing at the end of a statement, you would circle where the ; should be and write missing ;. Some errors here are syntax errors, while some are semantic (logic) errors. The code is SUPPOSED to allocate an array, and then fill each location with the square of the index number. int MAX = 20; short i = MAX; int[] values = int[i]; for (i=1; i <= MAX; i--){ int[max] = i*i; 3

3. The following code is supposed to write out the numbers from 5 to 1. It has two flaws. public class Loop { public static void main(string[] args) { int i; for(i=10; i>=0; i--) if (i / 2 == 0) System.out.print(i/2 +, ); // end of main // end of Loop a) What is the output of the code as it is written? b) Correct the code so that it works as intended. Write the corrected version of the main function in the box below. There is more than one way to fix the problems. 4

4. For each value of n below, give the output from the execution of the following program segment a. Consider While loop int i = 1, n; n = keyboard.nextint(); while ( i < n){ System.out.print(i+" "); i *= i; System.out.println("END."); i. n = 4 ii. n = -6 b. Consider Do-While loop int i = 1, n; n = keyboard.nextint(); do { System.out.print(i+ ); i *= i; while ( i < n); System.out.println( END. ); i. n = 4 ii. n = -6 c. Consider the If statements int i = 3, n; n = keyboard.nextint(); if (i == n) System.out.println( red ); else System.out.println( green ); if (i < n) System.out.println( blue ); else System.out.println( purple ); i. n = 3 ii. n = 99 5

5. Consider the following code: Write code that does the same as the following but uses a while loop or a dowhile loop instead of a for loop. int i; int sum = 0; for (i = 10; i >= 0; i--) { if ( i % 2 == 0) sum = sum + i*i; System.out.println(sum); a. Write code that does the same using a While loop b. Write code that does the same using a Do-While loop 6

6. a) Consider the code below on the left hand side. The right hand size represents the array created. Fill-in the values of each entry after the code is executed. The first two lines are filled in as an example. int [] n = new int[6]; 0 0 0 0 0 0 int i = 3; n[0] = 5; 5 0 0 0 0 0 n[i] += 5; n[2+i] = n[0] + 1; n[4-i] += 1; n[1] = n[0] + n[1]+ n[2] + n[3] + n[4]; n[1+2] = 1+i; n[n.length-1] = n[0]; n[n.length-i] = n.length-2; n[i*2-1] = n[2] * n[2]; b) Consider the code below on the left hand side. The right hand size is the output. If the code does not output anything then leave it blank or write BLANK. First two answers are given as an example. int i = 0; BLANK System.out.println(i); 0 System.out.println(i+2); i *= 2; System.out.println( Num is + i); System.out.println(i++); i += 2.0; System.out.println(++i); System.out.println(i % 2 ); System.out.println(i += 4); System.out.println(i = i * 4); System.out.println (i / 3.0); 7

7. a) Suppose a program reads a positive integer from the keyboard into the integer variable array_length. Then it will allocate space for an array of array_length floats. Use any of the following variables declared already in the rest of the program. You should not use any additional variables. int array_length; Scanner input = new Scanner(System.in); double[] d_arr; int[] i_arr; float[] f_arr; // Start here b) Declare and allocate an array of 1000 integers. Write a loop which fills the array locations with their reverse indices. For example, slot 0 of the array should hold the value 999, and slot 998 of the array should hold the value 1. c) Assume you have an array of doubles which has already been filled with data read in from the user. Assume this array is called scores. We are not sure how long the array is, but we know it is completely full of data. Write code which calculates the average of the POSITIVE values in the scores array. If there are no positive numbers then print out Array contains no positive numbers, avoid any run-time errors. 8

8. a) Assume you have an array of length 100 which has already been filled with data read in from the user. Assume this array is called values, and is an array of ints. Write code which finds the duplicates of the values in the values array, and reports both the duplicate value and the two indices of the array in which it was found. (ie. Found value 2 at indices 3 and 4) b) Write a while loop that repeatedly prompts the user for a number of type int, reads an integer number in from the keyboard. The loop should continue until the user enters value 0, and it should count how many positive and negative numbers the user entered. It should also keep track of positive_sum and negative_sum. After the loop, print out positive_average and negative_average. 9

9. Given the program shown below indicate the output in the box below. This is one continuous program. Follow each step carefully, you may want to draw the array created with values in each entry to help you answer this question. (partial credit) public class ProblemNine { public static void main(string[] args) { int LITTLE = 5, MEDIUM = 10, BIG = 20; int i,j, n = 9,temp; int[] num= new int[big]; num[0]=9; num[1]=3; num[2]=4; num[3]=8; num[4]=2; num[5]=1; num[6]=5; num[7]=6; num[8]=7; num[9]=10; num[little] = 1; i = MEDIUM; while (i < BIG) { num[i] = 2*num[i % MEDIUM]; i++; for (j=0;j<big;j++) { if (j % LITTLE == 0) System.out.println(); System.out.print(num[j]+", "); Output 10

10. Find and fix the bug(s) in the following code. For each line, indicate the line number, whether it is (C)ompiletime, (R)un-time, or (L)ogical, it can have multiple errors so circle all relevant ones. In the last entry, give the corrected code or SAME if there are no errors and changes needed. /** * Calculate the sum of all the elements Squared from the array * */ 0 public static void main (String [] args) { 1 double[] inp = {1.0, 2.0. 3,.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 2 int sum; 3 for (int i = 1; i <= inp.length-1; ++i ) { 4 Sum = inp[i] + inp[i*i]; 5 6 System.out.println( Sum of Squares is + sum); 7 Line # Error Type Corrected Code 0 C L R 1 C L R 2 C L R 3 C L R 4 C L R 5 C L R 6 C L R 7 C L R 11

11. Count the number of iterations for each of the loops below, assume max = 20. b. for (int i = 0; i < max; i++) c. for (int i = 0; i <= max; i++) d. for (int i = max-1; i > 0; i--) e. for (int i = max-1; i < 0; i--) f. for (int i = 1; i <= max; i++) if (i == max/2) break; g. for (int i = 1; i <= max; i++) if (i == max/2) continue; h. for (int i = 0; i <= max; i += 4) i. for (int i = 0; i <= max; i *= 4) j. for (int i = 1; i <= max; i += 4) k. for (int i = 1; i <= max; i *= 4) 12

12. Answer if each of the following statements are valid or invalid. Invalid means Eclipse will flag it as an error. You can assume that after an invalid statement(s) the program keeps running as if they are correct for this problem. int i = 0; // Valid Invalid int max = 10; int count; int[] arr = new int[max-1]; // Valid Invalid for (int i = 0; i < max; i++); // Valid Invalid System.out.println(arr[i] = i); // Valid Invalid while (i < max) { i += 2; // Valid Invalid if (i % 2 == 0) { int j = 1; for (j = max-2; j >= 0; j--) // Valid Invalid System.out.println(arr[j]); count++; // Valid Invalid else { int count = j; // Valid Invalid for (int j = max-2; j >= 0; j--) // Valid Invalid System.out.println(arr[j]); count--; // Valid Invalid 13