fbpx
Select Page

Java Interview Questions

Sr No.Questions and Answers
Que.1How we can arrange heterogeneous data inside Array?
Ans.1We can arrange heterogeneous data inside array by declaring array of Object (class) type.

 

Explanation:

Ex.

We have two classes named as Student and Employee

So arrange objects of both classes in single array we will declare array of Object type as given below:

Object array[]=new Object[size];

so , if we write:

array[0]=new Student();

array[1]=new Employee();

This will not generate any error.

Que.2What is the difference between Collection and Collection Framework?
Ans.2 

 

CollectionCollection Framework
Collection represent the group of objects in a single entity.

 

Ex. Group of chairs, group of bottles… any object’s group is a Collection

Collection Framework defines the classes and interfaces to manage the collection.
Que.3What are the reasons behind making interface methods public and abstract?
Ans.3●     Methods of interface are public because they can be accessed by anyone, like third party vendors are going to use it so maximum accessibility should be their.

 

●     Method are abstract because the implementation is left for the implementation class or the third party vendors, in this case we just need structure of method not the body content.

Que.4What are the reasons behind making interface variables public static and final?
Ans.4●     Interface variables should be accessible everywhere for public modifier is needed.

 

●     Variables are static because they can be accessible inside static as well as instance methods.

●     Variables are final because static variables share a single copy throughout all the objects. If someone change it it will affect all objects and ultimate result will have its effect. So to make it constant it has been final.

Que.5What data type we can use for Financial calculations to get accurate results?
Ans.5●     For financial calculation we do not use float or double type when we expect the exact result of calculations. Because floating point calculations may not be exact as it differs on different JVMs.

 

●     Use BigDecimal, long or int for monetary calculation.

●     Use BigDecimal constructor with String constructor

Que.6Overriding with static methods is not possible, Why?
Ans.6●     Instances of class are dependent on overriding.

 

●     Because instances are not associated with any static methods. So the scope is for that dedicated class only and the concept is not applicable.

Que.7Can we use blank final variable? If yes then how?
Ans.7●     Before object creation final variable of a class should assign any value.

 

●     And that is possible by using constructor. Because, it has been invoked automatically before object has been created.

●     This is frequently used for immutable objects.

Que.8How to store passwords?
Ans.8●     Character array is better choice then String for storing passwords,

 

➢     Strings are immutable in java if store password as plain text it will be available in memory until Garbage collector clears it and since String are used in String constant pool for reusability there is pretty high chances that it will remain in memory for long duration.

➢     Memory dump can be accessed by anyone and that is why we encrypt the password to prevent from stealing or hacking

➢     With char[] you can set all elements blank.

Que.9How to create immutable class and what are the advantages and dis-advanteges of it?
Ans.9●     Any modification must result in new Object.

 

●     All fields should be final.

●     Class must be final to restrict subclassing.

●     Prons-

➢     Thread safe, no need for synchronization (boost performance)

➢     Can reuse them, serving them from cache using static factory methods

●     Cons-

➢     New instances create a lot of work for Garbage collector.

Que.10What is the difference between Stateless Beans, Stateful Beans vs Http Session?
Ans.10●     Stateless Session Beans are not tied to one client and there is no guarantee for one client to get the same instance with each method invocation.

 

●     There is a high possibility that stateless beans will have instance variables and will not be specific for any single client.

●     HTTP is a stateless protocol which means that it is actual transport protocol the server and the client is “stateless because it remembers nothing between invocations.

●     Stateful Session Bean are dedicated to one client for their entire life, there is no swapping or pooling of instances and maintain conversational state.

●     It means while method invocation instance variables of bean keep relative data of a client.

●     And this makes possible to have interdependent method calls.

Que.11How can you differentiate between final and effectively final?
Ans.11Local variables referenced from a lambda expression/anonymous class must be either-

 

➢     Final

➢     effectively final

Variable whose value is never changed after it is initialized is effectively final.

Que.12How can you differentiate between final, finally and finalize?
Ans.12●     ‘final’ modifier can be used with variables, methods and classes as well.

 

●     If we declare a variable as final it will become constant. We can’t re-assign any value for variable.

●     If a class is declared as final we can not extend that class.

●     If a method is declared as final then we can’t override that method in child class.

➢     ‘finally’ is a block, which is always associated with try and catch blocks.

➢     We write cleanup code inside finally block. That is resource deallocation.

➔     ‘finalize()’ is a method present in Object class, which has been called by the Garbage collector to perform cleanup activity just before destroying object.

