Miscellaneous
Interview Questions
1. What are the advantages and disadvantages of object cloning?
Advantage of Object Cloning
- You don’t need to write lengthy and repetitive codes. Just use an abstract class with a 4- or 5-line long clone() method.
- It is the easiest and most efficient way of copying objects, especially if we are applying it to an already developed or an old project. Just define a parent class, implement Cloneable in it, provide the definition of the clone() method and the task will be done.
- Clone() is the fastest way to copy the array.
Disadvantage of Object Cloning
- To use the Object.clone() method, we have to change many syntaxes to our code, like implementing a Cloneable interface, defining the clone() method and handling
CloneNotSupportedException
, and finally, callingObject.clone()
, etc. - We have to implement the Cloneable interface while it does not have any methods in it. We have to use it to tell the JVM that we can perform a clone() on our object.
- Object.clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it.
- Object.clone() does not invoke any constructor, so we do not have any control over object construction.
- If you want to write a clone method in a child class, then all of its superclasses should define the clone() method in them or inherit it from another parent class. Otherwise, the super.clone() chain will fail.
- Object.clone() supports only shallow copying, but we will need to override it if we need deep cloning.
2.What is a native method?
- A native method is a method that is implemented in a language other than Java.
- Natives methods are sometimes also referred to as foreign methods.
3.What is the purpose of the System class?
-
The purpose of the System class is to provide access to system resources such as standard input and output.
-
It cannot be instantiated. Facilities provided by System class are given below.
- Standard input
- Error output streams
- Standard output
- utility method to copy the portion of an array
- utilities to load files and libraries
There are the three fields of Java System class, i.e., static printstream err, static inputstream in, and standard output stream.
4.What comes to mind when someone mentions a shallow copy in Java?
Object cloning.
5.What is a singleton class?
- Singleton class is the class which can not be instantiated more than once.
- To make a class singleton, we either make its constructor private or use the static getInstance method.
public class Singleton {
private static Singleton instance = null;
int i;
private Singleton() {
i= 90;
}
public static Singleton getInstance(){
if(instance ==null){
instance=new Singleton();
}
return instance;
}
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
System.out.println("First instance integer value:"+s1.i);
s1.i += 90;
Singleton s2 = Singleton.getInstance();
System.out.println("Second instance integer value:"+s2.i);
}
}
First instance integer value:90
First instance integer value:180
6.What is an applet?
-
An applet is a small java program that runs inside the browser and generates dynamic content.
-
It is embedded in the webpage and runs on the client side. It is secured and takes less response time.
-
It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os, etc. However, the plugins are required at the client browser to execute the applet. The following image shows the architecture of Applet.
When an applet is created, the following methods are invoked in order.- init()
- start()
- paint()
When an applet is destroyed, the following functions are invoked in order.
- stop()
- destroy()
Java Bean
Interview Questions
7.What is a JavaBean?
JavaBean
is a reusable software component written in the Java programming language, designed to be manipulated visually by a software development environment, like JBuilder or VisualAge for Java. t.- A JavaBean encapsulates many objects into one object so that we can access this object from multiple places. Moreover, it provides the easy maintenance.
//Employee.java
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
8.What is the purpose of using the Java bea
- it is a reusable software component.
- A bean encapsulates many objects into one object so that we can access this object from multiple places.
- it provides the easy maintenance.
9.What do you understand by the bean persistent property?
The persistence property of Java bean comes into the act when the properties, fields, and state information are saved to or retrieve from the storage.
Java
Multithreading
andConcurrency
Interview Questions
10. What is multithreading?
-
Multithreading is a process of executing multiple threads simultaneously.
-
Multithreading is used to obtain the multitasking.
-
It consumes less memory and gives the fast and efficient performance. Its main advantages are:
- Threads share the same address space.
- The thread is lightweight.
- The cost of communication between the processes is low.