Harder Java Cards Flashcards
What are the primitive data types?
char, byte, short, int, long, float, double, boolean
Can a user define their own primitive data types?
No they cannot.
What is the printed when you use a variable of type char without initializing it (assuming that the declaration is an instance variable).
The default value of char is 0, which when printed, translates to an empty space.
When assigning a unicode value to a char variable, what is the allowed range?
‘\u000’ to ‘\uffff’
Can an identifier have the same spelling as a Java keyword?
Yes, but only if the case is changed, because Java is case sensitive. Int is a valid identifier, while int is not (because int is a reserved word).
Can a double variable be assigned to an int variable?
Yes, but only if the variable is cast to an int.
Can an int variable be assigned to a double variable?
Yes, and no casting is needed because Java widens the variable.
In what ways can an object of a wrapper class (i.e. Integer) be created?
By assigning a primitive to a wrapper class variable (autoboxing), by using the constructors of the wrapper class, and by calling the static method valueOf( ).
What wrapper classes define a constructor that accepts a String argument representing the value that needs to be wrapped?
All of them except for Character.
Which wrapper classes define a no argument constructor?
None of them.
What is autoboxing?
When a primitive value or variable is assigned to an reference variable of its wrapper type, Java automatically creates an object of that type and assigns its value to the primitive value passed.
What is unboxing?
When an object of a primitive wrapper class is converted to its corresponding primitive value.
How can you compare objects of wrapper classes for equality?
You can use the == operator because Java unboxes the wrapper objects into their corresponding primitives, and also use .equals( ) because it is overridden in all of the wrapper classes to compare the values of the objects passed.
What will happen if a wrapper reference variable that refers to null is unboxed?
Java will throw a RuntimeException of NullPointerException.
What happens to a char primitive when it is used as an operand to arithmetic operators?
Its corresponding ASCII value is used.