Midterm assessment - MAKEUP Fall 2010

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

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

M257 Putting Java to work

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

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

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

CMSC 331 Second Midterm Exam

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

CS Exam 1 Review Suggestions

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

2. (True/False) All methods in an interface must be declared public.

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

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

15CS45 : OBJECT ORIENTED CONCEPTS

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

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

Full file at Chapter 2 - Inheritance and Exception Handling

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

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

Introduction to Programming Using Java (98-388)

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

MIT AITI Swing Event Model Lecture 17

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

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

PESIT Bangalore South Campus

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

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

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

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

RAIK 183H Examination 2 Solution. November 10, 2014

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

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

Exam Duration: 2hrs and 30min Software Design

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

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

CSCI 201L Midterm Written SOLUTION Fall % of course grade

Java for Non Majors Spring 2018

CS 11 java track: lecture 3

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.

Final Examination Semester 3 / Year 2008

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

Graphics User Defined Forms, Part I

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

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

Declarations and Access Control SCJP tips

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

AP CS Unit 11: Graphics and Events

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

M257 Past Paper Oct 2007 Attempted Solution

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Object-Oriented Software Engineering (Re-exam for Object-Oriented Analysis, Design and Programming)

Practice for Chapter 11

RAIK 183H Examination 2 Solution. November 11, 2013

Java Object Oriented Design. CSC207 Fall 2014

We are on the GUI fast track path

UTM CSC207: Midterm Examination October 28, 2011

Come & Join Us at VUSTUDENTS.net

More on Objects in JAVA TM

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

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

SCHEME OF COURSE WORK

Instruction to students:

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

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

8. Polymorphism and Inheritance

Swing from A to Z Some Simple Components. Preface

M257 Past Paper Oct 2008 Attempted Solution

Object-Oriented Concepts

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

Java Threads and intrinsic locks

CS 116 Week 8 Page 1

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

Final Exam CS 251, Intermediate Programming December 13, 2017

20 Most Important Java Programming Interview Questions. Powered by

Final Examination Semester 2 / Year 2011

CS11 Java. Fall Lecture 4

1 Looping Constructs (4 minutes, 2 points)

Abstract Classes and Interfaces

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

(2½ hours) Total Marks: 75

Java, Swing, and Eclipse: The Calculator Lab.

Packages: Putting Classes Together

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

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

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Chapter 14 Abstract Classes and Interfaces

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS 251 Intermediate Programming Inheritance

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

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

University of Cape Town Department of Computer Science Computer Science CSC1017F

Logistics. Final Exam on Friday at 3pm in CHEM 102

CSCI 201L Midterm Written Fall % of course grade

Transcription:

M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name: Student ID: Section No.: IMPORTANT INSTRUCTIONS 1. This exam has 3 parts and ALL questions must be answered. 2. This exam consists of 7 pages (including the space you should use for your answer). 3. Student handbooks are NOT permitted in this examination. 4. The use of electronic devices that could have a memory is NOT permitted. 5. At the end of the examination, check that you have written your student ID, name and your section number on the first page. P a g e 1 M257 MTA Fall 2010

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (16 marks) 1.... translates and executes Java bytecode. a. Java interpreter c. Java debugger b. Java compiler d. Java disassembler 2.... means that subclasses modifies the methods they inherit from a superclass. a. Inheritance c. Extension b. Overriding d. Overloading 3. The Java Swing library includes... a. containers c. visual components b. layout managers d. all of the above 4. In Java, allow the primitive data types to be treated as objects. a. abstract classes c. generic classes b. object wrappers d. None of the above 5. is a reference to the class in which it appears. a. Constructor c. super b. this d. None of the above 6. The keyword that specifies that a particular class cannot be instantiated is: a. static c. abstract b. final d. protected 7. Which of these data type requires the most amount of memory? a. long c. short b. int d. byte 8. Strings that cannot be changed at all, once created are said to be: a. adjustable c. immutable b. replaceable d. mutable Essay: Answer the following questions. (28 marks) 1. What is the main difference between private, protected, and public class members in terms of how they can be accessed? (7 marks) Private: private members can be accessed only within its defining class Public: any class can access public members of a public class using a qualified name. Protected: classes in the same package AND subclasses can access protected members using a qualified name. P a g e 2 M257 MTA Fall 2010

2. In Java threads, what is the difference between the runnable state and the running state? (7 marks) Runnable State: The start method moves a thread into the runnable state. This does not mean that it will run immediately (there may be another thread that is being run by the CPU). Running state: When a thread is in the running state, it means the thread is actually executing. 3. What is meant by static variables and static methods? (7 marks) Static Variables: When you declare a static variable in a class, you are specifying that there is exactly one copy of the variable for all objects defined by that class. They are usually used to define constants, or one value for all objects Static Methods: Static methods carry out general functions not associated with objects. 4. In the context of Java programming, what is meant by composition? Give an example of a code with illustration of how it is implemented by the computer (7 marks) Composition occurs when objects of a class contain objects of another class; i.e. an object of the class is 'composed' of objects from one or more other classes. For Example: (any other example is acceptable) P a g e 3 M257 MTA Fall 2010

