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

Similar documents
CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

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

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

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

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

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

CONDITIONAL EXECUTION

COS 126 Midterm 1 Written Exam Fall 2014!

COS 126 General Computer Science Fall Exam 1

CIS Fall 2012 Midterm, 7 June 2012, Answer Key. Miscellaneous

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

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

COS 126 General Computer Science Fall Written Exam 1

COS 126 General Computer Science Spring Written Exam 1

COS 126 Midterm 1 Written Exam Spring 2015

Static methods. Not actually a valid Java static method. Fundamentals of Computer Science Keith Vertanen

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

CSCI 135 Final Fall Problem Points Score Total 100

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Variables and data types

COS 126 Midterm 1 Written Exam Fall 2011

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Arrays to the rescue!

Conditionals, Loops, and Style

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

2.1 Functions. f (x, y, z)

CIS Introduction to Computer Programming. 5 October 2012 Midterm

A Foundation for Programming

COS 126 Midterm 1 Written Exam, Fall 2009

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

COS 126 Midterm 1 Written Exam Fall 2012

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1)

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

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

COS 126 General Computer Science Fall Exam 1

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

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

ESC101 : Fundamental of Computing

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

CONDITIONAL EXECUTION

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

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

AP CS Unit 3: Control Structures Notes

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam Answer Key

COS 126 Midterm 1 Written Exam Fall 2013

Computação I. Exercises. Leonardo Vanneschi NOVA IMS, Universidade Nova de Lisboa. Leonardo Vanneschi Computação I NOVA IMS

M e t h o d s a n d P a r a m e t e r s

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Java Bytecode (binary file)

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

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

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

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Introduction to Computer Science Unit 2. Notes

CS170 (005): Introduction to Computer Science Exam 2

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam

CS 177 Spring 2009 Exam I

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

COS 126 Exam 2 Review Part 1

CIS 110 Introduction to Computer Programming. 13 February 2013 Make-Up Midterm Midterm

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

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

COS 126 General Computer Science Fall Exam 1

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

1.1 Your First Program

CSEN 202: Introduction to Computer Programming Spring term Final Exam

Prof. Navrati Saxena TA: Rochak Sachan

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

Inf1-OOP. Functions aka Static Methods. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

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

Condi(onals and Loops

JAVA OPERATORS GENERAL

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Comp 170 Test 1 SAMPLE June 8, 2000

2.1 Functions. x y. f (x, y, z) A Foundation for Programming. Functions (Static Methods)

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

Java Programming Language. 0 A history

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

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

Lab Assignment Three

University of Palestine. Mid Exam Total Grade: 100

( &% class MyClass { }

CS180. Exam 1 Review

Chapter 3. Selections

CS111: PROGRAMMING LANGUAGE II

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Place your name tag here

COMP 110 Programming Exercise: Simulation of the Game of Craps

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

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

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

1.1 Your First Program

Transcription:

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 7 problems on the following 6 pages. You may use your single- side hand- written 8 ½ x 11 note sheet during the exam. No calculators, computers, or communication devices of any kind are permitted. If you have a question, raise your hand and I will stop by. Since partial credit is possible, please write legibly and show your work. Problem Points Score 1 7 2 14 3 9 4 3 5 5 6 10 7 9 Total 57 1

1. Conditionals and loops (7 points). Consider the following code fragment: int n = 1; for (int i = 0; i < 5; i++) System.out.println(i + " " + n); if (n < 8) n = n * 2; a) Give the output produced by the code. 0 1 1 2 2 4 3 8 4 8 b) Add code into the boxes below to convert the above program to use a while loop instead of a for loop. public static void main(string [] args) int n = 1; int i = 0; while (i < 5) System.out.println(i + " " + n); if (n < 8) n = n * 2; i++; 2

2. Java expressions (14 points). Give the type and value of each of the following expressions. If an expression causes a compile or runtime error, write "error" in the type column (and leave the value blank). If an expression involves randomness, provide any value that could conceivably be generated by the expression. Expression Type Value 10 + 20-20.0 2.5 / 1.0 3 / 3 3 / 4 3 % 4 double 10.0 double 2.5 int 1 int 0 int 3 Math.random() double e.g. 0.33812 anything in [0.0, 1.0) (int) (Math.random() * 5) int e.g. 1 anything in [0, 4] "bob" + 123 String "bob123" 1 + 2 + 3 + "bob" Integer.parseInt(7) Integer.parseInt("7") Integer.parseInt("7.1") (1 <= (5-4)) (1 > 2) (1 > 3) (1 > 0) String error "6bob" int 7 error boolean boolean true true 3

