Final Exam CS 251, Intermediate Programming December 13, 2017

Similar documents
Midterm Exam CS 251, Intermediate Programming October 8, 2014

Final Exam CS 251, Intermediate Programming December 10, 2014

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

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

CS 251 Intermediate Programming GUIs: Event Listeners

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

CS 251 Intermediate Programming GUIs: Components and Layout

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

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

PIC 20A GUI with swing

CS Exam 1 Review Suggestions

CS 251 Intermediate Programming Inheritance

CS 251, Intermediate Programming Midterm Exam October 9, 2013

Class 16: The Swing Event Model

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

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

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

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

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.

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

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.

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

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

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

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

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

Content Area: Mathematics Course: Computer Programming Grade Level: R14 The Seven Cs of Learning

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

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

Inheritance. Chapter 7. Chapter 7 1

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

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

CMSC 132: Object-Oriented Programming II

AP CS Unit 11: Graphics and Events

Declarations and Access Control SCJP tips

Questions Answer Key Questions Answer Key Questions Answer Key

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

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

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

Example: Building a Java GUI

Example: Building a Java GUI

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

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Selected Java Topics

Use the Scantron sheets to write your answer. Make sure to write your Purdue ID (NOT Purdue Login ID) and your name on the Scantron answer sheet.

Exam 1 - (20 points)

CS180 Recitation. More about Objects and Methods

CS108, Stanford Handout #22. Thread 3 GUI

CSC 1351 The Twelve Hour Exam From Hell

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

APCS Semester #1 Final Exam Practice Problems

Swing: Building GUIs in Java

Midterm assessment - MAKEUP Fall 2010

CS11 Java. Fall Lecture 4

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

CS11 Java. Fall Lecture 3

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

CS 251 Intermediate Programming Methods and Classes

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

CS 251 Intermediate Programming Methods and More

CMSC 331 Second Midterm Exam

RAIK 183H Examination 2 Solution. November 10, 2014

Swing: Building GUIs in Java

1 Looping Constructs (4 minutes, 2 points)

CS260 Intro to Java & Android 03.Java Language Basics

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Final Exam. COMP Summer I June 26, points

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

CIT Special final examination

1 Method Signatures and Overloading (3 minutes, 2 points)

SYSC Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

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

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Review sheet for Final Exam (List of objectives for this course)

Graphical User Interface (GUI)

ITI Introduction to Computing II

Csci 102: Sample Exam

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Objects and Classes Lecture 2

Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam

Java Object Oriented Design. CSC207 Fall 2014

CSE 331 Final Exam 6/7/16

CMSC 132: Object-Oriented Programming II

More About Objects and Methods

Window Interfaces Using Swing Objects

1B1b Classes in Java Part I

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Lecture 5: Java Graphics

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

Java Interfaces Part 1 - Events Version 1.1

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

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

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

I. True/False: (2 points each)

Java: introduction to object-oriented features

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

Transcription:

Final Exam CS 251, Intermediate Programming December 13, 2017 Name: NetID: Answer all questions in the space provided. Write clearly and legibly, you will not get credit for illegible or incomprehensible answers. This is a closed book exam. However, each student is allowed to bring one page of notes to the exam. Print your name at the top of every page. Question: 1 2 3 4 5 6 7 8 Total Points: 16 24 8 6 12 12 12 10 100 Score: 1. Multiple choice questions: Select the single correct answer for each. (a) Which interface would be the best choice to associate student usernames with Student objects? (b) Which interface would be the best choice to hold a collection of unique student usernames? (c) Which interface would be the best choice as a parameter of a method that can accept any collection of student usernames, without assuming anything about order or uniqueness? (d) Which interface would be the best choice if you wanted to handle items in a LIFO (last in, first out) order? (e) Which interface would be the best choice if you wanted to be able to randomly shuffle the order of the items? (f) Which type could foo be in the following code snippet? boolean success = foo. contains (" hello "); A. Collection B. Deque C. List D. Set E. Queue F. SortedSet G. Any of these. H. None of these. (g) Which type could foo be in the following code snippet? Object obj = foo. get (0); A. Collection C. List E. Queue G. Any of these. B. Deque D. Set F. SortedSet H. None of these. (h) Which of the following correctly declares and instantiates a map that associates String keys with Double values? A. Map<String, Double> map = new HashMap<String, Double>(); B. HashMap<String, Double> map = new HashMap<String, Double>(); C. HashMap<String, Double> map = new HashMap<>(); D. Map<String, Double> map = new TreeMap<String, Double>(); E. Map<String, Double> map = new HashMap<>(); F. Map<String, Double> map = new TreeMap<>(); G. All of the above. H. None of the above. Page 1 of 8

