Computer Science 1 Bh

Similar documents
Computer Science 1 Bh

Computer Science 1 Bh

Computer Science 1 Ah

Computer Science 1 Bh

Computer Science 1 Ah

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

JAVA OPERATORS GENERAL

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Give one example where you might wish to use a three dimensional array

Full file at

Code No: R Set No. 1

Object-Oriented Programming and Software Engineering CITS1001 MID-SEMESTER TEST

CLO Assessment CLO1 Q1(10) CLO2 Q2 (10) CLO3 Q4 (10) CLO4 Q3a (4)

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CS 314 Exam 2 Spring

ITI Introduction to Computing II

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

COMPUTING SCIENCE 3Z: PROGRAMMING LANGUAGES 3

Draw a diagram of an empty circular queue and describe it to the reader.

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

Expressions and Casting

Programming Basics. Digital Urban Visualization. People as Flows. ia

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS 231 Data Structures and Algorithms, Fall 2016

Operators. Java operators are classified into three categories:

Introduction to Programming Using Java (98-388)

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

CSE wi Midterm Exam 2/8/18 Sample Solution

UNIVERSITY REGULATIONS

Selenium Class 9 - Java Operators

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

COMPUTER SCIENCE Paper 1

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS171 Midterm Exam. October 29, Name:

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

EXAM Computer Science 1 Part 1

CIS 121 Data Structures and Algorithms with Java Spring 2018

Solution to CSE 250 Final Exam

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

NATIONAL UNIVERSITY OF SINGAPORE

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Compiling expressions

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

CS 312 Midterm 1 Spring 2013

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Largest Online Community of VU Students

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

Compiler construction 2009

EXAMINATIONS 2008 MID-YEAR COMP431 COMPILERS. Instructions: Read each question carefully before attempting it.

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Java byte code verification

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Second Examination Solution

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

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

CSE wi Midterm Exam 2/8/18. Name UW ID #

UNIVERSITY REGULATIONS

Unit 3. Operators. School of Science and Technology INTRODUCTION

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Total Score /1 /20 /41 /15 /23 Grader

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

Java Simple Data Types

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

CS 307 Final Fall 2009

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

CSE 143 Final Part 1, August 18, 2011 Sample Solution

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Chapter 3: Operators, Expressions and Type Conversion

Operators and Expressions

VALLIAMMAI ENGINEERING COLLEGE

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Key Java Simple Data Types

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Unit 10: Sorting/Searching/Recursion

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Java Simple Data Types

Chapter 9 Introduction to Arrays. Fundamentals of Java

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

A Simple Syntax-Directed Translator

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

MLR Institute of Technology

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

GCE A level 1103/01 COMPUTING CG3

COMPUTER SCIENCE PAPER 1

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Transcription:

UNIVERSITY OF EDINBURGH course CS0077 FACULTY OF SCIENCE AND ENGINEERING DIVISION OF INFORMATICS SCHOOL OF COMPUTER SCIENCE Computer Science 1 Bh Degree Examination Date: Saturday 26th May 2001 Time: 12:00 13:30 (one and a half hours) Place: Roxburgh Place Room: Halls A and B Board of examiners Chair: Stuart Anderson. External Examiner: Muffy Calder. Instructions to candidates 1. Check that the question paper contains pages 1 12 after this cover page. If it does not, inform the invigilator. 2. You should attempt as many questions as possible. 3. The questions in this paper vary widely in difficulty. During your first pass through the paper, you are advised not to dwell on questions to which the answer is not readily apparent. 4. Write your answer to each question in the box or table provided. If you wish to write more in answer to a question, continue on the blank page opposite, indicating that you have done so. 5. Please write legible and concise answers. 6. The marks allocated to each part of a question are indicated in the margin. There are 100 marks in total.

