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

Similar documents
CS111: PROGRAMMING LANGUAGE II

Selected Questions from by Nageshwara Rao

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

Array. Prepared By - Rifat Shahriyar

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

Lecture 13: Two- Dimensional Arrays

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

Computer Science is...

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

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

Final Exam. COMP Summer I June 26, points

Introduction to Programming Using Java (98-388)

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

Chapter 6 Single-dimensional Arrays

CS 307 Midterm 2 Fall 2009

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

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

CS 307 Midterm 2 Spring 2008

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

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

H212 Introduction to Software Systems Honors

CS 307 Midterm 2 Fall 2010

APCS Semester #1 Final Exam Practice Problems

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

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

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

1. Expressions - 1 point each. -1 for missing or extra " OR.0. differences in capitalization of true and false okay. No limit on points off.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS 113 MIDTERM EXAM 2 SPRING 2013

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Practice Midterm 1. Problem Points Score TOTAL 50

More non-primitive types Lesson 06

Notes from the Boards Set BN19 Page

CS 159 Midterm #1 Review Questions

Primitive Data, Variables, and Expressions; Simple Conditional Execution

I. True/False: (2 points each)

CS1083 Week 2: Arrays, ArrayList

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

CS 307 Midterm 1 Fall 2007

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Chief Reader Report on Student Responses:

Last Name: Circle One: OCW Non-OCW

CS 139 Practice Midterm Questions #2

Final Exam Practice. Partial credit will be awarded.

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

CSE 11 Midterm Fall 2008

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

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

CSC 1051 Arrays - Review questions

AP Computer Science A Unit 7. Notes on Arrays

CS170 Introduction to Computer Science Midterm 2

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Spring 2010 Java Programming

CS 307 Midterm 2 Spring 2011

Computer Science II (20082) Week 1: Review and Inheritance

CS 101 Fall 2006 Midterm 3 Name: ID:

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Week 6 CS 302. Jim Williams, PhD

University Interscholastic League. Computer Science Competition

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

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

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

STUDENT LESSON A12 Iterations

Ticket Machine Project(s)

CS 177 Week 15 Recitation Slides. Review

Chapter 4 Defining Classes I

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Practice Questions for Chapter 9

Assignment 1 due Monday at 11:59pm

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

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

