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

Similar documents
I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

I. True/False: (2 points each)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

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

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

I. True/False: (2 points each)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

I. True/False: (2 points each)

I. True/False: (2 points each)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall What is your name?: (3 points for writing it on your answer sheet)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring What is your name?: (4 points for writing it on your answer sheet)

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2017

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 03

CS170 Introduction to Computer Science Midterm 2

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

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

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

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Problem Grade Total

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

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

CS 113 MIDTERM EXAM 2 SPRING 2013

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

Spring University of New Mexico CS152 Midterm Exam. Print Your Name

CS 101 Exam 2 Spring Id Name

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

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

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Midterm Exam 2 CS 455, Spring 2015

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

Warm-up: what does this print?

CSE 143 SAMPLE MIDTERM

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

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

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

Practice Midterm 1. Problem Points Score TOTAL 50

CS111: PROGRAMMING LANGUAGE II

CS 307 Midterm 2 Spring 2011

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

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

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

CS 101 Fall 2006 Midterm 3 Name: ID:

CS 113 PRACTICE FINAL

Computer Programming, I. Laboratory Manual. Final Exam Solution

CS 177 Week 15 Recitation Slides. Review

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

Question: Total Points: Score:

Recap: Assignment as an Operator CS 112 Introduction to Programming

Practice Midterm 1 Answer Key

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

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

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

CS 101 Spring 2007 Midterm 2 Name: ID:

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

CS 101 Spring 2006 Final Exam Name: ID:

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Programming

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

CSE 11 Midterm Fall 2012

CSE 114 Midterm 1. Please leave one seat between yourself and each of your neighbors.

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

AP CS Unit 3: Control Structures Notes

King Saud University College of Computer and Information Sciences Computer Science Department

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS180. Exam 1 Review

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

For example, when your method is added to the class with the main method below, the output should be:

CMPSCI 187: Programming With Data Structures. Review for First Midterm 9 October 2011

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

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

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

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

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

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

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

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

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

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

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

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Chapter 1: Introduction to Computers, Programs, and Java

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

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

Solutions to the 2005 exam

Esc101 Mid Semester Exam - II

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

Use the Scantron sheets to write your answer. Make sure to write your Purdue ID (NOT Purdue Login ID) and your name on the Scantron answer sheet.

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

Unit 10: Sorting/Searching/Recursion

COS 126 General Computer Science Spring Written Exam 1

ECE Fall 20l2, Second Exam

CS 302 Week 9. Jim Williams

University of Massachusetts Amherst, Electrical and Computer Engineering

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

CSCI 111 Midterm Spring 2014

Transcription:

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011 What is your name?: This test has the following sections: I. True/False... 60 points; (30 questions, 2 points each) II. Multiple Choice... 36 points; (6 questions, 6 points each) --------------- 96 + 4 points for free = 100 points total This test is worth 15% of your final grade. You must put your answers on the bubble form. All code is in Java unless stated otherwise. This test is open book and open notes. For the multiple choice problems, select the best answer for each one and select the appropriate letter on your answer sheet. Be careful - more than one answer may seem to be correct. Some questions are tricky. You have 50 minutes. I. True/False: (2 points each) On your bubble form fill out a for true and b for false. T F 1. T F 2. T F 3. T F 4. T F 5. T F 6. T F 7. T F 8. T F 9. Any program can be written using only a sequence of instructions with if statements and looping statements, without variables, assignment statements, or methods. Any program can be written using only a sequence of instructions with if statements, looping statements and methods, without variables or assignment statements. The contents of two strings can be compared using either double equals (==) or the equals() method. Any code that can be written with multiple if-else statements could also be written with a switchcase statement. Java variable names must begin with an alphabetic character, $ or _ Private instance variables can only be accessed directly through code in the class where they are declared. Private instance variables can be accessed indirectly through code in another class, as long as the method in the other class using these instance variables is also marked as private. Under some circumstances two methods in Java can have the same name and same number of parameters. Two methods in Java can have the same name, same number of parameters with the same types as long as the return type is different. T F 10. The following is a valid declaration in Java: String Int = "Char"; T F 11. If tostring() is not implemented in a class, then printing a class object displays the object s class name and address in memory. CS 102/107, Midterm #2, Spring 2011 page 1

T F 12. The section of code shown below would compile and run and give as output: 6 int sum = 0; for( int i=0; i<3; i++) sum += i; System.out.println( sum); T F 13. The section of code shown below would compile and run and give as output: Is 3 Done int x = 4; if( x = 3) System.out.print("Is 3 "); else System.out.print("Is 4 "); System.out.println("Done"); T F 14. The following statements compile and run in Java: char[] letters = 'Y','e','s'; if( letters.length() == 3) System.out.println( letters[ 3]); T F 15. The following code prints the words: undecided char c='a'; switch (c) case 'a': System.out.print("u"); case 'b': System.out.print("n"); case 'c': System.out.print("d"); case 'd': System.out.print("e"); System.out.println("cided"); T F 16. The following statements compile and run in Java: String last = "first"; if( last.equals( "first")) System.out.println("first = last"); T F 17. In order for the code shown below to compile and run properly, a copy constructor for the Date class must have been created by the user. Date d1 = new Date(2,14,2000); Date d2 = new Date( d1); T F 18. A chained constructor call must use this and must be the first action within the calling constructor. T F 19. When writing a class that includes instances of a class someone else has written, you will not be able to write a copy constructor unless that other class already implemented a copy constructor. CS 102/107, Midterm #2, Spring 2011 page 2

