Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
83 Cards in this Set
- Front
- Back
Can Java operators be overloaded?
|
No
|
|
Which operator comes overloaded out of the box?
|
The + operator which if applied to a String concatenates the right-hand operand to the operand on the left.
|
|
What are variables?
|
just bit holders, with a designated type
|
|
What does an object reference variable contain?
|
bits representing a way to get to the object - it is a value representing a specific object on the heap or null.
|
|
If the object reference value has not been assigned a value what does it hold?
|
bits representing "null"
|
|
What is the "assignment operator" and what does it do?
|
the equal (=) sign and is used to assign a value to a variable
|
|
What type is a literal integer(such as 7) always?
|
an "int"
|
|
What happens when you assign a literal integer to a byte variable?
|
the compiler automatically narrows the literal value to a byte, i.e. the compiler puts in the cast
|
|
What is the result of an expression involving anything "int"-sized or smaller?
What happens with this: byte b = 3; byte c = 8; byte d = b + c; |
It is always an "int".
It won't compile - you get the error "possible loss of precision". If you put in the cast it will work: byte d = (byte)(b + c); |
|
Every floating point literal is a _____ not ____.
|
double
float |
|
What happens if you do:
float f = 32.3; |
It won't compile.
|
|
What must you do to assign a floating-point literal to a "float" variable?
|
You must either cast the value or append an "f" (or "F") to the end of the literal.
|
|
What happens if you do:
byte a = 128; |
it won't compile - the compiler knows that it is too big to fit into the variable
"possible loss of precision" |
|
What happens when you do:
byte a = (byte)128 |
You lose all the bits to the left of the bits you're narrowing to. So we are left with 10000000 where 1 is the sign bit so this is really -128.
|
|
What actually happens when you do:
byte b = 3; b += 7; |
it is equivalent to:
byte b = 3; b = (byte)(b + 7); so it lets you add to the value of b without putting in an explicit cast. |
|
When you assign one primitive variable to another, what happens?
|
the contents of the right-hand variable are copied - they do not share a single value.
|
|
What 3 things does the following line do:
Button b = new Button(); |
1. Makes a reference variable named b, of type Button
2. Creates a new Button object on the heap 3. Assigns the newly created Button object to the reference variable b |
|
What does it mean to assign "null" to an object reference variable?
|
it means the variable is not referring to any object
|
|
You can use a reference variable to refer to any object that is a _____ of the declared reference variable type.
|
subclass
|
|
Can you change the value of a String object?
|
No, they are immutable
|
|
What happens when you use a String reference variable to modify a string?
|
1. A new string is created, leaving the original String object untouched.
2. The reference used to modify the String is then assigned the brand new String object |
|
For any object type except String, what happens when two references refer to the same object and either of these references is used to modify the object?
|
both references will see the change because there is still only a single object
|
|
How many String objects are created in the following:
String s = "Fred"; String t = s; t.toUpperCase(); |
two String objects were created but the newly created String holding "FRED" is toast since it isn't assigned to a String variable; both t and s referr to the String "Fred".
|
|
Comparison operators always result in a ______ value.
|
boolean (true or false)
|
|
What are the 4 comparison operators?
|
> (greater than)
>= (greater than or equal to) < (less than) <= (less than or equal to) |
|
Can characters be used in comparison operators?
|
Yes, when comparing a character with a character, or a character with a number, Java will take the ASCII or Unicode value of the character as the numerical value, and compare the numbers
|
|
Can the "instanceof" operator be used for primitive variables?
|
No, only for object reference variables
|
|
What do you use the "instanceof" operator for?
|
to check whether an object is of a particular class or interface type. It checks if the object referred to by the variable on the left side of the operator passes the IS-A test for the class or interface type on the right
|
|
Any object reference will evaluate to true if you use the "instanceof" operator against type _____.
|
Object
|
|
An object is said to be of a particular type (meaning it will pass the "instanceOf" test) if any of the object's _______ implement the interface.
|
superclasses
|
|
What happens when you test whether a "null" object (or "null" itself) is an instance of a class?
|
will always result in false
|
|
Is an array an object or a primitive?
|
It is an object, even if the array is an array of primitives
|
|
An array is always an instanceof _____.
|
Object
|
|
What 2 operators test for equality?
|
== equals
!= not equals |
|
The equality operators compare two things and return a ______ value.
|
boolean
|
|
Can you compare incompatible types?
|
No
|
|
What four different type of things can be tested for equality?
|
1. Numbers
2. Characters 3. Boolean primitives 4. Object reference variables |
|
What happens if a floating-point number is compared with an integer and the values are the same as in (5.0 == 5L)?
|
the == operator returns true
|
|
Can integer values be used where a boolean value is expected?
|
No
|
|
What does the == operator test for when used with object reference variables?
|
to see if they refer to the same object
|
|
What are the 4 basic arithmetic operators?
|
+ addition
- subtraction x multiplication (use *) / division |
|
What does the % operator do?
|
it is the remainder operator and divides the left operand by the right operand and the result is the remainder
|
|
What happens when you divide an integer by zero?
|
an ArithmeticException
|
|
What happens when you use the % operator with a right operand of zero?
|
an ArithmeticException
|
|
What happens when you divide a floating-point number by positive zero?
|
You get positive infinity
|
|
What happens when you divide a floating-point number by negative zero?
|
You get negative infinity
|
|
Will you get an ArithmeticException when you use the remainder operator on floating-point numbers, where the right operand is zero?
|
No
|
|
When does the + operator become a String concatenation?
|
When either operand is a String. If both operands are numbers, the + operator is the addition operator.
|
|
What are the two operators that will increment or decrement a variable by one?
|
++ increment (prefix and postfix)
-- decrement (prefix and postfix) |
|
What happens when you use the increment or decrement operator on a "final" variable?
|
a compiler error, final variables can't be changed
|
|
Give the 3 shift operators.
|
>> right shift
<< left shift >>> unsigned right shift(zero-filled right shift) |
|
Can you use shift operators on floating-point numbers?
|
No
|
|
How do you determine the result of a shift operator?
|
you have to convert to binary
|
|
With the left shift operator, what happens to the right side?
|
the right side is filled with zeros
|
|
When using the right shift operator, what happens to the left side?
|
the leftmost bits are filled in on the left with whatever the sign bit was
|
|
With the right shift operator, what happens to a negative number?
|
It stays negative
|
|
Except for a few special cases, the result of an unsigned right shift is always _____?
|
positive, regardless of the original sign bit
|
|
All operands in a bit shift are promoted to at least ____
|
an int
|
|
How many bits are shifted with the shift operators? How many bits are shifted when you shift an int by 34?
|
always the right operand modulus the total number of bits for that primitive type.
2 bits |
|
If you shift an "int" by any multiple of 32 or a "long" by any multiple of 64, what do you get?
|
the original value will not change
|
|
What actually happens when you use the right shift operator?
|
the number being shifted is "divided" by 2 to the power of the number of bits to shift.
|
|
What actually happens when you use the left shift operator?
|
the number being shifted is "multiplied" by 2 to the power of the number of bits to shift
|
|
What are the 3 bitwise operators?
|
& AND
| inclusive OR ^ exclusive OR |
|
What does the "&"operator do?
|
compares corresponding bits between two numbers. If both bits are 1, the final bit is 1. If only one of the bits is 1, the resulting bit is 0.
|
|
What does the "|" operator do?
|
sets the resulting bit to 1 if either (of both) of the bits is a 1
|
|
What does the "^" operator do?
|
compares two bits to see if they are different. If they are different, the result is 1.
|
|
What does the "~" operator do?
|
it is the flip-the-bits operator. It will change all 1s to 0s and vice versa.
|
|
Give the structure of the conditional operator.
|
someVariable = (boolean expression) ? value to assign if true: value to assign if false
|
|
Can you nest conditional operators into one statement?
|
Yes
|
|
What does "casting" let you do?
|
Convert primitive values from one type to another.
|
|
Typically, an implicit cast happens when you're doing a ______ conversion.
|
widening
|
|
The large-value-into-small-container conversion is referred to as _____.
|
narrowing
|
|
Which type of converions requires an explicit cast, widening or narrowing?
|
narrowing
|
|
What happens when you assign a floating-point number to an integer variable?
|
a compiler error
|
|
What are the 4 logical operators?
|
&& short-ciruit AND
|| short-circuit OR & | |
|
The "&&" operator is similar to the "&" operator except for what?
|
it evaluates only boolean values and can't be used as a bitwise operator.
|
|
What does the "&&" operator do if the left side of the operation resolves to false?
|
It doesn't bother looking at the right side of the operation
|
|
What does the "||" operator do if the left side of the operation resolves to true?
|
It doesn't bother looking at the right side of the operation.
|
|
Can the bitwise operators (& and |) be used in both logical and bitwise expressions?
|
Yes, but they are rarely used in logical expressions since they are not efficient - they evaluate both sides of the expression
|
|
What happens when you pass an object reference variable into a method?
|
both the caller and the called method will now have identical copies of the reference, and thus both will refer to the same exact (not a copy) object in the heap.
|
|
Is Java "pass-by-reference" or "pass-by-value"?
|
pass-by-value, which means pass-by-copy-of-the-variable so the called method can't change the caller's variable.
|
|
What does shadowing involve?
|
It involves declaring a variable that's already been declared somewhere else. The effect is to hide the previously declared variable in such a way that it may look as though you're using the shadowing variable
|
|
When a floating-point zero is divided by zero, what do you get?
|
NaN
|