CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2012

Similar documents
CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

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

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

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

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

RAIK 183H Examination 2 Solution. November 10, 2014

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

RAIK 183H Examination 2 Solution. November 11, 2013

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

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

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

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

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

Graphical User Interfaces 2

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

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

CMP 326 Midterm Fall 2015

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

Graphical User Interface

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

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

Swing from A to Z Some Simple Components. Preface

Midterm assessment - MAKEUP Fall 2010

Another IS-A Relationship

CSCI 201L Midterm Written SOLUTION Fall % of course grade

We are on the GUI fast track path

Programming Languages and Techniques (CIS120)

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

CS 2334: Programming Structures and Abstractions Exam 2 November 4, Please wait to open this exam booklet until you are told to do so.

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

Final Exam CS 251, Intermediate Programming December 13, 2017

Final Exam CS 251, Intermediate Programming December 10, 2014

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

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

GUI Forms and Events, Part II

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS Exam 3 - Spring 2010

Introduction. Introduction

H212 Introduction to Software Systems Honors

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

MIT AITI Swing Event Model Lecture 17

CS 113 PRACTICE FINAL

Lecture 5: Java Graphics

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS Exam 1 Review Suggestions

Graphical User Interface (GUI)

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

Class 16: The Swing Event Model

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

CS Exam 3 - Spring 2010

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CS56 final (E03) W15, Phill Conrad, UC Santa Barbara Wednesday, 03/18/2015. Name: Umail umail.ucsb.edu. Circle one: 4pm 5pm 6pm

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

CSIS 10A Assignment 7 SOLUTIONS

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

PART1: Choose the correct answer and write it on the answer sheet:

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

AP CS Unit 11: Graphics and Events

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

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

University of Cape Town Department of Computer Science Computer Science CSC1017F

Graphical User Interfaces 2

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

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

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

Introduction to Programming Using Java (98-388)

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

CSCI 201L Midterm Written Summer % of course grade

Graphical User Interfaces 2

CSCI 201L Midterm Written SOLUTION Summer % of course grade

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

SINGLE EVENT HANDLING

EXAMINATIONS 2010 END-OF-YEAR COMP 102 INTRODUCTION TO COMPUTER PROGRAM DESIGN

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

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

Lecture 3: Java Graphics & Events

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November 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!

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Page 1

1 Looping Constructs (4 minutes, 2 points)

Computer Programming, I. Laboratory Manual. Final Exam Solution

CSCI 201L Midterm Programming. Fall % of course grade

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

Transcription:

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2012 Name: This exam consists of 6 problems on the following 8 pages. You may use your double- sided hand- written 8 ½ x 11 note sheet during the exam. No computers, mobile devices, cell phones, or other communication devices of any kind are permitted. If you have a question, raise your hand and I will stop by. Since partial credit is possible, please write legibly and show your work. Problem Points Score 1 8 2 10 3 10 4 14 5 10 6 12 Total 64 1

1. Strings (8 points). Consider the following program: public class Prob1 public static void main(string [] args) String a = args[0].tolowercase(); String b = args[1].tolowercase(); for (int i = 0; i < Math.min(a.length(), b.length()); i++) if (a.charat(i) == b.charat(i)) System.out.print(a.charAt(i)); else System.out.print("- "); Below are four example executions of the program. Give the output produced by the program. If the given input would cause a runtime error, write "runtime error". Command line Output % java Prob1 BOB % java Prob1 BOB robert % java Prob1 tom ----- tommy % java Prob1 abcd AbCd 2

2. Methods (10 points). Match the method description on the left with the best answer on the right. Not every letter will be used. Each letter will be used at most once. A. public void run(thread t) A class that implements ActionListener must have this method. A class that implements Runnable must have this method. Your GUI has a class MyPanel that extends JPanel. This method is called when repaint() is executed on the JFrame containing your MyPanel object. When you run a *.class file using the java program, this method is the one that must exist and immediately gets executed. A class that implements Serializable must have this method. B. public void runnable(string name) C. public void run() D. public void actionperformed(actionevent e) E. public void actionperformed() F. public void buttonclicked(actionevent e) G. public static void main() H. private static void main(string [] args) I. public void main(string [] args) J. public static void main(string [] args) K. public void init(graphics g) L. public void paintcomponent(graphics g) M. public void repaint(graphics g) N. Trick question, this interface does not require any particular method be implemented. O. Trick question, doing this would result in a runtime error. P. Trick question, no method is executed in this situation. 3

3. Methods (10 points). Match the method description on the left with the best method on the right. Assume candidate methods appear inside a class named BigInt. Not every letter will be used. Each letter will be used at most once. A. public boolean foo(bigint [] a) _ A method that can return the count of BigInt objects in an array that are equal to a specified BigInt object. You have an object c of type BigInt. This method is called implicitly by the following line: System.out.println(c); A method that can return whether one BigInt is less than, equal to, or greater than another BigInt. A constructor that creates a BigInt based on another BigInt. A method that can return a new array of a specified number of BigInt objects. B. public int foo(bigint [] a, BigInt t) C. public BigInt[] foo(int t) D. public String tostring() E. public static String tostring() F. public void tostring(string s) G. public int foo(bigint a) H. public boolean foo(bigint a) I. public enum foo(bigint a, BigInt b) J. public void foo(bigint a) K. public BigInt() L. public BigInt(BigInt a) M. public static BigInt(BigInt a) N. private BigInt(BigInt a) 4