FORM 2 (Please put your name and form # on the scantron!!!!)

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

Final Exam COMP Fall 2004 Dec 16, 2004

Fundamentals of Programming Data Types & Methods

In Java there are three types of data values:

CS313D: ADVANCED PROGRAMMING LANGUAGE

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Prelim One Solution. CS211 Fall Name. NetID

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

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

CS 1331 Exam 1 ANSWER KEY

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

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

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

PIC 10A. Final Review: Part I

Practice Midterm 1 Answer Key

PIC 10A. Final Review

CS 101 Exam 1 Spring 200 Id Name

Section 2.2 Your First Program in Java: Printing a Line of Text

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

The Basic Parts of Java

9 Working with the Java Class Library

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

Transcription:

Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts each F 24-27 5 1 or 2 pts each G 28-32 5 1 pt each H 33-38 6 1 pt each I 39-46 8 1 pt each Total out of 50 Do Not Open Until the Exam Begins! Note There are NO syntax errors in this exam! Unless the question specifically states that the code might not comple, if you see one, it DOES NOT EXIST!!! A modified String API is on the last page of the exam. the mgmt Page 1

Part A From Exam 1 - worth 6% (1 pt each) Given these variables and corresponding initializations: Variable Declarations int in1, in2; double db1, db2; char ch1, ch2; Variable Initializations in1 = 1; in2 = 2; db1 = 1.1; db2 = 2.2; boolean b1, b2; String s1, s2; ch1 = 'a'; ch2 = 'b'; b1 = true; b2 = false; s1 = "hello"; s2 = "world"; What do these printlns display on the screen? Mark an X if they do not compile. 1 2 3 4 5 6 System.out.println( in1 / in2 ); System.out.println( in1 % in2 ); System.out.println( in1 * db1 ); System.out.println( ch1!= ch2 ); System.out.println( b1 b2 ); System.out.println ( s1 + " " + (in1 + in2) ); Page 2

Part B Types of Data - worth 6% (1 pt each) Mark [T] in the blank for any statements that are true. Leave false statements blank 7 [ ] Primitive types of variables include int, double, char, Boolean, and String. 8 [ ] A primitive type of variable can have only one name and one value. 9 [ ] A reference type of variable such as an object of the Card class and the Deck class that you wrote for Program 09 can have multiple references and can store multiple values of different types. 10 [ ] A primitive type variabls can be accessed, used, and changed only by code that is within the braces where the primitive variable is declared. 11 [ ] A reference types of variable such as an object of the Card class and the Deck class can be accessed, used, and changed by code that is outside the braces where the variable is declared. 12 [ ] All of the data stored in an array must be the same type of data. Page 3

Part C Representing the Structure of Data in Memory - 4 points (1 pt each) In the following questions, a series of drawings similar to those Jim used on the board in class to represent how the data is stored in memory are shown. Mark the drawing that best represents the type of data listed. 13. Mark the sketch that best shows how a primitive variable is represented: int num = 42; 14. Mark the sketch that best shows how a String object is represented: String str = Hi ; Page 4

15. Mark the sketch that best shows how a one-dimensional array is represented: int [ ] list = 1, 2, 3 ; 16. Mark the sketch that best shows how a two-dimensional array is represented: int [ ] [ ] mat = 1, 2, 3, 4 ; Page 5

Part D String Objects worth 5 points (1 pt each ) 17. 1 point Given this code fragment specifically the underlined code: String s1 = "Hi"; String s2 = new String( "Hi" ); if ( s1 == s2 ) System.out.println( "They are equal." ); else System.out.println( "They are NOT equal." ); Mark T in the blank for any of the statements that are true. Leave the false statements unmarked. This will not compile because the use of the equality operator for Strings is not allowed in Java. This compiles but does not always work properly. Depending on the way the Strings are initialized, it could return true or it could return false. This compiles and always works properly. It is always safe to use. 18. 2 points ( 1 error = -1, more than 1 error = -1 ) Given this code fragment: String s3 = "01234"; for( int i = s3.length( ) - 1; i > 0; i-- ) System.out.print( s3.charat( i ) ); System.out.println( ); Write the output for this code in the box below: Page 6

19. 2 points ( 1 error = -1, more than 1 error = -2 ) Given this code fragment: String s4 = "Hello"; for( int i = 0; i < s4.length( ); i++ ) System.out.print(s4.charAt(s4.length( ) - 1 - i)); System.out.println( ); Write the output for this code in the box below: Part E One Dimensional Arrays worth 5 points 20. 1 point Given this method definition: static public int mysterymethod1( int [ ] numbers, int key ) for ( int i = 0; i < numbers.length; i++) if ( numbers[ i ] == key ) return i; return -1; What does this method do? (Do NOT write what the code does: It is traversing the array. Rather, write what the method accomplishes when it is finished)? What would be a better name for this method? Page 7

21. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] list = 4, 2, 8, 5, 3, 1 ; mysterymethod2( list ); And this method definition: static public void mysterymethod2( int [ ] numbers ) for ( int i = 0; i < numbers.length; i++ ) if ( numbers[ i ] % 2 == 1) System.out.print( numbers[ i ] + " " ); System.out.println( ); Will the method, mysterymethod2 crash when it is executed [ yes ] / [ no ]? If the answer is [ yes ], why? If the answer is [ no ], write the output in the box below: Page 8

22. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] list = 4, 2, 8, 5, 3, 1 ; mysterymethod4( list ); And this method definition: static public void mysterymethod4( int [ ] list ) int temp = list[0]; for ( int i = 0; i < list.length - 1; i++ ) list[i] = list[i+1]; list[list.length-1] = temp; Before this method begins execution, the arrangement of the values in the array looks like this: [0] [1] [2] [3] [4] [5] 4 2 8 5 3 1 What is the arrangement of values in the array when the method is finished? [0] [1] [2] [3] [4] [5] Page 9

23. 2 points ( 1 error = -1, more than 1 error = -2 ) Given this main method code: static public void main( String [ ] args ) int [ ] list = 4, 2, 8, 5, 3, 1 ; mysterymethod3( list ); And this method definition: static public void mysterymethod3( int [ ] list ) for ( int i = 0; i < list.length; i++ ) int hiindex = i; for( int j = i + 1; j < list.length; j++) if ( list[j] > list[hiindex] ) hiindex = j; int temp = list[hiindex]; list[hiindex] = list[i]; list[i] = temp; Before this method begins execution, the arrangement of the values in the array looks like this: [0] [1] [2] [3] [4] [5] 4 2 8 5 3 1 The method has two loops. The outer loop uses the loop variable i. The inner loop uses the loop variable j. After the end of the first iteration of the outer loop ( the loop that uses the loop variable, i) what is the arrangement of values in the array? [0] [1] [2] [3] [4] [5] Page 10

Part F Two Dimensional Arrays worth 5 points 24. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] [ ] mat = 0, 1, 2, 3, 4, 5 ; System.out.println( mysterymethod1( mat ) ); And this method definition: static public int mysterymethod1( int [ ] [ ] mat ) int number = 0; for ( int row = 0; row < mat.length; row ++) for( int col = 0; col < mat[row].length; col++) number = number + mat[row][col]; return number; What is the output of the println? 25. 1 point Given this main method code: static public void main( String [ ] args ) int [ ] [ ] mat2 = 0, 1, 2, 3, 4, 5, 6, 7, 8 ; System.out.println( mysterymethod2( mat2 ) ); And this method definition: static public int mysterymethod2( int [ ] [ ] mat2 ) int number = 0; for ( int i = 0; i < mat2.length; i++) number = number + mat2[i][i]; return number; What is the output of the println? Page 11

