THE UNIVERSITY OF AUCKLAND

Similar documents
Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Using Files as Input/Output in Java 5.0 Applications

public static void main(string[] args) { System.out.println("hello, world"); } }

CS 111 Classes I 1. Software Organization View to this point:

Simple Java I/O. Streams

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Object-Oriented Programming in Java

Introduction to Java

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Course Intro Instructor Intro Java Intro, Continued

Quick Introduction to Java

13 File Output and Input

What are exceptions? Bad things happen occasionally

Event-Driven Programming

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Files and input/output streams

Lecture J - Exceptions

Arrays. Introduction. Chapter 7

Computer Programming I

AP Computer Science File Input with Scanner

AP Computer Science Java Subset

Explain the relationship between a class and an object. Which is general and which is specific?

Chapter 2: Elements of Java

Homework/Program #5 Solutions

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Reading Input From A File

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Java Interview Questions and Answers

WRITING DATA TO A BINARY FILE

CMSC 202H. ArrayList, Multidimensional Arrays

Building a Multi-Threaded Web Server

JAVA.UTIL.SCANNER CLASS

CS506 Web Design and Development Solved Online Quiz No. 01

Creating a Simple, Multithreaded Chat System with Java

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

CS 1302 Ch 19, Binary I/O

Chapter 2 Introduction to Java programming

Crash Course in Java

J a v a Quiz (Unit 3, Test 0 Practice)

Stream Classes and File I/O

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Maria Litvin Phillips Academy, Andover, Massachusetts. Gary Litvin Skylight Publishing, Andover, Massachusetts

Java Programming Fundamentals

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Application Development with TCP/IP. Brian S. Mitchell Drexel University

INPUT AND OUTPUT STREAMS

core 2 Basic Java Syntax

Topic 11 Scanner object, conditional execution

Data Structures in the Java API

D06 PROGRAMMING with JAVA

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Software Engineering II. Java 2 Collections. Bartosz Walter <Bartek.Walter@man.poznan.pl>

Introduction to Programming System Design. CSCI 455x (4 Units)

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Java Application Developer Certificate Program Competencies

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

AP Computer Science Java Mr. Clausen Program 9A, 9B

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Software Development with UML and Java 2 SDJ I2, Spring 2010

The following program is aiming to extract from a simple text file an analysis of the content such as:

Lewis, Loftus, and Cocking. Java Software Solutions for AP Computer Science 3rd Edition. Boston, Mass. Addison-Wesley, 2011.

D06 PROGRAMMING with JAVA

Unit 6. Loop statements

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

Programming Languages CIS 443

Input / Output Framework java.io Framework

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

What Is Recursion? Recursion. Binary search example postponed to end of lecture

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Building Java Programs

Introduction to Programming

Sample CSE8A midterm Multiple Choice (circle one)

Analysis of Binary Search algorithm and Selection Sort algorithm

Basic Programming and PC Skills: Basic Programming and PC Skills:

CompSci-61B, Data Structures Final Exam

java Features Version April 19, 2013 by Thorsten Kracht

Decision-making Computer Science Lesson to Prepare for UIL Computer Science Contest

Basics of Java Programming Input and the Scanner class

CSC 551: Web Programming. Fall 2001

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Principles of Software Construction: Objects, Design and Concurrency. Java Collections. toad Fall 2013

D06 PROGRAMMING with JAVA

COSC 6397 Big Data Analytics. Mahout and 3 rd homework assignment. Edgar Gabriel Spring Mahout

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Class 32: The Java Collections Framework

Introduction to Java. CS 3: Computer Programming in Java

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Datastrukturer och standardalgoritmer i Java

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Transcription:

VERSION 00000001 COMPSCI 105 THE UNIVERSITY OF AUCKLAND FIRST SEMESTER, 2010 Campus: City Computer Science TEST Principles of Computer Science (Time Allowed: 50 MINUTES) Note: The use of calculators is NOT permitted. Compare the exam version number on the Teleform sheet supplied with the version number above. If they do not match, ask the supervisor for a new sheet. Enter your name and student ID on the Teleform sheet. Your name should be entered left aligned. If you name is longer than the number of boxes provided, truncate it. Answer Section all Multiple-choice questions on the Teleform answer sheet provided. Answer Section B in the space provided in this booklet. Attempt all questions. Use a dark pencil to mark your answers in the multiple choice answer boxes on the Teleform sheet. Check that the question number on the sheet corresponds to the question number in this question/answer book. If you spoil your sheet, ask the supervisor for a replacement. Write your answers in the space provided in the short answer section. Write as clearly as possible. The space provided will generally be sufficient but is not necessarily an indication of the expected length. Extra space is provided at the end of this exam book. An appendix with a simplified API is included on the last page. You may detach this appendix. Surname: First Name(s): Student ID: Login Name(UPI): MARKERS ONLY Section Marks Out Of A 25 B 25 Total 50

