1.What modifiers are allowed for methods in an interface?
only public and abstract modifiers are allowed for methods in an interface
2. What is a local, member, and a class variable?
Variables declared within a method are local. Variables declared within a class but outside of methods are member variables. Variables within a class that are static are called class variables.
3. What are Serialization and Deserialization?
Serialization is the process of saving/writing the state of an object to a byte stream.
Deserialization is the process of restoring a serialized object.
4. Can you have an inner class within a method and what variables can you access?
Yes. we can have an inner class with final variables within a method.
5. What is the clonable interface?
The clonable interface is a marker interface.
6. What is the difference between Integer and int?
Integer is a wrapper class for the primitive int. Integer can be used for generic like List<Integer> list. It cannot be List<int> list.
int is the primitive.
7. What is an inner class and an anonymous class?
An inner class is any class defined in another class even an inner class within a method.
An anonymous class is a class defined inside a method. This class does not have a name. It is instantiated and declared in the same place. It can’t have explicit constructors.
8. What modifiers can a top-class have?
public, abstract, and final.
9. What is the difference between a superclass and a subclass?
A superclass is a class that is inherited.
A subclass is a class that does the inheriting.
10. What is the difference between overloading and overriding?
Overloading is a relationship between different methods within a class.
Overriding is a relationship between a superclass and a subclass.
Overloading methods have the same name with different method signatures.
Overring methods must have the same method signatures.
11. What is final, finalize(), and finally?
final: a final class can’t be extended. a final method can’t be overridden. a final variable can be changed from its initial value.
finalize(): this method is called just be an object is garbage collected.
finally: a finally block a block which is part of a try or try and catch block. This block runs regardless of an exception is thrown or not.
12. What are the different types of access modifiers in Java?
public: can be accessed from everywhere.
private: can be accessed outside of the class in which private is used.
protected: can be accessed from the same package or subclasses from different packages.
Default modifier: can be accessed only from the same package.
13. What do static methods mean?
Static methods can be called using just the class name. Static methods belong to class not an object of the class. You don’t have to create an object to access static methods.
14. What does it mean that a method or a field is static?
Static variables and methods are instantiated only once per class. If you change the value of a static variable, it will change for all instances of that class.
15. What is the common usage of serialization?
When an object(and its state) is to be saved somewhere and be retrieved for later use then serialization comes into the picture. When you cache an object using a cache manager(Redis) the object must be serialized.
16. What is java String Pool?
Java String pool refers to a collection of Strings which are stored in heap memory. Whenever a new string object is created, String pool first checks whether the object is already present in the pool or not. If it is present, then the same reference is returned to the variable else new object will be created in the String pool and the respective reference will be returned.
17. What are the differences between this() and super()?
|1. this() represents the current instance of a class||1. super() represents the current instance of a parent/base class|
|2. Used to call the default constructor of the same class||2. Used to call the default constructor of the parent/base class|
|3. Used to access methods of the current class||3. Used to access methods of the base class|
|4. Used for pointing the current class instance||4. Used for pointing the superclass instance|
|5. Must be the first line of a block||5. Must be the first line of a block|
18. What are the differences between String, String Builder, and String Buffer?
|Factor||String||String Builder||String Buffer|
|Storage Area||Constant String Pool||Heap Area||Heap Area|
19. What is constructor chaining?
Constructor chaining is the process of calling a constructor from another constructor. This is done using this() keyword.
20. Why do we need constructor chaining?
Constructor chaining is useful when we want to perform multiple tasks in a single constructor rather than creating code for each task in a single constructor. We create a separate constructor for each task and make it a chain that makes the program more readable.
21. Why is String immutable?
In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that string, Java creates a new string object instead of updating the values of that particular string. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.
22. What are the differences between Array and ArrayList?
|Cannot contain values of different data types||It can contain values of different data types.|
|Size must be defined at the time of declaration||Size can be dynamically changed|
|Need to specify the index in order to add data||No need to specify the index|
|Arrays are not type parameterized||ArrayList are type|
|Arrays can contain primitive data types as well as objects||ArrayList can contain only objects, no primitive data types are allowed|
23. What is pass by value?
You are passing the values copied. Java is passed by value when you pass primitive data types and passed by reference when you pass custom object data types.
24. What is pass by reference?
You are passing the memory address of the variables you are passing.
25. What is an interface?
An interface is similar to a class but it is an abstract class completely. An interface has abstract methods that its subclasses have to implement. When we talk about an interface we are talking about its capabilities. We are not even talking about how these capabilities are formed, just what they are.