Chapter 2 PDF Flashcards
What is the typical program structure
Input some data
Perform some processing on the data
Output the results
Describe what could happen in each execution of the program
The data might be different
Instructions will be the same
What do all Java Application programs consist of
All programs consist of at least one class
What are identifiers used to name
Classes, variables and methods
What are some identifier rules
Must start with a Java Letter
Can contain essentially any number of java letters and digits but no spaces
Case sensitive
Cannot be keywords or reserved words
Is this identifier valid: taxRate
yes
Is this identifier valid: char
No. Char is a keyword
Is this identifier valid: intNumber
Yes, the keyword int is embedded but the identifier is not identical to a keyword
Is this identifier valid: 2008Taxrate
No. The identifier starts with a digit
Describe the statement in a program building block
Performs some action
Terminates with a semicolon;
Can span multiple lines
Describe the block in the program building block
0,1, or more statements
begins and ends with curly braces {}
Can be used anywhere a statement is allowed
What are white space characters. Describe them
- Space, tab, and newline are white space characters.
- At least one white space character is required between a keyword and identifier.
- Any number of white space characters are permitted between identifiers, keywords, operators, and literals.
What are comments used for
Comments explain the program to yourself and others.
How do you comment a line
Begin with /* and end with */
What is a requirement for all data
That you assign a name and a data type
What is the purpose of a data type
How much memory to allocate
How to store the data
The types of operations you will perform on the data
What are the eight primitive data types
byte short int long float double char boolean
How many values can a variable hold
Variables hold one value at a time, but the value can change as the program executes
Describe the naming convention for variable names
First letter is lowercase
Embedded words begin with an uppercase letter
What are boolean values used for
Boolean values are used for decision making or as flag variables
Describe what putting an assignment operator does to variables
Value on the right of the operator is assigned to variable on the left
What can the variable on the right be
A literal, another variable, or an expression
What is a literal
Text representing a specific value
What is an expression
A valid combination of variables, operators and constants
Describe the literal int, short, byte
Optional initial sign (+ or -) followed by digits 0-9 in any combination
Describe the literal long
Optional initial sign (+ or -) followed by digits 0-9 in any combination, terminated with an L
What are interger literals that begin with 0 considered
They’re considered to be octal values
What are integer literals that begin with 0x considered to be
They’re considered to be hexadecimal values
Describe the float in floating point literals
Optional initial sign (+, or -) followed by a floating-point number in fixed or scientific format, terminated by a F
Describe the double in floating point literals
Optional initial sign (+ or -) followed by a floating point number in fixed or scientific format
what is a common error with integer or floating point literals
Commas, dollar signs, and percent signs (%) cannot be used in integer or floating point literals
What is a char
Any printable character enclosed in single quotes
What is rule 1 of assigning the values of other variables
Variable 1 needs to defined before this statement appears in the source code
What in rule 2 of assigning values of other variables
Variable 1 and variable 2 need to be compatible data types; in other words, the precision of variable 1 must be lower than or equal to that of variable2
What is a common error trap with variables
You need to declare a variable only once. After a variable is declared, its data type cannot be changed.
After a variable is declared, its data type cannot be changed.
What is a string
String is a class not a basic data type.
What is a string variable
String variables are objects
What do String Concatenation Operator do
Combines String literals with other data types for printing
What is the common error trap with string literals
String literals must start and end on the same line
What is important to do with long strings to avoid common errors
Break long strings into shorter strings and use the concatenation operator
How do you include a special character in a string
You use an escape sequence
In what situation would you use a constant
Sometimes you know the value of a data item, and you know that its value will not change during program execution. Then you would define it as a constant.
Whats the convention for naming a constant
use all capital letters for constants and separate words with an underscore
What is an expression
Operators and operands that evaluate to a single value,
What happens to the single value (as a result of an expression)
The value is then assigned to a target
What is the requirement for a target (in relation to a single value as a result of an expression)
The target must be a variable or a constant and the value must be compatible with target’s data type
When is operator precedence used
If more than one operator is used in an expression, the order of evaluation is determined using an operator precedence.
What is the order used in operator precedence
Expressions in parentheses are evaluated first
Multiplication, division, and modulus are performed before addition and subtraction
How would you evaluate an expression that has multiple arithmetic operators of the same precedence
They are evaluated left to right. An assignment is performed last and is performed right to left.
What happens when two integers are divided
The quotient is an integer
Any fractional part is truncated (discarded).
How do you get a remainder when you divide two intergers
Use the modulus operator with the same operands
Whats the result in floating point division( by 0) if the dividends not 0
The result is infinity
What is the result in floating point division( by 0) if the dividend and divisor are both 0
Nan (not a number)
What occurs when you perform calculations with operands of different data types
Lower precision operands are promoted to higher precision data types, then the operation is performed..
What is special about implicit type casting
Promotion is effective only for expression evaluation; not a permanent change