Lecture 5 Flashcards

1
Q

Java and Python difference in storing objects.

A

Java stores some values on the heap as objects.
Python stores everything.

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

Java and Python difference in values not stored on the heap.

A

Java stores them at bit-sequences.
Python does not do this.

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

Java and Python difference in Type information.

A

Java has type information specified in the program (at compile time).
Python stores type information in the object itself (at run time).

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

Numeric types, Java vs. Python.

A

Java: Bit sequences.
Python: Objects.

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

Integer range. Java vs. Python.

A

Java: Limited range.
Python: Not limited.

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

Types of integers. Java vs. Python.

A

Java: Multiple types.
Python: One type.

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

Types of floats. Java vs. Python.

A

Java: Multiple types.
Python: One type.

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

Float ranges. Java vs. Python.

A

Java: Different types, different ranges.
Python: One type, one limited range.

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

Numeric types in Java.

A

int
byte (8 bits)
short, a kind of int
long, a kind of int
double, double* precision float.
float
char, represents code units in Unicode

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

Usage advice of number types for CMPT270.

A

Int for counted quantities.
Double for measured.
Others for special purposes only. Don’t worry about them too much.

Note: Java’s numeric types are very similar to C/C++.

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

Java arithmetic operators.

A

Same as Python. +,-,*,/,%
int operated with int leads to int
float operated with float leads to float
mixed types operated on leads to implicit conversion to a floating point
% is restricted to integers only

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

Numeric expressions. Java vs. Python.

A

Java: Results in bit sequences that can be copied somewhere.
Python: Makes a new object.

Python’s way results in extra overhead and takes a bit longer.

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

Booleans in java.

A

boolean: true, false

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

char in Java.

A

Literals written with single quotes. Any single letter, number, keyboard symbol.

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

Strings in Java.

A

Any sequence of characters. 0 or more.
Written with double quotes.
Strings are a class*, not a primitive.

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

Boolean expressions in Java.

A

&&
||
!
These are from C.

17
Q

Java rational expressions.

A

==
>=
etc…
Inherited from C.

18
Q

Where is data for primitive types stored in Java?

A

On the stack.

19
Q

How do reference types work with memory?

A

The reference type in the stack holds a pointer that leads to an object on the heap.

20
Q

Java. What happens when you set a reference type variable to another?

A

A copy of the pointer is made. Like in Python?

21
Q

Java declaring variables.

A

tell the compiler the name and type.
type name;
The compiler allocates exactly enough memory for the type.
Every variable must be declared before it can be used.

22
Q

Java. What is initializing a variable?

A

The first time a variable is assigned to something.

23
Q

How many times can a variable be declared and initialized?

A

One once for any variable. Although new values can be assigned.

24
Q

Constants in Java.

A

Use final keyword.
i.e.
final type name;

These can be useful for adding clarity to calculations.
final tells the compiler that no future changes are allowed.

25
Q

Type casting in Java.

A

Not allowed implicitly.
I.e.
double exampleDouble = 9.8;
int exampleInt = (int) exampleDouble;

exampleInt is 9
Type casting double to int truncates, not rounds.

26
Q

Scope in Java.

A

{} shows the structure.
A variable’s scope is it’s lifetime. They are not usable outside it’s scope.

27
Q

Java. Conditional statements.

A

if (condition)
{
stuff;
}
else if (condition)
{
stuff;
}
else
{
stuff;
}

Note the “else if” instead of “elif” like in python.

28
Q

Java while loops.

A

while (condition)
{
stuff;
}

29
Q

Java for loop.

A

Example:
for (int i = 0; i<10; i++)
{
stuff;
}

30
Q

Java. Do while loops.

A

do
{
stuff;
} while (condition);

31
Q

Console output in java.

A

Printing uses System.out object.
System.out.print(); has no newline
System.out.println(); has a newline.

32
Q

Java. How to get string length?

A

.length() method

33
Q

Java. String concatenation.

A

string1 + string2
This also works with other data types.

34
Q
A