Full file at

Similar documents
Console Input and Output

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

STUDENT LESSON A7 Simple I/O

Console Input and Output

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

Introduction to Java Applications; Input/Output and Operators

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

What two elements are usually present for calculating a total of a series of numbers?

A+ Computer Science -

More on variables and methods

Full file at

Formatting Output & Enumerated Types & Wrapper Classes

JAVA Ch. 4. Variables and Constants Lawrenceville Press

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

Object Oriented Programming. Java-Lecture 1

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

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

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

Full file at

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Chapter 3. Selections

Lecture 8 " INPUT " Instructor: Craig Duckett

Full file at

Midterm Examination (MTA)

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

Getting started with Java

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

COMP String and Console I/O. Yi Hong May 18, 2015

Ch. 6. User-Defined Methods

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

Course Outline. Introduction to java

Lecture Set 2: Starting Java

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

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

Lecture Set 2: Starting Java

Chapter 2: Basic Elements of Java

Reading Input from Text File

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

How the BigDecimal class helps Java get its arithmetic right

Introduction to Java Applications

COMP 202 Java in one week

Top-Down Program Development

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

b. Suppose you enter input from the console, when you run the program. What is the output?

Full file at

Topics. The Development Process

java_advanced_io_demos_index 08/14/18 07:14:41 walton 1 of 1

Formatted Output / User IO

Oct Decision Structures cont d

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

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

Chapter 5 Lab Methods

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

COMP6700/2140 Std. Lib., I/O

Building Java Programs

Task #1 The if Statement, Comparing Strings, and Flags

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

Introduction to Computer Science Unit 2. Notes

Nested Loops. A loop can be nested inside another loop.

Chapter 02: Using Data

A+ Computer Science -

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

CEN 414 Java Programming

2.5 Another Application: Adding Integers

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

AP Computer Science Unit 1. Writing Programs Using BlueJ

Section 2: Introduction to Java. Historical note

5) (4 points) What is the value of the boolean variable equals after the following statement?

Classes and Objects Part 1

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

Java Coding 3. Over & over again!

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Full file at

COMP 110 Project 1 Programming Project Warm-Up Exercise

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Algorithms and Conditionals

Text User Interfaces. Keyboard IO plus

Claremont McKenna College Computer Science

Conditional Programming

CS 101 Fall 2006 Midterm 3 Name: ID:

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

AP CS Unit 3: Control Structures Notes

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

St. Edmund Preparatory High School Brooklyn, NY

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

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

Introduction to Computer Science Unit 2. Notes

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

2.8. Decision Making: Equality and Relational Operators

Transcription:

Chapter 2 Console Input and Output Multiple Choice 1) Valid arguments to the System.out object s println method include: (a) Anything with double quotes (b) String variables (c) Variables of type int (d) All of the above D 2) Which statement or group of statements produces the output: Java programming is fun! (a) System.out.print(Java programming); System.out.print(is fun!); (b) System.out.println(Java programming is fun!); (c) System.out.println( Java programming ); System.out.println( is fun! ); (d) System.out.print( Java programming ) System.out.println( is fun! ); D 3) If a hyphen is added after the % in a format specifier, the output will be. (a) Left justified (b) Right justified (c) Centered (d) None of the above A 4) The statement: System.out.printf("%6.2f", 597.7231); displays: (a) 597.723 (b) 597.72 (c) 000597.72 (d) None of the above B 1

2 Walter Savitch Absolute Java 4/e: Chapter 2, Test Bank 5) The Java method printf is based on the language. (a) Pascal (b) C++ (c) C (d) ADA C 6) The class NumberFormat allows you to specify a constant representing which country s currency format should be used. To use this constant you must import: (a) java.util.locale (b) java.util.currency (c) java.util.properties (d) None of the above. A 7) Standard code libraries in Java are called: (a) Methods (b) Classes (c) Packages (d) Statements 8) What does the following code output? C DecimalFormat percent = new DecimalFormat("0.00%"); System.out.println(percent.format(0.308)); (a) 3.080% (b) 30.80% (c).0308% (d) 308.0% B

