CS 180 Fall 2006 Exam II

Similar documents
CS 180 Fall 2006 Final Exam

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Introduction. Introduction

CS 251 Intermediate Programming GUIs: Components and Layout

Agenda. Container and Component

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

MIT AITI Swing Event Model Lecture 17

First Name: AITI 2004: Exam 2 July 19, 2004

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

H212 Introduction to Software Systems Honors

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

CS 3331 Advanced Object-Oriented Programming Final Exam

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

First Name: AITI 2004: Exam 2 July 19, 2004

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Do not turn to the next page until the start of the exam.

Systems Programming Graphical User Interfaces

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Part I: Learn Common Graphics Components

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

CS Exam 1 Review Suggestions

Graphical User Interfaces. Comp 152

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CS 11 java track: lecture 3

Java Programming Lecture 6

Graphical User Interfaces. Swing. Jose Jesus García Rueda

AP CS Unit 11: Graphics and Events

Containers and Components

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

To gain experience using GUI components and listeners.

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Window Interfaces Using Swing Objects

Class 16: The Swing Event Model

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Window Interfaces Using Swing Objects

Window Interfaces Using Swing. Chapter 12

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

GUI Forms and Events, Part II

CS 209 Programming in Java #10 Exception Handling

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CS 177 Spring 2010 Exam I

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

SINGLE EVENT HANDLING

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Do not turn to the next page until the start of the exam.

CS 180 Final Exam Review 12/(11, 12)/08

Instruction to students:

CS Fall Homework 5 p. 1. CS Homework 5

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

PIC 20A GUI with swing

Final Examination Semester 2 / Year 2011

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Contents Chapter 1 Introduction to Programming and the Java Language

Full file at Chapter 2 - Inheritance and Exception Handling

1 Short Answer (10 Points Each)

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Final Exam CS 251, Intermediate Programming December 13, 2017

Part 3: Graphical User Interface (GUI) & Java Applets

Introduction to the JAVA UI classes Advanced HCI IAT351

Swing/GUI Cheat Sheet

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Graphics User Defined Forms, Part I

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

CS Exam 3 - Spring 2010

CS Exam 3 - Spring 2010

The Islamic University Gaza Department of Electrical & Computer Engineering. Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324

RAIK 183H Examination 2 Solution. November 10, 2014

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

GUI in Java TalentHome Solutions

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Programming Languages and Techniques (CIS120)

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Midterm assessment - MAKEUP Fall 2010

Programming - 2. Common Errors

Graphic User Interfaces. - GUI concepts - Swing - AWT

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

CS 251 Intermediate Programming GUIs: Event Listeners

Example: Building a Java GUI

Final Examination Semester 2 / Year 2010

RAIK 183H Examination 2 Solution. November 11, 2013

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Example: Building a Java GUI

Chapter 6: Graphical User Interfaces

BASICS OF GRAPHICAL APPS

Lecture 5: Java Graphics

Transcription:

CS 180 Fall 2006 Exam II There are 20 multiple choice questions. Each one is worth 2 points. There are 3 programming questions worth a total of 60 points. Answer the multiple choice questions on the bubble sheet given and the programming questions on the exam booklet. Fill in the Instructor, Course, Signature, Test, and Date blanks. For Instructor put your Recitation Instructor s last name. For Course put CS 180. For Test put Exam 2. Fill in the bubbles that correspond to your name, section and Student ID in the bubble sheet. For your section number, use 0830, 0930, 1030, 1130,... based on the start time of your Friday recitation. Consult the following list: 08:30 recitation in LWSN B134: Elizabeth Blythe 09:30 recitation in LILY G401: Matt Carlson 10:30 recitation in LILY G424: Matt Carlson 10:30 recitation in CIVL 1266: Alvin Law 11:30 recitation in REC 122: Alvin Law 12:30 recitation in LILY G424: Isuru Ranaweera 01:30 recitation in REC 308: Nick Sumner 02:30 recitation in LWSN B134: Nick Sumner 03:30 recitation in REC 226: Isuru Ranaweera For your student ID, use the 10 digit ID number on your student ID card. DO NOT USE YOUR SOCIAL SECURITY NUMBER! Exams without names will be graded as zero. Only the answers on the bubble sheet will be counted. The questions will be discarded. Recitation Start Time Recitation TA s Name Student Last Name Student First Name

