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

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

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

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

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Midterm Examination

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

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

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

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

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Final Examination

Faculty of Science COMP-202A - Foundations of Computing (Fall 2014) - All Sections Final Examination

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

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

COMP102: Test. 26 April, 2006

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Final Examination

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2010) - All Sections Final Examination

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

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

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

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

I. True/False: (2 points each)

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

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

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

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

Last Class. While loops Infinite loops Loop counters Iterations

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

CSE 1223: Exam II Autumn 2016

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

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Full file at

A variable is a name for a location in memory A variable must be declared

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

Computational Expression

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

AP COMPUTER SCIENCE A

Using Java Classes Fall 2018 Margaret Reid-Miller

Place your name tag here

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

Question: Total Points: Score:

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

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

Final Exam. COMP Summer I June 26, points

We now start exploring some key elements of the Java programming language and ways of performing I/O

Programming with Java

CS 231 Data Structures and Algorithms, Fall 2016

CSE 114 Computer Science I

Sample Examination Paper Programming and Software Development

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

Final Exam Practice. Partial credit will be awarded.

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

COMP102: Test 1 Model Solutions

Lara Technologies Special-Six Test

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

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

CONDITIONAL EXECUTION

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Example Program. public class ComputeArea {

I. True/False: (2 points each)

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

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

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

Question: Total Points: Score:

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

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

CS 211: Existing Classes in the Java Library

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

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

COS 126 Midterm 1 Written Exam, Fall 2009

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CIS 110 Introduction to Computer Programming Summer 2018 Midterm. Recitation ROOM :

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

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

CS 312 Midterm 1 Spring 2013

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

CSE 142 Sample Midterm Exam #3

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Transcription:

First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination November 11th, 2013 Examiners: Jonathan Tremblay [Sections 3 (3:30-4:30) and 2 (1:00-2:30)] 18:00-21:00 Dan Pomerantz [Section 1 (10:30-11:30)] Instructions: DO NOT TURN THIS PAGE UNTIL INSTRUCTED This is a closed book examination; only a letter-sized (8.5 by 11 ) crib sheet is permitted. This crib sheet can be single or double-sided; it can be handwritten or typed. Non-electronic translation dictionaries are permitted, but instructors and invigilators reserve the right to inspect them at any time during the examination. Besides the above, only writing implements (pens, pencils, erasers, pencil sharpeners, etc.) are allowed. The possession of any other tools or devices is prohibited. Answer all questions on the scantron sheet. This examination has 16 pages including this cover page, and is printed on both sides of the paper. On page 16, you will find information about useful classes and methods. You may detach page 16 from the examination if you wish. MAKE SURE TO WRITE YOUR NAME AND STUDENT ID ON THE SCANTRON AS WELL AS TO FILL IN THE BUBBLE PROPERLY AT THE BEGINNING OF THE EXAM Scoring The exam will be scored as follows: 1. Questions 1 until 25 will be worth 2 points each (25 questions times 2 points for a total of 50 points) 2. Questions 26 until 39 will be worth 3 points each (14 questions times 3 points for a total of 42 points) 3. Questions 40 to 49 will be worth 1 point each (10 questions times 1 point each for a total of 10 points) 4. Question 50 is just for fun. Question 51 is to help get feedback for the future. Both are worth 0 points. Version-6

COMP-202A - Fall 2013 - Midterm Examination Page 2 Regular multiple choice: 2 points each 1. Which of the following is NOT a legal Java statement: (A) int x = 3; (B) double x = "3"; (C) double x = 7 / 2; (D) double x = 3; (E) double x = 3.0; 2. How many times is the condition of the while loop checked in the following code? int i = 0; while (i < 10) { System.out.println("Repeat!"); i++; (A) 1 (B) 0 (C) 9 (D) 11 (E) 10 3. What will print in the following code snippet? int i = 1; int j = 2; i = j; j = i; System.out.println(i + " " + j); (A) 2 1 (B) 2 2 (C) 1 1 (D) 3 (E) 1 2 4. Suppose you have a method with the following header: public static void fun(double x, int y) Which of the following are legal ways to call the method: I. fun(3, 4.0); II. fun(3, 4); III. fun(4.0, 3); (A) I only (B) I, II, and III

COMP-202A - Fall 2013 - Midterm Examination Page 3 (C) III only (D) II and III (E) II only 5. Which of the following Java expressions do NOT have a value of 0. (A) (int)(((double)1) / 2) (B) (double)(1 / 2) (C) ((double)1) / ((int)2) (D) 1 / 2 (E) (int)1.0 / 2 6. Which of the following Java expressions have a value of 0. (A) "1.0 / 2.0" (B) 1.0 / 2.0 (C) 1 / 2.0 (D) (int)1.0 / 2 (E) (double)(1 * 2) Type of Errors For each of the following answer whether they would cause an compiler error, runtime error, or a bug. In other cases the mentioned error may not cause a problem for the computer and is merely a question of style. In this case you should choose Style In other cases the question may not cause a problem at all in which case you should answer No Error For the purpose of this question, you can use the following definitions: Compiler error : An error that occurs during the compilation phase of your development. In this case no.class file is produced. Runtime error : An error that occurs while you are running your program. Your program starts to execute but terminates before the main method has finished executing. Logical error or bug : An error that results in the wrong results being seen Style error : The error does not make a difference to the computer but is something that may be considered bad code. 7. Omitting a semi-colon at the end of a variable assignment. (A) Bug (logical) Error (B) Style (C) Runtime Error (D) Compiler Error (E) No Error 8. Declaring two variables with the same name in different methods. (A) Compiler Error (B) Style (C) Bug (logical) Error

COMP-202A - Fall 2013 - Midterm Examination Page 4 (D) Runtime Error (E) No Error 9. Dividing an int expression by 0. (A) Style (B) No Error (C) Runtime Error (D) Compiler Error (E) Bug (logical) Error 10. Omitting the main method from a class and then executing that class. (A) Bug (logical) Error (B) Compiler Error (C) Runtime Error (D) No Error (E) Style 11. Storing a double expression into an int. (A) Bug (logical) Error (B) Compiler Error (C) Runtime Error (D) No Error (E) Style 12. What prints as a result of the following code being run? public static void main(string[] args) { int x = 8; int y = 3; foo(x,y); System.out.println(x + " " + y); public static int foo(int x, int y) { x = x * 2; int temp = x; x = y; y = y * 2; x = temp; return x; (A) 16 6 (B) 16 3 (C) 8 3 (D) 3 8 (E) 16 8 13. What prints as a result of the following code being run?

COMP-202A - Fall 2013 - Midterm Examination Page 5 public static void main(string[] args) { int x = 8; int y = 3; foo(x,y); foo(x,y); public static int foo(int y, int x) { System.out.print(y + " " + x + " "); int temp = x; x = y; y = x; return y; (A) 3 8 8 8 (B) 3 8 8 3 (C) 8 3 8 3 (D) 8 3 3 8 (E) 3 8 3 8 14. What value is returned by the following method if the input array is { 2, 3, 4, 1, 2, 1 public static int mystery(int[] array) { int foo = 1; for (int i = 0; i < array.length; i++) { foo = foo * array[i]; return foo; (A) 48 (B) 2 (C) 24 (D) 16 (E) 4 15. What will the following method print when the input number n has value 5 public static void mystery(int n) { for (int i = -1; i < n; System.out.print(i + " ")) { i++; (A) 0 1 2 3 4 (B) -1 0 1 2 3 4 5 (C) 0 1 2 3 4 5 (D) -1 0 1 2 3 4 (E) The code does not compile.

COMP-202A - Fall 2013 - Midterm Examination Page 6 16. Suppose m and n are both int variables with positive values. Which of the following boolean expressions are guaranteed to be equal in value to the expression that follows? (m % n == 0 && n % m == 0) I. m == n II.!(m < n m > n) III.!(m < n && m > n) (A) I, II, and III (B) I and II (C) I and III (D) None of the expressions are guaranteed to be the same. (E) I only 17. How many times does the letter A print in the following code? for (int i = -1; i < 3; i++) { System.out.println("A") (A) 2 (B) 1 (C) 4 (D) 3 (E) 0 18. How many times does the letter B print in the following code? for (int i = 0; i < 6; i++) { for (int j = 0; j <= 5; j++) { System.out.println("B"); (A) 5 (B) 0 (C) 36 (D) 30 (E) 11 19. How many times does the letter C print in the following code? for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 2; k++) { System.out.println("C"); System.out.println("C"); System.out.println("C");

COMP-202A - Fall 2013 - Midterm Examination Page 7 (A) 24 (B) 8 (C) 36 (D) 18 (E) 30 20. How many times does the letter D print in the following code? for (int i = 0; i < 3; i++) { for (int j = i+1; j < 3; j++) { System.out.println("D"); (A) 0 (B) 6 (C) 1 (D) 2 (E) 3 21. What prints in the following question. int[][] x = { { 1, 2, 3, {4, 5, {6, 7; x[0] = new int[4]; System.out.println(x.length); (A) 8 (B) 7 (C) There is a compiler error. (D) 2 (E) 3 22. What does the following method return when the input array is { 1, 2, 3? public static int mystery(int[] array) { int i = 0; int count = 0; while (i <= array.length) { if (array[i] % 2 == 0) { count++; i++; return count; (A) There is a runtime error. (B) 3 (C) There is a compiler error.

COMP-202A - Fall 2013 - Midterm Examination Page 8 (D) 0 (E) 2 23. What String does the following code return if the input to the method is the String with contents ABCD. public static String mystery(string input) { String tricky = ""; for (int i = 0; i < input.length(); i++) { tricky = input.charat(input.length() - i - 1) + tricky; return tricky; (A) DDCCBBAA (B) There is a runtime error. (C) AABBCCDD (D) ABCD (E) DCBA 24. What String does the following code return if the input to the method is the String with contents ABCDE. public static String mystery(string input) { String tricky = ""; for (int i = 0; i < input.length(); i++) { tricky = tricky + input.charat(i); i++; tricky = tricky + input.charat(i); return tricky; (A) There is a runtime error (B) EDCBA (C) EEDDCCBBAA (D) AABBCCDDEE (E) ABCDE 25. What String does the following code return if the input to the method is the String with contents ABCD. public static String mystery(string input) { String tricky = ""; for (int i = 0; i < input.length(); i++) { tricky = tricky + input.charat(i); i++; tricky = tricky + input.charat(i-1); i--; return tricky;

COMP-202A - Fall 2013 - Midterm Examination Page 9 (A) DCBA (B) AABBCCDD (C) ABCD (D) DDCCBBAA (E) There is a runtime error. Longer multiple choice questions (3 points each) Drawing Shapes on a Grid The following questions are based of the following code, please read it carefully. public class Line { public static void main(string[] args) { int[][] grid; final int GRID_SIZE = 10; grid = new int [GRID_SIZE][GRID_SIZE]; AddLineToGrid(grid, 0,0,5,3); DisplayGrid(grid); public static void AddLineToGrid(int[][] grid, int x0,int y0,int x1,int y1) { int xmin = Math.min(x0, x1); int xmax = Math.max(x0, x1); int ymin = Math.min(y0, y1); int ymax = Math.max(y0, y1); int dx = xmax - xmin; int dy = (ymax - ymin); int err = dx+dy ; int e2 = 0; int xpos = xmin; int ypos = ymin; double error = 0; while(xpos <= xmax && ypos <= ymax) { grid[xpos][ypos] = 1; xpos++; ypos += (dy / dx); error += ((double)dy / dx) - (dy / dx); if (error >= 1) { ypos++; public static void DisplayGrid(int[][] grid) { for(int j = 0; j< 10; j++) { for(int i = 0; i< 10; i++)

COMP-202A - Fall 2013 - Midterm Examination Page 10 { if(grid[i][j] == 1) System.out.print("#"); else System.out.print(" "); System.out.println(); 26. What is the significance of writing the word final before the declaration of GRID SIZE? (A) It denotes that the identifier GRID SIZE that follows should be capitalized. (B) It is used by the compiler to specify the GRID SIZE can not have its value changed. (C) It does not have any meaning because this is a midterm exam not a final exam. (D) It is used as a comment to future programmers to suggest to them that they not modify the variable GRID SIZE. (E) It specifies that it is the final or last variable declared in a method. 27. How many arrays are inside of the array grid (A) 5 (B) 3 (C) 10 (D) 100 (E) 0 28. What is the type of the expression grid[3] (A) int[] (B) int (C) int[][] (D) NullPointerException (E) ArrayOutOfBoundsException 29. Which method(s) in the code changes the values stored in grid: (A) main and AddLineToGrid (B) DisplayGrid (C) main (D) main and DisplayGrid (E) AddLineToGrid 30. In the method DisplayGrid for which value stored in grid will a # be printed: (A) 1 (B) 0 (C) DisplayGrid does not print anything (D) null (E) #

COMP-202A - Fall 2013 - Midterm Examination Page 11 31. What is stored in grid when we call AddLineToGrid(0,0,1,1);: (A) Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Col 0: 0 0 0 0 0 0 0 0 0 0 Col 1: 0 0 0 0 0 0 0 0 0 0 Col 2: 0 0 0 0 0 0 0 0 0 0 Col 3: 0 0 0 0 0 0 0 0 0 0 Col 4: 0 0 0 0 0 0 0 0 0 0 Col 5: 0 0 0 0 0 0 0 0 0 0 Col 6: 0 0 0 0 0 0 0 0 0 0 Col 7: 0 0 0 0 0 0 0 0 0 1 Col 8: 0 0 0 0 0 0 0 0 1 0 Col 9: 0 0 0 0 0 0 0 1 0 0 (B) Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Col 0: # 0 0 0 0 0 0 0 0 0 Col 1: 0 # 0 0 0 0 0 0 0 0 Col 2: 0 0 0 0 0 0 0 0 0 0 Col 3: 0 0 0 0 0 0 0 0 0 0 Col 4: 0 0 0 0 0 0 0 0 0 0 Col 5: 0 0 0 0 0 0 0 0 0 0 Col 6: 0 0 0 0 0 0 0 0 0 0 Col 7: 0 0 0 0 0 0 0 0 0 0 Col 8: 0 0 0 0 0 0 0 0 0 0 Col 9: 0 0 0 0 0 0 0 0 0 0 (C) RIGHT ANSWER - Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Col 0: 1 0 0 0 0 0 0 0 0 0 Col 1: 0 1 0 0 0 0 0 0 0 0 Col 2: 0 0 0 0 0 0 0 0 0 0 Col 3: 0 0 0 0 0 0 0 0 0 0 Col 4: 0 0 0 0 0 0 0 0 0 0 Col 5: 0 0 0 0 0 0 0 0 0 0 Col 6: 0 0 0 0 0 0 0 0 0 0 Col 7: 0 0 0 0 0 0 0 0 0 0 Col 8: 0 0 0 0 0 0 0 0 0 0 Col 9: 0 0 0 0 0 0 0 0 0 0 (D) Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Col 0: 1 1 1 1 1 1 1 1 1 1 Col 1: 1 1 1 1 1 1 1 1 1 1 Col 2: 1 1 1 1 1 1 1 1 1 1 Col 3: 1 1 1 1 1 1 1 1 1 1 Col 4: 1 1 1 1 1 1 1 1 1 1 Col 5: 1 1 1 1 1 1 1 1 1 1 Col 6: 1 1 1 1 1 1 1 1 1 1 Col 7: 1 1 1 1 1 1 1 1 1 1 Col 8: 1 1 1 1 1 1 1 1 1 1 Col 9: 1 1 1 1 1 1 1 1 1 1 (E) Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Col 0: 0 0 0 0 0 0 0 0 0 0 Col 1: 0 0 0 0 0 0 0 0 0 0 Col 2: 0 0 0 0 0 0 0 0 0 0 Col 3: 0 0 0 0 0 0 0 0 0 0 Col 4: 0 0 0 0 0 0 0 0 0 0 Col 5: 0 0 0 0 0 0 0 0 0 0 Col 6: 0 0 0 0 0 0 0 0 0 0 Col 7: 0 0 0 0 0 0 0 0 0 0 Col 8: 0 0 0 0 0 0 0 0 0 0 Col 9: 0 0 0 0 0 0 0 0 0 0

COMP-202A - Fall 2013 - Midterm Examination Page 12 32. What does AddLineToGrid do: (A) Draw a circle (B) Break an infinite loop (C) Nothing (D) Change values in grid (E) Draw a line 33. The variable error serves which purpose: (A) To keep track of the number of loops (B) Measure the distance from y0 to y1 (C) Keep track of accumulated mistake. (D) Measure the distance from x0 to x1 (E) Nothing. It is just to confuse us. 34. What sort of exception will occur if you call the method AddLineToGrid with input grid,0,0,0,0? (A) InputMismatchException (B) OutOfMemoryException (C) ArrayIndexOutOfBoundsException (D) ArithmeticException (E) NullPointerException 35. Which input for AddLineToGrid gives the following output: (Note the that # are at positions (2,0), (3,2),(4,4), (5,6), and (6,8)) 0123456789 0 # 1 2 # 3 4 # 5 6 # 7 8 # 9 (A) AddLineToGrid(grid,6,2,8,0) (B) AddLineToGrid(grid,8,2,6,0) (C) AddLineToGrid(grid,6,8,2,0) (D) AddLineToGrid(grid,8,6,0,2) (E) Nothing 36. The method DisplayGrid has hardcoded the size of the grid of 10 into the loop. What would happen if we changed the line of the main method final int GRID SIZE = 10 to be final int GRID SIZE = 20. (A) Everything will work perfectly still. (B) NullPointerException

COMP-202A - Fall 2013 - Midterm Examination Page 13 (C) There will be a bug as only part of the grid will print. (D) ArrayIndexOutOfBoundsException (E) Compiler Error since you can t change a final. 37. To avoid hard coding the value 10 into the loop, we should replace the header of the 2 for loops, which currently are for (int j = 0; j < 10; j++) { for (int i = 0; i < 10; i++) { For the most general answer, keeping in mind that the grid might not always be a square grid, what should we change the loop boundaries to? (A) for (int j = 0; j < grid.length; j++) { for (int i = 0; i < grid.length; i++) { (B) for (int j = 0; j < grid[0].length; j++) { for (int i = 0; i < grid[0].length; i++) { (C) for (int j = 0; j < grid.length; j++) { for (int i = 0; i < grid[0].length; i++) { (D) for (int j = 0; j < grid[0].length; j++) { for (int i = 0; i < grid.length; i++) { (E) for (int j = 0; j < grid.length * grid[0].length; j++) { for (int i = 0; i < grid.length; i++) { 38. How many calls of AddLineToGrid would you need to draw a triangle: (A) 1 (B) 3 (C) 0 (D) 2 (E) Impossible 39. Which statement creates the array to store the grid? (A) public static void main(string[] args) (B) int[][] grid; (C) None. There is no grid. (D) grid = new int[grid SIZE][GRID SIZE]; (E) final int GRID SIZE = 10;

COMP-202A - Fall 2013 - Midterm Examination Page 14 True false questions (1 point each) In the following questions answer A for true or B for false. 40. In Java, methods can be defined inside of other methods. (A) TRUE (B) FALSE 41. In Java, you can change the value of a reference variable declared in the main method in another method. (A) FALSE (B) TRUE 42. In Java, all methods must be declared inside of a class. (A) FALSE (B) TRUE 43. A variable of type char can be used to store text such as HelloWorld! (A) TRUE (B) FALSE 44. The Java compiler can detect infinite loops. (A) FALSE (B) TRUE 45. The Java compiler can detect array out of bounds errors. (A) FALSE (B) TRUE 46. The Java compiler can detect undeclared variable errors. (A) FALSE (B) TRUE 47. What is the value of the following boolean expression? true && (false!false) && (true false) (A) TRUE (B) FALSE 48. Once an array is created, you can not change its size. (A) TRUE (B) FALSE 49. The following code prints 5. System.out.println("" + 3 + 2); (A) TRUE

COMP-202A - Fall 2013 - Midterm Examination Page 15 (B) FALSE 50. Computer programming is fun all the time! (This question will not be graded) (A) FALSE (B) TRUE 51. I wish there were written programming questions on the exam! (This question will not be graded) (A) FALSE (B) TRUE

COMP-202A - Fall 2013 - Midterm Examination - Appendix Page 16 SUMMARY OF JAVA STANDARD LIBRARY METHODS FOR SELECTED CLASSES String (package java.lang) Methods: public boolean equals(object anobject): Compares this String to anobject. public int length(): Calculates the length of this String. public boolean equalsignorecase(string anotherstring): Compares, ignoring case considerations, this String to anotherstring. public int compareto(string anotherstring): Compares this String to anotherstring lexicographically; returns a negative value if this String occurs before anotherstring, a positive value if this String occurs after anotherstring, and 0 if both Strings are equal. public int comparetoignorecase(string anotherstring): Compares, ignoring case considerations, this String to anotherstring lexicographically; returns a negative value if this String occurs before anotherstring, a positive value if this String occurs after anotherstring, and 0 if both Strings are equal. public String substring(int start, int finish): Returns a new String composed of the this String starting from index start and up to, but not including index of finish public String replace(char c, char d) : Returns a new String with all occurrences of the character c in the this String replaced by the character d. public char[] tochararray(): Converts this String to a new character array. File (package java.io) Methods: public FileSstring pathname): Creates a new File instance that corresponds to the given pathname. Scanner (package java.util) Methods: public Scanner(Inputstream source): Constructs a new Scanner that produces values scanned from the specified input stream. public Scanner(File f): Constructs a new Scanner that produces values scanned from the specified File public double nextdouble(): Scans the next token of the input as a double. public boolean nextboolean(): Scans the next token of the input as a boolean. public int nextint(): Scans the next token of the input as an int. public String nextline(): Advances this Scanner past the current line and returns the input read. public boolean hasnextline(): Checks whether there are further lines left to scan. PrintStream (package java.io) Methods: public void print(boolean b): Prints boolean value b. public void print(double d): Prints double value d. public void print(int i): Prints int value i. public void print(object o): Prints Object o. public void print(string s): Prints String s. public void println(): Terminates the current line by writing the line separator string. public void println(boolean b): Prints boolean value b and then terminates the line. public void println(double d): Prints double value d and then terminates the line. public void println(int i): Prints int value i and then terminates the line. public void println(object o): Prints Object o and then terminates the line. public void println(string s): Prints String s and then terminates the line. Math (package java.lang) Methods: public static double pow(double a, double b): Returns the value of a raised to the power of b. public static double sqrt(double a): Returns the correctly rounded positive square root of double value a. public static double random(): Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. public static double exp(double a): Returns Euler s number e raised to the power of double value a. (base e) of double value a. of double value a.