Java Chapter 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A group of statements, such as the contents of a class or a method, are enclosed in __________.

braces {}

A variable must be declared before it can be used.

true

The negation operator is __________.

unary

When Java converts a lower-ranked value to a higher-ranked type, it is called a(n) __________.

widening conversion

What is the difference between comments that start with the // characters and comments that start with the /* characters?

Comments that start with // are single line comments. Everything appearing after the // characters, to the end of the line, is considered a comment. Comments that start with /* are multi line comments. Everything between these characters and the next set of */ characters is considered a comment. The comment can span multiple lines

Is the variable name Sales the same as sales? Why or why not?

Not the same, Variable names are case sensitive.

What are the Unicode codes for the characters 'C', 'F', and 'W'? (You may need to refer to Appendix B on the book's companion

The code for 'C' is 67. The code for 'F' is 70. The code for 'W' is 87.

Is the following comment a single-line style comment or a multi-line style comment? /* This program was written by M. */

The comment is a multi-line comment. Multi-line comments start with / and end with /.

Is the following comment a single-line style comment or a multi-line style comment? //

The comment is a single-line comment, You simply place two forward slashes (//) where you want the comment to begin. The compiler ignores everything from that point to the end of the line.

What is wrong with the following statement? char letter = "Z";

You cannot assign a string literal to a char variable. The statement should be: char letter = 'Z';

Write statements that do the following: a) Declare a char variable named letter. b) Assign the letter A to the letter variable. c) Display the contents of the letter variable

a) char letter; b) letter = 'A'; c) System.out.println(letter);

This type of operator lets you manually convert a value, even if it means that a narrowing conversion will take place.

cast

Comments that begin with // can be processed by javadoc.

false

Variable names may begin with a number.

false

This key word is used to declare a named constant.

final

The following data 72 'A' "Hello World" 2.8712 are all examples of __________.

literals

Every Java application program must have __________.

main method

Which is a character literal, 'B' or "B"?

'B' is a character literal

What is the purpose of the following types of dialog boxes? Message dialog Input dialog

A message dialog box is used to display a message to the user. An input dialog box is used to gather keyboard input from the user.

A program declares a float variable named number, and the following statement causes an error. What can be done to fix the error? number = 7.4

Append the F suffix to the numeric literal, such as: number = 7.4F;

Which Scanner class method would you use to read a double as input?

nextDouble

Is the division statement in the following code an example of integer division or floating-point division? What value will be stored in portion? double portion; portion = 70 / 3

Integer division. The value 23.0 will be stored in portion.

You can use this class to display dialog boxes.

JOptionPane

Assume that a program uses the named constant PI to represent the value 3.14. The program uses the named constant in several statements. What is the advantage of using the named constant instead of the actual value 3.14 in each statement?

One reason that the name PI is more meaningful to a human reader than the number 3.14. Another reason is that any time the value that the constant represents needs to be changed, we merely have to change the constant's initialization value. We do not have to search through the program for each statement that uses the value.

Briefly describe what programming style means. Why should your programming style be consistent?

Programming style refers to the way a programmer uses spaces, indentations, blank lines, and punctuation characters to visually arrange a program's source code. Programming style should be consistent because an inconsistent programming style can create confusion for a person reading the code.

Write statements using combined assignment operators to perform the following: a) Add 6 to x b) Subtract 4 from amount c) Multiply y by 4 d) Divide total by 27 e) Store in x the remainder of x divided by 7

a) x += 6; b) amount -= 4; c) y *= 4; d) total /= 27; e) x %= 7;

If one of an operator's operands is a double, and the other operand is an int, Java will automatically convert the value of the double to an int.

false

Assume the file SalesAverage.java is a Java source file that contains documentation comments. Assuming you are in the same folder or directory as the source code file, what command would you enter at the operating system command prompt to generate the HTML documentation files?

javadoc SalesAverage.java

Which Scanner class method would you use to read a string as input?

nextLine

You cannot change the value of a variable whose declaration uses the final key word.

true

How would the number 6.31 × 1017 be represented in E notation?

6.31E17

Which of the following are not valid assignment statements? (Indicate all that apply.)

72 = amount; profit = 129

These characters mark the beginning of a multi-line comment.

/*

These characters mark the beginning of a documentation comment.

/**

These characters mark the beginning of a single-line comment.

//

Every complete statement ends with a __________.

semicolon ;;;;;

A left brace in a Java program is always followed by a right brace later in the program.

true

What values can boolean variables hold?

true and false

What things must be considered when deciding on a data type to use for a variable?

A data type is a set of values, which are having predefined features and every variable is assigned to a data type. Depends on the input type of value datatypes are used for a variable - For integers and natural numbers integer (int) type is used - If the values are decimal numbers or fractions, then double or float data type is used - If the value of a variable contains a single character, then char data type is used - If the value of a variable contains alphanumeric characters, then the string data type is used - If the result is (0 or 1) , or (true or false) then Boolean (bool) data type is used

Describe what the phrase "self-documenting program" means.

A program that is written in such a way that you get an understanding of what the program does just by reading it's code.

Why are variable names like x not recommended?

A variable is not a name for the data itself but for a location in memory that can hold data. Variables are actually rather subtle. It should always choose names for you variables that give an indication of what they are used for. "x", gives no clue as to what the variable's purpose is.

What does a variable declaration tell the Java compiler about a variable?

A variable tells the Java compiler the variable's name and the type of data it will hold. That the particular value will hold certain amount of memory to store values. A variable is the name of a location where the data is stored when a program executes.

Briefly describe the difference between variable assignment and variable initialization.

Assignment can be done as many times as desired where as initialization can be done only once. In both cases a value is storing in a variable. An assignment statement can appear anywhere in a program. An initialization, however, is part of a variable declaration.

What is meant by "case-sensitive"? Why is it important for a programmer to know that Java is a case-sensitive language?

Capitalization must be precise in class, method, and variable names. Means that it regards uppercase letters as being entirely different characters than their lowercase counterparts. This is important to know because some words in a Java program must be entirely in lowercase.

How are documentation comments different from other types of comments?

Documentation comments can be read and processed by a program named javadoc. The javadoc program can read Java source code files and generate attractively formatted HTML documentation files. If the source code files contain any documentation comments, the information in the comments becomes part of the HTML documentation.

An expression adds a byte variable and a short variable. Of what data type will the result be?

If an expression, adds a byte variable and a short variable then the data type result will be an int.

Briefly explain how the print and println methods are related to the System class and the out object

The print and println methods are members of the out object. The out object is a member of the System class. The System class is part of the Java API.

How do you write a single line comment? How do you write a multi-line comment? How do you write a documentation comment?

To write a single line comment you begin the comment with //. To write a multi-line comment you begin the comment with /* and end it with */. To write a documentation comment you begin the comment with /** and end it with */.

What import statement do you write in a program that uses the JOptionPane class?

a) JOptionPane.showMessageDialog(null, "Greetings Earthling."); b) str = JOptionPane.showInputDialog("Enter a number.");

Which of the following are not valid println statements? (Indicate all that apply.)

a. System.out.println + "Hello world"; c. out.System.println(value); d. println.out(programming is great fun);


Kaugnay na mga set ng pag-aaral

English Unit test Chapter Study Questions -Catcher in the Rye

View Set

Deitel How to Program Java 7th Edition

View Set

Simulation Lab 4.1: Module 04 Repair a Duplicate IP Address (Networking CSNT 231)

View Set

AP Biology : Biotechnology Review Questions

View Set

Chapter 40: Care of Patients with Hematologic Problems

View Set