4. Multiple choice (2 points each). For each question, circle the ONE best answer. a) Which of the following Java expressions does NOT always correctly compute the mathematical average of the integer variables a, b, c and d? Assume a, b, c, and d are integers in the range [- 1000, 1000]. I. (double) ((a + b + c + d) / 4.0); II. ((double) (a + b + c + d)) / 4.0; III. (a + b + c + d) / 4; IV. (a + b + c + d) / 4.0; V. (a + (double) b + c + d) / 4; b) Consider the following recursive method: public static int foo(int n) if (n == 4) return 2; else return 2 * foo(n + 1); What is the value returned by the method call foo(2)? I. 2 II. 4 III. 8 IV. 16 V. 24 c) You would like to include an element on your web page that displays an animated cat. The cat attempts to chase the mouse cursor whenever the cursor is inside the element s section on the web page. Which of the following technologies could best be used to achieve this? I. Java web start II. Java applet III. Java JFrame application IV. Java REST web service V. Java object serialization d) All the following are true EXCEPT: I. When compiling to native code, the final executable will only run on a specific hardware architecture. II. The bytecode of a Java program compiled on Mac OS will run in Windows 7 or Linux without needing the original source code. III. A disadvantage of bytecode approaches such as Java and.net is that compile- time errors can only be detected when the program is run in the virtual machine. IV. A just- in- time (JIT) compiler improves performance by caching frequently needed conversions from bytecode to native instructions. 5

e) When writing an object to disk using Java object serialization, what keyword do you use to specify that an instance variable should NOT be written to disk? I. static II. private III. transient IV. synchronized V. final f) Which of the following is true about accessing a REST web service via Java: I. Communication with the remote web service can be done via the HTTP protocol using Java s URL and URLConnection classes. II. The first step is to download the JAR file containing the *.class file III. The first step is to download the XML code contained in the *.jnlp file IV. The remote web server must have Java installed V. The remote web server must be in the client s security sandbox g) Consider the following short program: int x = 1; int y = - 4; while ((x >= 0) (x <= y)) x- - ; y++; System.out.println("x = " + x + ", y = " + y); What values of x and y are printed after the while loop halts? I. x = 1, y = - 4 II. x = 0, y = - 3 III. x = - 1, y = - 2 IV. x = - 2, y = - 1 V. The loop never halts 6

5. OOP (10 points). Assume you have the following Java classes (some details have been omitted): public abstract class Supply private double cost; public double getcost() return cost; public abstract String tostring(); public abstract class Writing extends Supply protected boolean erasable; public boolean geterasable() return erasable; public void write(string s) /* stuff */ public class Coffee extends Supply private double volume; public void drink(int amount) volume - = amount; public String tostring() return "coffee!"; public class Pencil extends Writing /* stuff */ public class Pen extends Writing /* stuff */ a) What method(s) must be implemented in the classes Pencil and Pen in order for them to compile? b) Which of the above classes could you successfully instantiate an object of? c) Which of the above classes could you instantiate and add to the following variable named list: ArrayList<Writing> list = new ArrayList<Writing>(); d) What inherited method(s) could you potentially override in the class Pen? Circle the correct method(s). void drink(int amount) public double getcost() public boolean geterasable() public void write(string s) e) In an instance method in the Pen class, what inherited instance variables could you access directly (that is without using a getter/setter method)? 7

6. GUIs (12 points). Consider the following GUI program: public class Click extends JFrame implements ActionListener private JTextField field; public Click() JButton button = new JButton("Click me"); add(button, BorderLayout.WEST); button.addactionlistener(this); field = new JTextField(); field.addactionlistener(new Handler()); add(field, BorderLayout.CENTER); setsize(300, 100); setvisible(true); public void actionperformed(actionevent e) field.settext("" + Integer.parseInt(field.getText()) / 2); class Handler implements ActionListener public void actionperformed(actionevent e) field.settext("" + Integer.parseInt(field.getText()) * 2); public static void main(string [] args) Click click = new Click(); a) When the application starts, it appears as shown above. What happens if the user immediately clicks the Click me button? Explain why and describe how you might prevent this. b) Assume the user does the following: types 7 into the text field, hits enter, and then clicks the Click me button. What result is displayed in the text field? c) Assume the user has typed 5 into the text field. Give the result shown in the text field after four consecutive clicks of the Click me button. Before any click 1 st click 2 nd click 3 rd click 4 th click 5 8

6. GUI (continued) d) Add code to the previous GUI so that a text label appears to the right of the text field (e.g. as shown in the screenshot below). The label should start at a value of 0 and increment by one every time the button is pressed. We have reproduced the previous program below. Add or change the code below as necessary. public class Click extends JFrame implements ActionListener private JTextField field; public Click() JButton button = new JButton("Click me"); add(button, BorderLayout.WEST); button.addactionlistener(this); field = new JTextField(); field.addactionlistener(new Handler()); add(field, BorderLayout.CENTER); setsize(300, 100); setvisible(true); public void actionperformed(actionevent e) field.settext("" + Integer.parseInt(field.getText()) / 2); class Handler implements ActionListener public void actionperformed(actionevent e) field.settext("" + Integer.parseInt(field.getText()) * 2); public static void main(string [] args) Click click = new Click(); 9