Week 3 - Data Types and Variables Flashcards

1
Q

What are the two categories of data types in Java?

A

they are primitive data types and reference data types.

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

Give example values of primitive data types (4 in total), and explain their usage.

A

they include numbers( both integers and floats) , Boolean Values and characters like ‘?’, ‘G’ and ‘b’. The primitive types are built into the language and so are available to a programmer for use in constructing more complex data types.

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

What are reference data types?

A

these types are defined by classes and are used in relation to objects. (they are used to store an object reference).

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

What is an IDE?

A

an Integrated Development Environment.

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

How many integer types are there? Name them.

A

There are 4 integer types:
byte - 8bits of memory, smallest, -128 to 127
short - 16 bits, -32768 to 32767
int - 32 bits
long - 64 bits
The values of each of these types are positive and negative whole numbers and 0.
The differences between these types are in the range of values they can represent and the amount of memory each requires.

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

How does Java implicitly sign numeric types? What does this mean?

A

It means that each numeric type has a positive or negative value,.
Java uses the leftmost bit of the number expressed in binary to hold the ‘sign’ 0 indicates a positive and 1 indicates a negative.

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

What are the two floating type points that are used?

A

float: takes up 32 bits of memory
double: takes up 64 bits of memory. (greater accuracy than float.)

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

How many bits are required for a character type?

A

16 bits, this is for the one and only character primitive data type, char.

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

What is a primitive type variable?

A

they are variables declared to hold values of some primitive data type, they can also be referred to as primitive variables or a value type variable.

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

What is strong typing?

A

it is a feature of programming languages, it is a set of rules that make it impossible to accidentally use incompatible types, for example trying to store 1.7 in an int .

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

Write out 2 java statements, one which will declare a character variable called letter and then another that will assign the character ‘D’ to the variable.

A

char letter;

letter = ‘D’;

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

Describe the three steps that take place when the statement
kermit = new Frog ( );
is executed.

A

First, the keyword new causes a new instance of Frog to be created.
Second, the constructor Frog () causes the new instance of Frog to have its position instance variable initialised to 1 and its colour instance variable initialised to OUColour.GREEN.
Finally, the assignment operator = causes a reference to the new, initialised instance of Frog to be assigned to the reference variable kermit.
After these steps we can also more simply say that kermit references a Frog object.

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

Which of the following are valid variable identifiers in Java? Give a reason in each of the cases where you consider that the text is not an identifier.

a) kermit
b) hover frog
c) hoverfrog
d) my+Frog
e) myAcc
f) 3.2
g) “iAmAVariable”
h) MyNumber
i) $myMoney

A

a)
b) has a space
c)
d) has a + sign
e)
f) starts with an integer also not very informative
g) starts with “
h) ok but starts with a capital so could be confused with a class
i) ok but does not conform to M250 rules.

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

What is an expression?

A

An expression is a piece of code that can be evaluated to produce a single value.

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

When would integer division occur and what is it?

A

It would occur when one integer is divided by another, Java would drop the decimal and give an integer answer, in order to get an accurate answer you would need to make one of the arguments a double.

for example 7/2 would evaluate to 3
but 7.0/2 would evaluate to 3.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
int myNumber = 17;
int yourNumber = 5;
what is?
a) myNumber - 15
b) myNumber * yourNumber
c) myNumber / yourNumber
d) yourNumber > myNumber
e) yourNumber != myNumber
A

a) 2
b) 85
d) 3
e) False
d) True

17
Q

What is a message expression?

A

Message-sends that return a value (that is, they return a message answer of some sort) are called message expressions. You can think of them as evaluating to the returned answer.

18
Q

Some message-sends return answers, some change the state of the receiver and some do both. Based on the above discussion, give an example of a message-send for an Account object that would both return an answer and change the state of the receiver.

A

myAccount.debit(100) does both, assuming that myAccount is an instance of Account with a large enough balance. The message-send will cause the state of myAccount to change by reducing its attribute balance by 100 and the answer returned will be true.

19
Q

What is an escape sequence? how do these appear in Java?

A

Escape sequences are a common technique used in programming languages to tell the compiler that some text needs to be treated in a special way. Escape sequences begin with an escape character, which in Java is the backslash.

20
Q

String fred = “bill”;
What would be returned by:
a) “fred”.toUpperCase()
b) fred.toUpperCase()

A

a) “FRED” a string in that order

b) “BILL”

21
Q

Explain why ‘h’ and “h” are literal representation of different types.

A

The expression ‘h’ is a literal that represents the single char value h, and “h” is a literal representing a String object with just the single character ‘h’ in it. so ‘h’ represents a primitive data value, not an object, whereas “h” will evaluate to a reference to a String object containing the single character ‘h’.

22
Q

What is automatic type conversion?

A

When Java automatically changes the type of data so it can fit a specific variable.
More generally, if we arrange the most commonly used numerical types in the order
double, float, long, int
then it is permissible to assign a variable a value of its own type as well as a value of any type to the right.

23
Q

Explain the is-a relationship with regards to assigning a reference to some reference variable.

A
if a reference variable is of type T (i.e Frog), then an instance of a subclass of T (i.e HoverFrog) can be assigned to the variable.
To remember this remind yourself that we could say that a hoverfrog 'is a' frog.
24
Q

Frog kermit = new HoverFrog () ;

what is the object and of what type if the variable.

A

The HoverFrog object doesn’t stop being a HoverFrog

the variable kermit is still of the type Frog.

25
Q

Suppose that we have a class of type X and a reference variable of type Y. Under what circumstances can you assign a reference to an object of class X to the variable Y?

A

This assignment is possible when X is of the same class as Y was declared to be, or is a subclass of Y’s declared type.

26
Q

What is an expression?

A

Expressions are a way of performing work in a Java program. Among other things, expressions are used to compute values and to help control the execution flow of a program.

27
Q

How do you create an object?

A
Objects are created by using the keyword new and a constructor.
Every object has an associated textual (string) description, of which the default gives a class and memory address of the object.