Chapter 5 Flashcards
What is the flow of control?
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.
What’s the difference between = and == ?
= 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.
What are the logical operators used in boolean expressions?
It’s
! logical NOT
&& logical AND
|| logical OR
What happens if the first condition of a && boolean expression is false?
The rest of the conditions are not evaluated and therefore no operation inside is executed
How is the if statement written?
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.
Why comparing two floating values is risky?
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 are characters compared?
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 are two object compared?
Two objects are compared if they reference the same allocation in the memory.
How do you compare strings? Is there only one way?
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))
What does the compareTo method do?
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.
What is the syntax of a while statement?
while(condition)
statement;
The statement will be executed EACH TIME the condition will be true.
What is an iterator?
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.
What is an ArrayList? What’s its pecularity?
An ArrayList object stores a list of objects.
An ArrayList object grows and shrinks as needed, adjusting its capacity as necessary.
How do you references a specific object inside an ArrayList? What happens when you add or remove elements?
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.
Can one single ArrayList contain objects of different types?
No.