Wednesday, April 24, 2019

Java Interview

Q- What ate the features in Java Programming Language?
Answer:
  • Object-Oriented
  • Simple
  • Portable
  • Secured
  • Platform Independent
  • Robust
  • Interpreted
  • Architecture Neutral
  • Distributed
  • High Performance
  • Multithreaded
  • Dynamic
Q- What is the difference between JDK, JRE, and JVM?
Answer: 

Q- What is the difference between 32-bit java vs. 64-bit java OR What is the difference 32-bit and 64-bit jdk?

Q- What is the difference between encapsulation and abstraction in java?

Q- What is the difference between overloading and overriding in java?

Q- What is SOLID principle in java?

  • S:- Single Responsibility Principle: This principal says the one class or module should have single responsibility for example: Class User should have responsibility to manage user only.  
  • O:- Open Closed Principle: This principal says that software entities (classes, modules, functions, etc.) should be open for extension but close for modification.
  • L:- Liskov's Substitution Principle: As per the principle written by Barbara Liskov in 1988. Liskov Substitution Principle (LSP) says that, functions or method that uses references to base classes must be able to use objects of the derived i.e. parent class without knowing it. In other words we can say, derived classes must be substitutable for the base class. For example if we create a interface "MyInterface" and there are two implementation this interface are Class  "MyInterfaceImplFirst" and Class  "MyInterfaceImplSecond" and both Class (Implementation of interface) should be implements all the methods of  "MyInterface" .So that we can use any class where we required using interface. Click here to see in more details
  • I:- Interface Segregation Principle: Interface should not be very large that means interface should be small as per requirement. 
  • D:- Dependency Inversion Principle: As per dependency inversion principal. class or modules should be loosely coupling.  High-level class or modules should not depend on low-level class or modules and both should depend on abstraction. Details or concrete implementation should be just depends on abstraction rather than an abstraction depends on details.

Q- Coupling Vs Cohesion In Java?
Answer: Coupling Vs Cohesion In Java
Good software design has high cohesion and low coupling.
Q- What does it mean to refactor code? OR What is refactoring in Agile?
  • Code refactoring is the process of changing (clarifying and simplifying the design of existing code) a software system’s  internal structure in such a way that it does not alter the external behavior of the code( without modifying its external functional behavior or existing functionality) in order to improve internal non-functional properties of the software, for example to improve code readability, to simplify code structure, to change code to 
  • Agile teams are maintaining and extending their code a lot from iteration to iteration, and without continuous refactoring, this is hard to do.
  • Q- Why do we refactor our codes?
  • Refactoring is intended to improve nonfunctional attributes of the software. By continuously improving the design of code, we make it easier and easier to work with.
Q- What is serialization in java?

Q- What are the methods in serializable interface?
Answer : Serializable is a marker interface so it has no method. while we implements serializable interface in a class. This is used to indicate (informs or mark) to JVM that this java class can be serialized.

Q- What is marker interface in java?
Answer : Marker Interface is an empty interface (with no fields or methods) . It is used to tell the JVM that the class implementing an interface have some special behavior.
Example :- Serializable,Clonable.
  • Serializable :- To mark class can be serialized. 
  • Clonable :- To mark class can be cloned.
Q-  How to create marker interface in java?
Answer :-

Q- What is externalizable interface in java?

  • The Externalizable Interface is also used to serialize a class.
  • Externalizable interface extends java.io.Serializable interface.
  • This is use while we want to customize the default serialization behavior of JVM.
  • Externalizable interface have two methods writeExternal(), readExternal() methods. We must override the writeExternal(), readExternal() methods in our implementation class.

Q- How to serialize static variables in java?

Q- What is the default behavior of equals() method if not override?

by default equal method check references like "==". If  not override.
If  not override, then two class Object would be naver same.

Q- What is equals() and hashcode() contract in java?

hashCode() and equals() methods both are defined in Object class. which is parent class for all java objects. So that all java objects inherit a default implementation of these methods.
We can also use Apache Commons package’s utility classes HashCodeBuilder and EqualsBuilder. To correctly override.
Contract :
In hashCode() it says: If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. If you only override equals() and not hashCode() your class violates this contract.

That means  a.equals(b)  return true then a.hashCode() == b.hashCode() should be same

If two objects have the same hashcode then they are NOT necessarily equal by  equals(Object) method.
If a.hashCode() == b.hashCode()  then  a.equals(b)  return true NOT necessarily equal.

Method defined in object class like below mentioned.
public boolean equals(Object obj)
public int hashCode().

Q- How to Override equals() Method in Java?
There are 5 Rules for overriding equal method
  1. Reflexive: a.equals(a) == true , an object must equal to itself.
  2. Symmetry: if(a.equals(b)==true) then b.equals(a) == true.
  3. Transitive: if a.equals(b) and b.equals(c); then a.equals(c)
  4. Consistent: if a.equals(b)==true and no value is modified, then it’s always true for every call
  5. For any non-null object a, a.equals(null)==false
Q- String Vs StringBuilder Vs StringBuilder in java? 

String is immutable in java. ( Immutable Class In Java )
String concat and "+" operator internally uses StringBuffer or StringBuilder class.
StringBuffer and StringBuider are mutable classes.
StringBuffer is thread safe and synchronized
StringBuilder is not, thats why StringBuilder is more faster than StringBuffer.

Q- Is StringBuffer and StringBuilder class has equal() methods?

Answer: No.
StringBuffer and StringBuilder class don't have the implementation(don't not override) of equals() method of Object class. like one in the String class. That mean StringBuffer & StringBuilder  class don't have equal() methods.

Q- How to compare two instance of  StringBuffer or StringBuilder objects in java?

package com.javaiq.in.stringbuilder;

public class StringBuilderExample {

 public static void main(String[] args) {
  StringBuilder a = new StringBuilder("HELLO JAVA");
  StringBuilder b = new StringBuilder("HELLO JAVA");

  /*
   * Case-1 : Here this use equal method of object class. by default equal
   * method check for reference
   */
  if (a.equals(b)) {
   System.out.println("Output case-1 : Objects are equal");
  } else {
   System.out.println("Output case-1 : Objects are not equal");
  }

  /*
   * Case-2 : Here its use equal method of String class. its check for
   * content
   */
  if (a.toString().equals(b.toString())) {
   System.out.println("Output case-2 : Objects are equal");
  }
 }
}
Output case-1 : Objects are not equal
Output case-2 : Objects are equal

Q- What is arrays class in java?

Q- Is java pass by value or pass by reference?
Answer:  Java is Pass by Value.

Java is Pass by Value and Not Pass by Reference. then how the pass by value work in java.
Pass by Value: The parameter value of a method  are copied to another variable (second variable) and then the copied object is passed, that’s why it’s called pass by value.
And these two different reference are point to same memory location that's why when we change the value of different variable (reference variable) does the change in first variable.



Q- What is the mean of Pass by Reference in java?
Answer:- 

Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.


Related Tutorial

No comments:

Post a Comment