2. Write the answer in the blank provided. (a) What is the keyword used to make a variable that cannot be reassigned? (b) What is the keyword used to access a member variable hidden by a local variable? (c) What is the keyword used to call the parent class version of a method overridden in the child class? (d) What is the keyword used to make a class that cannot be instantiated? (e) What is the keyword used to make a class that cannot be extended? (f) What is the keyword used to test if an object is of a particular type? (g) What is the keyword used to make a variable or method belong to a class rather than an instance? (h) If I have a member variable in a top level class that is visible to methods within a class nested inside it, but not to methods within another top level class in the same package, what access modifier have I used on the member variable? (i) If I declare an int variable but do not initialize it, what is its value? (j) If I declare an Integer variable but do not initialize it, what is its value? (k) What is occurring when I have a method in a child class that has the same name as a method in its parent, but a different parameter list? (l) If I have a variable of type Object, an object of any type could be assigned to it. If I call the tostring method on this variable, how does Java know which version of the method to execute? (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) (k) (l) Page 2 of 8

3. Why don t the following code snippets compile? Select the single correct answer for each. (a) public class MyClass { private int x = 10; x ++; A. Cannot access private variable x from a public method. B. Cannot access x without an instance of MyClass. C. Variable x is a constant, so cannot be incremented in main. D. The println method expects a String, not an int. E. Some other error. F. This code will successfully compile. (b) List < boolean > truthvalues ; A. The list is not initialized. B. Cannot use interface List as variable type. E. Some other error. F. This code will successfully compile. (c) C. Cannot use primitive type boolean as generic type parameter. D. List is an interface and cannot be instantiated. ActionListener listener = new ActionListener () { public void actionperformed ( ActionEvent ev ) { System. out. println ( " Action happened " ); ; A. ActionListener is an interface and cannot be instantiated. B. Missing semicolon after call to ActionListener constructor. C. Extra curly braces around actionperformed method. D. Need to add the listener to a JButton. E. Some other error. F. This code will successfully compile. (d) public class MyListener implements ActionListener { private int count = 0; public void actionperformed ( ActionEvent ev ) { count ++ A. Missing constructor for MyListener class. B. Should use extends instead of implements in first line. C. Cannot access private variable count from a public method. D. Variable count is out of scope in the actionperformed method. E. Some other error. F. This code will successfully compile. Page 3 of 8

4. Consider the following code. (6) import java. util.*; public class MyPoint { private final int x; private final int y; public MyPoint ( int x, int y) { this.x = x; this.y = y; public int getx () { return x; public int gety () { return y; public String tostring () { return "(" + x + ", " + y + ")"; List < MyPoint > points = new ArrayList < >(); points. add ( new MyPoint (1,2)); points. add ( new MyPoint (3,4)); points. add ( new MyPoint (5,6)); System. out. println (" points = " + points ); MyPoint p = new MyPoint (1,2); System. out. println ("Is " + p + " in points? " + points. contains (p )); The output of this code is points = [(1, 2), (3, 4), (5, 6)] Is (1, 2) in points? false Why isn t my point (1, 2) found in the list even though I see (1, 2) when I print the list? What should I do to fix this without changing the main method? Page 4 of 8

5. Consider the following classes. What is the output of this code? (12) public class Foo { protected double x; protected static int y = 10; protected String z; public Foo () { this (" Winter "); y ++; public Foo ( String x) { this.x = x. length () / 4.0; this.z = x; y ++; public void doprint ( String y) { System. out. println (y); System. out. println (z); public class Bar extends Foo { public Bar ( String x) { y ++; public Bar ( String y, int x) { super (y); public void doprint ( int x) { doprint (x * 1.25); public void doprint ( String z) { super. doprint (z); doprint ( y / 2 ); public void doprint ( double z) { System. out. println (y); System. out. println (z); Foo a = new Bar (" CS251 "); a. doprint (" Final "); Bar b = new Bar (" Exam ", 42); b. doprint ; Page 5 of 8

6. Consider the following classes. (12) public class Parent { public void methoda () { System. out. println ("A"); public class Child extends Parent { public void methoda ( int n) { public void methodb () { public void methodc () { public void methodb () { System. out. println ("B"); Parent x = new Parent (); Child y = new Child (); Parent z = new Child (); // What works here? Which of the following lines of code would successfully compile and run when placed in the main method of Child after the code that is already there? Select all that apply. A. x.methoda(); B. y.methoda(); C. z.methoda(); D. x.methoda(42); E. y.methoda(42); F. z.methoda(42); G. x.methodb(); H. y.methodb(); I. z.methodb(); J. x.methodc(); K. y.methodc(); L. z.methodc(); M. Child c = (Child)x; N. ((Child)x).methodC(); O. ((Child)z).methodC(); P. Object obj = new Parent(); Q. Child obj = new Parent(); R. Parent obj = new Child(); S. Parent p = x; T. Parent p = y; U. Parent p = z; V. Child c = x; W. Child c = y; X. Child c = z; 7. Consider the following interface describing a to-do list with tasks represented as Strings. (12) import java. util.*; public interface ToDoInterface { /** * Adds a task to the todo list. * @ param task The task to add. */ void addtask ( String task ); /** * Removes a task from the todo list after completion. * ( It is possible the completed task is not the expected next task.) * @ param task The completed task. */ void completetask ( String task ); Page 6 of 8

/** * Get the next task from the todo list. * ( Does not remove the task from the todo list.) * @ return Next task to do, or null if none left. */ String getnexttask (); /** * Get all the tasks on the todo list. * @ return All tasks we still have to do. */ Collection < String > getalltasks (); Write a small but complete class that implements this interface, handling the tasks in a firstin-first-out order. Use a private member variable of an appropriate collection type to hold the tasks. You do not need to include Javadoc comments. Page 7 of 8

8. Consider the following program. What would be displayed when it is run? Draw a picture to (10) illustrate. import java. awt.*; import javax. swing.*; public class LayoutExample { public static void createandshowgui () { JFrame frame = new JFrame (" Last Problem!"); frame. setdefaultcloseoperation ( JFrame. EXIT_ON_CLOSE ); JPanel panel1 = new JPanel (); panel1. add ( new JButton ("A" )); panel1. add ( new JButton ("B" )); JPanel panel2 = new JPanel (); panel2. setlayout ( new BoxLayout ( panel2, BoxLayout. PAGE_ AXIS )); panel2. add ( new JButton ("C" )); panel2. add ( new JButton ("D" )); panel2. add ( new JButton ("E" )); frame. add ( new JButton ("F"), BorderLayout. CENTER ); frame. add ( panel1, BorderLayout. PAGE_START ); frame. add ( panel2, BorderLayout. LINE_END ); frame. pack (); frame. setvisible ( true ); SwingUtilities. invokelater ( new Runnable () { public void run () { createandshowgui (); ); Page 8 of 8