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.
What is the value of 1234.0 + 0.5 + “”? What about “” + 1234.0 + 0.5?
The first one is treated as (1234.0 + 0.5) + “” aka its output is 1234.5. The second output, however, is 1234.00.5.
What is an escape sequence?
It’s the \ symbol, that has a special meaning to the Java compiler.
How do we include a double quote in a string literal?
"
Give three examples of an int value?
5
300
31542
Why is this double type called double?
It’s called double because it uses a means of storing the numbers called double precision.
Why does:
System.out.println(0.7 + “ is equal to “
+ (1 - 0.1 - 0.1 - 0.1));
print 0.7 is equal to 0.700000000000001?
Because even though the type double provides a really precise answer, it can not go on long enough, to eventually understand, that it’s 0.7.
What three things does a variable have?
A name.
A value.
A type.
When we declare a variable, what two things do we write, and in what order do we write them?
We must first declare the variable by stating the type and then the name of the variable.
Then we have to initialize is by giving it a value.
Write a single line of Java that declares a variable, x, which is a double, and gives it the value 123.4
double x = 123.4;
If the double variable x has the value 10.0, what is the value of x / 2? How would this be different if x was an int?
The value will be 5.0. If x was an int, the value would have been 5.