Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Similar documents
Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 2

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Module Contact: Dr Taoyang Wu, CMP Copyright of the University of East Anglia Version 1

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Module Contact: Dr Anthony J. Bagnall, CMP Copyright of the University of East Anglia Version 2

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Answer BOTH questions from Section A and ONE question from Section B.

Introduction to Programming Using Java (98-388)

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Do not turn over until you are told to do so by the Invigilator.

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CompSci 125 Lecture 11

Question: Total Points: Score:

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

CS111: PROGRAMMING LANGUAGE II

Chapter 1b Classes and Objects

Module Contact: Dr Tony Bagnall, CMP Copyright of the University of East Anglia Version 1

COP 3330 Final Exam Review

CSC Java Programming, Fall Java Data Types and Control Constructs

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS1004: Intro to CS in Java, Spring 2005

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Sample Paper of Computer for Class 10

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

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

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

COSC 1010 Introduction to Computer Programming

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

Chapter 4 Defining Classes I

APCS Semester #1 Final Exam Practice Problems

ECE 122. Engineering Problem Solving with Java

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

WES-CS GROUP MEETING #9

Introduction to Computing II (ITI 1121) Final Examination

CS313D: ADVANCED PROGRAMMING LANGUAGE

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

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

EXAMINATION FOR THE BSC (HONS) INFORMATION TECHNOLOGY; BSC (HONS) INFORMATION SYSTEMS & BSC (HONS) COMPUTER SCIENCE; YEAR 1

Chief Reader Report on Student Responses:

Sequential Search (Searching Supplement: 1-2)

Exam Duration: 2hrs and 30min Software Design

Engr 123 April 25, 2018 Final Exam Review. 3. Write a method which will accept a string and return the number of three-letter words in the string.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Course Outline. Introduction to java

[CHAPTER] 1 INTRODUCTION 1

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

This exam is open book. Each question is worth 3 points.

Review Questions II KEY

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Happy Cinco de Mayo!!!!

Assignment 2.4: Loops

Full file at

Objects and Iterators

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

INFO 2313: Project. School of Business Kwantlen Polytechnic University. Nov 19, 2016 It is due at 12:00 PM (noon) on Sunday, Dec 4, 2016

Java Review. Fundamentals of Computer Science

Introduction to Software Development (ISD) Week 3


Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I

Sample Examination Paper Programming and Software Development

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

Chapter 6 Introduction to Defining Classes

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Happy Cinco de Mayo!!!!

COS 126 General Computer Science Spring Written Exam 1

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

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

CSCI 355 Lab #2 Spring 2007

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

Software Systems Development Unit AS1: Introduction to Object Oriented Development

Final. Please write (printing clearly) the infonnation requested in the boxes at the top of this page.

Functions. Arash Rafiey. September 26, 2017

Problem Solving with C++

Arrays and Strings. Arrays - homogeneous indexed collections Selection sort as array example. String class, revisited

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

