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

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

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

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

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

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

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

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

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

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

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

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

Selected Questions from by Nageshwara Rao

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

CSCI 135 Final Fall Problem Points Score Total 100

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

Chapter 4 Defining Classes I

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

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

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

CSCI 201L Written Exam #1 Fall % of course grade

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

Contents. 8-1 Copyright (c) N. Afshartous

CIS Introduction to Computer Programming. 5 October 2012 Midterm

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

Lecture #6-7 Methods

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

COS 126 Midterm 1 Written Exam Fall 2014!

Final Exam. COMP Summer I June 26, points

COS 126 Midterm 1 Written Exam Fall 2011

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

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

Csci 102: Sample Exam

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Lecture 5: Methods CS2301

Introduction to Programming Using Java (98-388)

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

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

CSCI 201L Written Exam #1 Fall % of course grade

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

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

CMSC 132: Object-Oriented Programming II

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Final Exam Practice. Partial credit will be awarded.

CSE 11 Midterm Fall 2008

CS 231 Data Structures and Algorithms, Fall 2016

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

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

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

CS 113 PRACTICE FINAL

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

CSE 131 Introduction to Computer Science Fall Exam II

COMP 110 Programming Exercise: Simulation of the Game of Craps

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

NATIONAL UNIVERSITY OF SINGAPORE

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CO Java SE 8: Fundamentals

Examination Questions Midterm 1

Place your name tag here

CMPSCI 187 Midterm 1 (Feb 17, 2016)

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

Prelim One Solution. CS211 Fall Name. NetID

ANSWER KEY First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

Problem Points Score Grader Total 100

Chapter 6 Introduction to Defining Classes

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

Introduction to Java

Oracle 1Z Java Certified Programmer. Download Full Version :

CSE 143 SAMPLE MIDTERM

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

Problem Grade Total

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

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

CSE 11 Midterm Fall 2012

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

CONDITIONAL EXECUTION

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

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

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

CSE 131 Introduction to Computer Science Fall Exam II

Unit 10: Sorting/Searching/Recursion

1 Shyam sir JAVA Notes

COS 126 Midterm 1 Written Exam Fall 2012

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

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

COS 126 Exam 2 Review Part 1

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

Final Exam CS 251, Intermediate Programming December 13, 2017

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

Csci 102: Sample Exam

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

COMP 401 Spring 2013 Midterm 1

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Computer Science Foundation Exam

Programming - 2. Common Errors

Introduction to Programming (CS112): Sample

Transcription:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 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 10 2 10 3 14 4 10 5 12 6 12 Total 68 1

1. Loops, input (10 points). Consider the following program: public class Prob1 public static void main(string [] args) for (int i = 0; i < args.length; i++) System.out.print(args[i]); if (i < args.length - 1) System.out.print(", "); 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 hi ho hi ho % java Prob1 hello % java Prob1 % java Prob1 one two three % java Prob1 1 2 3 2

2. Multiple choice (10 points, 2 points each). Circle the best single answer. I. Data encapsulation is a desirable goal when implementing your own data types. Which of the following is the best way to achieve data encapsulation: a) Declare all methods using lower camel case. b) Declare all methods as static. c) Declare all instance variables as final. d) Declare all instance variables as private. II. Consider the following code fragment: Point p1 = new Point(1.0, 1.0); Point p2 = new Point(2.0, 2.0); p1 = p2; The next time the Java garbage collector runs, what point(s) will be garbage collected? a) No points will be garbage collected. b) The point that was at (1.0, 1.0). c) The point that was at (2.0, 2.0). d) Both the point at (1.0, 1.0) and the point at (2.0, 2.0). III. You are debugging a recursive method that currently produces a StackOverflowError exception. What is the most likely explanation of the cause of the problem? a) You forgot to include a tostring() method in the enclosing class. b) You forgot to include a base case in your recursive method. c) You forgot to use the this keyword when referring to parameters to the recursive method. d) You forgot to use the final keyword when referring to parameters to the recursive method. IV. The class MyHelper has a static method declared as follows: public static void foo(int a) /* some code */ Which of the following lines would be a valid way to call the above method from the main() method of the class MyGame: a) MyHelper.foo(4 + 5); b) MyHelper.foo(4.5); c) foo(4 + 5); d) MyGame.foo(4); V. Queues and stacks are two fundamental abstract data types (ADTs) in computer science. Which of the following statements is TRUE: a) Queues implement a last-in first-out (LIFO) removal policy. b) Stacks implement a first-in first-out (FIFO) removal policy. c) Traditionally, a queue ADT names the add operation pop() and the remove operation push(). d) Queues and stacks can be implemented using a linked list data structure. 3

