1st Semester Examinations CITS1001 3

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

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

MID-SEMESTER TEST 2014

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

School of Computer Science & Software Engineering SEMESTER 2, 2008 EXAMINATIONS 2008 JAVA PROGRAMMING 1200 (CITS1200)

CSSE SEMESTER 1, 2017 EXAMINATIONS. CITS1001 Object-oriented Programming and Software Engineering FAMILY NAME: GIVEN NAMES:

CITS1001 exam 2013 SAMPLE SOLUTIONS O cial cover page to be substituted here. May 10, 2013

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

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

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

2. [20] Suppose we start declaring a Rectangle class as follows:

CITS1001 week 6 Libraries

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

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

University of Massachusetts Amherst, Electrical and Computer Engineering

Final Exam Practice. Partial credit will be awarded.

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

5 More sophisticated behavior

Final Exam CS 251, Intermediate Programming December 13, 2017

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

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

COS 126 General Computer Science Spring Written Exam 1

COS 126 General Computer Science Fall Exam 1

CS 231 Data Structures and Algorithms, Fall 2016

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 13 th February

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Name CIS 201 Midterm II: Chapters 1-8

Scope of this lecture

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

This page intentionally left blank

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

CS111: PROGRAMMING LANGUAGE II

You must bring your ID to the exam.

Midterms Save the Dates!

University of Palestine. Mid Exam Total Grade: 100

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CS 177 Week 15 Recitation Slides. Review

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

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

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

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

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

CIS November 27, 2018

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Main loop structure. A Technical Support System. The exit condition. Main loop body

Computer Science is...

CIT Special final examination

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CITS1001 week 4 Grouping objects lecture 2

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Assignment 1 due Monday at 11:59pm

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

COMP 401 Spring 2013 Midterm 1

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Distributed Systems Recitation 1. Tamim Jabban

int b = 2; for (int a = 0; a < 10 b < 20; a++) { a = a + b; b = b + a; }

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

Object- Oriented Analysis, Design and Programming. Instructions. Medialogy, 4 th Semester, Aalborg. Tuesday 30 August 2011,

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

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

1 Inheritance (8 minutes, 9 points)

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

Activity 1: Introduction

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

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

APCS Semester #1 Final Exam Practice Problems

Java Simple Data Types

Computer Science 1 Ah

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Lab Assignment Three

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

XC Total Max Score Grader

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

HST 952. Computing for Biomedical Scientists Lecture 5

Transcription:

