M257 Past Paper Oct 2008 Attempted Solution

Similar documents
M257 Past Paper Oct 2007 Attempted Solution

Introduction to Programming Using Java (98-388)

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Chapter 6 Introduction to Defining Classes

1 OBJECT-ORIENTED PROGRAMMING 1

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

15CS45 : OBJECT ORIENTED CONCEPTS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

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

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

[Course Overview] After completing this module you are ready to: Develop Desktop applications, Networking & Multi-threaded programs in java.

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

Java Object Oriented Design. CSC207 Fall 2014

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

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

CSE 501N Final Fall Points Possible

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

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

Abstract Classes and Interfaces

Midterm assessment - MAKEUP Fall 2010

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

PESIT Bangalore South Campus

1 Shyam sir JAVA Notes

Building Java Programs. Inheritance and Polymorphism

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

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

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

C12a: The Object Superclass and Selected Methods

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

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.

Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You ll learn much less if you just look at

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Come & Join Us at VUSTUDENTS.net

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

CSE 143 Lecture 22. The Object class; Polymorphism. read slides created by Marty Stepp and Ethan Apter

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

COP 3330 Final Exam Review

Java Review: Objects

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

The class Object. Lecture CS1122 Summer 2008

This page intentionally left blank

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Java Support for developing TCP Network Based Programs

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

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

Full file at

Type of Classes Nested Classes Inner Classes Local and Anonymous Inner Classes

Murach s Beginning Java with Eclipse

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

UMBC CMSC 331 Final Exam

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

JAVA MOCK TEST JAVA MOCK TEST IV

Java Training JAVA. Introduction of Java

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

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

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

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

Introduction to Inheritance

Exam Duration: 2hrs and 30min Software Design

Compaq Interview Questions And Answers

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

JAVA MOCK TEST JAVA MOCK TEST II

Objects and Iterators

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

C# Programming for Developers Course Labs Contents

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

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

Inheritance & Polymorphism

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

Java Threads and intrinsic locks

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

INHERITANCE. Spring 2019

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Inheritance and Polymorphism

CSE 70 Final Exam Fall 2009

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

WA1278 Introduction to Java Using Eclipse

CH. 2 OBJECT-ORIENTED PROGRAMMING

Contents. iii Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1998, Revision B

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Threads in Java (Deitel & Deitel)

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

More on Objects in JAVA TM

Methods Common to all Classes

Modern Programming Languages. Lecture Java Programming Language. An Introduction

CSC Java Programming, Fall Java Data Types and Control Constructs

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

VALLIAMMAI ENGINEERING COLLEGE

Transcription:

M257 Past Paper Oct 2008 Attempted Solution Part 1 Question 1 A version of Java is a particular release of the language, which may be succeeded by subsequent updated versions at a later time. Some examples of Java versions are Java 1.0, Java 2 version 1.2 and Java 2 version 1.3. An edition of Java is a particular variation of the language, which is aimed at a particular platform. For example, Java 2 Enterprise Edition is aimed at large, enterprise-level systems, whereas Java 2 Micro Edition is aimed at small devices with less memory and system resources. Question 2 A for-loop iterates a specific number of times, the number of which is determined by the loop counter variable. For instance, a for-loop that begins with the code for (int i = 0; i < 10; i++) will iterate 10 times. A for-each loop iterates through all of the items in a collection or array, regardless of how many items there are. A for-loop would be used if a certain section of code needed to be executed a specific number of times, such as printing the string "Hello world!" three times. A for-each loop would be used if the programmer wanted to perform some processing on a collection or array of items, such as printing the value of each item in an ArrayList of String objects. Question 3 Object is the class at the top of the hierarchy, from which all other classes are descended. Two advantages of this placement are: 1. All objects can be relied upon to respond to methods inherited from Object, such as clone, tostring and equals. 2. The programmer can create reference variables of type Object, to which objects of any other class can then be assigned, since all classes are subclasses of Object. Question 4 A stream is a sequence of bytes, which represents a flow of data between a source and a destination. A buffered stream is particularly useful when accessing disk or using other network facilities. Buffered streams read/write data from/to a memory area which can free the program up to carry out other activities. Unbuffered streams access the resource directly.

Flushing a stream makes sure that all of the data in the buffer has been processed, so that none of the data is lost when the stream is closed. Question 5 Composition is a relationship in which an object of one class contains objects of another class. Inheritance is a relationship in which an object of one class inherits data members and behaviour from a superclass. Car is likely to be the superclass of FordCar, since FordCar is-a Car; the FordCar class is likely to be a more specialised version of the Car class. This is an example of inheritance. There is likely to be a has-a relationship between Car and CarRadio, since a Car object could feasibly contain a CarRadio object. This is an example of composition. Question 6 Select location Manchester Select destination Paris Book travel Question 7 Event-driven programming means that certain methods are executed in response to events which are external to the program. This differs from ordinary programming in that: The program is less "linear"; it may not be possible to trace a route through the code to show the order of execution. The programmer does not know how many times each event will occur, when they will occur or in which order they will occur, since this is decided by the user's interaction with the GUI. Two examples of an event are: A button press A mouse click Question 8 The method notify instructs the scheduler to move one blocked thread into the runnable state, and notifyall moves all of the currently blocked threads into the runnable state.

