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

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

I. True/False: (2 points each)

I. True/False: (2 points each)

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

I. True/False: (2 points each)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 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

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 #1 - Prof. Reed Fall What is your name?: (3 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

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CS170 Introduction to Computer Science Midterm 2

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

CS 101 Spring 2007 Midterm 2 Name: ID:

Problem Grade Total

CIS October 19, 2017

Some Practice Midterm Problems

Practice Midterm 1 Answer Key

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

AP COMPUTER SCIENCE A DIAGNOSTIC EXAM. Multiple Choice Section Time - 1 hour and 15 minutes Number of questions - 40 Percent of total grade - 50

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CS 177 Week 15 Recitation Slides. Review

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

Final Exam COMP Fall 2004 Dec 16, 2004

CS 307 Midterm 2 Spring 2011

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

Term 1 Unit 1 Week 1 Worksheet: Output Solution

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 101 Fall 2006 Midterm 3 Name: ID:

Solutions to Sample JAC444 Midterm Test

Intro to Programming in Java Practice Midterm

Practice Midterm 1. Problem Points Score TOTAL 50

int x = 42, y = 33; //1 int z = x + y; //2 System.out.println(x + "+" + y + "=" + z); //3

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

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

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

CS 113 PRACTICE FINAL

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

CIS March 1, 2018

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

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

CSC122A, Spring 2005 Computer Science II Final Exam

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

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

Final Exam. COMP Summer I June 26, points

Esc101 Mid Semester Exam - II

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

AP CS Unit 8: Inheritance Exercises

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

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

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

Unit 10: Sorting/Searching/Recursion

Midterm Practice Exam

CIS October 16, 2018

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

CS111: PROGRAMMING LANGUAGE II

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

CS : Introduction to Computer Programming Final Exam December 9, Please wait to open this exam booklet until you are told to do so.

CS 307 Midterm 2 Spring 2008

COS 126 Midterm 1 Written Exam, Fall 2009

Code Analysis Sample Questions

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.

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

CSE 11 Midterm Fall 2012

CSC122A, Spring 2005 Computer Science II Final Exam

Prelim One Solution. CS211 Fall Name. NetID

CS 101 Spring 2006 Final Exam Name: ID:

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

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

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

Final Exam Practice. Partial credit will be awarded.

CMSC132 Summer 2018 Midterm 1. Solution

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

CS 312 Midterm 1 Spring 2013

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

Introduction to Computer Science Unit 2. Exercises

Final Examination CS 125 Introduction to Computer Science Fall Hours

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

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

Introduction to Computer Science Midterm 3 Fall, Points

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

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

CS 101 Exam 2 Spring Id Name

COS 126 General Computer Science Fall Written Exam 1

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

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

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

CS : Introduction to Computer Programming Final Exam December 9, Please wait to open this exam booklet until you are told to do so.

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

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

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

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

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

Transcription:

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2010 What is your name?: There are three sections: I. True/False..............60 points; (30 questions, 2 points each) II. Multiple Choice........40 points; (10 questions, 4 points each) --------------- 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. 1. In a Java program a method that does not return anything can either have a return type of void or can leave off the return type altogether. 2. The section of code shown below would compile and run and give as output: 3 int sum = 0; for( int i=0; i<=3; i++) sum += i; System.out.println( sum); 3. 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"); 4. Any code that can be written with multiple if-else statements could also be written with a switch-case statement. 5. The following statements compile but then give a run-time error in Java: char[] letters = a, b, c ; if( letters.length() == 3) System.out.println( letters[ 3]); CS 102/107 - Intro. to Programming, Midterm Exam #2, page 1 of 9

6. The following code prints the words: abcdefg 7. The following is a valid declaration in Java: int Class = 3; char c='a'; switch (c) case 'a': System.out.print("a"); case 'b': System.out.print("b"); case 'c': System.out.print("c"); System.out.println("defg"); 8. The following statements compile and run in Java: String first = "last"; if( first.equals( "last")) System.out.println("first = last"); 9. When writing a class that is to be used by others, you should at a minimum implement instance variables, the get and set methods, default constructor, fully qualified constructor, copy constructor, equals method, and the tostring method. 10. Two methods in Java can have the same name and same number of parameters. 11. 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); 12. A chained constructor call must use this and must be the first action within the calling constructor. 13. 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"); 14. If tostring() is not implemented in a class, then printing a class object displays the object s class name and address in memory. 15. 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); CS 102/107 - Intro. to Programming, Midterm Exam #2, page 2 of 9

16. 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); 17. Given the final version of the Date class discussed in our lectures, the follow code will give the output: Nov/1/2010 Date d3 = new Date( "Nov",1,2010); Date d4= new Date(d3); d4.setday(2); System.out.println( d3); 18. Private instance variables can only be accessed directly through code in the class where they are declared. 19. 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. 20. The following Java code would compile, run, and display a value for x: int[] values = 3,4,5,6; int x = values[ values[1] ]; System.out.println( x); 21. Every program can be written with all the looping code implemented using recursion, without any for, while, or do loops. 22. Recursive methods typically have a base case which is the code that stops the recursion. 23. 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. 24. 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. 25. An integer array called values could be sorted into ascending order using: Arrays.sort( values); 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. CS 102/107 - Intro. to Programming, Midterm Exam #2, page 3 of 9