VERSION 00000001-2 - COMPSCI 105 Given the nested if-else structure below, answer the following TWO questions. (Question 1 and Question 2) if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; Question 1 [1 mark] If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? (a) 4 (b) 2 (c) 3 (d) 5 (e) 0 Question 2 [1 mark] If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed? (a) 5 (b) 3 (c) 2 (d) 4 (e) 0 Question 3 [2 marks] If x is an int where x = 1, what will x be after the following loop terminates? while (x < 50) x *= 2; (a) 2 (b) 64 (c) 100 (d) 50 (e) This is an infinite loop. Question 4 [2 marks] Given that s is a String, what does the following loop do? for (int j = s.length( ); j > 0; j--) System.out.print(s.charAt(j-1)); (a) It prints s out backwards after skipping the last character (b) It prints s out forwards (c) It yields a run-time error because there is no character at s.charat(j-1) for j = 0 (d) It prints s out backwards but does not print the 0th character (e) It prints s out backwards

VERSION 00000001-3 - COMPSCI 105 Question 5 [1 mark] Given the following: double[][] things ={ {1.2, 9.0,{9.2, 0.5, 0.0,{7.3, 7.9, 1.2, 3.9 ; What is the value of things[2].length? (a) 0 (b) 3 (c) 9 (d) 4 (e) 2 Question 6 [2 marks] What is the output of the following code fragment? ArrayList<Integer> list = new ArrayList <Integer> (); for (int i=0; i<4; i++) list.add(i,i); System.out.println(list); (a) [3, 2, 1, 0] (b) Run-time Error (c) [1, 2, 3, 4] (d) [0, 1, 2, 3, 4] (e) [0, 1, 2, 3] Question 7 [2 marks] Which of the following loops would adequately add 1 to each element stored in the array values? (a) for (int j=1; j<values.length; j++) values[j]++; (b) for (int j=1; j<values.length-1; j++) values[j]++; (c) for (int j=0; j<=values.length; j++) values[j]++; (d) for (int j=0; j<values.length-1; j++) values[j]++; (e) for (int j=0; j<values.length; j++) values[j]++; Question 8 [1 mark] To declare a three-dimensional int array called threed, which of the following would you use? (a) int[ ][ ][ ] threed; (b) int [ [ [ ] ] ] threed; (c) int[ ] threed[3]; (d) int[,, ] threed; (e) int[3] threed; Question 9 [2 marks] What will be the value for x[1] after the following code is executed? int[ ] x = {22, 33, 44; arrayprocess(x);... public void arrayprocess(int[ ] a) { for(int k = 0; k < a.length; k++) a[k] = a[k] + 5; (a) 27 (b) 38 (c) 44 (d) 22 (e) 33

VERSION 00000001-4 - COMPSCI 105 Use the code below to answer the following TWO questions. (Question 10 and Question 11) Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. try { BufferedReader infile; infile = new BufferedReader(new FileReader(filename)); // i1 int x = Integer.parseInt(infile.readLine( )); // i2 a[++i] = (double) (1 / x); // i3 catch (FileNotFoundException ex) {... // e1 catch (NumberFormatException ex) {... // e2 catch (ArithmeticException ex) {... // e3 catch (ArrayIndexOutOfBoundsException ex) {... // e4 catch (IOException ex) {... // e5 Question 10 [2 marks] An exception raised by the instruction in i1 would be caught by the exception(s). (a) either FileNotFoundException or ArrayIndexOutOfBoundsException (b) either FileNotFoundException or IOException (c) ArithmeticException only (d) NumberFormatException only (e) ArrayIndexOutOfBoundsException only Question 11 [2 marks] An exception raised by the instruction in i2 would be caught by the exception(s). (a) FileNotFoundException only (b) either NumberFormatException or IOException (c) ArrayIndexOutOfBoundsException only (d) either FileNotFoundException or IOException (e) IOException only Question 12 [1 mark] If int[ ] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown? (a) ArithmeticException (b) NullPointException (c) ArrayIndexOutOfBoundsException (d) NegativeArraySizeException (e) NumberFormatException

VERSION 00000001-5 - COMPSCI 105 Question 13 [2 marks] What is the big-o complexity of the following code fragment in terms of n, the size of the array? public static void swap(int[] a, int index1, int index2) { int temp = a[index1]; a[index1] = a[index2]; a[index2] = temp; (a) O(3) (b) O(n) (c) O(1) (d) O(n 2 ) (e) O(log n) Question 14 [2 marks] What is the big-o complexity of the following code fragment in terms of n? x = 2 * n; y = n - 4; total = 0; while (x > 0) { if ((x % 2) == 0) total = total + y; else total = total - x; x = x - 1; (a) O(1) (b) O(n 3 ) (c) O(n 2 ) (d) O(n) (e) O(log n) Question 15 [2 marks] What is the big-o complexity of the following code fragment in terms of n? int total = 0; for (int i=0; i<100; i++) total += n; (a) O(n 3 ) (b) O(n 2 ) (c) O(log n) (d) O(1) (e) O(n)

VERSION 00000001-6 - COMPSCI 105 SECTION B Answer all questions in this section in the space provided. If you run out of space then please use the Overflow Sheet and indicate in the allotted space that you have used the Overflow Sheet. ( An appendix with a simplified API is included on the last page. You may detach this appendix.) Question 16 [8 marks] (a) An int two-dimensional array stores the following values. 1 2 3 4 5 6 7 8 9 10 11 12 Write a code fragment to create and initialize the above two-dimensional array. (2 marks) (b) Complete the following method that takes an array of Strings as a parameter, and returns a single String that is the concatenation of all Strings in the array using a for-each loop. public String arrayconcatenatorbyforeachloop(string[] str){ String allelements = ""; return allelements; (3 marks) (c) Complete the following method that takes an ArrayList as a parameter, and returns a single String that is the concatenation of all Strings in the ArrayList using a simple for-loop. public String arraylistconcatenatorbyforloop (ArrayList<String> a){ String allelements = ""; return allelements; (3 marks)

VERSION 00000001-7 - COMPSCI 105 Question 17 [8 marks] (a) Complete the following method to use a Scanner object that opens a file named autos.txt and displays each line to the screen. public void readfromscanner() throws IOException { Scanner s = null; try { catch (Exception e) { System.out.println(e); finally { if (s!= null) s.close(); (4 marks) (b) Complete the following method to use a BufferedReader object that opens a file named autos.txt and displays each line to the screen. public void readfrombufferedreader() throws IOException { BufferedReader b = null; String line; try { catch (Exception e) { System.out.println(e); finally { if (b!= null) b.close(); (4 marks)

VERSION 00000001-8 - COMPSCI 105 Question 18 [3 marks] Show how the following array would change as a SELECTION SORT algorithm is used to sort the array into ascending numerical order. {25, 9, 12, 38, 11, 6 Original Array 25 9 12 38 11 6 1 st step 25 9 12 6 11 38 2 nd step... 6 9 11 12 25 38 (3 marks)

VERSION 00000001-9 - COMPSCI 105 Question 19 Given the following method factorial(): public static double factorial (double n) { if (n == 0) { return 1; else { return n * factorial(n-1); [6 marks] What is the value returned when the integer 3 is the argument to the factorial method? Perform a box trace of the call to the factorial method. Clearly indicate each subsequent recursive call. factorial(3) = (6 marks)

VERSION 00000001-10 - COMPSCI 105 Overflow pages please number answers carefully

VERSION 00000001-11 - COMPSCI 105 Overflow pages please number answers carefully

VERSION 00000001-12 - COMPSCI 105 Overflow pages please number answers carefully APPENDIX FOLLOWS

APPENDIX - 13 - COMPSCI 105 java.lang class Integer boolean equals(object obj) class String boolean equals(object anobject) char charat(int index) APPENDIX java.io class BufferedReader BufferedReader(Reader in) void close() int read() String readline() class PrintStream void print(string s) PrintStream printf(string format, Object... args) void println(string x) class FileReader FileReader(String filename) class File File(String pathname) java.util class Scanner Scanner(InputStream source) Scanner(File source) void close() boolean hasnext() boolean hasnextint() boolean hasnextline() String next() int nextint() String nextline() class ArrayList ArrayList() boolean add(e o) void add(int index, E element) boolean contains(object elem) E get(int index) boolean isempty() E remove(int index) E set(int index, E element) int size() void clear() for (initialization; termination; increment) { statement(s) for (type var : arr) { statement(s) while (expression) { statement(s) do { statement(s) while (expression);