Que.13What are different modifiers available in Java?
Ans.13List of modifiers are:

 

  1. public
  2. private
  3. protected
  4. default
  5. final
  6. abstract
  7. static
  8. synchronized
  9. native
  10. strictfp
  11. transient
  12. volatile
Que.14How can you differentiate between interface, abstract class and concrete class?
Ans.14●     If we don’t know anything about implementation just we have requirement specification that 100% abstraction then we should go for interface.

 

Ex. Servlet

●     If we want implementation but not completely that is partial then we should go for abstract class.

Ex. GenericServlet

●     If we want implementation completely and ready to provide service then we should go for concrete class.

Ex. MyServlet

Que.15How can you differentiate between access specifiers and access modifiers?
Ans.15●     In old languages like C++ public, protected, private and default are the access specifiers. Apart from this everything is access modifiers.

 

●     But in java there no terminology like specifiers. All are by default considered as modifiers.

  1. public
  2. private
  3. protected
  4. default
  5. final
  6. abstract
  7. static
  8. synchronized
  9. native
  10. strictfp
  11. transient
  12. volatile
Que.16Explain System.out.println();
Ans.16●     System is a class available in java.lang package.

 

●     out is a static variable available in System class, called as PrintStream object.

●     and println() is the method present in PrintStream class.

Que.17Why we can not use ‘this’ inside static context?
Ans.17●     If we want to access any object functionality inside the static area we use object reference variable.

 

●     Every java application has two context areas i.e. static context and non-static context (context is an area where we process our code)

●     Static is free access area whereas non-static is restricted area where we should take permissions to access it.

Que.18Why abstract methods can’t be final?
Ans.18●     When we have abstract method we are not able to write its body part their because it means to be implemented inside of implementation class by overriding it.

 

●     final methods are having overriding prevention, so we can not override it.

●     If we declare method as abstract it means it needs to be overridden and if we declare it final  then are not able to override it. So both the concepts are contradictory at their places. So can not declare method as both abstract and final. This is illegal combination of modifiers.

Que.19Why abstract methods can’t be static?
Ans.19●     Static means common functionality to the object, whereas non-static is different  functionality to every object.

 

●     Ex. IFSC code for bank account holders we can consider the common functionality whereas account is different for every account holder.

●     Where abstract is specific for pertical child.

●     So functionality like common and specific we can not combine. So these both static and abstract are illegal combination modifiers.

Que.20How can you differentiate between checked and unchecked exception?
Ans.20●     Checked exception are the exceptions which are caught by the compiler and we get compiled error.

 

Ex. IOException thrown by readLine() method of BufferedReader class.

●     Unchecked exceptions are those exceptions that we get at runtime and not caught by compiler. In this case we have to apply exception handling mechanism to handle it.

Ex. Divide by zero generates ArithmeticException at runtime.

Que.21Why we can only access static variables inside static method?
Ans.21●     Instance variables are bound to objects and object creation  process is after invocation of static method.

 

●     The availability of instance variable is after invocation of static methods.

●     So only static variables are available when we invoke static variable.

●     So we can say that JVM execution flow is responsible for it.

Que.22Can we write try without catch or finally?
Ans.22●     We can write try block without catch block, but in that case finally should be there.

 

●     If we write try catch block without finally block then its is still fine.

●     But we can not write it without both block.

●     If we want to write it we have a concept in java i.e. try with resource.

Que.23start() method of Thread class can be overridden or not?
Ans.23●     Yes, we can override the start method of Thread class.

 

●     But as the functionality run() method invocation is done by start() automatically when JVM schedule it then we have again call the start() of thread class by specifying the following statement inside start() method,

➔     super.start();

Que.24Why we are not able to create object of abstract class?
Ans.24●     When a class in loaded inside JVM it needs memory estimation for object.

 

●     As we know abstract class contains incomplete methods so memory estimation for object is not done by JVM as its implementation is depends on its implementation class.

●     So without memory estimation we are not able to create object of it.

Que.25If we are not able to create object of abstract class then can we create constructor inside abstract class? And if are able to then how to pass parameters to parameterized constructor?
Ans.25●     Yes, We can create constructor in abstract class.

 

●     And we do upcasting to access concrete methods of abstract class.

●     In this case we have implemented class object and abstract class reference.

●     So according to inheritance functionality we can pass parameters to super class constructor by using the super keyword.

 

For Free Demo classes Call: 8237077325

Registration Link:Click Here!

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.






Pin It on Pinterest