Java QnA




1. What are the Object Oriented features in JAVA?
1. Class
2. Object
3. Encapsulation
4. Abstraction
5. Inheritance
6. Polymorphism


2. What Java is callled Object Oriented language.
Java is an object oriented programming language, because, JAVA is able to store data in the
form of Objects only.

What is Encapsulation?
The process of binding data and coding part is called as "Encapsulation".

What is Abstraction?
The process showing neccessary data or implementation and hiding unneccessary data or
implementation is called as "Abstraction".

Note: In Object Oriented programming languages,both encapsulation and abstraction are improviding
"Security".

What is Inheritance?
The process of getting variables and methods from one class to another class is called as "Inheritance".
The main objective of inheritance is "Code Reusability".

3. What is Polymorphism?
If one thing is existed in more than one form then it is called as Polymorphism.
Polymorphism is a Greak word, where Poly means many and morphism means structers or forms.

a.Static Polymorphism
b.Dynamic Polymorphism

a. Static Polymorphism:
    If polymorphism is existed at compilation time then it is called as Static Polymorhism.
EX: Overloading.

b. Dynamic Polymorphism:
    If the polymorphism is existed at runtime then that polymorphismn is called as Dynamic
    Polymorphism.
EX: Overriding

4. What are the types of Method Overloading?
Overloading:
1.Method Overloading
2.Operator Overloading

a. Method Overloading - If we declare more than one method with the same name and with the different parameter list
then it is called as Method Overloading
Ex. 
          class A{
void add(int i, int j){
}
void add(float f1, float f2){
}
void add(String str1, String str2){
}
}

b. Operator Overloading - very rarely used feature
If we define more than one functionality for any single operator then it is called as Operator
Overloding.
Ex:
int a=10;
int b=20;
int c=a+b;// + is for Arithmetic Addition.
System.out.println(c);// 30

String str1="abc";
String str2="def";
String str3=str1+str2;// + is for String concatination.
System.out.println(str3);// abcdef

Note:
1.Operator overloading is very rarely used feature in application development.
2.Operator overloading is a bit confusion oriented feature.


5. What is function o f JDB, JVM, JRE,JDK in java?
The Java Debugger (JDB or jdb) is a command-line java debugger that debugs the java class. It is a part of the Java Platform Debugger Architecture (JPDA) that helps in the inspections and debugging of a local or remote Java Virtual Machine (JVM).

The JVM (Java Virtual Machine) enables a computer to run Java or other language (kotlin, groovy, Scala, etc.) programs that are compiled to the Java bytecode. 

The JRE (Java Runtime Environment) is a part of JDK that contains the Java class libraries, Java class loader, and the Java Virtual Machine. 

The JDK (Java Development Kit) is a software development environment used to develop Java applications and applets.


6. How to Iterate through HashTable?
We can iterate through Hashmap using three way.
1. Iterator and EntrySet
2. EntrySet and for Loop
3. Using lambda java8 



Encapsulation Interview Questions

7. What is Encapsulation? 
    Encapsulation is the technique of making the fields in a class private and providing access to these fields with the help of public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. 
Therefore encapsulation is also referred to as data hiding. 

8. What is the primary benefit of encapsulation? 
The main benefit of encapsulation is the ability to modify the implemented code without breaking the code of others who use our code. 
It also provides us with maintainability, flexibility and extensibility to our code. 

9. What is an interface? 
An interface is a collection of abstract methods. 
A class implements an interface, thereby inheriting the abstract methods of the interface. 
Mention some features of interface. 
It includes -Interface cannot be instantiated. An interface does not contain any constructors. All the methods in an interface are abstract. 

10. What are the features of encapsulation? 
Encapsulation means combining the data of our application and its manipulation at one place. Encapsulation allows the state of an object to be accessed and modified through behavior. It reduces the coupling of modules and increases the cohesion inside them. 


11. Difference between Abstract class and Interface
Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. 
  
A Java abstract class can have instance methods that implements a default behavior.
Variables declared in a Java interface is by default final.
An abstract class may contain non-final variables.
Members of a Java interface are public by default.
A Java abstract class can have the usual flavors of class members like private, protected, etc..
Java interface should be implemented using keyword “implements”;
A Java abstract class should be extended using keyword “extends”.
An interface can extend another Java interface only,
an abstract class can extend another Java class and implement multiple Java interfaces.
A Java class can implement multiple interfaces but it can extend only one abstract class.
Interface is absolutely abstract and cannot be instantiated;
A Java abstract class also cannot be instantiated, but can be invoked if a main() method exists.
In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.


12. Is it mandatory to define a constructor in a class?
No, it is optional. If we do not define a constructor, compiler will automatically consider it as a default constructor. Define a constructor. Constructor is a special method provided in OOP language for creating and initializing an object.
In Java, the role of constructor is only to initialize an object and new key role is creating an object.

13. Can we define a method with same name as that of the class? 
Yes, it is allowed to define a method with same name as that of a class. No compile time error or runtime error will occur, but this practice is not recommended as per coding standards.

14. How can a compiler and a JVM differentiate between constructor and method definitions if both have same name as the class has? 
A compiler and a JVM can differentiate between the definitions of the the constructor and method with the help of return type. If there is a return pe then it is considered as a method else it is a constructor. 

15. Why a return type is not allowed for constructor? 
As there is a possibility of a method having the same name as class name, return type is not allowed in a constructor to differentiate constructor block from method block. 

16. Why a constructor name is same as class name? 
Every class object is created using the same 'new' keyword, so it must have information about the class to which it must create object. 
For this reason constructor name should be same as class name. 

17. What is inheritance? 
Inheritance is one of the oops concept in java. Inheritance is concept of getting properties of one clas  object to another class object.
Inheritance represent the IS-A relationship, also known as parent-child relationship.

18. Why we need to use Inheritance? 
For Code Reusability.
For Method Overriding. 

19. What are the types of inheritance? 
Multiple inheritance (java doesn't support multiple inheritance). 
Multilevel inheritance. 

20. What are points to consider in terms of access modifier when we are overriding any method? 
Overriding method cannot be more restrictive than the overridden method.
Reason In case of polymorphism, at object creation jvm look for actual runtime object, jvm does not look for reference type and while calling methods it look for overridden method. 
If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime—when the JVM invokes the true object's version of the method rather than the reference type's version then it will be problematic.
    In case of subclass and superclass define different package, we can override only those method which have public or protected access.
We can not override any private method because private method can not be inherited and if method can not be inherited then method can not be overriden.


21. Define the main method
• public: Access modifier, making the main() method available globally so that JVM can invoke it from outside the class [as it is not present in the current class].
• static: Keyword, making the main() method class-related [not object] so that JVM can invoke it without instantiating the class.

• void: Keyword, used to specify that a method doesn’t return anything. As soon as the main() method terminates, the java program terminates too.

• main: Identifier, that the JVM looks for as the starting point of the java program. It’s not a keyword.

• String[] args: Parameter, stores Java command line arguments in an array of type String class. Note: the name <args> is not fixed, user can use any name.


22. SOLID Principle?






















2 comments:

  1. You are providing a post that is very useful for developing my knowledge and I learn more info from your blog.
    Blue Prism Online Course

    ReplyDelete

How to install Java on EC2

***************************************** How to install Java on EC2 ***************************************** To be continued, In this post...

All Time Popular Post