READ AND OBSERVE THE FOLLOWING RULES:

Similar documents
EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

CPSC 121 Midterm 1 Friday October 14th, Signature: Section (circle one): 11:00 15:30 17:00

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

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

CPSC 410 Advanced Software Engineering Mid-term Examination (Term I ): Solution Instructor: Gail Murphy

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

This examination has 11 pages. Check that you have a complete paper.

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

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

Computer Science 304

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 110: MIDTERM 1 Part B May 26, Important notes about this examination

UNIVERSITY REGULATIONS

CPSC 121 Sample Final Examination December 2013

UNIVERSITY REGULATIONS

CPSC 320 Midterm 2. July 13, 2007

Final Exam CS 251, Intermediate Programming December 13, 2017

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

CS 134 Test Program #2

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

CS 411 Midterm Feb 2008

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

H212 Introduction to Software Systems Honors

Object- Oriented Analysis, Design and Programming

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Full Name: CS Account:

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

CS Exam 1 Review Suggestions

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

The University of British Columbia Final Examination - December 02, 2014 Mathematics 308. Closed book examination. No calculators.

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

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

CPSC 311, 2010W1 Midterm Exam #2

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CPSC 121 Midterm 1 Friday February 5th, Signature: Section (circle one): Morning Afternoon

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

Inheritance (continued) Inheritance

1 Inheritance (8 minutes, 9 points)

CSE 1223: Exam II Autumn 2016

INSTRUCTIONS TO CANDIDATES

Graphical User Interfaces 2

CSE351 Winter 2016, Final Examination March 16, 2016

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

CSE 115: Computer Science I Spring Sample Final Exam

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

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

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

Final Exam CS 251, Intermediate Programming December 10, 2014

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

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

Midterm assessment - MAKEUP Fall 2010

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

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

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

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

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE

CSE 131 Introduction to Computer Science Fall Final Exam

Introduction to Computing II (ITI 1121) Final Examination

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 121: MIDTERM 2 Group March 12, 2014

Language Features. 1. The primitive types int, double, and boolean are part of the AP

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

Java: introduction to object-oriented features

1 Shyam sir JAVA Notes

Final Examination Semester 3 / Year 2010

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

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

JAVA MOCK TEST JAVA MOCK TEST II

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

RAIK 183H Examination 2 Solution. November 10, 2014

CS-202 Introduction to Object Oriented Programming

Graphical User Interfaces 2

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CLASS DESIGN. Objectives MODULE 4

Java Object Oriented Design. CSC207 Fall 2014

Midterm Exam CS 251, Intermediate Programming October 8, 2014

CSC 1351 The Twelve Hour Exam From Hell

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

CS 251, Intermediate Programming Midterm Exam October 9, 2013

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

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

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 261: MIDTERM 1 February 14, 2017

COS 126 General Computer Science Spring Written Exam 1

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CSSE SEMESTER 1, 2017 EXAMINATIONS. CITS1001 Object-oriented Programming and Software Engineering FAMILY NAME: GIVEN NAMES:

Questions Answer Key Questions Answer Key Questions Answer Key

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

Transcription:

This examination has 11 pages: check that you have a complete paper. Check that you have a complete paper. Each candidate should be prepared to produce, upon request, his or her SUNY/UB card. This is a closed book, closed notes, closed neighbor examination. Use of calculators, cell phones, mp3 players, or any aids whatsoever, is NOT permitted. This examination has 10 questions. Each question has a part worth 120 competency points and a part worth 1 proficiency point. Answer all 10 questions. You have 3 hours (180 minutes) to complete this examination. READ AND OBSERVE THE FOLLOWING RULES: Write the following, in ink, in the space provided on the back page, your usual signature, and your UB person number, All of your writing must be handed in. This booklet must not be torn or mutilated in any way, and must not be taken from the examination room. Show all of your work in arriving at an answer, unless instructed otherwise. Partial credit will be awarded as appropriate. Candidates are not permitted to ask questions of the invigilators, except in cases of supposed errors or ambiguities in examination questions. You may not leave the examination room in first 30 minutes of the exam, or in the last 15 minutes. You may not enter the examination room after the first 30 minutes of the exam. CAUTION Candidates guilty of any of the following, or similar, dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action. Making use of any memory, computational, communication or similar devices, including but not limited to books, papers, calculators, computers, cell phones, and Google glass. Speaking or communicating with other candidates, in any way. Purposely exposing written papers to the view of other candidates The plea of accident or forgetfulness shall not be received. DO NOT WRITE BELOW Competency Points Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 COMPETENCY /120 /120 /120 /120 /120 /120 /120 /120 /120 /120 /1200 Proficiency Points Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 PROFICIENCY /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /10 1