3. Conditionals and loops (9 points). You are writing a program that can find the sum, minimum or maximum of a list of floating- point numbers. The program gets its data from two or more command- line arguments. The first argument is an integer specifying the mode: 0 = sum, 1 = minimum, 2 = maximum. Following the mode integer, there are one or more floating- point data points. Here are a number of sample runs: % java Calc 0 1.0 2.5 Points: 2 Result: 3.5 % java Calc 0 16.2 Points: 1 Result: 16.2 L N E H J P % java Calc 1 1.0 2.5-2.7 Points: 3 Result: - 2.7 % java Calc 1 16.2 Points: 1 Result: 16.2 C % java Calc 2 1.0 2.5-2.7 Points: 3 Result: 2.5 % java Calc 2 16.2 Points: 1 Result: 16.2 Below is the skeleton of the program. In the empty boxes, write the letter of the code fragments that combine to create a correct implementation. Not all letters will be used and each letter can only be used once. public class Calc public static void main(string [] args) int mode = double value = for (int i = 2; i < double d = ; if value += d; else if value = ; ; i++) else value = O ; System.out.println("Points: " + B ); System.out.println("Result: " + value); A. (args.length + 1) B. (args.length - 1) C. args.length D. Integer.parseInt(args) E. Integer.parseInt(args[0]) F. Double.parseDouble(args[0]) G. Integer.parseInt(args[1]) H. Double.parseDouble(args[1]) I. Integer.parseInt(args[i]) J. Double.parseDouble(args[i]) K. (mode.equals("0")) L. (mode == 0) M. (mode.equals("1")) N. (mode == 1) O. Math.max(d, value) P. Math.min(d, value) Q. Math.max(d, Double.POSITIVE_INFINITY) R. Math.min(d, Double.POSITIVE_INFINITY) 4

4. Static methods (3 points). Consider the following program: public class Cube public static void cube(int i) i = i * i * i; public static void main(string[] args) for (int i = 0; i < 1000; i++) cube(i); How many times does the for loop iterate in the main method? 1000 5. Static methods (5 points). What does the following program output? public class MethodJumping public static void printworld() System.out.print("mundo"); public static int addnums(int num1, int num2) return num1 + num2; public static void main(string [] args) System.out.print("Hola "); printworld(); System.out.print(", 1 + 2 = "); int a = addnums(20, 30); System.out.println(a); "Hola mundo, 1 + 2 = 50" 5

6. Standard input (10 points). All code fragments below read data from standard input. In each box, write the letter corresponding to the best description of what each program does. Letters may be used 0 or more times. String [] a = new String[N]; for (int i = 1; i <= N; i++) a[i - 1] = StdIn.readString(); for (int i = N - 1; i >= 0; i- - ) System.out.print(a[i] + " "); A A. Reads in N strings into an array. Prints out the words in reverse order. B. Reads in N - 1 strings into an array. Prints out the words in reverse order. int [] a = new int[n]; a[i] = StdIn.readInt(); double b = 0.0; b += a[i]; System.out.println(b); int [] a = new int[n]; a[i] = StdIn.readInt(); double b = 0.0; b += a[i]; System.out.println(b / a.length); K B J B String [] a = new String[N]; A a[i] = StdIn.readString(); System.out.print(a[N - 1 - i] + " "); C. Reads in N strings into an array. Prints out every other word. D. Reads in N strings into an array. Sorts the array in alphabetical order. E. Reads in N strings into an array. Converts the strings to integers and prints the number of integers that were equal to the length of the array. F. Reads in N web site products into parallel arrays. Each product is specified by a quantity, a description and a price (in that order). Prints out a sequence of lines where each line describes one product in the order. G. Reads in N web site products into parallel arrays. Each product is specified by a quantity, a description and a price (in that order). Prints out the order total. H. Reads in N numbers into parallel arrays. Outputs the minimum, maximum and sum of each array. int [] a = new int[n]; String [] b = new String[N]; double [] c = new double[n]; double d = 0.0; a[i] = StdIn.readInt(); b[i] = StdIn.readString(); c[i] = StdIn.readDouble(); d += a[i] * c[i]; System.out.println(d); G B I. Reads in N integers into an array. Outputs the count of the number unique integers in the array. J. Reads in N integers into an array. Outputs the average of the numbers in the array. K. Reads in N integers into an array. Outputs the sum of all the numbers in the array. 6

7. Debugging (9 points). You are working on program to print a square grid of alternating symbols. The user specifies three command- line arguments: an integer square size and two symbols. Each symbol is specified by a string which you can assume is always a single character. Here is the expected output for some examples: % java Square 4 a B abab BaBa abab BaBa % java Square 1 a B a % java Square 3. #.#. #.#.#. Here is your current program which has three bugs: 1 int N = args[0]; 2 String sym1 = args[1]; 3 String sym2 = args[2]; 4 5 6 7 for (int j = 0; j < N; i++) 8 9 if ((i + j) % 2 == 0) 10 System.out.print(sym1); 11 else 12 System.out.print(sym2); 13 System.out.println(); 14 15 A. Which bug prevents the program from compiling successfully? Identify the line number with the mistake that causes the compile error. Give a correct version of this line of code. Line number 1 Correct version: int N = Integer.parseInt(args[0]); B. After fixing the compile error, your program now runs but gets stuck in an infinite loop. Identify the line causing the infinite loop. Give a correct version of this line of code. Line number 7 Correct version: for (int j = 0; j < N; j++) C. After fixing the infinite loop bug, your program now terminates. It outputs the correct number of symbols and they alternate, but each appears on its own line. What do you need to do to fix this final bug? Move line 13 to after curly brace of for- j loop. 7