You might use notify in situations in which it does not matter whether all of the threads eventually get the chance to move into the running state, but notifyall would be preferable in situations where all of the threads need to have the chance of running. Question 9 Socket objects are used on the client side, whereas ServerSocket objects are used on the server side. The Socket constructor requires two arguments, the server address and port number, whereas the ServerSocket constructor only requires the port number. A Socket object initiates a connection to a server, whereas a ServerSocket object waits for a connection from a client. Question 10 A servlet is a module of Java code that is stored and run on a web server. Servlets are appropriate for: Processing data separately from the client interface, so that the client software can be simple and responsive (i.e. a thin client). Accessing a database on a separate database server; since servlets use the full Java language with no security restrictions, they can communicate with other computers. Two circumstances in which you might want to use a servlet are: 1. A web-based to-do list application, which stores the data relating to each user's account and to-do lists in a database held on a separate server. The servlet could dynamically generate web pages displaying overdue and upcoming tasks for the user, and could also handle the log-in process (i.e. checking the user's username and password against those held in the database). 2. An online banking website, which allows users to interact with the servlet to perform various banking tasks, such as viewing statements and setting up money transfers. The servlet could process the account data and generate web pages displaying the user's account balance and transaction history. Part 2 Question 11 (a) public class MyString private String s; public MyString(String str) s = str;

public char getlastchar() return s.charat(s.length() - 1); public String gets() return s; public void dispalternate() if ((s == null) (s.length() == 0)) System.out.println("empty"); else for (int i = 0; i < s.length(); i++) if (i % 2 == 0) System.out.print(s.charAt); (b) 1, 2, 3, 8 4, 5, 8 (iii) 3 (c) The following variables are created: A MyString reference variable with the identifier cat; A String reference variable with the identifier m; A char primitive variable with the identifier temp. A new String object is created with the value "MOUSE", and is assigned to the variable m. The variable m is passed to the MyString constructor, and a new MyString object is created, with "MOUSE" as the value of its s instance variable, which is assigned to cat. The last character of the s value is then assigned to temp. The string "MUE" is printed to the screen.

A reference variable of type Object is created, with the identifier o. A second MyString object is created, with "MOUSE" as the value of its instance variable s, which is assigned to o. (d) public boolean equals(object other) MyString o = (MyString) other; return s.equals(o.gets()); (e) The method above overrides the method equals(object) which is inherited from the class Object, because both methods have the same signature. (f) The code would not compile, because there is no zero-argument constructor for the class MyString, so the statement cat = new MyString(); is not valid. Question 12 The Iterator interface provides a way of iterating through the items in a collection. The Iterator interface allows the deletion of items in the collection using the remove method. <E> is a formal type parameter, which means that the actual argument supplied by the programmer must be a type rather than a reference or primitive value. (iii) Iterator <ArrayList> listit = alist.iterator() //iterator method is polymorphic while (listit.hasnext()) System.out.println(listIt.next().toString()); (iv) A class can implement multiple interfaces, so it is similar in this respect to multiple inheritance. Also, interfaces can extend multiple other interfaces, so multiple inheritance is allowed in this sense for interfaces. However, classes can only inherit abstract methods and constants from interfaces, and cannot inherit variables or concrete methods from them, so this is different to true multiple inheritance.

(b) two 0 one 1 two 2 one 3 two 4 one 5 two 6 one 7 Since both threads access the same ArrayOfInt object, they update the value of the instance variable next sequentially by invoking the next method. There are no lost updates, since the next and hasnext methods are synchronized. Question 13 (a) private class ButtonWatcher implements ActionListener public void actionperformed(actionevent a) Object buttonpressed = a.getsource(); if (buttonpressed.equals(greybutton)) greysquirrels++; if(buttonpressed.equals(redbutton)) redsquirrels++; Implementing classes such as ButtonWatcher as inner classes allows the programmer to keep classes that are logically related to each other together. The inner class has access to all of the methods, variables and constants of the surrounding class. (iii) Create an instance of ButtonWatcher for each button.

Add each instance to its associated button as an action listener by invoking the method addactionlistener on the button with the ButtonWatcher object as its argument. (b) public void senddata() try openstreams(); toserver.print(greysquirrels + " " + redsquirrels); System.out.println(fromServer.readLine()); closestreams(); catch (IOException e) System.out.println("Error: " + e); (c) Scanner sc = new Scanner(fromClient.readLine()); greysquirrels = greysquirrel + sc.getnextint(); sc.next(); redsquirrels = redsquirrels + sc.getnextint(); socket = ss.accept(); openstreams(); processclientrequest(); closestreams(); socket.close(); (iii) The architecture used in this application is not Peer to Peer, since there is a central computer that stores the squirrel totals, which acts as a server to many clients. This architecture is more like a 2-tier structure.