Question 1 [Competency] This question assesses your ability to write basic code elements. Define a class which whose fully qualified name is q1.everykeylistener. [GRADING: 2 points] The class must implement the java.awt.event.keylistener interface, whose definition is given here: package java.awt.event; import java.util.eventlistener; public interface KeyListener extends EventListener { public void keytyped(keyevent e); public void keypressed(keyevent e); public void keyreleased(keyevent e); The class must have: 1. An instance variable of type code.model [GRADING: 2 points] 2. A constructor with one parameter whose value is used to initialize the instance variable. [GRADING: 2 points] 3. Definitions for all the methods of the KeyListener interface, as follows: a. The keytyped method must call the method remove on the code.model object whose reference is stored in the instance variable. Assume that remove method takes a char as an argument. Pass the value returned by getkeychar() method (called on a KeyEvent object) as argument. [GRADING: 2 points] b. The keypressed method must be defined so that it does nothing when called. [GRADING: 1 point] c. The keyreleased method must be defined so that it does nothing when called. [GRADING: 1 point] Do NOT include anything else in the definition of the class. Write your answer below: package q1; import java.awt.event.keyevent; import java.awt.event.keylistener; import code.model; public class EveryKeyListener implements KeyListener { private Model _model; public EveryKeyListener(Model m) { _model = m; @Override public void keytyped(keyevent e) { _model.remove(e.getkeychar()); @Override public void keypressed(keyevent e) { @Override public void keyreleased(keyevent e) { Question 1 [Proficiency] Define an anonymous event handler for a JButton that uses System.out.println to print click whenever the button it is attached to is clicked. button.addactionlistener(new ActionListener() { @Override public void actionperformed(actionevent e) { System.out.println("click"); ); 2

Question 2 [Competency] This question assesses your command of UML class diagram notation. [GRADING: 120 total marks 120 points for a correct diagram, 80 points for minor mistakes/omissions, 40 points for a diagram with more than minor mistakes/omissions, 0 points otherwise (e.g. no answer or wrong type of diagram)] Draw a UML class diagram for the following code. Show all relationships present in the code (even if implicit). package code.model; public class MatchingGameModel { private Observer _observer; private Board _board; public MatchingGameModel() { _observer = null; _board = new Board(this); public void setobserver(observer obs) { _observer = obs; // OTHER CODE OMITTED If Observer is drawn as an interface (a box with just two compartments) and no relationship to Object, that is acceptable as well. Object MatchingGameModel Observer Question 2 [Proficiency] What is the name of the design pattern used for event handling in Java. It is the Observer Pattern. 3

Question 3 [Competency] This question assesses your understanding of variables, objects and references. [GRADING: 120 total marks 120 points for a correct diagram, 80 points for minor mistakes/omissions, 40 points for a diagram with more than minor mistakes/omissions, 0 points otherwise (e.g. no answer or wrong type of diagram)] Given the code in question 2, draw an object diagram showing the state of the program after three lines are executed (but before any memory reclamation happens): MatchingGameModel m = new MatchingGameModel(); Observer obs = new MatchingGameGUI(); m.setobserver(obs); Show all objects explicitly created in the code, all instance variables explicitly declared in the code, and the local variables m and obs. MatchingGameModel object _observer m obs MatchingGameGUI object Question 3 [Proficiency] Redraw the diagram assuming that also this code is executed after the above code is run: Observer pnd = new MatchingGameGUI(); m.setobserver(pnd); m _observer MatchingGameModel object obs MatchingGameGUI object pnd MatchingGameGUI object 4

Question 4 [Competency] This question assesses your basic vocabulary comprehension. [GRADING: 120 total marks 12 points for each correctly identified item.] In the code below, clearly circle and identify by number one (and only one) example of each of the following. Be sure to identify each item precisely (circle no more and no less than necessary). If you believe no example exists in the given code, write no example" next to that item. 1. an assignment operator 2. a relational operator 3. a parameter list 4. a for statement (entire statement) 5. the name of a supertype 6. a boolean literal value 7. a comment 8. an int expression 9. a local variable declaration 10. a method call package code.gui; import java.awt.dimension; import java.awt.font; import java.awt.gridlayout; import javax.swing.jbutton; import javax.swing.jpanel; import code.model.matchinggamemodel; import code.model.observer; public class MatchingGameGUI implements Runnable, Observer { private MatchingGameModel _model; private JPanel _boardpanel; private JFrame _window; public MatchingGameGUI(MatchingGameModel m) { _model = m; _model.setobserver(this); @Override public void run() { /* DEFINITION OMITTED */ public void createandpopulateboard() { _boardpanel = new JPanel(); _boardpanel.setfocusable(false); _boardpanel.setlayout( new GridLayout(MatchingGameModel.ROWS, MatchingGameModel.COLS)); for (int c=0; c<matchinggamemodel.cols; c++) { for (int r=0; r<matchinggamemodel.rows; r++) { JButton b = new JButton(); b.setopaque(true); b.setfocusable(false); b.setpreferredsize(new Dimension(100, 100)); b.setfont(b.getfont().derivefont(font.bold, b.getfont().getsize()*4)); _boardpanel.add(b); update(); @Override public void update(){ /* DEFINITION OMITTED */ Question 4 [Proficiency] Identify a Java annotation in the above code. 5

Question 5 [Competency] This question assesses your ability to write a simple method involving a loop. [GRADING: 120 total marks 120 points for a correct method, 80 points for minor mistakes/omissions, 40 points for a method with more than minor mistakes/omissions, 0 points otherwise (e.g. no answer, no method, or a completely incorrect method)] Define a void method named write which has a parameter of type ArrayList<Beverage>. You may name the parameter however you wish, as long as the name adheres to our naming conventions for parameters. Define the method so it prints the name, flavor and calorie count of each of the Beverage objects in the ArrayList on a separate line. The Beverage class is defined as shown below. package q6; public class Beverage { private String _name; private String _flavor; private int _calories; public Beverage(String n, String f, int c) { _name = n; _flavor = f; _calories = c; public int calories() { return _calories; public String name() { return _name; public String flavor() { return _flavor; For example, running this code, ArrayList<Beverage> list = new ArrayList<Beverage>(); list.add(new Beverage("H2O","nothing",0)); list.add(new Beverage("Ginger Ale","ginger",120)); list.add(new Beverage("Flavored tea","peach",15)); write(list); must produce the following output: H2O tastes of nothing and has 0 calories. Ginger Ale tastes of ginger and has 120 calories. Flavored tea tastes of peach and has 15 calories. public void write(arraylist<beverage> list) { for (Beverage d : list) { System.out.println(d.name() + " tastes of " + d.flavor() + " and has "+d.calories()+" calories."); Question 5 [Proficiency] Rewrite the method to produce padded output so all names line up, based on the longest name in the list. For the above example the output must be: H2O tastes of nothing and has 0 calories. Ginger Ale tastes of ginger and has 120 calories. Flavored tea tastes of peach and has 15 calories. public static void write2(arraylist<beverage> list) { String longest = ""; for (Beverage d : list) { while (d.name().length() > longest.length()) { longest = longest + " "; for (Beverage d : list) { System.out.println(d.name() + longest.substring(0, longest.length()-d.name().length())+" tastes of " + d.flavor() + " and has "+d.calories()+" calories."); 6

Question 6 [Competency] This question assesses your ability to write a simple method involving a conditional. [GRADING: 120 total marks 120 points for a correct method, 80 points for minor mistakes/omissions, 40 points for a method with more than minor mistakes/omissions, 0 points otherwise (e.g. no answer, no method, or a completely incorrect method)] Consider the following table mapping numeric grades to letter grades: >= < Letter grade 60 F 60 70 D 70 80 C 80 90 B 90 A Define a method named grademapper which accepts as an argument a numeric grade (expressed as a value of type int) and returns the corresponding letter grade, according to the table above, as a String. You may assume that the argument will be in the range 0 to 100. For example, grademapper(81) must return the String "B". public String grademapper(int ng) { if (ng < 60) { return "F"; if (ng < 70) { return "D"; if (ng < 80) { return "C"; if (ng < 90) { return "B"; return "A"; Question 6 [Proficiency] Solve the same problem in a different way. For example, if you used if-else statements above, use if statements instead. public String grademapper(int ng) { String answer = "A"; if (ng < 60) { answer = "F"; else if (ng < 70) { answer = "D"; else if (ng < 80) { answer = "C"; else if (ng < 90) { answer = "B"; return answer; 7

Question 7 [Competency] This question assesses your code tracing/reading abilities. [GRADING: 120 total marks 120 points for a correct answer, 80 points for an answer which demonstrates an understanding of how the code executes, but which has some minor mistakes, 40 points for an answer that is relevant to question but clearly wrong, displaying minimal understanding of how the code executes, 0 points otherwise.] Consider the following method: public boolean binarysearch(arraylist<integer> list, int target) { int left = 0; // first included element int right = list.size(); // first excluded element while ( left!= right ) { int mid = (left + right) / 2; System.out.println("L: "+left+" M: "+mid+" R: "+right); if ( list.get( mid ) == target ) { System.out.println("---> "+list.get(mid)+"=="+target); return true; if ( list.get( mid ) > target ) { System.out.println("---> "+list.get(mid)+" > "+target); right = mid; if ( list.get( mid ) < target ) { System.out.println("---> "+list.get(mid)+" < "+target); left = mid + 1; System.out.println("Not found."); return false; Notice the println( ) statements! Show what is printed when the following code is executed: ArrayList<Integer> list = new ArrayList<Integer>(); list.add(375); list.add(402); list.add(452); list.add(473); list.add(522); list.add(578); list.add(685); list.add(792); binarysearch(list, 620); L: 0 M: 4 R: 8 ---> 522 < 620 L: 5 M: 6 R: 8 ---> 685 > 620 L: 5 M: 5 R: 6 ---> 578 < 620 Not found. Question 7 [Proficiency] Assume each println statement was modified to include the value of mid at the front, as in this example: System.out.println(mid + " ---> "+list.get(mid)+"=="+target); What would be printed when running the above code? L: 0 M: 4 R: 8 4---> 522 < 620 L: 5 M: 6 R: 8 6---> 685 > 620 L: 5 M: 5 R: 6 5---> 578 < 620 Not found. 8

Question 8 [Competency] This question assesses your comprehension of inheritance, overriding and polymorphism. [GRADING: 120 total marks 120 points for a completely correct answer, 80 points for a response with minor mistakes/omissions, 40 points for an answer that is clearly wrong but somehow related to the question at hand, and 0 points otherwise.] Consider the following definitions: public abstract class Base { private int _v; private int _w; public Base() { this(2); public Base(int x) { this(x,3); public Base (int r, int s) { _v = r; _w = s; public int foo() { return _v * _w; public int v() { return _v; public int w() { return _w; public abstract String name(); public class Derived extends Base { public Derived() { super(5); @Override public String name() { return "Derived"; public class Child extends Base { public Child() { super(4,7); @Override public int foo() { return super.foo() + 3; @Override public String name() { return "Child"; public class Kid extends Base { @Override public int foo() { return 3*v() + 5*w(); @Override public String name() { return "Kid"; Show what is printed when the following code executes: ArrayList<Base> list = new ArrayList<Base>(); list.add(new Derived()); list.add(new Child()); list.add(new Kid()); for (Base x : list) { System.out.println(x.name() + " produces " + x.foo()); Derived produces 15 Child produces 31 Kid produces 21 Question 8 [Proficiency] What is printed if the same code is run, but Kid contains this definition: @Override public int v() { return 2 * super.v() - 1; Derived produces 15 Child produces 31 Kid produces 24 9

Question 9 [Competency] This question assesses your understanding of basic programming concepts. [GRADING: 120 total marks 24 points for each correctly defined term, 12 points for a reasonable attempt, 0 points otherwise.] Briefly explain what a variable is: A variable is a storage location in memory; it has a name (in the high-level program), a location (address), a type, a value (contents), lifetime and scope. A 2-point answer must capture at least that a variable can hold a value. Briefly explain what a reference is: A reference is the address of a block of memory. Briefly explain what an object is: An object is an instance of a class. An object has properties (instance variables: things it knows) and behaviors (methods: things it can do). Briefly explain what this is: this is an implicit parameter of every (instance) method; it holds a reference to the object on which the method was invoked. Briefly explain what null is: null is a reference that does not refer to any object. Question 9 [Proficiency] Briefly explain what JSON is, and how it can be used. JSON, short for JavaScript Object Notation, is a human-readable textual format for storing and transmitting data. It can be used to communicate data from one application to another, independent of the languages used to implement those applications. 10

Question 10 [Competency] This question assesses your understanding of JSON [GRADING: 120 total marks 120 points for a completely correct answer, 80 points for a response with minor mistakes/omissions, 40 points for an answer that is clearly wrong but somehow related to the question at hand, and 0 points otherwise.] What is printed when the following main method is called? public static void main(string[] args){ // The double quotes need to be escaped with the \ character. This tells // Java that we want the " character instead of ending the String. String jsonstring = "[5,8,\"data\",\"elements\"]"; JsonValue jsonvalue = Json.parse(jsonString); JsonArray jsonarray = jsonvalue.asarray(); JsonValue index0 = jsonarray.get(0); int firstvalue = index0.asint(); JsonValue index1 = jsonarray.get(1); int secondvalue = index1.asint(); JsonValue index2 = jsonarray.get(2); String thirdvalue = index2.asstring(); JsonValue index3 = jsonarray.get(3); String fourthvalue = index3.asstring(); System.out.println("firstValue: " + firstvalue); System.out.println("secondValue: " + secondvalue); System.out.println("thirdValue: " + thirdvalue); System.out.println("fourthValue: " + fourthvalue); Question 10 [Proficiency] How would you construct a JSON object that stores a student s name, their person number, a semester identifier (e.g. F17) and the set of courses they are taking (e.g. CSE115, CSE191, ENG201, MTH141). JsonObject student = new JsonObject(); student.add("name", "Sally Smith"); student.add("person number", 11223344); student.add("semester", "F17"); JsonArray courses = new JsonArray(); courses.add("cse115"); courses.add("cse191"); courses.add("eng201"); courses.add("mth241"); student.add("courses", courses); 11