Chapter 2 Data and Expressions Flashcards
What is a string literal?
A string literal is a sequence of characters delimited by double quotes.
What is the difference between the print and println methods?
Both the print and println methods of the System.out object write a string of characters to the monitor screen. The difference is that, after printing the characters, the println performs a carriage return so that whatever’s printed next appears on the next line. The print method allows subsequent output to appear on the same line.
What is a parameter?
A parameter is data that is passed into a method when it is invoked. The method usually uses that data to accomplish the service that it provides. For example, the parameter to the println method indicates what characters should be printed.
What output is produced by the following code fragment?
System.out.println(“One “);
System.out.print(“Two “);
System.out.println(“Three “);
The output produced by the code fragment is
One
Two Three
What output is produced by the following code fragment?
System.out.print("Ready "); System.out.println(); System.out.println("Set "); System.out.println(); System.out.println("Go ");
The output produced by the code fragment is
Ready
Set
Go
What output is produced by the following statement? What is produced if the inner parentheses are removed?
System.out.println(“It is good to be “ + (5 + 5));
The output produced by the statement is
It is good to be 10
The + operator in the sub-expression (5 + 5) represents integer addition, since both of its operands are integers. If the inner parentheses are removed, the + operators represent string concatenation and the
output produced is
It is good to be 55
What is an escape sequence? Give some examples.
An escape sequence is a series of characters that begins with the backslash () and that implies that the following characters should be treated in some special way. Examples: \n represents the newline character, \t
represents the tab character, and " represents the quotation character (as opposed to using it to terminate a string).
Write a single println statement that will output the following exactly as shown (including line breaks and quotation marks).
“I made this letter longer than usual because I lack the
time to make it short.”
Blaise Pascal
System.out.println(“"I made this letter longer than “
+ “usual because I lack the time to\nmake it short."”
+ “\n\tBlaise Pascal”);
What is a variable declaration?
A variable declaration establishes the name of a variable and the type of data that it can contain. A declaration may also have an optional initialization, which gives the variable an initial value.
Given the following variable declarations, answer each question.
int count = 0, value, total;
final int MAX_VALUE = 100;
int myValue = 50;
a. How many variables are declared?
b. What is the type of these declared variables?
c. Which of the variables are given an initial value?
d. Based on the above declarations, is the following
assignment statement legal? Explain.
myValue = 100;
e. Based on the above declarations, is the following
assignment statement legal? Explain.
MAX_VALUE = 50;
Given those variable declarations, the answers are:
a. Five variables are declared: count, value, total,
MAX_VALUE, and myValue.
b. They are all of type int.
c. count, MAX_VALUE, and myValue are each given an
initial value.
d. Yes, it is legal. myValue is a variable of type int and 100
is an int literal.
e. No, it is not legal. MAX_VALUE is declared as a final
variable and therefore it cannot be assigned a value
other than its initial value.
Your program needs a variable of type int to hold the number of CDs in a music collection. The initial value should be zero. Write a declaration statement for the variable
The variable name you choose should reflect the purpose of the variable. For example:
int numCDs = 0;
Your program needs a variable of type int to hold the number of feet in a mile (5,280). Write a declaration statement for the variable.
The variable name you choose should reflect the purpose of the variable. Since the number of feet in a mile will not change, it is a good idea to declare a constant. For example:
final int FT_PER_MILE = 5280;
Briefly describe three reasons for using a constant in a program instead of a literal value.
First, by carefully choosing the name of the constant, you can make your program more understandable than if you just use the literal value. Second, using a constant ensures that the literal value represented by the variable will not be inadvertently changed somewhere in the program. Third, if you ever do have to rewrite the program using a different literal
value, you will only need to change that value once, as the initial value of the constant, rather than many places throughout the program.
What is primitive data? How are primitive data types different from objects?
Primitive data are basic values such as numbers or characters. Objects are more complex entities that usually contain primitive data that help define them
How many values can be stored in an integer variable?
An integer variable can store only one value at a time. When a new value is assigned to it, the old one is overwritten and lost
What are the four integer data types in Java? How are they different?
The four integer data types in Java are byte, short, int, and long. They differ in how much memory space is allocated for each and therefore how large a number they can hold.
What type does Java automatically assign to an integer literal? How can you indicate that an integer literal should be considered a different type?
Java automatically assigns an integer literal the data type int. If you append an L or an l on the end of an integer literal, for example 1234L, Java will assign it the type long
What type does Java automatically assign to a floating point literal?
How can you indicate that a floating point literal should be considered a different type?
Java automatically assigns a floating point literal the data type double. If you append an F or an f on the end of a floating point literal, for example 12.34f, Java will assign it the type float
What is a character set?
A character set is a list of characters in a particular order. A character set defines the valid characters that a particular type of computer or programming language will support. Java uses the Unicode character set.