3. Types, variables and expressions Flashcards

1
Q

What principles do we apply to keep out source lines short and readable?

A

We keep our source code lines shorter than 80 characters, and use indentation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are two reasons for hard coding data?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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?

A

addition (+)
subtraction (-)
multiplication (*)
division (/)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the value of -15 / 4 (in Java!)?

A

-3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the value of 9 * 4 / 2 - 9 / 2 * 4 (in Java!)?

A

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the value and type of 9.0 / 2 (in Java!)?

A

4.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is the value of 10 - 6 / 2 not 2?

A

Because the / operator has higher presence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

Left associativity. This expression has a value of 2 in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Double.parseDouble() do?

A

It turns a text data string, into the real (fractional decimal number) it represents.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does Integer.parseInt() do?

A

It turns a text data string, into the integer (whole number) it represents.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What property must some value have in order for it to be assignable to a particular variable?

A

It must have the same type as the variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a literal value? Give an example of an integer literal.

A

Literal value is a constant, that we can add to a variable.

E.g noOfPeopleLivingInMyStreet = 47;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Apart from bits of syntax, what two main parts does an assignment statement consist of?

A

The variable, and the value of an expression being assigned to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Name three kinds of data.

A

Numbers, text data, images.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between 51 and “51”?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the value of 1234.0 + 0.5 + “”? What about “” + 1234.0 + 0.5?

A

The first one is treated as (1234.0 + 0.5) + “” aka its output is 1234.5. The second output, however, is 1234.00.5.

17
Q

What is an escape sequence?

A

It’s the \ symbol, that has a special meaning to the Java compiler.

18
Q

How do we include a double quote in a string literal?

A

"

19
Q

Give three examples of an int value?

A

5
300
31542

20
Q

Why is this double type called double?

A

It’s called double because it uses a means of storing the numbers called double precision.

21
Q

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?

A

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.

22
Q

What three things does a variable have?

A

A name.
A value.
A type.

23
Q

When we declare a variable, what two things do we write, and in what order do we write them?

A

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.

24
Q

Write a single line of Java that declares a variable, x, which is a double, and gives it the value 123.4

A

double x = 123.4;

25
Q

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?

A

The value will be 5.0. If x was an int, the value would have been 5.