4,777 total views, 3 views today
- Explain the creation of a thread-safe singleton in Java using double-checks locking
Singleton is made with double checked rushing as before Java 5 acts as an broker and it’s been possible to have distinctive instances of Singleton when different strings makes an event of Singleton meanwhile. Java made it easy to make string safe Singleton using Enum. Using a precarious variable is essential for the same.
- How can we restrict inheritance for a class?
We can restrict inheritance for class by following steps.
- By using private constructors
- By using final keyword
- Why Java doesn’t support multiple inheritances.
Java doesn’t support multiple inheritances because we cannot use dissimilar methods in one class it creates an ambiguity.
- Are constructors inherited? Can a subclass call the parent’s class constructor?
We can’t inherit a constructor. We make an event of a subclass using a constructor of one of its superclass. Since override the superclass constructor isn’t our want so that, we override a superclass constructor, by then we wreck the epitome limits of the language.
- Name the methods of Object Class?
- clone() – It helps to create and return a copy of the object.
- finalize() – Called by the garbage collector on an object
- equals() – It is used for comparison
- getClass() – It helps to return the runtime class of an object.
- toString() – It helps to return a string representation of the object.
- hashCode() – It helps to return a hash code value for the object.
- notify(), notifyAll(), and wait() – It helps to coordinate the activities of independently running threads in a program.
- Can we import same package/class two times? Will the JVM load the package twice at runtime?
A package or class can be procured several times in a program code. JVM and compiler won’t make any issue. Other than JVM normally stacks the class inside once paying little notice to times it is called in the program.
- Define Abstract class?
A class which holds the abstract keyword in its declaration is called as abstract class. It can have abstract and non-abstract methods.
- This class can have public, private, protected or constants and default variables.
- If a class has at least one abstract method, then the class must be declared abstract.
- It needs to be extended and its method implemented. It cannot be instantiated
- Describe the Annotations.
Annotations are tag which represents metadata associated with class, methods, interface, fields, etc. It does not directly affect the operations. The additional information carried by annotations is used by java compiler and JVM.
- Distinguish between static loading and dynamic class loading?
Static loading: Classes are loaded statically with operator “new”.
Dynamic class loading: It is a method for programmatically appealing the functions of a class loader at run time.
- Explain Struts 1 Classes are not Thread Safe whereas Struts 2 classes are thread safe?
Struts 1 action is a singleton. So all threads operate on the single action object and hence make it thread-unsafe.
Struts 2 actions are not a singleton and a new action object copy are created each time a new action request is made and hence it threads safe.
- Define JAXP & JAXB?
JAXP: – Stands for Java API for XML Processing provides a common interface for creating and using SAX, DOM and XSLT APIs in Java irrespective of which vendor’s implementation is actually being used.
JAXB: – Stands for Java API for XML Binding describes a system for a script out Java objects as XML and for creating Java objects from XML structures.
- Define an enumeration?
Enumeration is usually known as an enum. It is an interface containing methods for retrieving the original data structure from which the enumeration is achieved. It allows sequential access to all the elements stored in the collection.
- How can we find the actual size of an object on the heap?
In Java, we can’t find out the actual size of an object on the heap.
- Which API is provided by Java for operations on a set of objects?
Java provides a Collection API which provides various useful methods which can be functional to a set of objects. Some of the significant classes provided by Collection API include HashMap, ArrayList, TreeSet, and TreeMap.
- Why we used Vector class?
The Vector class provides the aptitude to execute an array of objects. It is very useful if you don’t know the size of the array, or we need one that can change sizes over the lifetime of a program.
- What is the difference between Process and Thread?
A process is a free execution environment and it can be seen as a program or application while Thread is a single task of execution inside the methodology. Java runtime environment continues running as a single strategy which contains different classes and ventures as strategies. Thread can be called lightweight process. Thread requires less resource for make and exists at the same time, thread shares the strategy resources.
- What are the benefits of multi-threaded programming?
In Multi-Threaded programming, multiple threads are performing concurrently that increases the performance because CPU is not idle in case some thread is waiting to get some resources. Multiple threads share the heap memory, so it’s good to create multiple threads to perform some task rather than making multiple processes.
- What is difference between user Thread and daemon Thread?
When we create a Thread in java program, it’s known as user thread. A daemon thread runs in background and doesn’t prevent JVM from terminating. When there are no user threads running, JVM shutdown the program and quits. A child thread created from daemon thread is also a daemon thread.
- What are different states in lifecycle of Thread?
When we create a Thread in java program, its state is New. Then we start the thread that change it’s state to Runnable. Thread Scheduler is responsible to allocate CPU to threads in Runnable thread pool and change their state to Running. Other Thread states are Waiting, Blocked and Dead. Read this post to learn more about life cycle of thread.
- What are the advantages of java package?
Packages are used as a piece of Java in-order to check naming conflicts, to make chasing and utilization of classes, to control access, interfaces, details and annotations, etc., less complex. This also helps you organize files within your project. For example, java.io package do something related to I/O and java.net package do something to do with network and so on. If we tend to put all .java files into a single package, as the project gets bigger, then it would become a nightmare to manage all your files
- Can I import same package/class twice? Will the JVM load the package twice at runtime?
You can import the same package or same class several times. Neither compiler nor JVM cries anything about it. Likewise, the JVM will inside load the class just once paying little respect to how regularly you import a comparative class.
- Explain the usage of Java packages.
A Java package is a naming framework for classes and interfaces. It is used to influence an alternate name space for groups of classes and interfaces. Packages in like manner used to form related classes and interfaces into a singular API unit and to control receptiveness to these classes and interfaces.
- Difference between Class#getInstance() and new operator?
|It doesn’t call the constructor||Need to have a matching constructor or compiler should provide a default constructor.|
- What is the difference between >> and >>>?
Both bitwise right shift operator (>>) and bitwise zero fill right shift operator (>>>) are used to shift the bits towards right.
The difference is:
|Right shift operator (>>)||Zero fill right shift operator (>>>)|
|Protect the sign bit||It will not protect the sign bit. It always fills 0 in the sign bit.|