Unit 2: Algorithms, Variables & Data Types
data types for integers
byte, short , int, long
Which one of the following is not true about Assignment Statements? Select one: a. The expression on the right is processed and the result is stored in the variable on the left. b. The variable's current value is overwritten by the newly assigned value. c. The assignment operator is a plus sign '+' d. The assigned value must match the variable's data type.
c. Correct. The actual assignment operator is a single equals sign '='.
Variable names start with lower case then use --- (example: numberOfStudents).
camelCase
...treat a value temporarily as another type.
casting
we use --- when we need to treat a value temporarily as another type.
casting
• average = (float) total/count; is an example of: 1. assignment conversion 2. promotion 3. casting
casting
• A -- variable stores a single character
char
What happens when you try to store something in a container that cannot hold it ? For example try storing 128 in a byte
compile time error - incompatible type error
What happens when you try to use a variable name that is a reserved word? For example try creating an integer called class
compile time error: <identifier> expected
an identifier that is similar to a variable except that it holds the same value during its entire existence
constant
declaration of the variable is the specification of -----
data type
an identifier in java cannot start with a ---
digit
Which one of the following is not a component of an algorithm? Select one: a. Instruction b. Selection c. Sequence d. Repetition e. Compile
e. compile
the difference between a constant and a variable is that constant ---
value remains unchanged during its existence
-- name for a location in memory
variable
containers for values, allowing them to be changed without recompilation of the source code
variables
Spaces, blank lines, and tabs are called
white space
--- conversions are safest because they tend to go from a small data type to a larger one (such as a short to an int)
widening
Only --- conversions can happen via assignment widening / narrowing
widening
double score; int points = 10; score = points; The code above is an example of what type of conversion? widening or narrowing
widening conversion
special identifiers that has special meaning in the language and that cant be used in naming variables for example
reserved words
• Character literals are delimited by ---
single quotes
in addition to the primitive char variable, which holds only one character, Java has a -- object, which can hold multiple characters
string
by convention we use ------ case for class names
title case
constant names should be all --- and seperated with an ---
upper case - underscore
• There are -- primitive data types in Java and they are:
8, byte short int long float double char boolean
In Java, data conversions can occur in three ways
1. assignment conversion 2. promotion 3. casting
Look at the expression below: int answer = 10 + 5 * 2 - (5 + 3); What would be the value stored in "answer"?
12
a byte can hold a max value of --
127
a double can store up to -- significant digits
15
size of float/double
32 / 64 bits
Look at the code below: int salary = 400; salary++; What is the value of the variable "salary"?
401
a float can store up to -- significant digits
7
size of byte/short/int/long
8 / 16 / 32 /64 bits
A variable can hold multiple values at any time. (T/F)
False
algorithm term is named after
Muhammad ibn Musa al-Khwarizmi of Khowarezm (now Khiva in Uzbekistan),
--- conversions can lose information because they tend to go from a large data type to a smaller one.
Narrowing
commonoly used literals are:
String literals: often used with println statements. Numbers
a variable type that holds either true or false
boolean
by convention we use ------- case for constants
Upper case
class names starts with
a capital letter
a process or set of rules to be followed in calculations or other problemsolving operations, esp. by a computer.
algorithms
...occurs when a value of one type is assigned to a variable of another.
assignment conversion
• float money; • int dollars = 3; • money = dollars; this is an example of 1. assignment conversion 2. promotion 3. casting
assignment conversion
When do we normally design algorithms? Select one: a. After we are done coding b. Before writing our code c. If our code does not work
b. Correct! The idea comes before the coding
In the variable declaration below: int total; What is "int" ? Select one: a. The variable's value. b. The variable's data type. c. It indicates that the variable's value is constant. d. The variable's name (identifier)
b. Precisely. This means that this variable can only hold values that are whole numbers.
To assign a value to a variable of type float, you need to add an -- to the value
f float salary = 1221.45f;
An algorithm must not contain English text or mathematical expressions (true/false)
false. Algorithms must be clear and easy to follow, which is why the use of English text and mathematical expressions can be quite helpful when done in a clear and precise manner.
In Java, we use the ---modifier to declare a constant
final
data types for floating point numbers
float, double
an identifier in java can be made up of --, -- , and the --- and ---- characters
letters, digits , underscore and dollar
exact values typed into the source code of a program
literals
...happens automatically when operators in expressions convert their operands.
promotion
result = sum / count; if sum is a float and count is an int, the value of count is converted to a floating point value to perform the calculation this is an example of 1. assignment conversion 2. promotion 3. casting
promotion
Operator precedence (order of evaluation):
• Operators in parentheses (innermost first) • multiplication, division, and remainder • addition, subtraction, and string concatenation • operators with the same precedence are evaluated from left to right • assignment operator