CS 61B Discussion 5: Inheritance II Fall 2014

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Computer Components. Software{ User Programs. Operating System. Hardware

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Final Exam Practice. Partial credit will be awarded.

CS61B Lecture #2. Public Service Announcements:

Transcription:

UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2017-18 PROGRAMMING 1 CMP-4008Y Time allowed: 2 hours Answer FOUR questions. All questions carry equal weight. Notes are not permitted in this examination. Do not turn over until you are told to do so by the Invigilator. CMP-4008Y Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Page 2 1. (a) In the context of the Java programming language: (i) Distinguish between an instance variable and a local variable. (ii) Distinguish between a class and an object. (b) The implementation of the bubble sort algorithm (used to sort an array of integers into ascending order) shown in the code fragment below contains five errors. Identify these errors and give a corrected implementation. public static sort(int[] x) boolean sorted; do sorted = true; for (int i = 1; i < x.length; i++); if (x[i-1] < x[i]) int tmp = x[i]; x[i-1] = tmp; x[i] = x[i-1]; sorted = false; while (sorted); (c) A prime number is an integer greater than 1 that is only divisible, without leaving a remainder, by itself and by 1. For example seven is a prime number, but nine is not as it is divisible by three. Write a static method that returns an array containing the first n prime numbers, where n is a formal parameter of the method.

Page 3 2. (a) With the aid of diagrams and examples, describe the waterfall model of software development. (b) What is the output of the following code fragment? Justify your answer. Hint: the str.charat(i) returns the char with index i (i.e. the (i + 1) th character) in the String object referenced by str (20 marks) String str = "I think, therefore I am!"; for (int i = 0; i < str.length(); i++) switch (str.charat(i)) case a : case e : case i : case o : case u : continue; case, : break; case t : System.out.print( T ); case? : System.out.print(! ); System.out.print(str.charAt(i)); TURN OVER

Page 4 3. The duration of an event, for instance the time taken to run an Olympic marathon, can be represented by three integer values, representing the number of hours, minutes and seconds that have elapsed since the start. (a) Define a Java class describing a duration, represented by three integer instance variables. (b) Define an appropriate set of constructors. (c) Define appropriate accessor methods for the instance variables. (d) Define a method returning the total number of seconds elapsed for a Duration object as an int. (e) Define an instance method implementing the addition of two Duration objects, returning the result as a new Duration object. (f) Overload the tostring method inherited from class Object, to generate a String representing a Duration object in an appropriate format. (g) Define the class such that it implements the interface shown below public interface Comparable int compareto(object o); The compareto method returns a negative integer value if the Object to which the method is applied is to be considered less than the Object passed to the method as an argument, zero if the objects are to be considered equal and a positive integer otherwise. (2 marks)

Page 5 4. (a) Define what is meant by an algorithm. (b) How are algorithms formally described at the specification level? (c) State what is meant by a recursive algorithm. (d) What rules must be followed when implementing a recursive algorithm? (e) Consider the recursive algorithm in Algorithm 1. Informally, describe what this algorithm does and give the output of the algorithm for an input array A = [5,8,2,4,6]. Algorithm 1 AnAlgorithm(A[1..n], length) Require: Input: Integer array A[1..n], Integer length 1: if length = 1 then 2: return A[1]; 3: end if 4: return A[length]+AnAlgorithm(A,length 1); (2 marks) (f) Formally describe an iterative implementation of the recursive algorithm given in Algorithm 1. (g) When is a recursive algorithm tail recursive? Is the algorithm in Algorithm 1 tail recursive? TURN OVER

Page 6 5. (a) In the context of Java, what is an Exception and what are they used for? (b) Explain how Exceptions are handled in Java using the try, catch, and finally keywords. (c) Consider the following Java code: public static void progexamalgo() System.out.println("Please enter a month" + " between 1 and 12"); Scanner scan = new Scanner(System.in); int input = scan.nextint(); String season = getseason(input); System.out.println(season); public static String getseason(int input) String season = "Winter"; if(input > 2 && input <= 5) season = "Spring"; else if(input > 5 && input <=8) season = "Summer"; else if(input > 8 && input <= 11) season = "Autumn"; return season; Informally, explain what the progexamalgo algorithm does. Describe each step in a typical successful execution of the program and give an example output. (d) Assuming all relevant classes are correctly imported, what will happen if a user runs progexamalgo and: (i) enters 4 when prompted? (ii) enters Feb when prompted? (iii) enters 4.6 when prompted? (iv) enters 2 when prompted? Justify each of your answers. (e) Rewrite progexamalgo so that the code will exit normally if the user input is invalid. The code should throw an Exception if a valid integer outside of the range 1-12 is specified, and catch any other Exception. If an Exception of any kind is caught, a message should be printed to the user stating Invalid input". (f) Declare an enum called SEASONS that has four possible values: Spring, Summer, Autumn, and Winter. This enum should have public access and not require an instantiated instance of any object.

Page 7 6. (a) In the context of Java, what does inheritance mean? (b) Given the following class: public abstract class Building protected int numrooms; protected int numfloors; public abstract void printinfo(); (i) Write Java code for a class House that inherits from Building. It should have a field for the number of bedrooms, a field for the number of bathrooms, and an implementation of printinfo that prints the number of bedrooms. (ii) Write Java code for a class Office that inherits from Building. It should have a field for the number of desks, and an implementation of printinfo that prints the number of desks. (iii) Write Java code for a class Road. Road should have a single field to store an array of Building objects, and a method printroad that loops through all Building objects stored by the Road and calls printinfo on each. You do not need to include constructors, accessors, or mutators in your answers. (c) Assume that you have a Road object that contains both House and Office objects within its collection of Building objects. Explain what will happen when you call the printroad method for this Road. (d) Draw a UML diagram to describe the relationship between Building, House, Office, and Road. You do not need to include constructors, accessors, or mutators in your UML diagram. END OF PAPER