3. Types, variables and expressions Flashcards
What principles do we apply to keep out source lines short and readable?
We keep our source code lines shorter than 80 characters, and use indentation.
What are two reasons for hard coding data?
When we are developing a program, and we haven’t yet written the code that obtains the data from the appropriate place.
The other case would be if the data only rarely changes, then it’s fine to hard code it.
Name four binary infix operators and give their Java spellings. Two of these can also be used in another way - what do we call that sort of operator?
addition (+)
subtraction (-)
multiplication (*)
division (/)
What is the value of -15 / 4 (in Java!)?
-3
What is the value of 9 * 4 / 2 - 9 / 2 * 4 (in Java!)?
2
What is the value and type of 9.0 / 2 (in Java!)?
4.5
Why is the value of 10 - 6 / 2 not 2?
Because the / operator has higher presence.
In general, operators could have right or left associativity. Which would / need to have in order for 16 / 4 / 2 to equal 8? What value does that expression have in Java?
Left associativity. This expression has a value of 2 in Java.
What does Double.parseDouble() do?
It turns a text data string, into the real (fractional decimal number) it represents.
What does Integer.parseInt() do?
It turns a text data string, into the integer (whole number) it represents.
What property must some value have in order for it to be assignable to a particular variable?
It must have the same type as the variable.
What is a literal value? Give an example of an integer literal.
Literal value is a constant, that we can add to a variable.
E.g noOfPeopleLivingInMyStreet = 47;
Apart from bits of syntax, what two main parts does an assignment statement consist of?
The variable, and the value of an expression being assigned to it.
Name three kinds of data.
Numbers, text data, images.
What is the difference between 51 and “51”?
The integer literal 51 is an int, a number, whereas the string literal “51” is a text data string - a string of 2 separate characters.