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

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

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

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 #0 Fundamentals of Computer Science I Fall 2013

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

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

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

Introduction to Programming Using Java (98-388)

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

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

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

CSCI 135 Final Fall Problem Points Score Total 100

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

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

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

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

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

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

University of Palestine. Mid Exam Total Grade: 100

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

Chapter 4 Defining Classes I

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

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

CO Java SE 8: Fundamentals

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CIS Introduction to Computer Programming. 5 October 2012 Midterm

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

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

CHAPTER 7 OBJECTS AND CLASSES

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

CSCI 355 Lab #2 Spring 2007

CLASSES AND OBJECTS. Fundamentals of Computer Science I

AP CS Unit 3: Control Structures Notes

NATIONAL UNIVERSITY OF SINGAPORE

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

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

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CSCI 355 LAB #2 Spring 2004

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

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

Programming with Java

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CHAPTER 7 OBJECTS AND CLASSES

ESC101 : Fundamental of Computing

Lecture 5: Methods CS2301

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Introduction to Java

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

CSC Java Programming, Fall Java Data Types and Control Constructs

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Prelim One Solution. CS211 Fall Name. NetID

COS 126 Midterm 1 Written Exam Fall 2014!

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

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

Selected Questions from by Nageshwara Rao

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

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

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

CS260 Intro to Java & Android 03.Java Language Basics

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

COS 226 Algorithms and Data Structures Week 3: Comparators, & Sorting (Video 5.D & Algorithms 2.1 and 2.2)

Midterm Exam CS 251, Intermediate Programming March 12, 2014

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

CS1083 Week 2: Arrays, ArrayList

PROGRAMMING FUNDAMENTALS

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Computer Science II (20082) Week 1: Review and Inheritance

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

1. Find the output of following java program. class MainClass { public static void main (String arg[])

YOU ARE ALLOWED TO HAVE ONLY THE FOLLOWING ON YOUR DESK OR WORKTABLE:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

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

Final Exam Practice. Partial credit will be awarded.

COE318 Lecture Notes Week 4 (Sept 26, 2011)

1 Short Answer (5 Points Each)

CS 231 Data Structures and Algorithms, Fall 2016

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

Final Exam. COMP Summer I June 26, points

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

Lab Assignment Three

Pace University. Fundamental Concepts of CS121 1

Notes on Chapter Three

Full file at

Java Fundamentals (II)

CS 101 Fall 2006 Midterm 3 Name: ID:

Computer Programming, I. Laboratory Manual. Final Exam Solution

The Basic Parts of Java

Handout 7. Defining Classes part 1. Instance variables and instance methods.

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

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Object Oriented Programming. Java-Lecture 1

Transcription:

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014 Name: This exam consists of 8 problems on the following 8 pages. You may use your two- sided 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 6 2 12 3 12 4 6 5 3 6 10 7 10 8 6 Total 65 1

1. Loops, input (6 points). Consider the following program: public class Prob1 public static void main(string [] args) int val = Integer.parseInt(args[0]); String s1 = args[1]; String s2 = args[2]; for (int i = 3; i < args.length; i++) if (Integer.parseInt(args[i]) < val) System.out.print(s1); else System.out.print(s2); System.out.println("!"); Give the output of Prob1 for each of the following commands. If the program would crash, write "error": Command Program output % java Prob1 10 a % java Prob1-10 a b % java Prob1 10 a b 20 5 % java Prob1 3.3 a b 5 % java Prob1 3 a b 0 1 2 % java Prob1 1000-1 1 500 1000 2

2. Code fragments (12 points). a) Give a code fragment that uses a loop to print out the even numbers from 1 to 100 (inclusive, i.e. 2, 4, 6,, 100). Each number should be output on its own line. b) Give a code fragment that declares and instantiates an array of 10 double values. c) Using the array from part b, give a code fragment that sets each element of the array to a different random value in [0.0, 1.0). For full credit, your fragment should work (without change) even if the size of the array in part b is changed. d) Give a static method avg2 that takes two int values as arguments and returns the double value of the average of the two numbers. For example, avg2(3, 4) would return 3.5. For full credit, be sure to include an access modifier that lets code in any class call the avg2() method. 3

3. Java keywords (12 points). Match each of the Java keywords on the left with the letter of the best description of a potential use of the keyword. Not all letters will be used, a letter can only be used once. boolean break final new null private public return static this do void A. Used in a method declaration to signify the method has no return result. B. A primitive data type that can only take on two distinct values. C. A primitive data type that can hold a floating point value. D. A reference data type that can only take on two distinct values. E. Causes a reference data type to be instantiated including calling the relevant constructor. F. Used to achieve data encapsulation of instance variables. G. Used to prevent a variable from having its value changed after its initial assignment. H. Ensures that a method or instance variable is visible to programs in any other class. I. Used to specify a variable name refers to an instance variable and not a local variable. J. The value of a reference variable before it has been instantiated using the new operator. K. The value of a primitive variable after dividing by zero. L. Causes the enclosing loop to terminate without executing any further statements in the body of the loop. M. Causes the enclosing loop to terminate after executing the remaining statements in the body of the loop. N. Signifies a method that is forbidden from calling itself recursively. O. Signifies a method that is overloading an existing method. P. Signifies a method that can be called by preceding the method name with the name of the class in which the method appears (e.g. MyClass.foo). Q. Causes the entire Java program to immediately exit. R. Used to specify the output value (if any) returned by a method. No further lines of code in the method are executed after hitting this keyword. S. Start of a loop construct in which the body executes a minimum of 0 times. T. Start of a loop construct in which the body executes a minimum of 1 time. 4

