suraj k sahu


{ City } pune
< Country > india
* Profession * tl
User No # 111686
Total Questions Posted # 30
Total Answers Posted # 42

Total Answers Posted for My Questions # 47
Total Views for My Questions # 91921

Users Marked my Answers as Correct # 177
Users Marked my Answers as Wrong # 9
Answers / { suraj k sahu }

Question { Wipro, 2440 }

How does static modifier work?


Answer

Static Variable:
• The variables are class variables.
• These come into existence as class in loaded into JVM.
• The variable remains live as long as class is exists in JVM or garbage collected.
• The variable’s state is common for all instances of the belonging class.
• If any instance of belonging to class modify the value of static variable then the modified value reflects for all.
• Static variables are only accessible in static method only.
• As a good practice, it should be referenced by Class name not by instance.

Static Method:
• The methods are class methods.
• The method is equally defined for all instances the class.
• It is good practice, the static method should be referenced by its class name instead of instance variables.
• This method can only access static variables but not the instance variables.
• Static method will be overloaded but won’t be overridden.
• As there is a common functionality that will be used for a set of other classes in an application, then we have to keep the functionality in static method.
• Mostly factory pattern and singleton pattern use static method.
• Utility classes use static methods

Static Class
• Only a nested class can be static class, top-level class can’t be static.
• A static nested class can only access static members of top-level class.
• A static nested does not require an instance of top-level class to be instantiated.
<>.<> newClass = new <>.<>();
• It is completely separate class that has no link with instance of top-level class.

Static block
• Static block is used to initialize static variables of a class.
• Static block is loaded at once at the time of class loading.
• This block may present anywhere in java source but it is executed before constructor at run time.
• If there is multiple static blocks, then it is executed in a sequence as it is found in source file.

Is This Answer Correct ?    4 Yes 0 No

Question { Wipro, 2232 }

How does abstract modifier work?


Answer

Abstract means no concrete, it isn’t directly used. It depends on an implementation to be used. The abstract key word can be used before class (top-level/inner) or method.

Abstract class
• If we make a class abstract, it is not directly instantiated. It have to have a child class to be used.
• It is only be used through inheritance.
• The legacy or common functionality are kept in abstract class.
• It is also used for up casting and dynamic polymorphism.
• It is mostly used in factory design pattern.
• An abstract class can implement and extends another interface and class.

Abstract method
• An abstract doesn’t have any body.
• It has to have an override non-abstract method.
• If a class has an abstract method, then the class has to be abstract.
• An abstract method can only be set visibility modifier either public or protected.

Abstract inner class
• An inner class can be abstract but it is not commonly used and is not recommended also.
• A tight coupling utility is implemented as inner class but if the utility is too complex and we need to segregate into different inner subclasses, then we should go for abstract inner class. The scenario is very rare and it may not come in web applications but may come very big swing application.

Is This Answer Correct ?    4 Yes 0 No


Question { IBM, 2344 }

How does synchronized modifier work?


Answer

Synchronized is used to make a resource thread safe. If an object or resources is accessed by more than one thread, then these should be under synchronization. Only method and block can be synchronized.

Synchronized method
• As an instance method is synchronized then, the object is locked for a period of time as a thread is accessing the method.
• As a class method (static) is synchronized then, whole class is locked for a period of time as a thread is accessing the class method.

Synchronized block
• A particular set of statements (lines of codes) inside method or block is synchronized with synchronized block.
• An object reference is passed inside synchronized block to make a particular set of statements inside instance method or block.

synchronized (this){
}
• If a particular set of synchronized statements inside instance method or block is accessed by a thread then whole object is locked.
• A class reference is passed inside synchronized block to make a particular set of statements inside static method or block.

Synchronized (Class.class){
}

Is This Answer Correct ?    4 Yes 0 No

Question { Tech Mahindra, 2670 }

How does final modifier work?


Answer

Final in java is used to restrict user to change further. It is used in various context as below

Final variable
• As variable become final, its value will never be modified.
• The variables become read only variable forever.
• If final variable is static then it must be initialized at the time of declaration.
• A non-static final variable can be blank but must be initialized in constructor.
• A local variable can be final.
• Final variables usually come with static key word as constant.

Final method
• As method become final, it never be override but be overload.
• A final method can be static and synchronized but never be abstract.
• As we are sure that the method implementation is complete and will never be override then we should make it final.

Final class
• A final class is complete class and will never be sub-classed.
• Final class helps to define immutable class.
• There are some final classes like String, Integer and other wrapper classes.

Is This Answer Correct ?    5 Yes 0 No

Question { Infosys, 2610 }