Part I. Multiple Choice Questions (2 points each): 1. What happens when you try to assign a value larger than the maximum possible integer to an int variable? (a) The program terminates (b) You get an overflow error (c) The value becomes a negative value (d) The value becomes zero 2. Which of the following patterns is produced when the following code snippet is executed? for(int i = -2; i <= 2; i++) for(int j = -2; j <= 2; j++) if(i < j) System.out.print("*"); else System.out.print("."); System.out.println(); (a) *... **... ***.. ****. ***** (b) *... **... ***.. ****. ***** (c)......*...**..***.**** (d).****..***...**...*... 1

3. What keyword is used to specify that a data member is a class data member (shared among all instances of that class)? (a) final (b) shared (c) public (d) static 4. Which of the following is a characteristic of an overloaded method? (a) it must have a fixed number of parameters (b) it could never be a constructor (c) it shares a name with another method in the same class (d) it always returns void 5. What is the result of x when the following loop exits? (a) 5 (b) 10 (c) 9 (d) 6 int x = 0; for(int i = 0; i < 10; i++) if(i == 5) continue; x++; 6. Which of the following is a true statement regarding a constructor? (a) it has no return type (b) its return type changes based on how you write it (c) its return type is void (d) its return type is int 7. what is the result of count after the following loop exits? (a) 8 (b) 11 (c) 9 (d) 10 int count = 10; do count--; while(count >= 10); 2

8. Which of the following is true about exceptions in Java? I. A RuntimeException does not need to be caught by a try/catch block. II. A NumberFormatException must be caught by a try/catch block. III. A NullPointerException does not need to be caught by a try/catch block. (a) I only (b) I and III (c) II and III (d) III only 9. Given the program below, what will be the values of asum and bsum? public class MyArray private int [ ] X=3, 4, 5; public MyArray( ) public int value( ) return(x[0]); public int value(int k) return(x[k]); public static void main (String[ ] args) MyArray [ ] Y = new MyArray[10]; int asum = 0, bsum = 0; for(int i=0; i<10; i++) Y[i]=new MyArray( ); asum = asum + Y[i].value( ); for(int j=0; j<3; j++) bsum = bsum + Y[i].value(j); System.out.println("asum: "+ asum + " bsum: " + bsum); (a) asum: 120 bsum: 120 (b) asum: 30 bsum: 120 (c) asum: 120 bsum: 30 (d) asum: 30 bsum: 30 3

10. Which of the following is true about try/catch blocks in Java? I. All try/catch blocks must have a finally block. II. A try/catch block is limited to two or less catch blocks. III. Barring the Java virtual machine from exiting, the finally block will always be executed. (a) I and III (b) I only (c) III only (d) I and II 11. What will the word apple become after being encrypted 101 times using the following encryption table? a -> e e -> l l -> p p -> a (a) eaapl (b) pllea (c) leeap (d) apple 12. What is the output of the following code? try int num = Integer.parseInt("four thousand two hundred and ninety-five"); System.out.println("Your number is: " + num + "."); catch (NumberFormatException n) System.out.println("You don t have a number."); catch (Exception e) System.out.println("Something went terribly terribly wrong."); finally System.out.println("Number parsed successfully!"); (a) You don t have a number. Number parsed successfully! (b) Something went terribly terribly wrong. Number parsed successfully! (c) Your number is: 4295. Number parsed successfully! (d) You don t have a number. 4

13. Which subclass of Throwable is an exception checked at compile time? (a) NullPointerException (b) RuntimeException (c) IOException (d) ArrayIndexOutOfBoundsException 14. What is displayed by the following code? String result = ""; int[] arrr = 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ; for ( int index = 0; index < 10; ++index ) result += (arrr[ index*index ] + " "); System.out.println( result ); (a) 9 8 7 6 5 4 3 2 1 0 (b) 0 1 4 9 16 25 36 49 64 81 (c) 9 8 5 0 (d) an ArrayIndexOutOfBoundsException 15. Which layout manager places components in one of five regions: north, south, east, west, and center? (a) AbsoluteLayout (b) GridLayout (c) BorderLayout (d) FlowLayout 16. A frame s designates the area of the frame excluding the title, menu bar, and the border. (a) content pane (b) layout manager (c) event listener (d) action listener 17. Clicking a mouse button will always generate which event? (a) MouseButtonEvent (b) ActionEvent (c) MouseClickEvent (d) MouseEvent 5