Question 1 Suppose we have a program which implements the following class hierarchy fragment: ClassA / \ ClassB ClassC / \ ClassD ClassE Suppose further that we have five variables declared and assigned by this program: ClassA a = new ClassA(); ClassB b = new ClassB(); ClassC c = (classc) (new ClassE()); ClassD d = null; ClassE e = new ClassE(); Fill in the result of executing the following program fragments. If you think that the Java compiler will reject the fragment, write does not compile. Remember that the Java compiler knows the class hierarchy and will reject programs where the types are always incompatible, whatever the run-time value of variables. System.out.println(a instanceof ClassA); System.out.println(a instanceof ClassB); System.out.println(b instanceof ClassA); System.out.println(b instanceof ClassC); System.out.println(c instanceof ClassA); System.out.println(c instanceof ClassB); System.out.println(c instanceof ClassE); System.out.println(d instanceof ClassD); System.out.println(d instanceof Object); System.out.println(e instanceof Object);

Question 2 A library class ArrayBinaryTree has an array field a declared by: private Object a[]; This array is used to represent a binary search tree of objects. The public methods of ArrayBinaryTree ensure that the tree is always balanced, which means that it has the smallest depth possible for a given number of elements stored in the tree. The tree is arranged in the array like this: a[0] a[1] a[2] a[3] a[4] a[5] a[6] If the tree does not have a node at a particular position, then the corresponding element in the array is null. i. The diagram shows an array big enough to hold a tree with up to 7 elements. If the number of elements stored in the tree is n, what is the size of the array needed? [2 marks] ii. Show, as trees, the possible ways of storing the integers 1, 2, 3, 4, 5, 6 in an ArrayBinaryTree object. iii. Write a method findleast which returns the integer index of the least element in the tree, making use of the binary search tree property. You may assume that the tree always has at least one node. [5 marks]

Question 3 (a) This question concerns interfaces in Java. Recall that the purpose of an interface is to describe some methods which are to be implemented by a class. i. All methods in an interface are abstract. What does this mean? ii. Since all methods are abstract, what else should be provided to explain an interface? iii. Show how you would write the standard Comparable interface in Java, which declares the compareto method. This method takes an object parameter and returns an integer, defining an ordering relation on objects in a class. (b) This question concerns abstract classes in Java. i. What is the difference between an abstract class and an ordinary class? ii. Show how you would write an abstract class AbstractReverseComparable which implements the Comparable interface and contains a concrete definition of a method reversecompareto method in terms of compareto, which reverses the sense of the compareto comparison. This means that if a.compareto(b) = 1, then a.reversecompareto(b) = 1, and similarly for 1 and 1. [4 marks]

