York University Fall 2001 / Test #1 Department of Computer Science

Similar documents
Java By Abstraction - Test-B (Chapters 1-6)

Selected Questions from by Nageshwara Rao

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

H212 Introduction to Software Systems Honors

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

More on variables and methods

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

Using Java Classes Fall 2018 Margaret Reid-Miller

CS 307 Midterm 2 Fall 2010

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

Final Exam Practice. Partial credit will be awarded.

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

Lecture 4. Strings, Wrappers, & Containers

Lecture 7. Strings & Stepwise Refinement

appreciate the difference between a char and a string understand and use the String class methods

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

String is a Class; Quoted Text is an Object

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

Object-Oriented Programming

CS 101 Fall 2006 Midterm 1 Name: ID:

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

IS502052: Enterprise Systems Development Concepts Lab 1: Java Review

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Lab 14 & 15: String Handling

We now start exploring some key elements of the Java programming language and ways of performing I/O

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Utilities (Part 2) Implementing static features

Lab5. Wooseok Kim

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

1B1b Classes in Java Part I

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CS162 Computer Science I Fall 2018 Practice Exam 1 DRAFT (9 Oct.)

A variable is a name for a location in memory A variable must be declared

University of Palestine. Mid Exam Total Grade: 100

Computer Science II (20082) Week 1: Review and Inheritance

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

GROUP - A <10 questions x 4 points each = 40 points >

Practice Midterm 1. Problem Points Score TOTAL 50

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Computer Science 145 Midterm 1 Fall 2016

CS 113 MIDTERM EXAM 2 SPRING 2013

WES-CS GROUP MEETING #9

APS101- Midterm Sample Questions

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Advanced Object Oriented Programming EECS2030Z

STUDENT LESSON A10 The String Class

CSC Java Programming, Fall Java Data Types and Control Constructs

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

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

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

Designing data types. Overview. Object Oriented Programming. Alan Kay. Object Oriented Programming (OOP) Data encapsulation. Checking for equality

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSCI 161 Introduction to Computer Science

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Designing data types. Fundamentals of Computer Science I

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

CSE 1223: Exam II Autumn 2016

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

COE318 Lecture Notes Week 5 (Oct 3, 2011)

Java Basic Programming Constructs

1. Data types ( =13 points)

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

CMPS 134: Computer Science I Fall 2011 Test #1 October 5 Name Dr. McCloskey

Prelim One Solution. CS211 Fall Name. NetID

A Foundation for Programming

Strings! Today! CSE String Methods!

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS 307 Final Spring 2008

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

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS 139 Practice Midterm Questions #2

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CS112 Lecture: Characters and Strings

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Programming Language Concepts: Lecture 2

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

1007 Imperative Programming Part II

Final Exam. COMP Summer I June 26, points

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Controls Structure for Repetition

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Introduction to Programming Using Java (98-388)

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Transcription:

York University all 2001 / est #1 Department of Computer Science COSC1020.01 his is a closed book, 90-minute test. ill in your personal data below and wait. You may use pen or pencil but answers written in pencil will not be re-marked. Once the test starts, verify the number of pages and write your name on each. Last Name Return the marked test to (circle one): irst Name A 14:30MW D 19:00 H 12:30MW Login cs B 13:00/R E 19:00W J 15:30MW York ID C 10:30MW G 14:30R K 19:00M String Methods (invoke on a string s) boolean equals(string t) Returns true if s and t have equal contents. int indexof(string t, int f) Looks for the string t within s, starting at position# f in s. Returns the position in s where the match was found. Returns -1 if no match was found. int indexof(string t) Looks for the string t within s (as above), starting at the beginning of s. String substring(int f,int t) Returns all characters in s with position numbers f and < t. String substring(int f) Returns a substring of s that begins at f and extends to the end of s. String replace(char x,char y) Returns a string with all occurrences of character x in s replaced by y. String toupper/lowercase() Returns a string of all characters in s converted to upper / lower case. char charat(int p) Returns the character at position# p in s. int compareo(string t) Returns a negative number if s<t, zero if s=t, and a positive number if s>t. Integer.parseInt(s) Double.parseDouble(s) Static methods to convert a string s that contains a number to a primitive type. String trim() Returns the same content as s but with any leading/trailing white-space removed. Static methods in Math double abs(double x) Returns the absolute value of x. double pow(double x, double y) Returns x raised to y. double rint(double a) Returns the closest double value to a that is equal to a mathematical integer. Do not write below this line A (30%) B (30%) C (40%) O