Chapter 2 Console Input and Output 3 9) What does the following code output? DecimalFormat dfquestion = new DecimalFormat("#0.##E0"); System.out.println(dfQuestion.format(12.7896987)); (a) 12.79E0 (b) 12.8E0 (c) 1.28E1 (d).13e2 A 10) What Java package includes the class Scanner? (a) awt (b) swing (c) io (d) util D True/ 1) Efficiency is lost in importing the entire package instead of importing the classes you use. 2) Every Java program automatically imports the java.util package. 3) The new line character is represented as \n. True 4) The method printf is used the same way as the method println but has the added feature that allows you to add formatting instructions. 5) The printf method can be used to output multiple formatted values. True 6) The Scanner class has a method next that allows an entire line of string text to be read. 7) Echoing input is good programming practice because it can reveal problems in the input. True

4 Walter Savitch Absolute Java 4/e: Chapter 2, Test Bank Short Answer/Essay 1) Write a Java statement to display your name in the console window. System.out.println("Wally Wonders"); 2) Write ONE Java statement to display your first and last name on two separate lines. System.out.print("Wally\nWonders"); 3) Write Java statements to apply currency formatting to the number 100. Indicate the package you need to import. import java.text.numberformat; NumberFormat nfmoney = NumberFormat.getCurrencyInstance(); System.out.println(nfMoney.format(100)); 4) Write a Java program to create and display 57.32% using the DecimalFormat class. Include the necessary import statement to use the DecimalFormat class. import java.text.decimalformat; public class decimalclass public static void main(string[] args) DecimalFormat df = new DecimalFormat("00.00%"); System.out.println(df.format(.5732));

Chapter 2 Console Input and Output 5 5) Explain the significance of the pattern string used by the DecimalFormat object, and give an example of a valid pattern. The pattern string represents the format in which the number passed to the DecimalFormat object is formatted. The pattern can either specify the exact number of digits before and after the decimal, or it can specify the minimum numbers of digits. The character 0 is used to represent a required digit and the character # is used to represent optional digits. Valid patterns include: 0.00, #0.0##. 6) What does it mean to prompt the user? Prompting the user means to display a meaningful message to the user asking for some type of input. An example of prompting the user would be displaying a JOptionPane to ask the user to input their name. 7) Why is echoing user input a good programming practice? Echoing input is a technique that is commonly used to allow the user to check their input for accuracy before it is actually sent to the program for processing. This technique reduces the chances of errors in the program. 8) If there is no loss of efficiency in importing an entire Java package instead of importing only classes you use into your program, why would you not just import the entire package? Importing only the classes you need into your program makes your program easier to read as well as aiding in documenting the program. Program readability is very important since humans read computer programs, too. 9) Write a complete Java console application that prompts the user for two numbers, multiplies the numbers, and then displays the result to the user. import java.util.scanner; public class ConsoleMultiply public static void main(string[] args) //Create the scanner object for console input Scanner keyboard = new Scanner(System.in); //Prompt the user for the first number

6 Walter Savitch Absolute Java 4/e: Chapter 2, Test Bank System.out.print("Enter the first integer: "); //Read the input int firstnumber = keyboard.nextint(); //Prompt the user for the second number System.out.print("Enter the second integer: "); //Read the second number int secondnumber = keyboard.nextint(); System.out.println(firstNumber + "*" + secondnumber + " is " + firstnumber * secondnumber); 10) What do the format specifiers d, f, e, g, s and c represent? The format specifiers d, f, e, and g are all used for numeric representation. Specifier d represents a decimal integer, specifier f represents a fixed-point floating-point number, specifier e represents E-notation floating-point, and specifier g represents general floating-point in which Java secedes whether to use E-notation. The format specifiers s and c are used for string and character representation, respectively. 11) Write a Java statement to create and initialize a Scanner object named input. Scanner input = new Scanner(System.in); 12) What is whitespace and why is it import when reading input from the keyboard using the Scanner class? Whitespace is any string of characters, such as blank spaces, tabs, and line breaks, that prints as whitespace when written on (white) paper. Whitespace servers as delimiters for many of the Scanner class methods.