27. Assuming method swap( ) is correctly implemented, the code shown below would sort values in an integer array into ascending order. public void bubblesort( int[] thearray) int pass, current; int size = thearray.length; for ( pass=0; pass < (size-1); pass++) for ( current=0; current < (size-1); current++) if ( thearray[ current] > thearray[ current+1]) swap( thearray, current, current+1); 28. If the cost of a swap( ) operation were high, then using Selection Sort would give better performance than using Bubble Sort. 29. In a Java program using arrays, the maximum size of an array must be known before we begin running the program. 30. An ArrayList can be used in Java to dynamically adjust the size of an array as the program runs. II. Multiple Choice (4 pts. each) 31. Consider the code given below. If its output is: 12 16 20 15 20 25 18 24 30 21 28 35 what are the values for variables start, end, first and last? for( int i=start; i<=end; i++) for( int j=first; j<last; j++) System.out.printf("%5d", i*j); System.out.println(); a) int start=7, end=3, first=6, last=4; b) int start=4, end=7, first=3, last=6; c) int start=7, end=4, first=6, last=3; d) int start=3, end=7, first=4, last=6; CS 102/107 - Intro. to Programming, Midterm Exam #2, page 4 of 9

32. When using a binary search to search for a particular key within a sorted array of 65 values, the maximum of comparisons the program would need to make before finding the key (if it is there) is: a) 4 b) 5 c) 6 d) 7 33. 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=x; for (int i=0; i<y; i++) z += 1; return z; public int second(int x, int y) int z=0; for (int i=0; i<y; i++) z = first( z, x); return z; 34. What is the output of the following line of code: System.out.print( mystery1() ); where method mystery1( ) is shown below, and user input is: O y e a h! public char mystery1() char returnvalue = ' '; String input = keyboard.next(); char c = input.charat(0); if (c!= '!') mystery1(); returnvalue = c; return returnvalue; a) O b) y c) h d)! CS 102/107 - Intro. to Programming, Midterm Exam #2, page 5 of 9

35. What is the output of the following line of code: System.out.print( mystery2() ); where method mystery2( ) is shown below, and user input is: O y e a h! public char mystery2() String input = keyboard.next(); char c = input.charat(0); if (c!= '!') c = mystery2(); return c; a) O b) y c) h d)! 36. Consider the class given below, along with the driver class for it. class ClassA private int x; public ClassA() x = 1; public void addvalue(int val) x = x + val; //end ClassA class ClassADriver public void doit() int value = 7; 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: 6 b) value is: 7 c) value is: 8 d) doesn t compile CS 102/107 - Intro. to Programming, Midterm Exam #2, page 6 of 9

37. 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 a, int b) int temp = x; x = y; y = 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); //end method doit() a) Answer is: 3 b) Answer is: 4 c) Answer is: 5 d) Answer is: 7 CS 102/107 - Intro. to Programming, Midterm Exam #2, page 7 of 9

38. 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 39. What is the output of the code given below when method usearray() is called? public void usearray() char[] thearray= 'o','b','f','u','s','c','a','t','e'; processarray( thearray); for(int i=0; i<thearray.length; i++) System.out.print( thearray[i]); public void processarray(char[] w) int x = w.length; char c; for( int i=0; i<x; i++) c = w[i]; w[i] = w[x-i-1]; w[x-i-1] = c; a) The contents of the original array in reverse order b) The contents of the original array in the original order c) The contents of the original array with half of the characters reversed d) The original array with characters rearranged so they are neither in the original nor reversed orde CS 102/107 - Intro. to Programming, Midterm Exam #2, page 8 of 9

40. Consider the following Maze program similar to the one covered in class: class Maze private static int start = 11; private static int end = 57; private int[] maze = /* + 0 1 2 3 4 5 6 7 8 9 */ /* 0 */ 1,1,1,1,1,1,1,1,1,1, /*10 */ 1,0,0,0,0,1,1,1,1,1, /*20 */ 1,1,1,0,1,1,1,1,1,1, /*30 */ 1,1,1,0,0,0,1,1,1,1, /*40 */ 1,1,1,1,1,0,1,1,1,1, /*50 */ 1,1,1,1,0,0,0,0,1,1, /*60 */ 1,1,1,1,1,1,1,1,1,1; private int[] moves = 10,1,-1,-10; public Maze() makemove( start); public void makemove( int current) System.out.print(current + ", "); if ( current == end) System.out.println("Got to destination!"); return; for (int i=0; i<4; i++) int next = current + moves[i]; if ((maze[next]!= 1) ) makemove( next); //end makemove //end Maze What is the output from creating an instance of Maze and running this code? a) The solution path in order b) The solution path with an infinite loop at the end going back and forth c) A bunch of numbers, but not the solution path d) An infinite loop near the beginning of the search CS 102/107 - Intro. to Programming, Midterm Exam #2, page 9 of 9