Chapter 5 Flashcards

1
Q

What is the flow of control?

A

The flow of control is the (almost) linear list of statements that a program executes. In some cases there might be decisions (based on boolean values) that might lead to repetitions or to different parts of our program.

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

What’s the difference between = and == ?

A

= is an assignment operator, it assigns the value of the right side to the variable on the left side.
== is a boolean expression that checks whether the elements on both side of it are equal between each other.

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

What are the logical operators used in boolean expressions?

A

It’s
! logical NOT
&& logical AND
|| logical OR

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

What happens if the first condition of a && boolean expression is false?

A

The rest of the conditions are not evaluated and therefore no operation inside is executed

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

How is the if statement written?

A

if(condition)
statement;
If the condition is verified (true), the statement is executed.
An
else
statement2;
can be added to execute a piece of code in case the condition is not verified.

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

Why comparing two floating values is risky?

A

The boolean expression checks whether the binary composition of the two variables is exactly the same. This implies that even if two values are approximately the same, if even one bit is different the result will be false, leading to errors.
In such cases, you should subtract the values and check whether the result is below a certain tolerance (that you define).

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

How are characters compared?

A

Characters are compared on the idea that each has a numeric value defined in the Unicode table. In a boolean expression their numeric values will be considered.

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

How are two object compared?

A

Two objects are compared if they reference the same allocation in the memory.

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

How do you compare strings? Is there only one way?

A

Strings are like objects, when you control with == two objects, their references will be checked.
You can used the method equals to check whether they contain exactly the same characters
(c1.equals(c2))

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

What does the compareTo method do?

A

The compareTo method relates two strings and sees which of them has a bigger sum of characters.
With name1.compareTo(name2)
It returns zero if they’re the same
A negative value if name1 is less than name2.
A positive value otherwise.

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

What is the syntax of a while statement?

A

while(condition)
statement;
The statement will be executed EACH TIME the condition will be true.

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

What is an iterator?

A

An iterator is an object that allows you to process a collection of items one at a time.

hasNext returns true if there is at least one more item to process.
next methods returns the next item.

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

What is an ArrayList? What’s its pecularity?

A

An ArrayList object stores a list of objects.

An ArrayList object grows and shrinks as needed, adjusting its capacity as necessary.

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

How do you references a specific object inside an ArrayList? What happens when you add or remove elements?

A

Inside an ArrayList there’s an index (starting from 0), each number corresponds to an object inside the ArrayList.
If one object is removed the indexes of all the other objects is changed as well, as to occupy the least memory as possible.

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

Can one single ArrayList contain objects of different types?

A

No.

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

How do you declare the type of objects that an ArrayList can hold?

A

You make use of the generics. Generics provide additional type checking at compile time.
Ex:
ArrayList names = new ArrayList<>();

17
Q

What precisely are generics used for? What is its relationship with primitive data values?

A

Generics is a generics class or itnerface that is parameterized over types. It achieves two things:

  1. Stronger type checking
  2. Elimination of casting

Ex:
ArrayList list = new ArrayList();
list.add(“hello”);
String s = (String) list.get(0);

ArrayList list = new ArrayList();
list.add("hello");
String s = list.get(0);

Primitive data values cannot be stores inside generic classes, but you can use wrapper classes:
Box intBox = new Box<>();

18
Q

What are raw types of classes? Can you assign parametrized types to a raw type? And viceversa?

A

If you don’t declare the actual type of the object, a raw type of the generic type object is created.

You can declare a parametrized type object to a raw type object (it will work like a widening):
Box stringBox = new Box<>();
Box rawBox = stringBox;

You cannot although declare a raw type to a parameterized type, as Java won't know how to treat the given data:
Box rawBox = new Box();
Box intBox = rawBox;
19
Q

With what method does an ActionEvent method determine who caused the event?

A

With getSource();

20
Q

What is the ToggleGroup object used for?

A

A ToggleGroup object is used for programs with radio buttons. Adding the radio buttons to one ToggleGroup object will allow only one of the buttons to be selected at a time.