GROUP - A <30 points > here are 6 questions in this group each containing 5 statements. Circle if a statement is true, if it is false, and leave blank if you re not sure. You get +1 for each correct circle, 1 for each wrong one, and 0 for blanks. Your total mark in any of the 6 questions is never less than zero. Note that there could be no true statement or more than one true statement in each question. A.1 he API of a class: a. lists all private fields and methods in the class b. specifies, for each listed field, whether it is static or not c. specifies, for each listed method, whether it is static or not d. specifies the data type of each listed field e. specifies the return type of each listed method A.2 he output of the following statement is 8 a. York.println("7+1"); b. York.println(Integer.parseInt("7") + 1); c. York.println("7" + 1); d. York.println(7 + 1); e. York.println(Math.pow(2,3)); A.3 A constructor: a. does not have to have the same name as its class b. does not have to have parameters c. must appear in the API of its class d. does not have a return type in the API e. can be overloaded COSC1020.01 / ALL 2001 ES #1 Pg. 2 of 10

GROUP - A, continued A.4 he following statements are syntactically correct: a. int k = -5(2 + 8); b. int k = 3 2.5; c. int k = 5 / 2; d. double d = 5.5 / 3 * 2.5; e. double d = "5.5"; A.5 In Java: a. static fields cannot be changed once set b. if x,y,z are boolean variables, then the following boolean is always true: x && y == (x && z) && (y && z) c. he statement: double avg = (a + b) / 2; correctly computes the average of two initialised int variables a and b. d. he expression a*b*c/d is evaluated as ((a*b)*(c/d)) e. If a and b are two initialised int variables, then the following statement will generate a syntax error: boolean x = a < b; A.6 Assume that Quark is a valid class and that it has a default constructor. Assume also that x and y are declared to be of type Quark. a. Whenever the boolean (x!= null && x == y)is true, the boolean x.equals(y) must also be true b. Whenever the boolean (x == y)is false, the boolean x.equals(y) must also be false c. x = new Quark(); y = new Quark(); imply that the boolean (x == y)must be true d. he statement: x = new Quark(); may assign null to x. e. he statement: y = y + x; will generate a syntax error COSC1020.01 / ALL 2001 ES #1 Pg. 3 of 10

GROUP - B <30 points > here are 6 multiple-choice questions in this group with a number of answers listed under each. Put a checkmark in the box to the right of the one that best answers the question. You get +5 if you checked the correct answer, and zero if you checked the wrong one or checked more than one box per question. B.1 int age = 20; String s = "oo Young"; String t = age + " " + s; s.touppercase(); York.println(s + " " + t); he output of the above Java fragment is: a. oo Young 20 oo Young b. OO YOUNG 20 OO YOUNG c. OO YOUNG 20 oo Young B.2 he API of the Quark class indicates that it has a default constructor and lists an int field called charm. Quark x = new Quark(); Quark y = new Quark(); Quark z = new Quark(); x.charm = 5; y.charm = 7; z.charm = x.charm; x = y; y.charm = 10; York.print("" + x.charm + y.charm + z.charm); he output of the above Java fragment is: a. 101010 b. 5105 c. 10105 COSC1020.01 / ALL 2001 ES #1 Pg. 4 of 10

GROUP - B, continued B.3 int x = 3; int y = 27; if (x % 5 == 0) { y = y 7; } else if (x > -5) { y = y / 7; } else if (x == 3) { y = y + 3; } York.println(y); he output of the above Java fragment is: a. 3 b. 30 c. 20 B.4 int x = 5; for (int y = 2; y < 7; y++) { x = x + y; } York.println(x); he output of the above Java fragment is: a. 5 b. 19 c. 10 COSC1020.01 / ALL 2001 ES #1 Pg. 5 of 10

