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

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

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

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

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

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

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

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

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

More on variables and methods

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

H212 Introduction to Software Systems Honors

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

Object-Oriented Programming

Selected Questions from by Nageshwara Rao

String is a Class; Quoted Text is an Object

Using Java Classes Fall 2018 Margaret Reid-Miller

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

CompSci 125 Lecture 05. Programming Style, String Class and Literals, Static Methods, Packages

Introduction to Objects. James Brucker

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

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Utilities (Part 2) Implementing static features

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

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

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

Computer Science 145 Midterm 1 Fall 2016

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Lecture 4. Strings, Wrappers, & Containers

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

CS 61B Data Structures and Programming Methodology. June David Sun

JAVA OPERATORS GENERAL

Lab5. Wooseok Kim

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

AP CS Unit 3: Control Structures Notes

static String usersname; public static int numberofplayers; private static double velocity, time;

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

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

Java Basic Programming Constructs

Strings! Today! CSE String Methods!

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

Lecture 7. Strings & Stepwise Refinement

COMP 110 Programming Exercise: Simulation of the Game of Craps

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

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

Problem Grade Total

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Strings

CS112 Lecture: Characters and Strings

Introduction to Computer Science Unit 2. Notes

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

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

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

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

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

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

Character Strings COSC Yves Lespérance. Lecture Notes Week 6 Java Strings

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

CSC Java Programming, Fall Java Data Types and Control Constructs

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Lecture 17. For Array Class Shenanigans

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

AP Computer Science A

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

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

Programming Language Concepts: Lecture 2

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Midterm Examination

16-Dec-10. Consider the following method:

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

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

COMP-202 Unit 5: Basics of Using Objects

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

University of Palestine. Mid Exam Total Grade: 100

APS101- Midterm Sample Questions

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

CS 1302 Chapter 9 (Review) Object & Classes

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

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

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

Introduction to Computer Science Unit 2. Notes

COMP 250 Winter 2011 Reading: Java background January 5, 2011

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

COURSE: Introduction to JAVA Programming GRADE(S): UNIT 1: Introduction to Applets NATIONAL STANDARDS: ALL STUDENTS

Lab 14 & 15: String Handling

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

CS 139 Practice Midterm Questions #2

Bellwork Strings 11/25/13

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2011) - All Sections Midterm Examination

Course Outline. Introduction to java

Classes and Objects Part 1

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

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

Transcription:

