Edit
Reply




Edit

  1. Home
  2. Articles

Most Commonly Asked Java Interview Questions – Set 3

BOOKMARK

Java has become an integral part of our life. Everything we use, right from the household devices like DTH, to big technologies uses Java. Hence, it is not surprising that the candidate’s Java knowledge in placement interviews. To help you crack this placement interview with ease, we have already discussed the set 1 and set 2 of the most commonly asked Java interview questions. In this article, we will discuss the next set f questions.

Most Commonly Asked Java Interview Questions – Set 3

What are Wrapper classes?

Java has 8 primitive data types as boolean, byte, char, int, float, double, long, and short. Each of these data types has a class dedicated to it. As these classes ‘wrap’ the primitive data type into the object of that class, they are called as Wrapper Classes.  The functions of wrapper classes in Java are as follows:

To provide a mechanism to ‘wrap’ primitive values in an object so that primitives can do activities (like being added to ArrayList, Hashset, HashMap, etc) reserved for the objects.

To provide an assortment of utility functions for primitives like converting primitive types to and from string objects, converting to various bases like binary, octal or hexadecimal, or comparing various objects.

The primitive data types, their wrapper classes, and constructor arguments are given below:

 What is a Singleton class and how to make a class Singleton?

An object cannot be created outside a class. This property is useful in creating a Singleton class. Singleton class is the class whose one instance of a class (one object) can be created at a given time. By making its constructor private, a class can be made Singleton.

What are the differences between .equals() and == ?

Both .equals() and == are used to compare the objects and check their equality in Java. But, the difference is that, while the .equals() method does the content comparison, == operator, being the binary operator,  does address comparison.

For example:

public class Equaltest {
public static void main(String[] args) {
String abc1= new String(“ABCD”);
String abc2= new String(“ABCD”);
if(abc1 == abc2)
{
System.out.println("String 1 == String 2 is true");
}
else
{
System.out.println("String 1 == String 2 is false");
String abc3 = abc2;
if( abc2 == abc3)
{
System.out.println("String 2 == String 3 is true");
}
else
{
System.out.println("String 2 == String 3 is false");
}
if(abc1.equals(abc2))
{
System.out.println("String 1 equals string 2 is true");
}
else
{
System.out.println("String 1 equals string 2 is false");
}
}}

What are Stack and Heap memories?

Whenever an object is created, it's always stored in the Heap memory. Heap values exist on the heap. They are created at some point in time, and destructed at another either by the Garbage Collector or manually depending on the runtime.

Stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space. Stack values only exist within the scope of the function they are created in. Once it returns, they are discarded.

What is the difference between Heap and Stack memory?

Memory:
Stack memory is used only by one thread of execution whereas the heap memory is used by all the parts of the application.

Access:
Stack memory can’t be accessed by other threads while the objects stored in the heap are globally accessible.

Memory Management:
Stack Memory follows LIFO (Last In First Out) manner to free memory while the memory management in heap memory is based on generation associated with each object.

Lifetime:
The stack memory exists until the end of execution of the thread. But, the 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.

Lifecycle of JSP

There are 3 steps in the JSP lifecycle:

public void jspInit() will be invoked only once, same as with init method of the servlet. This initializes the database connections, open files, and create lookup tables.

public void _jspService(ServletRequest request,ServletResponse) throws ServletException, IOException will be invoked at each request, same as with service() method of the servlet. This is responsible for generating the response to the request and this method is also responsible for generating responses to all seven of the HTTP methods, i.e, GET, POST, DELETE, etc.

 public void jspDestroy() will be invoked only once, same as with destroy() method of the servlet. Overriding the jspDestroy will perform any cleanup tasks like releasing database connections or closing open files.

What are the scopes of the member variable, local variable and loop variable in java?

Member Variables: They provide Class Level Scope. The member variables must be declared inside the class (outside any function) and they can be directly accessed anywhere in the class.

Local Variables: They provide Method Level Scope.  Only the variables declared inside a method have method level scope and they cannot be accessed outside the method.

Loop Variables: They provide Block Scope. A variable declared inside a pair of brackets, "{” and “}”, in a method has scope within the brackets only.

Explain ‘this’ keyword

‘this’ is a keyword in Java which can be used within a method or a constructor of a class. It is a reference variable that refers to the current object. ‘this’ keyword has 6 usages in Java. They are as follows:

this can be used to refer current class instance variable.
this can be used to invoke current class method implicitly.
this() can be used to invoke the current class constructor.
this can be passed as an argument in the method call.
this can be passed as an argument in the constructor call.
this can be used to return the current class instance from the method.

For all placement-related information, stay in touch with FACE Prep.


3 Comments
×

Praveen Kumar Panigrahi
if the variable is instance or non static we can create object otherwise it will get compile time error

Thank ()
Reply
1 year ago


jaydeep pandya
another doubt is when we create object or instance of class

Thank ()
Reply
1 year ago


jaydeep pandya
can we create instance of singleton class outside class?

Thank ()
Reply
1 year ago

Open in App