GROUP - B, continued B.5 Assume that the file data.txt exists, and that it has just one record (line) that contains the word: york YorkReader yr = new YorkReader("data.txt"); String rec = YorkReader.readLine(); York.println(rec); he output of the above Java fragment is: a. york b. data.txt c. 1 B.6 he API of the String class includes the following method: int lastindexof(string str, int fromindex) Returns the index within this string of the last occurrence of the specified substring str. he returned index indicates the start of the substring, and it must be equal to or less than fromindex. If str occurs one or more times as a substring within this object at a starting index no greater than fromindex, then the index of the first character of the last such substring is returned. If it does not occur as a substring starting at fromindex or earlier, -1 is returned. String str = "Each of them offered to pay it off"; int k = str.lastindexof("of", 18); York.println(k); he output of the above Java fragment is: a. -1 b. 5 c. 13 COSC1020.01 / ALL 2001 ES #1 Pg. 6 of 10

GROUP - C <40 points > C.1 <20 points> Given a portfolio object reference pf, write a code fragment that performs the following task: if the portfolio has exactly one investment, compute the difference between the current and the book value of the portfolio, and then output a sentence that depends on the computed difference: if the difference is positive, say 12545.55, then output: Your profit is: $12,545.55 Otherwise (for negative or zero difference), output: You didn't make any profit. On the other hand, if the given portfolio has more than (or less than) one investment, your fragment must terminate the program and issue an appropriate error message like "Assertion ailure. Portfolio must have a single investment". Your output must have the same layout / format as the samples shown above. Recall that the API of the Portfolio class includes: Investment getirst() Determine the first investment in this portfolio. Investment getnext() Determine the next investment in this portfolio. int getcount() Determine the number of investments in this portfolio. And that the API of the Investment class includes: double getbookvalue() Determine the book value of the stock held in this investment. Stock getstock() Determine the stock held in this investment. int getqty() Determine the quantity of the stock held in this investment. And that the API of the Stock class includes: String getname() Determine the name of this stock. double getprice() Determine the price of this stock. String getsymbol() Determine the symbol of this stock. Write your fragment on the next page. COSC1020.01 / ALL 2001 ES #1 Pg. 7 of 10

C.1, continued Write the fragment here assuming pf refers to a valid Portfolio object: COSC1020.01 / ALL 2001 ES #1 Pg. 8 of 10

GROUP - C C.2 <20 points> Write a program that reads a string from the user containing a date in the form: d/m/y and outputs the corresponding day name, exactly as shown in the following log: Enter the date: 21/10/2001 his is a Sunday. You can assume that the input is valid (a day number followed by a slash followed by a month number followed by a slash followed by the year) and that it represents a valid date. Note that there is a period immediately after the day name in the output. Note also that the day/month in the entered string may be made up of one digit. October 1 st, 2001, for example, is entered as 1/10/2001, not 01/10/2001, and May 1 st of that year is entered as 1/5/2001, not 01/05/2001. Your program must use the services of the DiaryPage class whose API is shown (in part) below: Constructor Summary DiaryPage(int d, int m, int y) Construct a diary page object having the passed day d (1 d 31), month m (1 m 12), and year y (y>0). Method Summary int getdayofweek() Determine the day of the week of this diary page object. Return an integer between 1 and 7, with 1=Monday, 2=uesday, and 7=Sunday Higher marks will be given to programs that don't use selection (such as if or switch statements), make proper use of the language constructs, and respect the style guide. Nevertheless, partial credit will be given to all correct programs. Write the program on the next page. COSC1020.01 / ALL 2001 ES #1 Pg. 9 of 10

C.2, continued import york.*; public class C2 { public static void main(string[] args) { COSC1020.01 / ALL 2001 ES #1 Pg. 10 of 10