I. True/False: (2 points each)

Similar documents
I. True/False: (2 points each)

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

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

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)

I. True/False: (2 points each)

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

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

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

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

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

Question: Total Points: Score:

Practice Midterm 1. Problem Points Score TOTAL 50

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS 177 Week 15 Recitation Slides. Review

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

CS 312 Midterm 1 Spring 2013

CSCE 206: Structured Programming in C++

Practice Midterm 1 Answer Key

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

Problem Grade Total

Term 1 Unit 1 Week 1 Worksheet: Output Solution

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

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

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

Question: Total Points: Score:

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

Control Structures in Java if-else and switch

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

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

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

Functions. Arash Rafiey. September 26, 2017

Final Exam COMP Fall 2004 Dec 16, 2004

Final Exam. COMP Summer I June 26, points

CS111: PROGRAMMING LANGUAGE II

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 302 Week 9. Jim Williams

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

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

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

University of Massachusetts Amherst, Electrical and Computer Engineering

Final Exam CS 251, Intermediate Programming December 10, 2014

CS170 Introduction to Computer Science Midterm 2

CS 177 Spring 2010 Exam I

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

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

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

APCS Assessment Sheet Consider the following program segment int count = 1;

Largest Online Community of VU Students

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

CS 307 Midterm 2 Spring 2008

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

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

CSCI 355 LAB #2 Spring 2004

AP CS Unit 8: Inheritance Exercises

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

Use the scantron sheet to enter the answer to questions (pages 1-6)

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

IT Introduction to Programming for I.T. Midterm Exam #1 - Prof. Reed Spring 2008

CS 101 Spring 2007 Midterm 2 Name: ID:

Fall 2002 Page 1 of 9 READ THIS NOW!

CS 101 Spring 2006 Final Exam Name: ID:

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

CMP 326 Midterm Fall 2015

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

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

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

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

Test 1. CSC 121 Lecture Lecturer: Howard Rosenthal. March 4, 2014

Intro to Programming in Java Practice Midterm

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

Selected Questions from by Nageshwara Rao

Midterm Exam CS 251, Intermediate Programming October 8, 2014

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

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

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

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

COS 126 General Computer Science Spring Written Exam 1

Final Exam CS 251, Intermediate Programming December 13, 2017

CS1004: Intro to CS in Java, Spring 2005

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Esc101 Mid Semester Exam - II

University Interscholastic League. Computer Science Competition

Midterm Exam 2 CS 455, Spring 2011

Midterm Exam 2 CS 455, Spring 2013

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

CS 312 Midterm 1 Spring 2013

1 Inheritance (8 minutes, 9 points)

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

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

1. Data types ( =13 points)

Transcription:

CS 102 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2008 What is your name?: (2 points) There are three sections: I. True/False..............54 points; (27 questions, 2 points each) II. Multiple Choice........44 points; (11 questions, 4 points each) --------------- 98 points + 2 for name = 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. You have 50 minutes. For the True/False questions, if the answer is True, fill in the A bubble on your answer form. If the answer is False, fill in the B bubble on your answer form. 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. I. True/False: (2 points each) 1. According to our text, the name of a Java class (e.g. Student) should be the same as the filename (e.g. Student.java) 2. A program can have more than one Constructor. 3. One way to tell the difference between a Constructor and a method is that a Constructor has no parameters. 4. The name of a Constructor must be the same as the class name unless you declare it as type Constructor. 5. You can have Constructors with the same name as a method name, but you must leave off the return value and declare it as type Constructor.. 6. Overloaded methods can have the same name, but must have different numbers of parameters. 7. Methods can have the same name and number of parameters, as long as the types of the parameters are different in at least one case. 8. Methods can have the same name, number and type of parameters, as long as the return type of the methods are different. 9. If you don t implement the tostring() method in a class, then using an instance of that class in a System.out.println(...) statement displays a number. 10. Every method must have the return type declared unless the method doesn t return anything, or the return value is of type boolean. 11. A static method can not access non-static instance variables. CS 102 - Intro. to Programming, Midterm Exam #2, page 1 of 9

12. A method can call itself in Java. 13. If method A( ) calls method B( ) in Java, and method B( ) calls method C( ), then method C( ) may not call methods A( ) or B( ), since that would create an endless loop. 14. The length() method is used for strings however the length property is used for arrays. 15. Every set of if - else - if statements can be alternatively represented using a switch - case statement. 16. The output of the following code in Java is: 2525 int x, answer; for(x=1, answer=0; x<=100; x++) answer = answer + x; System.out.println( answer); 17. The output of the code below is the value: 13 int x=3, y=5, z=2; System.out.println(x + y * z); 18. The output of the following lines of code is: No boolean False = false; if (False = true) System.out.println("Yes"); else System.out.println("No"); 19. The following code is valid (compiles and runs) in Java: 20. The following if statement: for( ; ; ) ; // empty line if( a<b) if( c<d) if( e<f) answer = e; can equivalently be written as: if( (a<b) && (c<d) && (e<f) ) answer = e; CS 102 - Intro. to Programming, Midterm Exam #2, page 2 of 9

21. The output of the program segment below is the text: First One *** boolean value = false; if ( value = true) System.out.print("First One"); else System.out.print("Second One"); System.out.println(" *** "); 22. The following code in Java stores in variable sum the sum of positive integers less than 10: int sum=-1; for(int x=++sum; x<10; x++) sum+=x; 23. The following code prints the words: Grade is: B int x = 87; char result = A ; System.out.print("Grade is: "); if (x>90) result = A ; if (x>80) result = B ; if (x>70) result = C ; if (x>60) result = D ; System.out.print( result); 24. The following code prints the words: score is: 85 char val= A ; int score = 85; switch (val) case ( A ): score = 95; case ( B ): score = 85; case ( C ): score = 75; break; System.out.println("score is: " + score); CS 102 - Intro. to Programming, Midterm Exam #2, page 3 of 9

