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

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

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

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

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

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

Midterm assessment - MAKEUP Fall 2010

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

Threads. Fundamentals of Computer Science

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

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

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

Concurrency. Fundamentals of Computer Science

Michele Van Dyne MUS 204B Concurrency Issues

Michele Van Dyne MUS 204B Threads

CSCD 330 Network Programming

CSCD 330 Network Programming

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

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

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

1 Looping Constructs (4 minutes, 2 points)

Threads Chate Patanothai

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

CS180 Review. Recitation Week 15

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

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

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

Final Exam CS 251, Intermediate Programming December 13, 2017

CS 351 Design of Large Programs Threads and Concurrency

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Note: Each loop has 5 iterations in the ThreeLoopTest program.

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

Java s Implementation of Concurrency, and how to use it in our applications.

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

Core Java Syllabus. Pre-requisite / Target Audience: C language skills (Good to Have)

Introduction to Programming Using Java (98-388)

CSCD 330 Network Programming

F I N A L E X A M I N A T I O N

7. MULTITHREDED PROGRAMMING

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

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

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

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

Concurrent Computing CSCI 201 Principles of Software Development

Contents. 6-1 Copyright (c) N. Afshartous

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

Final Exam CS 251, Intermediate Programming December 10, 2014

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

Animation Part 2: MoveableShape interface & Multithreading

SELF-STUDY. Glossary

CSCI 201L Midterm Written SOLUTION Fall % of course grade

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

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

1 Constructors and Inheritance (4 minutes, 2 points)

JAVA MOCK TEST JAVA MOCK TEST III

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

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

Java Threads. Written by John Bell for CS 342, Spring 2018

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

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.

Come & Join Us at VUSTUDENTS.net

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

VALLIAMMAI ENGINEERING COLLEGE

CS Exam 1 Review Suggestions

Core Java Syllabus DAY -1 :

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

Inheritance and objects

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Core Java Contents. Duration: 25 Hours (1 Month)

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

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...

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

CS 556 Distributed Systems

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Instance Members and Static Members

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

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

CS 3331 Advanced Object-Oriented Programming Final Exam

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

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

CSCI 135 Final Fall Problem Points Score Total 100

Intro: Your brain on Java Who is this book for? What your brain is thinking. Bend your brain into submission What you need for this book

Multithreaded Programming

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

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Java SE 8 Programming

CMSC 132: Object-Oriented Programming II. Threads in Java

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

Self-test Java Programming

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

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

Internet Technology 2/7/2013

Inheritance and objects

We are on the GUI fast track path

Exam Number/Code : 1Z Exam Name: Name: Java Standard Edition 6. Demo. Version : Programmer Certified Professional Exam.

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

15CS45 : OBJECT ORIENTED CONCEPTS

Transcription:

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015 Name: This exam consists of 5 problems on the following 7 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 10 2 8 3 18 4 10 5 10 Total 56 1

1. Arrays (10 points). Consider the following program: public class Prob1 public static void main(string[] args) for (int i = 0; i < args.length; i = i + 2) if (args[i].equals(args[i + 1])) System.out.print("yes "); else System.out.print("no "); System.out.println("maybe"); Below are five example executions of the program. Give the output produced by the program. If the given input would cause a runtime error, write error. Command line Output % java Prob1 maybe % java Prob1 a a b b yes yes maybe % java Prob1 a b a b a error % java Prob1 a AB ab a no no maybe % java Prob1 abcdefg error 2

2. Threads (8 points). Consider the following two classes that differ only in name and the highlighted line: public class WorkerA implements Runnable private int [] d = null; public WorkerA(int [] d) this.d = d; public void reverse() for (int i = 0; i < d.length / 2; i++) int swap = d[i]; d[i] = d[d.length - i - 1]; d[d.length - i - 1] = swap; public class WorkerB implements Runnable private int [] d = null; public WorkerB(int [] d) this.d = d; public synchronized void reverse() for (int i = 0; i < d.length / 2; i++) int swap = d[i]; d[i] = d[d.length - i - 1]; d[d.length - i - 1] = swap; public void run() for (int i = 0; i < 999; i++) reverse(); public void run() for (int i = 0; i < 999; i++) reverse(); Each of the following code snippets uses either WorkerA or WorkerB. Give the program's output. If the program could potentially have different outputs depending on the thread scheduler, just write varies. int [] d = 0, 1, 2, 3, 4, 5; WorkerA w = new WorkerA(d); Output: 5 4 3 2 1 0 Thread t1 = new Thread(w); t1.start(); try t1.join(); catch (InterruptedException e) for (int i : d) System.out.print(i + " "); 3

2. Threads (continued) int [] d = 0, 1, 2, 3, 4, 5; WorkerA w = new WorkerA(d); Output: varies Thread t1 = new Thread(w); Thread t2 = new Thread(w); t1.start(); t2.start(); try t1.join(); t2.join(); catch (InterruptedException e) for (int i : d) System.out.print(i + " "); int [] d = 0, 1, 2, 3, 4, 5; WorkerB w = new WorkerB(d); Output: 0 1 2 3 4 5 Thread t1 = new Thread(w); Thread t2 = new Thread(w); t1.start(); t2.start(); try t1.join(); t2.join(); catch (InterruptedException e) for (int i : d) System.out.print(i + " "); int [] d = 0, 1, 2, 3, 4, 5; WorkerB w = new WorkerB(d); Output: varies Thread t1 = new Thread(w); t1.start(); for (int i : d) System.out.print(i + " "); 4