T F 20. Given the final version of the Date class discussed in our lectures, the follow code will give the output: Are equal Date d1 = new Date( 1,1,2010); Date d2 = new Date( d1); if( d1==d2) System.out.println("Are equal"); else System.out.println("NOT equal"); T F 21. Assuming the final version of the Date class as developed in our lectures, the tostring method gets called automatically in the following code: Date d3 = new Date(); System.out.println("Date is: " + d3); T F 22. Given the final version of the Date class discussed in our lectures, the follow code will give the output: Nov/2/2010 Date d1 = new Date( "Nov",1,2010); Date d2 = d1; d1.setday(2); System.out.println( d2); T F 23. The following Java code would compile, run, and display a value for x: int[] values = 8,3,2,6; int x = values[ values[2] ]; System.out.println( x); T F 24. When a method sends parameters to a second method which changes those parameter values, those changed values are not reflected back to the calling method. T F 25. When a method sends an array to a second method which changes the values in the array, the original array values are restored once control is returned back to the calling method. T F 26. Typically an n-dimensional array in Java will use n sets of square braces [] every time we want to read from or write to an array element. T F 27. In a Java program using arrays, the maximum size of an array must be known before we begin running the program. T F 28. If an array is explicitly initialized, the size doesn't need to be given since Java computes it automatically. T F 29. The size of an array could itself be a variable, where the value of the variable isn't known until the program runs. T F 30. Once an array is created in Java the grow( n) command can be used to add n elements to the array size. CS 102/107, Midterm #2, Spring 2011 page 3

II. Multiple Choice (6 pts. each) 31. Consider the class given below, along with the driver class for it. class ClassA int x; public ClassA() x = 4; addvalue( x); public void addvalue(int val) x = x + val; //end ClassA class ClassADriver public void doit() int value = 2; ClassA instance1 = new ClassA(); instance1.addvalue( value); System.out.println("value is: " + instance1.x); //end ClassADriver When running method doit() in the ClassADriver class, the output will be: a) value is: 4 b) value is: 6 c) value is: 8 d) doesn t compile 32. Assume that you create class Employee that includes an instance of class Date to store the startdate for each employee. Assume that you have written some test code in class EmployeeDriver shown below, where you change the startdate for e1. To your surprise when you run this code the startdate for e2 has changed as well. What is the most likely explanation for this? class EmployeeDriver public static void main(string[] args) Employee e1 = new Employee(); Employee e2 = new Employee( e1); e1.changedate( 11, 1, 2010); System.out.println(e1); System.out.println(e2); a) The Date class fields are declared as static b) The Date class copy constructor does not chain to the fully qualified constructor c) The new value happens to be the same as the default value d) The Employee copy constructor does not create a new Date CS 102/107, Midterm #2, Spring 2011 page 4

33. Assume an instance of class Confusing is created, and that instance is used to call method doit(). What is the output? class Confusing int x; int y; public Confusing() x = 3; y = 5; void first( int x, int b) int temp = x; y = b; b = temp; //end method first void second( int x, int y) first( x, y); x = y + 1; //end method second public void doit() int x = 1; int y = 3; first( x,y); second( y,x); System.out.println("Answer is: "+ this.x + " " + this.y); //end method doit() a) Answer is: 3 5 b) Answer is: 3 4 c) Answer is: 3 3 d) Answer is: 1 3 CS 102/107, Midterm #2, Spring 2011 page 5

34. What is the output from running the code in the following Maze program? public class Maze1 private static int startrow = 1; // starting row private static int startcol = 1; // starting column private int endrow = 8; // ending row private int endcol = 8; // ending column // Maze is a 2-D array where 1 is a wall, 0 is an open pathway. private int[][] maze = /* 0 1 2 3 4 5 6 7 8 9 */ /* 0 */ 1,1,1,1,1,1,1,1,1,1, /* 1 */ 1,0,0,0,1,1,1,1,1,1, /* 2 */ 1,1,0,1,1,1,0,0,1,1, /* 3 */ 1,1,0,1,1,1,0,1,1,1, /* 4 */ 1,1,0,0,0,0,0,1,1,1, /* 5 */ 1,1,1,1,1,0,1,1,1,1, /* 6 */ 1,0,1,1,1,0,1,0,0,1, /* 7 */ 1,1,1,1,1,0,1,0,1,1, /* 8 */ 1,1,0,0,0,0,0,0,0,1, /* 9 */ 1,1,1,1,1,1,1,1,1,1 ; public static void main(string[] args) Maze1 instanceofmaze1 = new Maze1(); instanceofmaze1.makemove( startrow, startcol); public void makemove( int row, int col) while( true) System.out.print("[" + row + "," + col + "], "); if (( row == endrow) && ( col == endcol)) System.out.println("\n Got to destination! "); break; if ( maze[ row+1][ col]!= 1) row++; continue; if (maze[ row][ col+1]!= 1) col++; continue; if (maze[ row-1][ col]!= 1) row--; continue; if (maze[ row][ col-1]!= 1) col--; continue; //end makemove //end class Maze1 a) The solution path in order b) The solution path with some additional output at the end c) Output that terminates, but not the solution path d) Output that is in an infinite loop CS 102/107, Midterm #2, Spring 2011 page 6

35. Consider method second shown at right, which itself uses method first. For positive numbers, how would you best describe its return value? a) x + y b) x * x c) x * y d) x y public int first( int x, int y) int z = 0; for( int i=0; i<x; i++) z += y; return z; public int second( int x, int y) int z=1; for( int i=0; i<y; i++) z = first( z, x); return z; 36. Recursion is when a method calls itself. It behaves the same as if there were unlimited copies of the same method, where each has its own copy of parameters. What is the output of the following line of code: System.out.print( mystery1( 314159) ); where method mystery1( ) is shown below, and user input is: 314159 int mystery1( int x) if( x < 10) return x; else return mystery1( x/10); a) 3 b) 31415 c) 9 d) 14159 CS 102/107, Midterm #2, Spring 2011 page 7