Java By Abstraction - Test-B (Chapters 1-6) Last Name First Name Do not write below this line B (60%) A (40%) TOTAL 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 compareto(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.

GROUP - A <10 questions x 4 points each = 40 points > For each question, write in the box the output of the shown fragment. If you believe the fragment will not produce output due to errors, write the error type and a brief explanation. You can assume all needed classes are properly imported. A.1 int k = 16412; int m = k % 10; k = k / 10; k = k % 1000; IO.println(m); IO.println(k); A.2 String s = "York University"; int p = s.indexof("n"); int q = s.substring(3, 9).indexOf("n"); IO.println(p); IO.println(q); A.3 Stock s = new Stock("NT"); Stock u = new Stock(); Stock t = s; u.setsymbol("nt"); IO.println(t.getSymbol()); s.setsymbol("aty"); IO.println(t.equals(u)); Java By Abstraction Test-B Pg. 1 of 10

GROUP - A, continued A.4 int i1, i2, i3; double r1, r2; i1 = 5; r1 = 3.0; r2 = 2 * r1; i2 = 2 * i1; i3 = i2 / r2; IO.println(r2); IO.println(i3); A.5 The BankAccount class has a constructor that takes two parameters: the name of the account (a String) and its initial balance (a double). The class has a double accessor method getbalance() that returns the balance of the account on which it was invoked. BankAccount a1 = new BankAccount("Mary", 1000); BankAccount a2 = new BankAccount("Mary", 1000); BankAccount a3; a3 = a1; int m = 0; if (a2 == a3) { m = 55; } int k = 0; if (a2 == a1) { k = -1 } else if (a3.getbalance() == a2.getbalance()) { k = -22 } else { k = -333 } IO.println(m); IO.println(k); Java By Abstraction Test-B Pg. 2 of 10

GROUP - A, continued A.6 int x = 5763; int y = 0; int k; for (k = 0; x > 0; k++) { y = y + x % 10; x = x / 10; } IO.println(k); IO.println(y); A.7 int a = 10; int b = 20; int c = 30; boolean m = a + 10 == c; IO.println(m); if (a > b b + 10 > c) IO.println("case 1"); else if (a < b && b + 10 < c) IO.println("case 2"); else if (!(a + 20 > c) a + 10 < b) IO.println("case 3"); else IO.println("case 4"); Java By Abstraction Test-B Pg. 3 of 10

GROUP - A, continued A.8 String s = "abcdefgh"; int k = s.length() - 6; IO.println(s.substring(k, s.length() - 1)); boolean b = s.substring(1,2) == "b"; IO.println(b); A.9 String s1 = "100"; String s2 = "20"; IO.println(s1 + s2 + 30); IO.println(9 / 2 + 30 + s1 + s2); A.10 Stock stk1 = new Stock("RY"); Stock stk2 = new Stock("BMO"); Stock stk3 = stk2; stk2 = null; boolean b1 = stk2 == stk3; IO.println(b1); stk3.setsymbol("ry"); boolean b2 = stk3 == stk1; IO.println(b2); Java By Abstraction Test-B Pg. 4 of 10

GROUP - B <60 points > B.1 <20 points> Consider the following (partial) API of two classes: Department Class Constructor Summary Department(String name, int budget) Constructs a Department object. Parameters: name - name of the department budget - budget of this department Method Summary void assign(employee who) Add an employee to this department Parameter: who - the employee to be assigned to this department int getheadcount() Returns: the number of employees in this department int getbudget() Returns: the budget of this department void changebudget(int delta) Increase or decrease the budget of this department by the passed amount. Delta is the increment or decrement, not the new budget. Parameter: delta - change the budget by adding this amount to it (to reduce budget, provide a negative amount). Employee Class Constructor Summary Employee(String name,int rank) Constructs an Employee object. Parameters: name - name of the employee rank - the rank (level) of the employee Java By Abstraction Test-B Pg. 5 of 10

B.1, continued Develop the Java application App whose main method performs the following tasks, in the order shown: 1. Create a department called "R&D" with budget 2,000,000. 2. Create an employee John whose rank is 3. 3. Create an employee Debbie whose rank is 2. 4. Assign both John and Debbie to the R&D department 5. Determine the head count of the R&D department by using a method, and store it in some variable count. 6. If count is greater than 10, increase the department budget by 5%, otherwise reduce it by 2%. Note that it is OK to use the above magic numbers no need to store them in finals. Write your app on the next page. Java By Abstraction Test-B Pg. 6 of 10

B.1, continued import type.lang.*; public class App { public static void main(string[] args) { Java By Abstraction Test-B Pg. 7 of 10

GROUP - B B.2 <20 points> Write the program App that starts by prompting for and reading a string from the user. If the length of the entered string is equal to or greater than 20, the program must terminate with the error message "String too long!". Otherwise, the program outputs the string after padding it with '+' characters at its two ends, so that the total length of the output is 20 and the entered string is at the centre of the output. If the number of '+' characters to be added is odd, you can put the extra '+' on either side. Three samples are shown. Enter a string: Toronto +++++++Toronto++++++ import type.lang.*; public class App { public static void main(string[] args) { final int WIDTH = 20; Enter a string: Computer Science Dept. String too long! Enter a string: 1234567890 +++++1234567890+++++ Java By Abstraction Test-B Pg. 8 of 10

GROUP - B B.3 <20 points> Write the program App that plays a game with the user as follows: it prompts the user to enter a guess for the role of the dice. It then simulates throwing one die by generating a random number (an integer between 1 and 6, inclusive) and displaying it on the screen. If the user's guess was correct, the user gets $2; i.e. the program adds $2 to the user's balance, otherwise, the user loses $2, and the new balance is displayed on the screen. The game continues indefinitely until the user enters an invalid guess (less than 1 or more than 6) or runs out of money. The user starts off with $10. Here is a partial API of the Random class whose services enable you to simulate the throwing of a dice: Random Class in the java.util package Constructor Summary Random() Creates a new random number generator. Method Summary int nextint(int n) Returns a random number uniformly distributed between 0 (inclusive) and the specified parameter n (exclusive); i.e. the return is greater or equal to 0 and less than n. Write the program on the next page. Java By Abstraction Test-B Pg. 9 of 10

B.3, continued import type.lang.*; public class App { public static void main(string[] args) { Java By Abstraction Test-B Pg. 10 of 10