OCA Flashcards
Can you redeclare variables in the initialization block of the for loop?
No
Can you use more datatypes in the initialization block of a for loop?
No
Are immutable classes final or not?
They are final
What happens with strings that are not in the string pool(they are not literals, hence, were created using “new”) when they are no longer used?
They are garbage collected, just like any other object
True or false: Varargs are always allowed in place of an array
True
What kind of operators can & have?
Integral and boolean
What does this code block print?
Class.forName(“not_a_real_class”)
ClassNotFoundException
What objects types can be caught (as Exceptions)?
All Throwable instances
What datatypes can be used as switch variables?
String, byte, char, short, int + their Wrappers, enum
True or false: a switch can contain two or more identical cases
False
Are java.time package classes mutable or immutable?
Immutable
True or false: you can create arrays of any type, with length zero
True
True or false: subclasses can define methods with the same signature as the private methods in the base class. Justify.
True (since private methods are not inherited)
True or false: polymorphism applies to both static and non-static methods.
False. When applied to static methods, it is called ‘method hiding’
Does an assigment executed inside the condition of a conditional block alter the assigned variable permanently?
Yes
Can a subclass override the superclass’ constructors? Justify
No, because constructors are NOT inherited
True or false: Any type of class can implement any interface
True
True or false: Any class(abstract/concrete) can extend any class
True
True or false: Native methods cannot be abstract
True
True or false: String, StringBuilder and StringBuffer cannot be extended. Justify.
True, because they are final
On what types of operands does % work?
Both integral and floating point
On what types of operands do !, && and || work?
Boolean
On what types of operands does ~(bitwise complement) work?
Only on integral types
True or false: A short variable can never be assigned to a char without explicit casting
True
True or false: A short constant can be assigned to a char, but only if the value fits into a char.
True
What is implicit narrowing and on what types does it work?
If you have a final variable and its value fits into a smaller type, then you can assign it without a cast because the compiler can already tell it can fit into the smaller type. It works only on byte, char, short and int.
Does int to float need a cast? Justify.
No, because float can hold any value of int
Does float to int need a cast? Justify.
Yes, because of the loss of precision
What happens when you use == on objects of different types? How about equals?
Compilation error. For equals(), it will always return false, because the compiler will know they cannot be the same object.
To what constructs can the ‘synchronized’ keyword be applied?
Methods and blocks
Are method parameters assigned default values?
No. Think of them as special local variables
True or false: you can call the superclass’ constructor from any method in the subclass
False. It can only be called from one of the subclass’ constructors
True or false: the call to the superclass’ constructor can be called only as the first statement of the base class’ constructor
True
What is the difference between super() and this()?
super() calls the base class’ constructor, while this() calls the subclass’ constructor
Will this compile?
if (false) ; else ;
Yes, because ; is the empty statement
Will this compile?
if(true) if(false);
Yes
When is a member with default access inherited?
When both classes are in the same package
Which things in a class never get inherited?
Constructors and static initializers, because they are NOT members
Can static methods be abstract? Justify.
No, because static methods are inherited, but can NOT be overriden (only hidden) in the subclass
What value does main() receive as argument if no parameter is passed to it?
A NON-null array of Strings, of length zero(it has zero elements).
What does this print:
System.out.println(1 + 2 + “ceva”);
3ceva
What does this print:
System.out.println(1 + “ceva” + 2);
1ceva2
Give the method signature: returns the number of characters in the string
int length()
What is the name of the interface for String and StringBuilder?
CharSequence
Which one is slower and why: StringBuilder or StringBuffer?
StringBuffer, because it’s thread safe
If you compare two Strings with ==, one of which is computed at runtime, what will be the result of the evaluation?
false
Does StringBuilder implement equals()?
NO
Do String and StringBuilder require imports?
NO
Are arrays stored on stack or on heap? Why?
Heap, because they are objects
Do array elements get initialized by default? If yes, what value do they get
Yes. They get the default value of the element type
Is equals() overriden for arrays?
No
Do object arrays allocate space for the element itself or the reference?
The reference
Does this have a default value or is it null (it’s a local variable)?
String names[];
null
Is this correct: Numbers sort before letters and uppercase sorts before lowercase
Yes
What is the order of initialization of a derived class? What about when we refer a class without a new call? Between the main and rules 1 and 2, which will run first?
1) Superclass
2) Static variable declarations and static initializers, in declaration order
3) Instance variable declarations and instance initializers, in declaration order
4) Constructor
Then only rules 1 and 2 apply and they always run before the main
When are the static variable declarations and static initializers executed?
When the class is loaded (the loading takes place only if the class is used)
How would main()’s signature look like if you passed varargs to it?
public static void main(String… args)
Does ArrayList implement toString()
Yes
Do collections accept primitives?
NO
What happens when you try to unbox a null?
NullPointerException
How would you sort an ArrayList called “arli”?
Collections.sort(arli);
Can a file which imports another package access its package-private classes or class members?
No
Are methods shared or does each instance get its own copy?
They are shared
Protected = subclasses + package. In what situation do we get compilation errors for trying to access protected members?
When the class containing the member is in one package, and the class trying to access this member, on a class instance, is in a different package and this class is NOT a subclass of the class containing the member
Where can you initialize a static variable?
At the time of its declaration or in a static block
What are the mandatory and the optional changes in the method signature for method overloading?
Mandatory: something in the parameter list
Optional: return type, access modifier, exception list
Does this code compile? Justify.
public void fly(int[] lengths) { }
public void fly(int… lengths) { }
No, because varargs and arrays are equivallent
Which can be called by passing an array? Which can be called by giving the elements directly?
public void fly(int[] lengths) { } //1
public void fly(int… lengths) { } //2
Both can be called by passing an array, but only the varargs one can be used to pass the elements directly
What is the order in which Java chooses the right overloaded method? (hint: there are 4 argument categories)
1) Exact match by type
2) Larger primitive type
3) Autoboxed type
4) Varargs
Given this method signature:
public void func(Long par)
Will this compile? Justify. func(4);
No, because 4 is an int. Java will convert it automatically to a long, but it can only do one conversion by itself, so it will not be able to autubox the long => compilation error