Flashcards in Harder Java Cards Deck (22)
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.
What is binding in the context of inheritance?
Binding refers to the resolving of variables or methods that would be called for a reference variable.
When are instance variables bound?
At compile time.
When are methods bound?
At run time.
What is variable hiding?
Variable hiding happens when a variable is declared in a local scope that has the same name as one that exists in the outer scope.
What happens when a child class defines a static method with the same signature as a static method in the parent class?
The child class method hides the parent class method.
When talking about polymorphism, what is the difference between method hiding and method overriding?
Hiding methods only happens when the methods are static, and binding occurs at compile time, as static methods are not polymorphic. Overriding instance methods are bound at runtime.