25. Assume the code shown below, where method first() is called. Output of this segment of code is: Values are: 8 3 class SwapThem int x = 3; int y = 8; void first() swapvalues( x, y); System.out.println("Values are: " + x + " " + y); //end method first() void swapvalues(int a, int b) int temp = x; x = y; y = temp; //end method swapvalues() //end class SwapThem() 26. Assume the code shown below, where method swapvalues1 is called. Output of this segment of code is: Values are: 2 3 int[] numbers = 1,3,2,4; swapvalues1( numbers[1], numbers[2]); System.out.println("Values are: " + numbers[1] + " " + numbers[2]); //... other code public void swapvalues1(int num1, int num2) int temp = num1; num1 = num2; num2 = temp; 27. Assume the code shown below, where method swapvalues2 is called. Output of this segment of code is: Values are: 2 3 int[] numbers = 1,3,2,4; swapvalues2( numbers, 1, 2); System.out.println("Values are: " + numbers[1] + " " + numbers[2]); //... other code public void swapvalues2(int[] numbers, int i, int j) int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; CS 102 - Intro. to Programming, Midterm Exam #2, page 4 of 9

II. Multiple Choice (4 pts. each) 28. Consider the program segment given below. Its output is: int x = 2; int y = 3; String z = ""; System.out.println(x + y + z + " is the answer"); a) 2 + 3 is the answer b) 23 is the answer c) 5 is the answer d) 2 3 is the answer 29. Consider the code given below. Its output is: int x = 3; int y = 6; int z = x+++y; System.out.println("Value is: " + x + y + z); a) Value is: 369 b) Value is: 469 c) Value is: 3710 d) Value is: 4610 30. Consider the code given below. If its output is: 9 12 15 12 16 20 15 20 25 18 24 30 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=3, end=6, first=3, last=6; b) int start=1, end=4, first=9, last=18; c) int start=9, end=4, first=9, last=3; d) int start=3, end=4, first=9, last=18; CS 102 - Intro. to Programming, Midterm Exam #2, page 5 of 9

31. What is the output of the code below if we call the code using: Compare compareinstance = new Compare( 4); class Compare int x=4, y=2; Compare( int x) this.x = x; first(); void first() int n=2; if( second(n,x) && second(y,n)) System.out.print(x + "," + y); System.out.println("Done"); //end method first() boolean second( int a, int b) x = b/a; y = y-x; if( x<3) return true; else return false; //end method second() //end class Compare() a) 4,2 Done b) 2,0 Done c) 0,2 Done d) Done 32. Again consider the code above. What is the output of the code above if we call the code using: Compare compareinstance = new Compare( 8); a) 4,2 Done b) 2,0 Done c) 0,2 Done d) Done CS 102 - Intro. to Programming, Midterm Exam #2, page 6 of 9

33. Consider the program method given below. Its output is: public void handlearray( int[] thenumbers) int x = thenumbers[ 0]; for (int i=0; i<thenumbers.length; i++) if (thenumbers[i] < x) x = thenumbers[i]; System.out.println( x); a) The minimum value in array thenumbers b) The maximum value in array thenumbers c) The average value in array thenumbers d) The sum of the values in array thenumbers 34. Assume you are doing a numbers guessing game, where you are attempting to guess a number between 500 and 750 (this is a binary search). After each guess, you are told if you need to guess higher or lower. What is the maximum number of guesses you would need to find the number? a) 8 b) 9 c) 10 d) 11 35. What is the output of the code given below when problem35driver() is called? public void problem35driver() char[] thearray = 'H','e','r','e','','w','e',' ','g','o'; problem35( thearray); // display array contents for( int i=0; i<thearray.length; i++) System.out.print( thearray[i]); System.out.println(); public void problem35(char[] w) int x = w.length; char c; for (int i=0; i<x/2; 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 - Intro. to Programming, Midterm Exam #2, page 7 of 9

36. What is the output of the program segment at right below when an instance of class Verify is created and used to call method verifyit()? a) -1 b) 0 c) 1 d) 2 class Verify int x = 0; void verifyit() if( first() && second() ) x--; System.out.println( x); boolean first() x++; return true; boolean second() x++; return false; //end class Verify 37. What is the return value of the call: methodb( 2,5); public int methoda( int x, int n) if (n == 0) return 0; else return x + methoda( x, n-1); public int methodb( int x, int n) if (n == 0) return 1; else if (n > 0) return methoda( methodb( x, n - 1), x); return 0; a) 7 b) 10 c) 25 d) 32 CS 102 - Intro. to Programming, Midterm Exam #2, page 8 of 9

38. What is the output of the code given in the two columns below when an instance of class Confuse is created and used to call method startup()? class Confuse int x=2; int y=5; private void first(int y, int x) x += 1; y += 2; private void second(int x, int s) x += 3; y = 7; private void third(int y) setxy( y); x += 2; y += 3; private void setxy( int y) x = y; this.y = x-1; second(y, x); private void display() System.out.println(x + y); public void startup() int x=1, y=2; first(x,y); second(x,y); third( 4); display(); //end class Confuse a) 9 b) 11 c) 13 d) 15 CS 102 - Intro. to Programming, Midterm Exam #2, page 9 of 9