Be Prepared Ch 1 - 2.3 Flashcards
Why shouldn’t you use a for each loop sometimes?
You cant modify stuff when using a for each loop. as the for each loop is making a copy of the stuff. Rather use a regular for loop
Free responses will typically be less than _____ lines of code
12
Appropriate Casting
Int a,b;
Double ratio;
…
Ratio = (double)a/b; // or a/(double) b;
//but not ratio = (double) (a/b) – this is a cast applied too late!
There are two forms fo the ++ and – operators in Java. What are they and how are they different?
The prefix form increments (or decrements) the variable before its value is used in the rest of the expression; the postfix form increments (or decrements) it afterwards.
While(i<=n) sum+= i++; is the same as
While (I <= n)
{
Sum+= I;//i
i++;
}
== and != compare the _________ of the objects, not the ______ To compare the ____ use the equals or compareTo method
addresses; values; values
== or != must be used when you need to compare an object to ____
null
“Everything” in Java not explicitly set to something is initialized to a ____ value.
zero;
For references (anything that holds an object) that is null.
For int/short/byte that is a 0.
For float/double that is a 0.0
For booleans that is a false.
Everything in Java, except _____, not explicitly set to something is initialized to a zero value.
Local Variables;
You need to explicitly initialize any local variables (by either initialization or assignment)
assignment
throwing away the old value of a variable and replacing it with a new one
initialization:
it’s a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.
declaration:
a _____ states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.
De Morgan’s Laws
!(a&&b) is the same as !a||!b
!(a||b) is the same as !a&&!b
Short-circuit evaluation
For the java logical operators, && and ||, if the value of the first operand is suffiecient to dertemine the result, the the second operand is not evaluated.
Bit-wise logical operators
Not in ap exam. But can cause synatx errors when comparing bits, or bolleans . .. Do not comply with short-ciruit evaluation when used in that way
For-each loop
Int[] scores = [87,85,76];
For (int score : scores)
System.out.println(score+” “);
Note: if you need access to indices, use a for or while loop