CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Similar documents
CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Introduction to Programming Using Java (98-388)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

CS1150 Principles of Computer Science Methods

CS1150 Principles of Computer Science Methods

Computational Expression

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES

CS-201 Introduction to Programming with Java

PROGRAMMING FUNDAMENTALS

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

VARIABLES AND TYPES CITS1001

Programming with Java

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Introduction to Programming (Java) 4/12

CS 231 Data Structures and Algorithms, Fall 2016

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Simple Java Reference

Lecture 5: Methods CS2301

Following is the general form of a typical decision making structure found in most of the programming languages:

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

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

Java+- Language Reference Manual

Lecture 3: C Programm

About this exam review

CSc Introduction to Computing

ECE 122. Engineering Problem Solving with Java

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Technical Questions. Q 1) What are the key features in C programming language?

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

PIC 20A The Basics of Java

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 4 Defining Classes I

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

Peer Instruction 1. Elementary Programming

Creating Classes and Objects

Introduction to Java

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

FORM 2 (Please put your name and form # on the scantron!!!!)

CS 2340 Objects and Design - Scala

CS111: PROGRAMMING LANGUAGE II

Java Fundamentals (II)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Packages & Random and Math Classes

Review for Test 1 (Chapter 1-5)

Getting started with Java

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Computer Programming, I. Laboratory Manual. Final Exam Solution

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Language Reference Manual

CS260 Intro to Java & Android 03.Java Language Basics

CS 251 Intermediate Programming Java Basics

Java Programming. Atul Prakash

Java Bytecode (binary file)

Introduction to Classes and Objects

Java is an objet-oriented programming language providing features that support

M.CS201 Programming language

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

COMP 202 Java in one week

Center for Computation & Louisiana State University -

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Basics of Java Programming

Objectives. Introduce static keyword examine syntax describe common uses

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

An overview of Java, Data types and variables

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

( &% class MyClass { }

CS11 Java. Fall Lecture 1

3. Java - Language Constructs I

CS162: Introduction to Computer Science II

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Chapter 1: Introduction to Computers, Programs, and Java

Full file at

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Java Fall 2018 Margaret Reid-Miller

Software and Programming 1

1 Lexical Considerations

Defining Classes and Methods

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Formatting Output & Enumerated Types & Wrapper Classes

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Lecture 2 Tao Wang 1

Transcription:

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : In mathematics, we learned that every integer, n has a successor, n + 1, that is greater than n. This is also true in Java. (b) T (c) T F : There are four different types of integers in Java so that we can save memory. F : The left hand side of an assignment statement can be a literal constant. (d) T F : A reference variable does not have to refer to an object, but in order to use an object, it must be referenced by a reference variable or parameter. (e) T (f) T F : In Java, some methods are not in any class. F : A single Java code file, compiled on different machines, can produce different results. (g) T F : If aliens from outer space prevent all computers from typing (or cut and pasting) the character (right curly brace), then no one could write any new Java code. (h) T F : Multiple Java files can be in a single package, but a single Java file cannot be in multiple Java packages. (i) T quad. F : There are eight primitive types in java char, bytes, short, long, float, double, and (j) T F : An Array in Java an object with a length field, a field for each element of the array, and a tostring field. 2. (10 points) For the following, put a check in front of the statements that are are valid constant expressions. 32 768 9934 0x1ABd 6.23e22 so called 0340721 x 0b729 CS-140 4.729 3. (10 points) For the following, put a check in front of the statements that are valid declaration statements. int x=17; int i = 37 295; int j=1,024; byte small = 3 * 110; float pi=3.1514; final double IF = 6.79e3; double break=5.0; final double IF = 6.79e3; double bignumber=inf; int num cookies=093; Page 1 of 6

4. (10 points) For the following, put a check in front of the statements that are syntactically correct, assuming the following declare statements: int x=3; char i n i t= T ; x = /* 3 / 0 */ x + 2; x = /* /* 3 / 0 */ x / 0 */ * 3; x = (int)init + 3; init = x+3; x = x % 12; init = / Z ; init = init; x = 4.3 / 1.765 ; x = 21 if (init== T ); x*3=9; 5. (10 points) Given the following declarations: int a = 1 3 ; int b=4; int c =2; double fx =1.3; double fy = 7.0; Determine the value of the following expressions (Be sure to use a decimal point in floating point answers, and leave out decimal points in integer answers.) (a) (b) = a + b = a % c (f) = fy + 7 (g) = (a + fx) * 2 (c) = (fy >= 0)? p : n (h) = (a<0) && (b!=1) (d) = b + 17 (i) = fy < fx (e) = fy / b (j) = 12-8.0 Page 2 of 6

6. (10 points) Given the following Java program: package t e s t 1 ; public class Tester { public static void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( Testing the Location c l a s s ) ; Location point1=new Location ( 1 0, 1 0 ) ; System. out. p r i n t l n ( point1 Expect ( 1 0, 1 0 ) : + point1 ) ; Location point2 = new Location ( point1 ) ; System. out. p r i n t l n ( point2 Expect ( 1 0, 1 0 ) : + point2 ) ; System. out. p r i n t l n ( point1. e q u a l s ( point2 ) Expect true : + point1. e q u a l s ( point2 ) ) ; System. out. p r i n t l n ( point1. inbounds ( ) Expect true : + point1. inbounds ( ) ) ; (a) How many instance methods are in this code? (b) How many class methods are in this code? (c) Is Location a constructor or an instance method? How can you tell? (d) Is inbounds a constructor or an instance method? How can you tell? (e) When the code invokes Location(75,75), will it run the same code as when it invoke Location(point1)? Why? (f) What are the advantages of testing the Location class from a different class, instead of testing the code in the Location class itself? Page 3 of 6

7. (10 points) The following is the contents of a file in the current directory called AvgArgs.java : package t e s t 0 1 ; public class AvgArgs { public static double avgargs ( S t r i n g [ ] args ) { double sum=0; for ( S t r i n g arg : args ) { sum += arg. length ( ) ; return sum/ args. l e n g t h ; public static void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( Average argument l ength : + avgargs ( args ) ) ; If you type the command: javac -d. AvgArgs.java, you will get no errors. If you then type the command: java -cp. test01.avgargs This is a test What will get printed to the monitor? Page 4 of 6

8. (10 points) Given the following code: package t e s t 0 1 ; public class Account { private double balance ; private double i n t e r e s t R a t e ; private int year ; public Account ( double i n i t B a l a n c e, double i n i t R a t e ) { balance=i n i t B a l a n c e ; i n t e r e s t R a t e=i n i t R a t e ; public double getbalance ( ) { return balance ; public double g e t I n t e r e s t R a t e ( ) { return i n t e r e s t R a t e ; public void s e t I n t e r e s t R a t e ( double newrate ) { i n t e r e s t R a t e=newrate ; public int getyear ( ) { return year ; public void d e p o s i t ( double dep ) { balance += dep ; public double withdraw ( double r e q ) { i f ( req > balance ) { req=balance ; balance =req ; return req ; public double waityear ( ) { double i n t e r e s t = balance i n t e r e s t R a t e ; this. d e p o s i t ( i n t e r e s t ) ; year++; return i n t e r e s t ; public S t r i n g t o S t r i n g ( ) { return Year : + year + $ + balance ; public static void main ( S t r i n g [ ] args ) { Account t e s t = new Account ( 1 0 0 0. 0, 0. 0 5 ) ; System. out. p r i n t l n ( t e s t ) ; t e s t. d e p o s i t ( 8 5. 6 3 ) ; t e s t. waityear ( ) ; System. out. p r i n t l n ( t e s t ) ; t e s t. withdraw ( 1 2. 9 9 ) ; t e s t. d e p o s i t ( 1 0 0. 0 0 ) ; t e s t. waityear ( ) ; System. out. p r i n t l n ( t e s t ) ; t e s t. withdraw ( 1 2. 9 9 ) ; t e s t. d e p o s i t ( 2 5 9. 0 0 ) ; t e s t. waityear ( ) ; System. out. p r i n t l n ( t e s t ) ; (a) An invocation record is pushed onto the program stack every time a method invokes another method in Java. During execution of the main method on the above code, what is the maximum depth of the program stack, and which methods are on the program stack? (You may assume that library methods do not call any lower level methods) (b) Does the main method test all the other methods in the Account class? If not, which methods are not tested? (c) Is the Account class an immutable class? If so, why, or if not, why not? (d) Name all class methods in the Account class. Page 5 of 6

9. (20 points) Find as many bugs as you can in the following code. (There are between 5 and 15 bugs in the code.) A bug may cause a compile error, or may compile but produce incorrect results. If you leave out a keyword that is the default anyway, that is not a bug. For each bug, specify the line number, the incorrect code, and your best guess at what the correct value should be. If the bug is caused by missing code, enter the line number that should precede the missing code, followed by a plus sign (+), and enter the word missing in the Error column. 1 public class Buggy { 2 private int [ ] data ; 3 public Buggy (nums... ) { 4 for ( int i =0; i <nums. length ( ) ; i++) { 5 data [ i ] = nums [ i ] ; 6 7 public getdata ( ) { return data ; 8 public double average ( ) { 9 int sum ; 10 for ( i : data ) { sum+=data [ i ] ; 11 return sum/ data. l e n g t h ; 12 13 Bug Line Error Correction 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Page 6 of 6