Wednesday 25 April 2018

Advanced Java Interview Question

1. Explain JVM, JRE and JDK?

JVM (Java Virtual Machine): It is an abstract machine. It is a specification that provides run-time environment in which java bytecode can be executed. It follows three notations:
  • Specification: It is a document that describes the implementation of the Java virtual machine. It is provided by Sun and other companies.
  • Implementation: It is a program that meets the requirements of JVM specification.
  • Runtime Instance: An instance of JVM is created whenever you write a java command on the command prompt and run the class.
JRE (Java Runtime Environment) : JRE refers to a runtime environment in which java bytecode can be executed. It implements the JVM (Java Virtual Machine) and provides all the class libraries and other support files that JVM uses at runtime. So JRE is a software package that contains what is required to run a Java program. Basically, it’s an implementation of the JVM which physically exists.
JDK(Java Development Kit) : It is the tool necessary to compile, document and package Java programs. The JDK completely includes JRE which contains tools for Java programmers. The Java Development Kit is provided free of charge. Along with JRE, it includes an interpreter/loader, a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development. In short, it contains JRE + development tools.

2. Explain public static void main(String args[]).

public : Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.

static : It is a keyword in java which identifies it is class based i.e it can be accessed without creating the instance of a Class.
void : It is the return type of the method. Void defines the method which will not return any value.
main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
String args[] : It is the parameter passed to the main method.

3. Why Java is platform independent?

Platform independent practically means “write once run anywhere”. Java is called so because of its byte codes which can run on any system irrespective of its underlying operating system.

4. Why java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive datatypes such as boolean, byte, char, int, float, double, long, short which are not objects.

5. What are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.
There are two types of constructors:
  1. Default constructor
  2. Parameterized constructor

6. What is singleton class and how can we make a class singleton?

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

7. What is the difference between Array list and vector?

Array List
Vector
Array List is not synchronized.
Vector is synchronized.
Array List is fast as it’s non-synchronized.
Vector is slow as it is thread safe.
If an element is inserted into the Array List, it increases its Array size by 50%.
Vector defaults to doubling size of its array.
Array List does not define the increment size.
Vector defines the increment size.
Array List can only use Iterator for traversing an Array List.
Except Hashtable, Vector is the only other class which uses both Enumeration and Iterator.

8. What is the difference between equals() and == ?

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.
“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Equaltest {
public static void main(String[] args) {
String str1= new String(“ABCD”);
String str2= new String(“ABCD”);
if(Str1 == str2)
{
System.out.println("String 1 == String 2 is true");
}
else
{
System.out.println("String 1 == String 2 is false");
String Str3 = Str2;
if( Str2 == Str3)
{
System.out.println("String 2 == String 3 is true");
}
else
{
System.out.println("String 2 == String 3 is false");
}
if(Str1.equals(str2))
{
System.out.println("String 1 equals string 2 is true");
}
else
{
System.out.prinltn("String 1 equals string 2 is false");
}
}}

9. What are the differences between Heap and Stack Memory?

The major difference between Heap and Stack memory are:
Features
Stack
Heap
Memory
Stack memory is used only by one thread of execution.
Heap memory is used by all the parts of the application.
Access
Stack memory can’t be accessed by other threads.
Objects stored in the heap are globally accessible.
Memory Management
Follows LIFO manner to free memory.
Memory management is based on generation associated to each object.
Lifetime
Exists until the end of execution of the thread.
Heap memory lives from the start till the end of application execution.
Usage
Stack memory only contains local primitive and reference variables to objects in heap space.
Whenever an object is created, it’s always stored in the Heap space.
10. What is association?

Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationship can be one to one, One to many, many to one and many to many.

11. What do you mean by aggregation?

Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy.

12. What is composition in Java?

Composition is again specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.
13. What are the differences between Get and Post methods?

Get
Post
Limited amount of data can be sent because data is sent in header.
Large amount of data can be sent because data is sent in body.
Not Secured because data is exposed in URL bar.
Secured because data is not exposed in URL bar.
Can be bookmarked
Cannot be bookmarked
Idempotent
Non-Idempotent
It is more efficient and used than Post
It is less efficient and used

14. What are the differences between forward() method and sendRedirect() methods?


Forward() method
SendRedirect() method
forward() sends the same request to another resource.
sendRedirect() method sends new request always because it uses the URL bar of the browser.
forward() method works at server side.
sendRedirect() method works at client side.
forward() method works within the server only.
sendRedirect() method works within and outside the server.

15. How does cookies work in Servlets?



  • Cookies are text data sent by server to the client and it gets saved at the client local machine.
  • Servlet API provides cookies support through javax.servlet.http.Cookie class that implements Serializable and Cloneable interfaces.
  • HttpServletRequest getCookies() method is provided to get the array of Cookies from request, since there is no point of adding Cookie to request, there are no methods to set or add cookie to request.
  • Similarly HttpServletResponse addCookie(Cookie c) method is provided to attach cookie in response header, there are no getter methods for cookie.
     

More about Advance JAVA:

1 comment:

  1. When I am confused about choosing my career in PPC, your blog on Best PPC course in delhi gives me clarity on how to start a career in PPC.

    ReplyDelete