NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Similar documents
1. An operation in which an overall value is computed incrementally, often using a loop.

Building Java Programs A Back to Basics Approach 4th Edition Reges TEST BANK Full download at:

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

CSE 142 Sample Midterm Exam #2

Building Java Programs

Building Java Programs

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

Type boolean. Building Java Programs. Recap: Type boolean. "Short-circuit" evaluation. De Morgan's Law. Boolean practice questions.

assertion: A statement that is either true or false.

Building Java Programs

Recitation: Loop Jul 7, 2008

CSE142 Sample Midterm, Winter 2018

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sample Midterm Exam #2

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

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

Computational Expression

The keyword list thus far: The Random class. Generating "Random" Numbers. Topic 16

Midterm Review Session

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Topic 16. battle -they are strictly limited in number, they require fresh horses, and must only be made at decisive moments." -Alfred North Whitehead

CS 112 Introduction to Programming

CSE 142 Sample Midterm Exam #3

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

AP Computer Science Unit 1. Programs

In this chapter, you will:

Some Sample AP Computer Science A Questions - Solutions

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

Chapter 2: Basic Elements of Java

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

Control Flow Practice Problems Mr. Fahrenbacher AP Computer Science A

Building Java Programs

CSE 142, Summer 2010 Midterm Exam, Friday, July 30, 2010

Repetition, Looping. While Loop

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Full file at

CSC 1214: Object-Oriented Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CSE 142 Sample Midterm Exam #3

CSC1590 Java Assignment 2-0. User Defined Classes

Input from Files. Buffered Reader

CSE142 Sample Midterm Spring Name UW NetId (e.g. whitab) Section (e.g., AA) TA

CIS 110: Introduction to Computer Programming

Building Java Programs

Prof. Navrati Saxena TA: Rochak Sachan

F I N A L E X A M I N A T I O N

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

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

Advanced if/else & Cumulative Sum

CIS October 16, 2018

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Object-Oriented Programming

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Getting started with Java

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Building Java Programs

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

Building Java Programs

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Object Oriented Programming. Java-Lecture 1

JAVA OPERATORS GENERAL

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Reading Input from Text File

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Classes Basic Overview

CSE 142 Sample Final Exam #2 (based on Autumn 2007's final)

COMP-202 Unit 4: Programming with Iterations

Building Java Programs Chapter 5

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

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

Java Basics Programming Fundamentals

Chapter 4: Control Structures I

CS Week 11. Jim Williams, PhD

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.

Chapter 4: Conditionals and Recursion

Programming with Java

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Conditional Execution

CSE 143. Lecture 9: introduction to recursion reading: 12.1

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CSE 143 Lecture 10. Recursion

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

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

Building Java Programs

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Array basics. Readings: 7.1

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Selenium Class 9 - Java Operators

Mr. Monroe s Guide to Mastering Java Syntax

Building Java Programs

COMP-202 Unit 9: Exceptions

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Transcription:

CSC116 Practice Exam 2 - KEY Part I: Vocabulary (10 points) Write the terms defined by the statements below. Cumulative Algorithm 1. An operation in which an overall value is computed incrementally, often using a loop. Relational 2. The < (less than) symbol is an example of what type of operator. Precondition 3. A condition that must be true before a method executes to guarantee that it can perform its task. Roundoff Error 4. A numerical error that occurs because floating-point numbers are stored as approximations rather than as exact values. NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file. True or False true When a Scanner object is passed as a parameter to a method, the input cursor does not reset to the beginning of the File. true When creating a Scanner object to read a file, if the specified input file does not exist, a FileNotFoundException will occur. false true The %8d format specifier results in a right-aligned 8-space wide real number field. (integer field) White box testing cannot test code located in the main() method. False (if/else if) An if/else statement construct is used to choose 0 or 1 of two paths.