What are legal modifiers that we can use to declare an inner class?


Answer

public, protected, private, abstract, static and final

Is This Answer Correct ?    3 Yes 0 No

Question { IBM, 2414 }

What is difference between “==” and equals()?


Answer

“==”: It compares references (memory locations). It does not compares values in the memory location. Hence it is not recommended to find the equality of two objects. It is mostly used to compare primitive types.

equals(): It is used to find the equality of two objects. It actually compares with the values that an object contains.

Is This Answer Correct ?    5 Yes 0 No

Question { Cognizant, 2584 }

Why do we need to override equals() and hascode() method of object class?


Answer

As per equality contract of Java if two objects are equal then they should return equal integer, means if obj1.equals(obj2) then obj1.hashCode() == obj2.hashCode();

As we override equals method we compares equality on value of each property inside 1st object with 2nd object, as it finds all properties are equal then returns true else false.

As we override hascode method we generate a unique integer by multiplying with prime number. If we multiply with prime number then there is most possibility to get unique integer. The prime number 31 is mostly used as the hascode method is override.

Is This Answer Correct ?    2 Yes 0 No

Question { Wipro, 2437 }

How to override equals() and hashCode() method in java?


Answer

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}

Employee emp = (Employee) obj;
return id == emp.id
&& (firstName == emp.firstName
|| (firstName != null && firstName.equals(emp.getFirstName())))
&& (lastName == emp.lastName || (lastName != null && lastName .equals(emp.getLastName())));

}// equals method ends

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 :frstName.hashCode());
result = prime * result + id;
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;

}// hashCode method ends

Is This Answer Correct ?    0 Yes 0 No

Question { Avaya, 2825 }

Are the equals() and hashCode() protected methods of object class?


Answer

No, the equals() and hashCode() are public methods of object class.

Is This Answer Correct ?    8 Yes 0 No

Question { HCL, 2986 }

What are the uses of final, finally and finalize in java?


Answer

final: It is used to stop modifying further. It is used before class (top-level/inner), variable (class/instance) and method.
If class become final, it never be sub-classed.
If variable become final, it never be modified through the program or application.
If method become final, it never be override.

finally: It is used to release resources with try or try-catch block. It is always executed block irrespective of throw or return statement. It can be only stopped with infinite loop or System.exit(0). Previously file handling code used finally block to close InputStream or OutputStream object. Now finally block is not used to close these object. They are implicitly auto closed as they are implementing AutoClosable interface.

finalize(): As we override the protected finalize method to release resources. It is the final step where we can release resource (means either nullify the object or close the stream objects etc.). It is called before GC .But issue is the below points,
•There is no guarantee that it will be called or if called the resource will be released by GC immediately.
•We should not completely rely on this way of releasing memory.
•We can urge JVM to execute our finalize method with below statements but it has no guarantee that the objects will be freed immediately by GC.
System.runFinalization() OR Runtime.getRuntime().runFinalization()

Is This Answer Correct ?    5 Yes 0 No

Question { Syntel, 3330 }

Describe OOP in java?


Answer

OOP means object oriented programming, where objects play active role to fulfill request of the user. As per the OOP concept, objects have the below 4 features

Encapsulation: Objects have the ability to hide its state (properties) and behavior (methods) with the use of access modifier.

Inheritance: It is a technique to define child class / interface by extending parent class / interface. As a class implements interface is also comes under inheritance. It helps to override legacy method or abstract method with new functionality. It also facilitates the child class to access protected members of its parent.

Polymorphism: It means to overload or override the behaviors (method) of a class. There are two types of polymorphism
• Static / Compile time: To overload a method with in same class in called static or compile time polymorphism. As we call overloaded method, compiler knows which overloaded method will be called.
Example
1.Class A has two method suppose sum(int n1, int n2), sum(long n1, long n2)
2.You created instance of class A inside main method and statically called either as below
A a = new A();
a.sum1(2, 3) or a.sum(2l, 3l)
3.Hence compiler knew which overloaded method called.
• Dynamic / Run time: To override a method using inheritance is called dynamic or runtime polymorphism. Override method execution is decided at run time hence JVM can decide which override method should be called.
Example
1.Class A has a method sum() and do addition of two integers.
2.Class B is child class of A and override sum() method and do addition of two float numbers.
3.There is an another class C which has main method and inside main method we have below code snippet
A a = new B();
a.sum();
4.Now compiler knows it is method of class A will be called but at runtime the override method of class B will be called, as instance of class A will be bound with the reference of class B at runtime dynamically.
5.Hence Overriding is called dynamic or runtime polymorphism.

Is This Answer Correct ?    1 Yes 0 No