4. Recursion (6 points). Consider the following recursive method: public static void foo(int n) if (n <= 0) return; System.out.println(n); foo(n - 3); foo(n - 2); For each of the following calls, give the sequence of integers printed. If the program would produce no output, write no output. If the program would crash with a stack overflow, write stack overflow. a) foo(- 1) b) foo(2) c) foo(6) 5

5. Debugging (3 points). The following data type represents a point in 3- dimensional space: public class Point3D private double x = 0.0; private double y = 0.0; private double z = 0.0; public Point3D(double x, double y, double z) x = x; y = y; z = z; public String tostring() return String.format("(%.2f, %.2f, %.2f)", x, y, z); public double distance(point3d other) double deltax = x - other.x; double deltay = y - other.y; double deltaz = z - other.z; return Math.sqrt(deltaX*deltaX + deltay*deltay + deltaz*deltaz); public static void main(string [] args) Point3D p = new Point3D(1, 2, 3); System.out.println(p.toString()); Running the test main() method currently produces the following: % java Point3D (0.00, 0.00, 0.00) Correct the line(s) in the above program such that it produces the expected output: % java Point3D (1.00, 2.00, 3.00) 6

6. Arrays, objects (10 points). Assume the problem in Point3D has been corrected and all method work correctly. You are working on a client program AllDiff that reads in a points from standard input into an array. Rather than specifying the number of points via standard input, instead the number of points is specified as the first command line argument to the program. The program outputs the distance rounded to two decimal places between all pairs of points. Here is an example input file and the desired output format: % more 3points.txt 0.0 0.0 0.0 1.0 1.0 1.0 0.5 1.5 2.0 % java AllDiff 3 < 3points.txt (0.00, 0.00, 0.00) <- > (1.00, 1.00, 1.00) = 1.73 (0.00, 0.00, 0.00) <- > (0.50, 1.50, 2.00) = 2.55 (1.00, 1.00, 1.00) <- > (0.50, 1.50, 2.00) = 1.22 Fill in the missing code in the following program such that AllDiff works as described. public class AllDiff public static void main(string[] args) final int N = ; Point3D [] points = ; for (int i = 0; i < N; i++) points[i] = (StdIn.readDouble(), StdIn.readDouble(), StdIn.readDouble()); for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) double dist =.distance(points[j]); System.out.printf(, points[i].tostring(), points[j].tostring(), dist); 7

7. Algorithms, objects (10 points). You are working on a different client program PointStats that makes use of your Point3D data type. This program s job is to read in via standard input a sequence of 0 or more points and computes: The distance of the path connecting the points in the sequence (the path goes in order, i.e. going from the first point in the input, to the second point, and so on). The minimum distance between any two consecutive points in the sequence. The maximum distance between any two consecutive points in the sequence. Here is an example run with four points lying on the x- axis: % more 4points.txt 0.0 0.0 0.0 1.0 0.0 0.0 3.0 0.0 0.0 2.5 0.0 0.0 % java PointStats < 4points.txt Distance = 3.5 Min = 0.5 Max = 2.0 Write the letters that combine to create a correct implementation. Letters may be used 0 or more times. public class PointStats public static void main(string[] args) Point3D last = null; double sum = 0.0; double min = ; double max = ; while ( ) Point3D p = (StdIn.readDouble(), StdIn.readDouble(), StdIn.readDouble()); if ( ) double dist = ; sum = sum + dist; min = ; max = ; last = ; System.out.println("Distance = " + sum); System.out.println("Min = " + min); System.out.println("Max = " + max); A. Math.abs(last - p) B. last = dist C. last.distance(p) D. last E. p F. last++ G. Double.POSITIVE_INFINITY H. Double.NEGATIVE_INFINITY I. 0.0 J. Point3D K. new L. break M. final N.!StdIn.isEmpty() O. StdIn.isEmpty() P. StdIn.readDouble() Q. last < max R. last == null S. last!= null T. Math.min(dist, sum) U. Math.max(dist, sum) V. Math.min(dist, min) W. Math.max(dist, max) 8

8. Designing objects (6 points). You decide your Point3D data type also needs a text label. a) Give the declaration of an appropriate instance variable to support this label feature. For full credit, demonstrate the principle of data encapsulation. b) Describe how you would change the class in order to support setting of the label during the creation of the Point3D object. 9