1st Semester Examinations CITS1001 3 Question 1 (10 marks) Write a Java class Student with three fields: name, mark and maxscore representing a student who has scored mark out of maxscore. The class has two methods: addbonus adds a given bonus score to the student s mark, tostring returns a String summarising the current state of the Student. You should select suitable types and parameters for your class its constructor and methods. Javadoc comments are not required for this question. Answer Question 1 here public class Student private String name; private int mark; private int maxscore; public Student(String name, int mark, int maxscore) if (mark > maxscore //bonus if some error handling this.name = name; this.mark = mark; this.maxscore = maxscore; public int addbonus(int bonus) mark = mark + bonus; if (mark > maxscore) mark = maxscore; //bonus for this public String tostring() return ("Stu: " + name + " has scored " + mark + " out of " + maxscore);

1st Semester Examinations CITS1001 4 Question 2 (10 marks) Using your Student class from Question 1, write an e cient method public Student topstudent(arraylist<student> cits1001) that returns the Student object with the highest percentage score from a given ArrayList of Students called cits1001. Use a helper method percentmark that returns the student s mark as a percentage rounded to the nearest integer. Javadoc comments are not required for this question. Answer Question 2 here private int percentmark() double pct = (double) mark * 100 / maxscore; //minor error for mark*100 / maxscore rounds down int mark = Math.round(pct); return ( mark ); public Student topstudent(arraylist<student> cits1001) int max = cits1001.get(0).getpercentmark(); Student best = cits1001.get(0); for (Student si : cits1001) if (si.percentmark() > max) max = si.percentmark(); best = si; return best;

1st Semester Examinations CITS1001 5 Question 3 (10 marks) Move tickets at the Windsor Cinema in Nedlands are priced as follows: Adult $16.50 Subscriber Concession $12.00 Senior / Pensioner / Children $11.00 Tuesday is our cheap day at the Windsor Cinema: $8.50 before 6pm and $10.50 after 6pm on the cheap days (a) Write a signature for a method ticketprice to return the cost of a movie ticket (in cents) given the day of the week, session time, and customer status (Adult, Subscriber Concession, Senior, Pensioner, Child). Then design up to 5 test cases for this method, writing a JUnit assertequals statement for each test. (b) Write code to implement the ticketprice method. Include a Javadoc header for the method that describes its parameters and return value. You may assume that the given input parameters are all valid. Javadoc comments are required for this question. Answer Question 3(a) here public int ticketprice(string day, int session, String status) Test cases such as assertequals(850, ticketprice(2,1400,"adult")); assertequals(1050, ticketprice(2,1400,"child")); assertequals(1100, ticketprice(5,1400,"senior")); assertequals(1200, ticketprice(6,1400,"subscriber Concession")); assertequals(1650, ticketprice(1,1400,"adult")); NOT needed, illegal input checks eg assertequals(-1, ticketprice("anyday",0,"martian")); legal input check not needed if ( (day<0) (day > 6) (hour > 2359) ) return -1;

1st Semester Examinations CITS1001 6 Answer Question 3(b) here /** * Calculate ticket price for Windsor cinema, assuming all inputs are legal * @param day int day of week from 0 "Sunday" to 6 "Saturday" * @param session int in 24 hour clock for the session time * @param status String must be Adult, Subscriber Concession, * Senior, Pensioner, Child to determine price * @return int ticket price in cents */ public int ticketprice(int day, int session, String status) a if (day==2) if (session < 1800) return 850; else return 1050; else if ( status.equals("senior") status.equals("pensioner") status.equals("child") ) return 1100; else if (status.equals("subscriber Concession")) return 1200; else return 1650;

1st Semester Examinations CITS1001 7 Question 4 (10 marks) The tech-support project studied in labs and lectures mapped user query words to technical responses. Write a method, including a Javadoc header, with the signature public String generateresponse( HashSet<String> words, HashMap<String, String> responsemap) The method should return a selected response from its (already initialised) parameter responsemap linked to one of the words from the HashSet parameter. If no matching response is found then return "Sorry I don t know". /** Answer Question 4 here * Generate a response from a given set of input words. * * @param words A set of words entered by the user * @return A string that should be displayed as the response */ public String generateresponse(hashset<string> words) for (String word : words) String response = responsemap.get(word); if(response!= null) return response; return "Sorry I don t know"; //otherwise

1st Semester Examinations CITS1001 8 Question 5 (10 marks) Compare the methods sort1 and sort2 for sorting an array of integers. Comment on the strengths and weakness of each implementation using the criteria of readability, e ciency, cohesion and correctness. public static void sort1(int[] a) for (int pass=1; pass<a.length; pass++) int x=a[pass]; int y=pass-1; while (y>=0 && a[pos]>x) a[y+1]=a[y]; y--; a[y+1]=x; public static void sort2(int[] a) for (int pass=1; pass < a.length; pass++) for (int j = 0; j < a.length-pass; j++) if (a[j] > a[j+1]) // exchange out of order elements swap(a, j, j+1) private void swap(int[] a, int pos1, int pos2) int swap = a[pos2]; a[pos2] = a[pos1]; a[pos1] = swap; Answer Question 5 on the next page

1st Semester Examinations CITS1001 9 Answer Question 5 here readability: sort1 does not use meaningful var name, sort2 does sort2 includes a useful comment explaining why the swap efficiency: sort2 is selection sort which is less efficient than sort1 is insertion sort because sort1 only moves part of the array cohesion: sort2 uses a helper method which makes the main method more readable and the method more cohesive correctness: both are correct for non-null arrays. both will crash if a is null (when a.length is called)

1st Semester Examinations CITS1001 10 Question 6 (50 marks, 20 questions) Question 6 must be answered on the machine readable multiplechoice answer sheet provided. 1. Which one of the following lines of Java code may cause a compilation error? *[a.] String a = Hello; b. short a = 14; c. double a = 14; d. boolean a = true; e. int[] a = 1,2,3; 2. Consider the Java variable declarations: int x = 7 / 5; int y = (-7) / 5; int z = 7 % 5; What are the values of x, y and z (respectively) after these declarations. a. 1, -1, 1 b. 1.4, -1.4, 2 *[c.] 1, -1, 2 d. 1, -2, 2 e. 1.4, -1.4, 2 3. You are writing a program for the Olympics Committee. The application has an Athlete class with fields to keep track of the name, gender, age, and sport of each athlete. What are the most appropriate types (respectively) to use for these variables? a. String, String, double, String b. String, boolean, double, double c. String, String, int, double *[d.] String, char, int, String e. String, char, Integer, char[]

1st Semester Examinations CITS1001 11 4. What sort of variables are used to store the state of an individual object? a. Local variables. *[b.] Field variables. c. Class variables. d. Argument variables. e. Method variables. 5. The Picture class of the shapes project studied in lectures and labs includes the declaration private Circle sun; In the statement sun.changecolor("yellow"); what does changecolor refer to? a. It is a field belonging to the class Circle. *[b.] It is a method belonging to the class Circle. c. It is a method belonging to the class Picture. d. It is a method belonging to the object sun. e. It is a field belonging to the object sun. 6. Which of the following statements is not true about information hiding in object oriented programming? a. It prevents information overload b. It reduces complexity by restricting access on a need to know basis. c. It is achieved, in part, by making instance variables private *[d.] It makes it harder to call the methods of a class because the implementation details are hidden. e. It makes it easier for teams of programmers to produce reliable software.

1st Semester Examinations CITS1001 12 7. Consider the following block of code, where variables a, b and c each store integer values: if (a < b) if (a < c) System.out.println(a); else System.out.println(c); else if (b < c) System.out.println(b); else System.out.println(c); Which one of the following values for the variables a, b and c (respectively) will cause the value in variable b to be printed? a. 1, 2, 3 b. 3, 2, 1 c. 1, 3, 2 *[d.] 2, 1, 3 e. 2, 2, 1 8. What is the e ect of the following Java statement Die[] dice = new Die[10]; a. A Die object representing a 10-sided die is formed b. An array containing 10 newly constructed Die objects is formed c. A label for an array is created, and initialized to the default value null *[d.] An array containing 10 null references is formed e. Ten labels, each referring to an array of objects of type Die are created.

1st Semester Examinations CITS1001 13 9. The class UniBook has the following fields: private String author; private String title; private int yearpublished; private double cost; Suppose that you are asked to write a method computetotalcost that computes the total cost of books owned by each student. Which of the following method signatures would be most appropriate for the method? a. public void computetotalcost() b. private double computetotalcost() c. public double computetotalcost(double[] booklist) d. public double computetotalcost(unibook book) *[e.] public double computetotalcost(arraylist<unibook> booklist) 10. What will the method call sillymethod(875) return, where the method is defined as follows: public int sillymethod(int n) int m = 0; while (n > 10) m = n % 10; n = n / 10; return m; a. 875 b. 8 *[c.] 7 d. 5 e. 0

1st Semester Examinations CITS1001 14 11. What output will the method call show(5) produce on the terminal window if the method show is defined as follows: public void show(int n) int num = 0; for (int i=1; i<n; i++) for (int j=0; j<i; j++) System.out.print(num++); System.out.print(" "); System.out.println(" "); a. 1 2 3 4 5 6 7 8 9 10 b, 0 0 0 0 0 0 0 0 0 0 c. 0 *[d.] e. 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

1st Semester Examinations CITS1001 15 12. Suppose that an array int[] f contains frequency data for some application. The cumulative frequency array is obtained by replacing each element in the array by the sum of itself and all of the previous elements in the array. In other words, the cumulative frequency array is f[0], f[0]+f[1], f[0]+f[1]+f[2],... Which of the following three methods will correctly transform the frequency array f into a cumulative frequency array? // Method 1 public void cumulate(int[] f) for (int i=1; i<f.length; i++) f[i] = f[i] + f[i-1]; // Method 2 public void cumulate(int[] f) for (int i=f.length-1; i>=0; i--) for (int j=0; j<i; j++) f[i] = f[i] + f[j]; // Method 3 public void cumulate(int[] f) for (int i=0; i<f.length; i++) for (int j=0; j<i; j++) f[i] = f[i] + f[j]; a. All of them. b. None of them. c. 2 and 3 only. d. 1 and 3 only. *[e.] 1 and 2 only.

1st Semester Examinations CITS1001 16 13. What values would the variables answer and words hold after executing the following Java statements. HashMap<String,String> translate = new HashMap<String,String>(); translate.put("hello","guten tag"); String answer = translate.get("goodbye"); int words = translate.size(); a. "" and 1 b. "guten tag" and 1 *[c.] null and 1 d. null and 2 e. No values because this code would cause a run-time exception to be thrown 14. Suppose that we execute the following code snippet int[] a = new int[5]; int[] b = new int[5]; for (int i=0;i<5;i++) a[i] = 2*i; b[i] = a[i]; What is the value of the expression b == a, and why does it have that value? *[a.] false because a and b are di erent arrays, even though they contain the same values. b. true because a and b are arrays of the same length with the same values. c. true because they are both arrays of the same type. d. false because the values stored in the arrays are di erent. e. false because == always returns false whenever reference types are compared.

1st Semester Examinations CITS1001 17 15. Consider an incorrect implementation of a method maxpos in a utility class AM. The method is intended to return the index of the maximum element in its argument array: public static int maxpos(int[] a) if ((a == null)) return -1; int max = a[0]; int maxpos = -1; for (int i = 0; i < a.length; i++) if (a[i] > max) max = a[i]; maxpos = i; return maxpos; Which of the following JUnit4 tests will fail for this method? 1. assertequals(3,am.maxpos(new int[] 5,7,1,10,-2,5)); 2. assertequals(0,am.maxpos(new int[] 5)); 3. assertequals(4,am.maxpos(new int[] 5,10,-2,5,11)); 4. assertequals(-1,am.maxpos(new int[] )); 5. assertequals(-1,am.maxpos(null)); a. 1, 4 and 5 only b. 1 and 2 only *[c.] 2 and 4 only d. 2, 3 and 4 only e. all of them

1st Semester Examinations CITS1001 18 16. Suppose that a class ClassA has a method with the signature public void method() and that variable a is declared and created in another class, ClassB, using: ClassA a = new ClassA(); Consider the following three Java statements that occur in a method belonging to ClassB. Which one(s) are valid Java statement(s)? 1. ClassA.method(); 2. a.method(); 3. method(); a. 1 only b. Only 1 and 2 *[c.] 2 only d. Only 1 and 3 e. All of them 17. If a method uses checked exceptions to deal with unexpected (but recoverable) run-time events, how is that fact communicated to potential users of that method? a. In its return type b. In its name c. In its arguments *[d.] In its signature e. In its body 18. Which of the following collections would be most appropriate for storing a directory of names and phone numbers? a. ArrayList<String> b. HashSet<String> *[c.] HashMap<String, String> d. String[] e. HashMap<String, Integer>

1st Semester Examinations CITS1001 19 19. What does mystery(a, b) calculate, assuming a 0 and b > 0? public int mystery(int a, int b) if (a < b) return a; else return mystery(a - b, b); a. It calculates a + b. b. It calculates a - b. c. It calculates a * b. d. It calculates a / b. *[e.] It calculates a % b. 20. What happens when the statement int[] ra = reverse(new int[] 3,6,8,9); is executed where public int[] reverse(int[] a) int[] rev = new int[a.length]; for (int i=0; i < a.length; i++) rev[i] = a[a.length return rev; a. The array ra contains 9,8,6,3 - i]; *[b.] A java.lang.arrayindexoutofboundsexception is thrown c. A java.lang.illegalargumentexception is thrown d. The array ra contains 8,6,3 e. None of the above

1st Semester Examinations CITS1001 20 Blank Page for Rough Work