Question 4 (a) Client-server systems require a method of identification of particular services on particular hosts. What is the naming and numbering system which is used for this identification? How are connections between hosts implemented in the Java programming language? [5 marks] (b) A Java program in the client-server model passes in turn each element of an array of messages (msgs) from the client to the server. The server acknowledges all non-empty messages. As usual, the server s input stream (serverin) is the client s output stream (serverout) and the client s input stream (clientin) is the server s output stream (clientout). An extract of the code is shown below. On the Server: On the Client: while (line!= null) { for (int i = 0 ; i < msgs.length ; i++) { if (line.equals("")) { serverout.println(msgs[i]); // ignore empty lines serverout.flush(); } else { System.out.println(clientIn.readLine()); System.out.println (line); } clientout.println("acknowledged"); clientout.flush(); } line = serverin.readline(); } After the system has been in use for some time an error occurs. What do you think is the likely error? How are these sorts of errors usually dealt with in client-server systems? [5 marks]

Question 5 (a) Provide an equivalent Java byte code version for the Java method below. [Hint: you should use the instructions iconst, ifge, iload and ireturn.] int minabs (int x) { if (x < 0) return 0; else return x; } (b) Consider a Java virtual machine which has the following values in memory and on the stack. 1 2 memory 5 4 3 stack 2 Giving this starting state, show the intermediate states of the memory and the stack as each instruction is executed. [7 marks] 1 imul 2 istore 1 3 iload 2 4 iinc 2 1 5 istore 2 6 iconst 1 7 istore 2

Question 6 (a) You are to implement the graphical interface of a Java program to play chess. Your interface is to draw a chessboard as shown below, made up of alternating black and white squares. The frame in which the chessboard is to be drawn is 800 800 pixels, so each of the black and white squares above measures 100 100 pixels. Recall that the Color class provides the constants Color.white and Color.black and a graphics context Graphics g provides the following methods: public void setcolor (Color c) public void fillrect (int x, int y, int width, int height) Using these ingredients, provide an implementation of the paint() method for a class which displays the chessboard. [6 marks] (b) The program needs to register mouse clicks in the frame in order to identify the squares on the chessboard by their row and column position. These positions are used to index into a two-dimensional array of ChessPiece objects and so their values are to range between 0 and 7. Implement the method public void mousepressed(mouseevent e) for the MouseAdapter class listening for these mouse clicks. Recall that the MouseEvent class provides methods getx() and gety(). Update the int variables row and column accordingly. [4 marks]

Question 7 (a) Briefly describe the four steps you would undertake in debugging a program. [4 marks] [ Question continues on next page ]

(b) The method given below is intended to find the number that is the minimum of all the numbers in the Vector v that are larger than the minimum of the elements in v. You may assume that v contains at least two elements, and that that the first two elements are different numbers. int second (Vector v) { // 1 int min; // 2 int secmin; // 3 // 4 if (((Integer)v.elementAt(0)).intValue()<= // 5 ((Integer)v.elementAt(1)).intValue()){ // 6 min = ((Integer)v.elementAt(0)).intValue(); // 7 secmin = ((Integer)v.elementAt(1)).intValue(); // 8 } else { // 9 min = ((Integer)v.elementAt(1)).intValue(); // 10 secmin = ((Integer)v.elementAt(0)).intValue(); // 11 } // 12 // 13 for (int i = 2; i < v.size(); i++) { // 14 if (((Integer)v.elementAt(i)).intValue() <= min) { // 15 int tmp = min; // 16 min = ((Integer)v.elementAt(i)).intValue(); // 17 secmin = tmp; // 18 } else if (((Integer)v.elementAt(i)).intValue() <= secmin) { // 19 secmin = ((Integer)v.elementAt(i)).intValue(); // 20 } // 21 } // 22 return secmin; // 23 } // 24 i. Describe a vector of Integers that will give the correct result using the above method. [2 marks] ii. Describe a vector of Integers that will give incorrect results using the above method. [2 marks] iii. Outline how you would go about repairing the fault in the program so it would give correct results with both your tests. [2 marks]

Question 8 Consider the following brief description of the Informatics Teaching Office (ITO): The ITO maintains information about each student taking a taught course in the Division of Informatics. Each student follows a programme of studies comprising a collection of individual courses and for each course an assessment record is maintained. Each student s director of studies maintains oversight over the selection of courses and the students progress. An analyst has begun to build a class model for this system and has identified: Student, Programme, Course, Assessment and DoS have been identified as class names in building the class model. (a) Explain what is meant by an association and how associations are used to make a more elaborate class model. Illustrate your answer by identifying some associations between the classes identified in the question. (b) Explain how you might go about validating a class model. Use the example above to illustrate your answer. (c) The DoS has the responsibility to check students they supervise are making satisfactory progress. Describe, using the classes identified above and the associations you have identified, how a DoS can carry out this responsibility. You may identify additional associations if necessary. [2 marks] (d) What is meant by refactoring in object-oriented design? Explain its role in the design process. [2 marks]

Question 9 The Java language provides an operation + for concatenating two strings. (a) Using only the operations of addition, subtraction and concatenation, by repeated concatenation or otherwise, write a Java implementation of a method static String repeat(string s, int n) such that, if s is a String and n is a natural number (i.e. a non-negative integer), then repeat(s, n) is the result of concatenating n copies of s. [4 marks] [ Question continues on next page ]

(b) The following function e defined on strings makes 2 n copies of the string s. The definition uses the function + to concatenate strings. e(s, 0) = s e(s, n + 1) = e(s, n) + e(s, n) The following Java method is intended to be an implementation of this function: static String exp(string s, int n) { if (n == 0) { return s; } else { String res = exp(s,n-1); return res+res; } } Show by induction on n that e(s, n) = exp(s, n). State clearly the base case, induction step, and induction hypothesis. [6 marks]

Question 10 This question concerns stack-based evaluation of arithmetic expressions. (a) Draw syntax trees for the following expressions: 5 ((2 3) + 4) (5 2) (3 + 4) ((5 2) 3) + 4 (b) Give the expressions derived from each of these trees by (i) infix and (ii) postfix tree traversal. (c) Using the Java byte code instructions imul, iadd, iconst, show the result of compiling the expression 5 ((2 3) + 4). [4 marks]