Part II: Miscellaneous (4 points) 1. Factor the following code to make it more efficient: if (num < 100 ) { System.out.println( The number is: + num); num *= 2; System.out.println( The new number is: + num); else { System.out.println( The number is: + num); num /= 2; System.out.println( The new number is: + num); System.out.println( The number is: + num); if (num < 100 ) { num *= 2; else { num /= 2; System.out.println( The new number is: + num); 2. Fix the following code: if ( 2 < y <= 8 ) { return true; if ( y > 2 && y <= 8 ) { return true; 3. What type of Java control structure is represented by the following control flow template? do/while Loop

Part III: Assertions (6 points) Identify the various assertions in the following code as being always true, never true, or sometimes true at different points in program execution. The "Point X" comments in the code indicate the points of interest. public static int mystery(scanner console) { int y = console.nextint(); int count = 0; int x = 0; // Point A while (y!= 0) { // Point B if (y == x) { // Point C count++; x = y; y = console.nextint(); // Point D // Point E return count; y == 0 x == 0 x == y Point A SOMETIMES ALWAYS SOMETIMES Point B NEVER SOMETIMES SOMETIMES Point C NEVER NEVER ALWAYS Point D SOMETIMES NEVER SOMETIMES Point E ALWAYS SOMETIMES SOMETIMES

Part IV: Boolean Expressions (10 points) Given the following variable declarations: int x = -5; int y = 10; int z = 20; boolean p = x < y; boolean q = z < x; What is the value of each of the following Boolean expressions? 1. (x <= y) && q true && false = false 2. (y / z > 0) p false true = true 3.!(z % y!= 0)!p &&!(x == z p!= q)!false!true &&!(false true) true false && false true false = true 4. z > y y / (x + 5) == 0 true (java will not evaluate short circuiting so no divide by 0 exception) = true Use De Morgan's laws and negation logic to simplify this Boolean expression. Your final expression should NOT have an! preceding any expression in parentheses.!(b > 0 c!= a && a <= 5)!(b>0) &&!(c!=a && a <=5) b<=0 && (!(c!=a)!(a <=5)) b<=0 && (c==a a>5)

Part V: Loops (15 points) Give the output of each code segment below: Segment 1: Write output here: int count = 0; 10 0 int num = 12; 8 1 do { 6 2 num = num - 2; System.out.println(num + " " + count++); while (count < num / 2); Segment 2: Write output here: int x = 1; 2 2 int y = 1; 4 3 while (x < 12) { 7 4 x = x + y; 11 5 y++; 16 6 System.out.println(x + " " + y);

Consider the following method: public static int mystery(int n) { if (n < 0) { return -1; while ( n > 0) { if (n % 2 == 0) { n = n / 2; else if (n == 1) { return 1; else { n = n * 3 + 1; return 0; For each call below, indicate what value is returned. Method Call Return Value mystery(-1); -1 mystery(0); 0 mystery(1); 1 mystery(2); 1 mystery(10); 1

Part VI: File I/O (20 points) Complete the FileCapitalize class below. In the main() method, create a Scanner object to read from a file named input.txt and create a PrintStream object to write to a file named capitalized.txt. Use try/catch and output an error message if you catch a FileNotFoundException. Call the capitalize() method which will read each line of the input file and write it to the output file in all capital letters. import java.util.*; import java.io.*; public class FileCapitalize { public static void main(string[] args) { try { Scanner infile = new Scanner( new File ("input.txt")); File file = new File ("capitalized.txt"); PrintStream outfile = new PrintStream (file); //can combine this and last line, like infile capitalize(infile, outfile); catch (FileNotFoundException e) { System.out.println("Error: "+ e); //Can use a different error message //read each line in the input file and write it to the output file in all capital letters public static void capitalize (Scanner input, PrintStream output) { while(input.hasnextline()) { String line = input.nextline(); output.println(line.touppercase());

Part VII: Conditional Logic (20 points) 1. Write a method called getstatuslightcolor that returns "red" if the fuel amount of a vehicle is less than 10% of its fuel capacity, "green" otherwise. The fuel amount and fuel capacity should be passed to the method as parameters of type double. public static String getstatuslightcolor(double fuelamount, double fuelcapacity) { String result; if (fuelamount <.10 * fuelcapacity) { result = red ; else { result = green ; return result; 2. A company charges $10 per item to ship an item within the continental U.S., $15 for each item shipped to AK (Alaska) or HI (Hawaii), and $20 for each item shipped out of the country. Complete the method below, which returns the total shipping charge based on the country (and state, if within the U.S.). For example, the method call getshippingcharge(4, USA, NC ) would return 40, getshippingcharge(3, USA, HI ) would return 45, getshippingcharge(5, UK, ) would return 100. It s OK to use magic numbers. public static int getshippingcharge (int numberofitems, String country, String state) { int charge; if (country.equals( USA )) { if (state.equals( AK ) state.equals( HI )) { charge = numberofitems * 15; else { charge = numberofitems * 10; else { charge = numberofitems * 20; return charge;

Part VIII: Random Numbers (15 points) Complete the following program that simulates the flipping of a coin. The coin will be flipped 5 times, after each flip output either Heads or Tails. Generate a random integer, a 1 or a 2, with 1 indicating heads and 2 indicating tails. Do not use magic numbers. import java.util.*; public class CoinFlip { public static final int TAILS = 2; public static final int NUM_FLIPS = 5; public static void main(string[] args) { Random rand = new Random() ; int count = 1 ; while ( count <= NUM_FLIPS ) { // simulate coin flip int flip = _rand.nextint(tails) + 1 ; System.out.print("Flip number " + _count + ": "); if (flip == TAILS ) { System.out.println( _"Tails" ) ; else { System.out.println( "Heads" ) ; count++ ;