Java Interview Questions Flashcards
(55 cards)
Comparable vs. Comparator
If sorting of objects needs to be based on natural order then use comparable.
If they need some customized sorting order then use comparator
Serializable vs Externalizable
Serializable : class is serialized automatically by default
Externalizable : gives complete control over class serialization process
What is ‘transient’ keyword?
Used to make a data member not serializable
How many ways to create object?
There are 4
- new keyword
- clone
- deserialization
- Class.forName(“classname”).newInstance()
new keyword vs. newInstance()
In general, new operator is used to create objects but if we want to decide type of object to be created at runtime the newInstance() method needs to be used
Object type can be passed as a parameter using newInstance() method
public static void fun( string c ){ Object obj = Class.forName(c).newInstance(); }
What is java ‘volatile’ keyword?
Volatile is used to indicate that a variables value will be modified by different threads
Protected vs. Private
Protected: can be accessed from the same class to which they belong or from the sub classes, and from the classes of the same package, but not from the outside
Private: can be accessed from the same class to which they belong
When to use Static Nested vs. Singleton
Static: used when you want to collect related pieces of functionality but you don’t need to have any internal state in any object
Singleton: used when you do want an actual object (with its own internal state etc.) and you want to limit your system to exactly one instance of that object
How to create an immutable class?
Declare class final and making all data members final
What is reflection?
The ability to inspect and modify the runtime behavior of application
We can inspect the java class interface, enum and get their methods and field details
Purpose of finalize() method?
Invoked just before garbage collection and is used to perform clean up processing
What is Externalizable ?
Used to write objects to the byte stream in compressed format.
Why are string objects immutable?
Because Java uses the concept of string literals.
Suppose you have five variables all referencing one object (string) if one variable changes the value of the object it will affect all the referencing variables.
What is composition and what are the benefits?
Holding the reference to another class within some other class
Has-A relationship is created.
Benefits:
- Get around inheritance restrictions
- code reuse, grants more flexibility and extensibility than inheritance.
What is Object Serialization?
To convert an object’s state to the byte stream, so that the byte stream can’t be reverted back into a copy of the object.
Why use Generics?
- Stronger type checking at compile time.
- Elimination of casts.
- Enables code reuse through the implementation of generic algorithms.
What are the data types supported by Java?
- byte
- short
- int
- long
- float
- double
- boolean
- char
What is Type Erasure?
Replace all type parameters
Insert type casts if necessary to preserve type safety.
Generate bridge methods to preserve polymorphism in extended generic types.
What is Abstraction?
Abstraction is the process of hiding the implementation details and showing only functionality to the user
- Abstract class
- interface
Can we override static methods?
No, static methods to not be overridden because they are part of the class and not the object.
What is inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behavior of another object of another class.
Creates IS-A Relationship.
Can we have a try without catch block?
Yes we can have try-finally
How many ways to create string?
There are two ways
- new keyword
- passing a string literal
What is a string literal?
Sequence of characters enclosed in double quotation marks
Example: “ Hi”