3. Variables and expressions (14 points). a) Give the type and value of each of the following expressions. If an expression causes an error, write "error" in the type column (and leave the value blank). Expression Type Value 0.0 * 123 123 * (int) 0.1 (Math.random() < 1.0) 1 / 2.0 + 2 11 % 2 Integer.parseInt("12" + "34") b) Consider the following class: (2 <= 3) (3 < 2) 01 public class Scope 02 03 private int a; 04 public final double PI = 3.0; 05 06 public void foo(int a, String [] s) 07 08 09 for (int i = 0; i < s.length; i++) 10 11 12 13 14 double d = 2.5; 15 16 17 18 public void foo2(scope other) 19 20 21 22 For each of the specified lines in the above program, check the boxes of the variables that would be in scope at that line (i.e. you could use the variable name in some appropriate way at that line and it would compile). Line a PI this.a d other.a s i 08 11 15 20 4

4. Using objects (10 points). Here is the API for a class that represents charged particles: public class Charge ---------------------------------------------------------------------------- Charge(double x0, double y0, double q0) // create particle at (x0,y0) with charge q0 double potentialat(double x, double y) // electric potential at (x,y) due to charge String tostring() // string representation of particle You are writing a client program FourCharge that takes a double value w from the first command line argument, creating four Charge objects each with charge value 1.0 that are each distance w in each of the four cardinal directions from (0.5, 0.5). It also prints the potential at (0.25, 0.5) due to all the charged particles. Fill in the missing code (denoted by underscores) in the following program: public static void main(string[] args) double w = ; Charge c1 = ; Charge c2 = ; Charge c3 = ; Charge c4 = ; double x = 0.25; double y = 0.5; double v1 = ; double v2 = ; double v3 = ; double v4 = ; System.out.println( ); 5

5. Arrays (12 points) a) Give a single line of Java code that declares and instantiates an array containing 1000 boolean values. b) Give Java code that sets each value in the previous array to true. For full credit, make your code work regardless of how big the array is. c) Give a single line of Java code that declares and instantiates an array that can hold 50 Charge objects (see the previous problem for details about the Charge class). d) After the line of code from part c, what value does every element of your Charge array have? e) Give Java code that fills your Charge array with random particles. Each particle should have a random x- position in [0.0, 1.0), a random y-position in [0.0, 1.0), and each particle has a 50% chance of having a negative charge of -1.0 and a 50% chance of having a positive charge of +1.0. 6

6. Linked lists (12 points). You have developed a linked list class that holds a sequence of double values. It uses the following inner class Node: private class Node double value; Node next; Currently the linked list has the following data and structure: Below are some instance methods contained in your linked list class. Your class has an instance variable first that points to the front of the linked list. In each method's box, write the letter that best describes what that method does with respect to the linked list. Each letter will be used at most once. public int foo1() int a = 0; a++; public double foo4() double a = 0.0; a = current.value; public void foo2() first = null; public void foo5() if (first!= null) first = first.next; public double foo3() double a = Double.NEGATIVE_INFINITY; if (current.value > a) a = current.value; public double foo6() double a = 0.0; a = a + current.value; A. Returns the minimum value in the list. B. Returns the maximum value in the list. C. Returns the number of entries in the list. D. Returns the value of the last entry in the list. E. Returns sum of all positive numbers in the list. F. Returns sum of all negative numbers in the list. G. Returns sum of all numbers in the list. H. Deletes the last entry in the list. I. Deletes all entries in the list. J. Delete the first entry in the list. K. Deletes all entries that are equal to 0.0. L. Inserts a new entry at the beginning of the list. M. Inserts a new entry at the end of the list. N. Creates a new list with a single entry. 7