3. Multiple choice (18 points, 2 points each). For each question, circle the single BEST answer. a) In Java socket programming, which of the following lines causes a server program to block (i.e. suspend its execution) until a client requests a new connection: I. InputStreamReader stream = new InputStreamReader(sock.getInputStream()); II. BufferedReader reader = new BufferedReader(stream); III. PrintWriter writer = new PrintWriter(sock.getOutputStream()); IV. Socket sock = new Socket("localhost", 5000); V. Socket sock2 = sock.accept(); b) You are developing a client- server program using Java sockets. The client program establishes a connection to the server and has a multi- step conversation over the course of many seconds. What problem would result if you implement a single- threaded server utilizing a single Socket object? I. If a single client is connected to the server, no other clients will be able to receive service until the first client finishes. II. The client would not be able to obtain the IP address of the server via DNS. III. Multiple clients could connect at the same time, but the server would be slow to respond since it would not be able to use any multiple processor cores present on the server. IV. Deadlock would occur due to concurrency issues handling the simultaneous client requests. V. The server would quickly exhaust its available pool of socket port numbers. c) You are developing a client- server program using Java sockets. When establishing the connection, the client provides both a domain name and a port number. What is the purpose of the port number? I. The client uses the port number to translate the domain name of the server to an IP address. II. Allows the client's connection to work through all firewalls. III. Allows the client to target a specific process running on the target host machine. IV. Allows the client to target a specific host machine somewhere on the network. V. Specifies the version of the IP protocol that the server should use for communication. d) Which of the following is TRUE about threading in Java: I. Creating more Java Thread objects than the number of CPU cores causes a runtime exception. II. When a thread calls Thread.sleep(1000), it yields the CPU for a maximum of 1000 milliseconds. III. Catching an InterruptedException is optional when calling join() or sleep(). IV. Calling join() on a worker thread causes the program to wait for that worker thread to complete. V. The Runnable interface requires you implement a single method, namely int compareto(). e) You are programming a GUI in Java. Which of the following Java interfaces is used to respond when a user clicks on a JButton widget? I. Iterable II. Runnable III. Comparable<JButton> IV. Comparable V. ActionListener 5

f) You are programming a GUI in Java. You need an area to display images, lines, and circles. What widget would be most appropriate? I. JPanel II. JLabel III. JButton IV. JDrawingArea VI. JDrawable g) Consider the following screenshot of a Java GUI that uses the default layout manager for a JFrame: What layout manager is this? I. FlowLayout II. BoxLayout III. BorderLayout IV. CompassLayout V. CenterLayout The next two questions are about the following class: public class Cow extends Animal implements Comparable<Cow> public Cow() super("bessy"); //...more code... //...more code... h) What does the first line of the constructor do? I. Calls a constructor in the class Cow that takes a single String as a parameter. II. Calls a constructor in the class Animal that takes a single String as a parameter. III. Calls a constructor in the class Comparable that takes a single String as a parameter. IV. Calls a method Bessy in the class Cow that takes a single String as a parameter. V. Calls a method Bessy in the class Animal that takes a single String as a parameter. i) What other method must be implemented in Cow in order for the class to compile? I. public Cow(String name) II. public Animal(String name) III. public int compareto(cow other) IV. public void run() V. public int run(cow other) 6

4. Classes and collections (10 points). The following class represents a dog character in a game. Each Dog can be at a different (x, y) location in the game. All dogs are drawn using the same image and make the same barking sound. Add or change the code to achieve the following: a) Reduce the memory requirements by adding a keyword to one or more of the instance variables. b) Add code in main() to create and populate an ArrayList containing the number of dogs specified by the 1 st command line argument. Give each dog a random x- and y- location, both in [0.0, 1.0). c) Add an instance variable and code to constructor to keep track of the total number of Dog objects constructed. Your code should work in any client that creates Dog objects via the new operator. d) Add a getcount() method to return the total number of created Dog objects. public class Dog private double x = 0.0; private double y = 0.0; private static final String IMAGE = "dog.jpg"; private static final String AUDIO = "dog.wav"; private static int count = 0; public Dog(double x, double y) count++; this.x = x; this.y = y; public static int getcount() return count; public static void main(string [] args) final int N = Integer.parseInt(args[0]); ArrayList<Dog> dogs = new ArrayList<Dog>(); for (int i = 0; i < N; i++) dogs.add(new Dog(Math.random(), Math.random())); 7

5. Methods and inheritance (10 points). Consider the following hierarchy of classes: For each part, list the class or classes meeting the stated condition. If no classes do, write none. a) Classes that can be instantiated using the new operator Circle, Rectangle, CircleBorder, RectangleBorder b) Classes that override an implemented method in a parent (i.e. they override a non- abstract method) CircleBorder, RectangleBorder c) Classes that have one or more overloaded methods none d) Subclasses of Rectangle RectangleBorder e) Subclasses of CircleBorder none 8