18. What is displayed by the following code? (a) 0, 9 void shivertimbers( int[] yarrr ) int[] local = yarrr; for ( int jolly = 0; jolly < local.length/2; ++jolly ) int blackbeard = local[jolly]; local[jolly] = local[local.length - 1 - jolly]; local[local.length - 1 - jolly] = blackbeard;... int[] roger = 0, 78, 74, 99, 69, 9 ; shivertimbers( roger ); System.out.print( roger[0] ); System.out.println( ", " + roger[5] ); (b) 74, 0 (c) 9, 0 (d) 0, 74 19. What is displayed by the following code? ArrayList plank = new ArrayList( 5 ); int sum = 0; for ( int peg = 0; peg < 20; ++peg ) plank.add( peg, new Integer(peg) ); for ( int ipatch = 0; ipatch < 20; ipatch+=2 ) sum += ((Integer)plank.get( ipatch )).intvalue(); System.out.println( sum ); (a) an InvalidArgumentException (b) 210 (c) 90 (d) an ArrayIndexOutOfBoundsException 6

20. What is the value of arrr after the following? int[][] arrr = 0, 0,1, 0,1,2, 0,1,2,3 ; for ( int i = 0; i < arrr.length; ++i ) for ( int j = 0; j < arrr[i].length; ++j ) arrr[i][j] = arrr[3-j][3-i]; (a) 0, 0, 1, 0, 1, 1, 0, 0, 0, 0 (b) 3, 2, 2, 1, 1, 2, 0, 1, 2, 3 (c) 0, 1, 1, 2, 2, 1, 3, 2, 1, 0 (d) 0, 0, 1, 0, 1, 2, 0, 1, 2, 3 The version of your test is A. Please FILL IN CIRCLE (A) for the TEST FORM field on the BUBBLE SHEET directly under the DATE field and turn in your exam booklet and answer sheet to the stack labeled (A) in the front of the classroom. Thank you. 7

This page is left blank intentionally. 8

Part II. Programming Questions (60 points total): 1. (20 points) You are creating a High-Low game. The first player will enter the magic number. Your program must accept a magic number greater than zero and less than 100. You should continue prompting for the magic number until a valid number is entered. You should then prompt the second player for their guess. You should print out Higher if the magic number is higher than the guess and you should print out Lower if the magic number is less than the guess. Continue prompting the user until they have guessed the number. When the magic number has been guessed you should print out a corresponding message and then end the program. You should use the JOptionPane to prompt for input and display output. Remember it will return a string you will have to parse. You can assume all input will be integers. You are not required to write methods. All of your code can be in the main function. 9

Solution for programming question 1: 10

2. (20 points) You are given a partially complete SimpleGUI class which pops up a GUI with one text field, two buttons ( ok and cancel ), and a feedback label. You must augment this class to make it respond to button events. Implement the following behavior: ok button pushed: Set the feedback label to the text in the textfield. Clear the textfield. cancel button pushed: Set the feedback label to cancel pushed. Clear the textfield You must add the necessary code to the SimpleGUI constructor in order for this class to receive action events. You must add the necessary code to implement the described behavior. If you need to alter the class declaration, you should be sure to do that as well. SimpleGUI template is given on the following pages. 11

This page is left blank intentionally. 12

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SimpleGUI extends JFrame private Container contentpane; private JTextField textfield; private JButton ok, cancel; private JLabel feedback; private JPanel panel; public static void main( String[] args ) SimpleGUI gui = new SimpleGUI(); gui.pack(); gui.setvisible( true ); public SimpleGUI() contentpane = getcontentpane(); contentpane.setlayout( new GridLayout( 3, 1 ) ); textfield = new JTextField(); textfield.setcolumns( 22 ); contentpane.add( textfield ); panel = new JPanel( new FlowLayout() ); ok = new JButton( "OK" ); cancel = new JButton( "Cancel" ); panel.add( ok ); panel.add( cancel ); contentpane.add( panel ); feedback = new JLabel(); contentpane.add( feedback ); // TODO Part1 // Insert code here to register this frame to receive action events setdefaultcloseoperation( EXIT_ON_CLOSE ); // TODO Part2 // Insert code here to implement the described behavior 13

Solution for programming question 2: Please note you do not need to copy any of the given code. Just label your code with TODO Part1 and TODO Part2. If you need to do any other code modification of the given template, please label it TODO Part3. 14

This page is left blank intentionally. 15

This page is left blank intentionally. 16

3. (20 points) Write a program that asks the user to enter 300 integers between 0 and 2000 inclusive. After the user enters these numbers, the statistical mode should be displayed. The statistical mode is the most frequently occurring number. If there is a tie for the mode, no mode should be displayed. You need not perform error checking. Example: Enter an integer in [0,2000]: 3 Enter an integer in [0,2000]: 3 Enter an integer in [0,2000]: 3 Enter an integer in [0,2000]: 5 Enter an integer in [0,2000]: 8... Mode: no mode Hint: You should consider using an array of integers to keep a running total of the number of times a particular number has been entered. e.g. count[180] can contain the total number of times the user entered the number 180. 17

Solution for programming question 3: 18