1: Qs wk 3 Flashcards
Name the two equality operators and what types of data they operate on
== and != are sometimes called equality operators. They can be used on any kind of data, whereas the other four relational operators are limited to the 6 numeric types plus type char.
Describe how relational operators may be used to compare char values.
Each character has a unicode number associated with it.
- Space has the lowest unicode character.
- Numbers as characters, i.e. ‘1’, ‘2’, ‘3’, etc. start at Unicode #48.
- Uppercase letters come before lowercase letters (i.e. the uppercase letters have a lower Unicode number than lowercase letters).
E.g.
‘X’ > ‘A’ is true (because the code for ‘X’ is bigger than the code for ‘A’)
‘3’ > ‘0’ is true (because the code for ‘3’ is bigger than the code for ‘0’)
‘b’ > ‘W’ is true (because all lower-case letters have higher codes than any upper-case letter)
What is identity equality?
== and != may also be used with object references. Comparing object references with != or == compares the references of the objects (memory addresses) directly. That means we are testing to see if they refer to the same object.
Identity equality: obj1 == obj2
What is content equality?
Content equality is different from identity equality (in which we are using == or != to compare object references). Content equality is testing to see if two objects are considered to be equal.
Content equality: obj1.equals(obj2)
What are the Boolean operators?
! (NOT)
&& (AND)
|| (OR)
Write an operator precedence table
parentheses; array subscript; member selection: () . [ ] .
unary postfix operators: expr++ . expr–
unary prefix operators: ++expr –expr +expr -expr !(NOT)
instantiation; type cast: new . (type)
multiplicative: * / %
additive: + -
relational and instanceof : < > <= >= instanceof
equality: == !=
logical AND: &&
logical OR: ||
conditional: ? :
assignment: = += -= *= /= %=
Compound condition
A compound condition is one that makes use of logical operators to join ‘sub conditions.q’
What is a predicate method?
A method that returns type boolean is called a predicate method. The equals method used to test for content equality is an example of a predicate method.
What is “multi-way selection”?
A “multi-way selection” situation is a situation in which one choice is made from a set of three or more possibilities. There are always multiple conditions in multi-way selection structures.
What is a switch statement?
A switch statement is a control structure used to perform multiway selection when the decision can be made by matching specific values of a variable or expression. The switch statement tries to match the value of an expression with one of several specific values listed in case statements. The action associated with the first match is executed.
Loop control variable
The variable that has its value tested before entering the loop is known as a loop control variable. The loop control variable must be initialized - given an initial value - before it is tested. The loop control variable must also be updated, so that there is a way for the loop to terminate. The update needs to be inside the body of the loop.
Name the 5 key elements of a loop.
- Initialization. Set up the proper conditions for the start of the loop, including identifying and assigning an initial value to the loop control variable.
- Loop test. Specify the conditions that must be satisfied for the loop to be executed.
- Main work. Statements to be executed during each iteration of the loop.
- Update. The value of the loop control variable must be changed inside the body of the loop.
- Finalization. Add any final steps to properly wrap things up.
Explain the difference between definite and indefinite loops.
With a definite loop, such as counting loops, you can tell from the algorithm how many times it will iterate. With indefinite loops, you will not know in advance how many times the loop will iterate.
What are the three different control (loop) structures in Java?
while
for
do…while
What is a pre-test loop?
Both the while and for loop are pre-test loops, because the test of the loop control variable happens before the first iteration of the loop can happen. This allows for the possibility that the loop will never be executed.