Computer Science 145 Midterm 1 Fall 2016

Similar documents
Computer Science 145

Programming with Java

Computational Expression

Using Java Classes Fall 2018 Margaret Reid-Miller

Full file at

Peer Instruction 1. Elementary Programming

Primitive Data Types: Intro

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Classes and Objects Part 1

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Java Classes: Math, Integer A C S L E C T U R E 8

Full file at

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

CS 302: Introduction to Programming

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Computational Expression

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

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

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

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

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

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

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

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

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

ECE 122 Engineering Problem Solving with Java

Fundamentals of Programming Data Types & Methods

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

CS111: PROGRAMMING LANGUAGE II

AP Computer Science A

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

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

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

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

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

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

Chapter 2: Data and Expressions

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

CEN 414 Java Programming

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

Introduction to Programming Using Java (98-388)

Input. Scanner keyboard = new Scanner(System.in); String name;

I. Variables and Data Type week 3

Chapter 4 Classes in the Java Class Libraries

Data Conversion & Scanner Class

Datatypes, Variables, and Operations

Activity 3: Data Types

Lecture Set 2: Starting Java

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

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

Lecture Set 2: Starting Java

ing execution. That way, new results can be computed each time the Class The Scanner

CS 101 Fall 2006 Midterm 1 Name: ID:

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

A+ Computer Science -

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

Methods CSC 121 Fall 2016 Howard Rosenthal

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal

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

Fundamental Java Syntax Summary Sheet for CISC101, Spring Java is Case - Sensitive!

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

CS110: PROGRAMMING LANGUAGE I

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

Chapter 3: Using Classes and Objects

Chapter 2: Using Data

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

COMP 202 Java in one week

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Section 2.2 Your First Program in Java: Printing a Line of Text

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

Basic Computation. Chapter 2

CS 101 Spring 2007 Midterm 2 Name: ID:

Chapter 2 Elementary Programming

Programming with Java

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

Chapter 02: Using Data

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Section 2: Introduction to Java. Historical note

Chapter 2 ELEMENTARY PROGRAMMING

Methods CSC 121 Spring 2017 Howard Rosenthal

Mr. Monroe s Guide to Mastering Java Syntax

Chapter 2. Elementary Programming

More on variables and methods

Introduction to Java Unit 1. Using BlueJ to Write Programs

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 2: Data and Expressions

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

Midterms Save the Dates!

What did we talk about last time? Examples switch statements

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

Important Java terminology

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

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

CS 106 Introduction to Computer Science I

double float char In a method: final typename variablename = expression ;

Question: Total Points: Score:

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Basic Computation. Chapter 2

CS111: PROGRAMMING LANGUAGE II

Transcription:

Computer Science 145 Midterm 1 Fall 2016 Doodle here. This is a closed-book, no-calculator, no-electronic-devices, individual-effort exam. You may reference one page of handwritten notes. All answers should be clearly written. Questions that require code should be written using correct Java syntax. You may write SOP to represent System.out.println. Class Method/Constructor Description Scanner Scanner(System.in) create Scanner for parsing System.in Scanner(String text) create Scanner for parsing text String next() get next delimited word double nextdouble() get next delimited double boolean nextboolean() get next delimited boolean int nextint() get next delimited integer String int length() get number of characters char charat(int i) get the character at index i String touppercase() get a String like this one, but in all-caps int indexof(char c) find the index of the first c String substring(int a) get substring from index a to String s end String substring(int a, int b) get substring from index a to before index b Math int max(int a, int b) get the maximum of a and b. int min(int a, int b) get the minimum of a and b. double pow(double base, double exponent) raise base to the exponent power. Random Random() create a random number generator. nextint(int i) get random number in [0,i 1]. nextdouble() get random number in [0.0, 1.0). 1

1. Identify four primitive types available in the Java programming language. 2. What is the type of the following expression? Be exact. aleph 3. What is the type of the following expression? Be exact. 1.0 + 1 4. What is the type of the following expression? Be exact. 10 / 3 5. Which of the following code sequences use unnecessary casts? Check zero or more. (a) int a = 5; long b = (int) a; (b) float a = 1.3; int b = (int) a; (c) short a = 127; byte b = (byte) a; (d) int a = 2017; int b = (int) a; 6. You see the name RandomAccessFile in code that follows Java coding conventions. What is RandomAccessFile? Check zero or more. (a) avariable (b) a constant (c) a String literal (d) a class 7. You see the name ntrees in code that follows Java coding conventions. What is ntrees? (a) a constant (b) a String literal (c) a class (d) avariable 8. Which of the following statements about formal parameters are true? Check zero or more. (a) Formal parameters are required for all methods. (b) Formal parameters are a primary means for making methods reusable for varying data. (c) Formal parameters must have the same names as actual parameters. (d) Formal parameters look like variable declarations. 2

9. Which of the following are reasons we write methods? Check zero or more. (a) To make chunks of code reusable. (b) To keep shared data in a central location. (c) To give meaningful names to complex sequences of statements. (d) To create units of computation that can be tested independent of the larger program. 10. Which of the following are true of actual parameters? Check zero or more. (a) Actual parameters are separated from one another by commas. (b) Actual parameters are declarations in the method s definition. (c) Actual parameters must conform to the types of the formal parameters. (d) Actual parameters are expressions in the code calling a method. 11. Fill in blanks a and b with the best answer given the surrounding code. public static a printaspect(int w, b ) { System.out.println(w / (double) h); } 12. Fill in blanks a, b, c, andd with the best answer given the surrounding code. public static a comment( b ) { c = z.indexof( ( ); d = z.lastindexof( ) ); return z.substring(l, r + 1); } 13. Put the following scrambled code back in order: a: double numerator = in.nextdouble(); b: } c: in.usedelimiter( / ); d: return numerator / denominator; e: public static double fractiontodecimal(string expression) { f: double denominator = in.nextdouble(); g: Scanner in = new Scanner(expression); The letters to the left are just labels, not actual Java code. For your answer, write the letter of the first line of code, then the letter of the second, and so on. 14. Write a main method that prompts and retrieves from the user two ints, and then prints out the smaller of the two followed by the larger of the two, all on one line of output. 15. Write a method named roll2d6 that simulates rolling two six-sided dice. It returns the sum of the two dice, which is in the interval [2, 12]. 16. Write a method named getfirstsentence. It accepts a String parameter and returns its first sentence, which you may assume exists, ends in a period, and contains no other periods (i.e., no Mr. Brown can moo. ). 3

Name: Computer Science 145 Midterm 1 Answer Sheet Fall 2016 1. (a) (b) (c) (d) 8. a b c d 9. a b c d 2. 10. a b c d 3. 11. (a) 4. (b) 5. a b c d 6. a b c d 12. (a) (b) (c) (d) 7. a b c d 13.

14. 15. 16.