Given this main method code: static public void main( String [ ] args ) int [ ] [ ] mat2 = 0, 1, 2, 3, 4, 5, 6, 7, 8 ; int [ ] [ ] mat3 = 0, 1, 2, 3, 3, 3, 0, 1, 2 ; System.out.println( mysterymethod3( mat2 ) ); System.out.println( mysterymethod3( mat3 ) ); And this method definition: static public boolean mysterymethod3( int [ ] [ ] mat2 ) int toprow = 0, bottomrow = mat2.length-1; while( toprow < bottomrow ) int col = 0, right = mat2[0].length; while( col < right ) if ( mat2[toprow][col]!= mat2[bottomrow][col] ) return false; col = col + 1; toprow = toprow + 1; bottomrow = bottomrow - 1; return true; 26. (1 point) What does this method do? (Do NOTwrite what the code does: It is traversing the array. or It returns true. Rather, write what the method accomplishes when it is finished)? 27. (2 points) What is the output of the two printlns? Println #1: Println #2: Page 12

Part G Properties of Arrays worth 5 points Below is a one dimensional array named list and a variable named count. Use this array for your answers to the questions in this part: list 4 6 1 8 6 3 9????????? count 7 28. 1 point When we work with one dimensional arrays that are not full, we have to have a variable named count to use when we traverse the loop. Why? Mark the true statements in the following list with a T. Leave the false statements unmarked. 1 point each 29 [ ] The array is full when the value of count is equal to list.length. 30 [ ] If there is room in the array, new values go in element list[count]. 31 [ ] The index of the last valid element of the array is list[list.length]. 32 [ ] The following code fragment effectively deletes the last element in the array: count = count 1; Page 13

Part H OOP worth 6 points (1 pt each) 33. Below is the definition of the class Coin used in lecture. Circle any and all constructors ( if any are present in the code ): public class Coin private int value; private String name; public int getvalue( ) return value; Coin ( int v, String n ) setvalue( v ); setname( n ); Coin( ) this( 1, "Penny"); private void setvalue ( int newvalue ) if ( newvalue > 0 ) value = newvalue; else value = 0; public String getname( ) return name; public String tostring( ) return "Name: " + name + " --- value: " + value + " cents"; private void setname ( String newname ) if (newname == null) name = "Error"; else name = newname; 34. In the diagram label the default constructor if one exists in the code. Page 14

35. Below is the definition of the class Coin used in lecture. Circle any and all accessors ( if any are present in the code ): public class Coin private int value; private String name; public int getvalue( ) return value; Coin ( int v, String n ) setvalue( v ); setname( n ); Coin( ) this( 1, "Penny"); private void setvalue ( int newvalue ) if ( newvalue > 0 ) value = newvalue; else value = 0; public String getname( ) return name; public String tostring( ) return "Name: " + name + " --- value: " + value + " cents"; private void setname ( String newname ) if (newname == null) name = "Error"; else name = newname; 36 In the following code fragment, the first line declares and news a Coin object named c. The second line must print the value of this Coin object. Finish the code: Coin c = new Coin( 10, Dime ); System.out.println( ); Page 15

The following code fragment declares and initializes an array of Coin object that can contain ten coins with different values and different names. Coin[ ] coincolletion = new Coin [ 10 ]; int count = 0; count = readfile( coincollection, args[ 0 ] ); 37. Write the code needed to print the value and name of the last Coin in the array, coincollection : 38. Write the code needed to print the name of the first Coin in the array, coincollection : Page 16

Part I Scope (ownership) of Variables worth 8 points (1 point each) public class PartI1 static public void main( String [ ] args ) int number = 42; System.out.println( number ); // print #1 testmethod( number ); System.out.println( number ); // print #4 static void testmethod( int number ) System.out.println( number ); // print #2 number = 0; System.out.println( number ); // print #3 What is the output for each of the numbered printlns? 39. Print #1: 40. Print #2: 41. Print #3: 42. Print #4: Page 17

public class PartI2 static public void main( String [ ] args ) int [ ] numberlist = 42 ; System.out.println( numberlist[ 0 ] ); // print #1 testmethod( numberlist ); System.out.println( numberlist[ 0 ] );; // print #4 static void testmethod( int [ ] numberlist ) System.out.println( numberlist[ 0 ] ); // print #2 numberlist[ 0 ] = 0; System.out.println( numberlist[ 0 ] ); // print #3 What is the output for each of the numbered printlns? 43. Print #1: 44. Print #2: 45. Print #3: 46. Print #4: =============================================== This is a modified partial String API for your use on the exam. char charat(int index) Returns the char value at the specified index. boolean equals(string str) Compares this string to the argument int length() Returns the length of this string. Page 18