Problem Solving: Answer the following questions (56 marks) 1. Re-write the following code using for loops: (10 marks) int i, j; i = 1; while(i < 10){ System.out.print("O"); j = 10; while(j > 1){ System.out.print("X"); j = j - 2; System.out.println("O"); i = i + 2; for (int i = 1; i < 10; i = i + 2) { System.out.print("O"); //first loop //second loop //first loop for (int j = 10; j > 1; j = j - 2) { //second loop System.out.print("X"); System.out.println("O"); 2. Write Java code to read only the first line of text from the file c:\test.txt, and then display it on the screen. Your program should have the following specifications: (12 marks) Use the BufferedReader class to read the input. If an IOException occurred, an appropriate error message should be displayed. Use try catch statement to handle the IOException try{ BufferedReader in = new BufferedReader(new FileReader("c:/test1.txt")); System.out.println(in.readLine()); catch(ioexception exception){ System.out.println("Error reading the file"); P a g e 4 M257 MTA Fall 2010

3. Consider the following class, and then answer the questions about it. (20 marks) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class GUI extends JFrame { JLabel msg; JButton b1, b2; public GUI() { setsize(100, 150); setlayout(new FlowLayout()); msg = new JLabel("How old are you?"); b1 = new JButton("Over 30"); b2 = new JButton("Under 20"); add(msg); add(b1); add(b2); public static void main(string[] args) { GUI g = new GUI(); g.setvisible(true); a) Draw (or describe) the result obtained when running the main method in the above class. Your drawing doesn t have to of an accurate scale. (5 marks) b) What will happen if you clicked the close button (i.e. the X button on the top right corner) on the displayed output window? (3 marks) The window will not be closed (i.e. it will be hidden). But the program will still be running in the background. P a g e 5 M257 MTA Fall 2010

c) Modify the above code by adding an action listener so that: (11 marks) When the button b1 is clicked, the message You are old is displayed in the label msg. When the button b2 is clicked, the message You are young is displayed in the label msg. In your answer, you need to write a private class name myhandler that implements the ActionListener interface. The class myhandler should be used to handle the events of both buttons b1 and b2. Note: you don t need to rewrite the above code. You may just write the line numbers of the statements to be modified, or the line numbers at which additional code is inserted. After line 9: After line 17 b1.addactionlistener(new myhandler()); b2.addactionlistener(new myhandler()); private class myhandler implements ActionListener{ public void actionperformed(actionevent e) { JButton b = (JButton) e.getsource(); if(b==b1) msg.settext("you are old"); else if(b==b2) msg.settext("you are young"); 4. Consider the following Java code then answer the questions about it: (14 marks) 1 public class ThreadQ extends Thread{ 2 private String name; 3 public ThreadQ(String s) {name = s; 4 public void run() { 5 for (int i = 0; i < 3; i++) { 6 System.out.println(name + " is active"); 7 try { 8 Thread.sleep(100); 9 catch (InterruptedException ex) { 10 11 System.out.println(name + "is DONE!"); 12 13 public static void main(string[] args) { 14 ThreadQ t1q = new ThreadQ("Thread 1"); 15 ThreadQ t2q = new ThreadQ("Thread 2"); 16 ThreadQ t3q = new ThreadQ("Thread 3"); 17 t1q.start(); 18 t2q.start(); 19 t3q.start(); 20 P a g e 6 M257 MTA Fall 2010

(a) What is the output of the above code as displayed on the screen? Justify your answer. (6 marks) (a) The output would be something like the following:: Thread 2 is active Thread 1 is active Thread 3 is active Thread 2 is active Thread 1 is active Thread 3 is active Thread 2 is active Thread 1 is active Thread 3 is active Thread 2is DONE! Thread 3is DONE! Thread 1is DONE! (This output could be different (i.e. the order of threads is different; e.g. 123231132 etc) Justification: No one thread had exclusive access to the CPU for its entire execution. It is not the case that the threads take it in turns to run. One thread has access for a certain amount of time and then another thread has access, and so on. (b) Modify the above class so that it uses alternative technique for creating threads. (8 marks) Note: you don t need to rewrite the above code. You may just write the line numbers of the statements to be modified, or the line numbers at which additional code is inserted. public class ThreadQ implements Runnable{... //SAME CODE UP TO THE MAIN METHOD public static void main(string[] args) { ThreadQ t1q = new ThreadQ("Thread 1"); ThreadQ t2q = new ThreadQ("Thread 2"); ThreadQ t3q = new ThreadQ("Thread 3"); Thread t1 = new Thread(t1Q); Thread t2 = new Thread(t2Q); Thread t3 = new Thread(t3Q); t1.start(); t2.start(); t3.start(); P a g e 7 M257 MTA Fall 2010