Question { Syntel, 3330 }

Describe OOP in java?


Answer

OOP means object oriented programming, where objects play active role to fulfill request of the user. As per the OOP concept, objects have the below 4 features

Encapsulation: Objects have the ability to hide its state (properties) and behavior (methods) with the use of access modifier.

Inheritance: It is a technique to define child class / interface by extending parent class / interface. As a class implements interface is also comes under inheritance. It helps to override legacy method or abstract method with new functionality. It also facilitates the child class to access protected members of its parent.

Polymorphism: It means to overload or override the behaviors (method) of a class. There are two types of polymorphism
• Static / Compile time: To overload a method with in same class in called static or compile time polymorphism. As we call overloaded method, compiler knows which overloaded method will be called.
Example
1. Class A has two method suppose sum(int n1, int n2), sum(long n1, long n2)
2. You created instance of class A inside main method and statically called either as below
A a = new A();
a.sum1(2, 3) or a.sum(2l, 3l)
3. Hence compiler knew which overloaded method called.
• Dynamic / Run time: To override a method using inheritance is called dynamic or runtime polymorphism. Override method execution is decided at run time hence JVM can decide which override method should be called.
Example
1. Class A has a method sum() and do addition of two integers.
2. Class B is child class of A and override sum() method and do addition of two float numbers.
3. There is an another class C which has main method and inside main method we have below code snippet
A a = new B();
a.sum();
4. Now compiler knows it is method of class A will be called but at runtime the override method of class B will be called, as instance of class A will be bound with the reference of class B at runtime dynamically.
5. Hence Overriding is called dynamic or runtime polymorphism.
Abstraction: Abstraction means to hide the complexity to the client. To make a class or method abstract is also a part of abstraction. The legacy and older implementations are kept in abstract class and we hide that legacy system to the client using abstraction. Suppose client is using override functionality then knowing about legacy system is of no use for client. Use of interface is also part of abstraction. If we have a fully functional DAO methods in DAO layer and we want to hide the method implementation to Service layer, then we have to use interface to provide only access of method to service layer. This way we create abstraction between DAO and service layer.

Is This Answer Correct ?    1 Yes 0 No

Question { Accenture, 3989 }

What is difference between overloading and overriding?


Answer

Overloading: As more than one methods are defined with in same class with same name but have any one of the below differences is called method overloading. It is also call static / compile time polymorphism.
1. Input parameter data type should be different.
2. Number of input parameters should be different.
There are two types of overloading as below
1. Constructor overloading.
2. Method overloading (instance / class)
Note: Overloading is not considered on below things
1. Return type
2. Access modifier
3. Sequence of input parameters.
4. Different exceptions

Overriding: As a method in parent class is defined with exact same signature in one of its child class is call method overriding. It is called dynamic or runtime polymorphism. Method overriding is still considered even if the override method in child class is differ as below from its parent method.
1. The visibility can be broader but can’t be narrower.
2. The exception can be specific but can’t be generic.
3. The return type can be narrower but can’t be broader.

Is This Answer Correct ?    6 Yes 0 No

Question { Accenture, 3989 }

What is difference between overloading and overriding?


Answer

Overloading: As more than one methods are defined with in same class with same name but have any one of the below differences is called method overloading. It is also call static / compile time polymorphism.
1. Input parameter data type should be different.
2. Number of input parameters should be different.
There are two types of overloading as below
1. Constructor overloading.
2. Method overloading (instance / class)
Note: Overloading is not considered on below things
1. Return type (different type)
2. Access modifier ( broader / narrow)
3. Sequence of input parameters.
4. Different exceptions (new / broader)

Overriding: As a method in parent class is defined with exact same signature in one of its child class is call method overriding. It is called dynamic or runtime polymorphism. We have to consider below things while method overriding.
1. The visibility can be broader but can’t be narrower.
2. The exception can be specific but can’t be generic for checked exception.
3. The override method can throw any or new unchecked exception.
4. The return type can be narrower but can’t be broader.
5. The override method can’t be static / final / private.

Is This Answer Correct ?    2 Yes 0 No

Question { Virtusa, 3187 }

Why do we need main method to execute a java program?


Answer

Main method is auto called by JVM. It is the place, where JVM enters into the class. It is called before the class is instantiated by its constructor. There are below reasons on its signature.
1. Public access modifier: it is visible to outer environment called JVM.
2. Static non-access modifier: it is called before the class is instantiated, hence JVM use class name to call the main method.
3. Void return type: It return no value to JVM.
4. String array parameter: It takes command line arguments.

Is This Answer Correct ?    11 Yes 1 No

Prev    1    [2]   3    Next