Saturday, June 12, 2021

Most Important 15 basic Java Question and answers






1. Why Java doesn't support multiple Inheritance?
The reason behind this is to prevent ambiguity. Consider a case where class B extends class A and class C and both class A and class C have the same method "display()". Now Java compiler can't decide,
which display method it should inherit. To prevent such situations, multiple inheritance is not allowed in Java.


2. Why are string objects immutable in Java?
Because Java uses the concept of string literal. Suppose there are 2 reference variables, all refer to one object "Hello". If one reference variable changes the value of the object, it will affect the other reference variable. Also, String class in Java is final.


3. Define Class and Object in Java.
In OOPS, a class is a template for creating and describing objects (real-life entities) and providing
initial values for the state and implementation of behaviour.
States are called as attributes or data members. Behaviours are called as methods or member functions.
An object is an instance of the class. It is a real-life entity that has states and behaviour(s).


4. What do you mean by encapsulation? 
It is defined as wrapping up of data members and member functions into a single unit called class.
Since the data is hidden from other classes, it is also known as data hiding.



5. What is Polymorphism?
It is the ability of an object to take multiple forms. There are two types of polymorphisms.
i) Compile time polymorphism - The function call is resolved by the compiler at the compile time itself. For example, function or method overloading. It is also known as Early binding.
ii) Runtime polymorphism - The function call is resolved by the compiler at the runtime. For example, method overriding. It is also known as Late binding or Dynamic binding.


6. Define an Interface.
An interface is an abstract data type that is used to specify a behaviour that classes must implement.
It is used to achieve 100% abstraction. Members of an



7. What is Abstraction?
It is the property by virtue of which only essential details are displayed to the user.
For example, consider the case of a driver driving a car.
The driver only knows how to use the controls to drive the car but is unaware of the internal working of those components of the car.
In Java, abstraction is achieved by using interfaces and abstract classes.100% abstraction can be achieved using interfaces.


8. What do you understand by Object-Oriented Programming?
OOP is a programming paradigm based on the concept of "objects" that contain data and methods.
It is used to increase flexibility and maintainability of programs.


9. Mention some features of Java.
1. Platform independent. 2. Object-oriented 3. Automatic memory allocation & garbage collection 4. Portable 5. Used in creation of Desktop, Mobile and Web applications.


10. Why doesn't Java have pointers?

Java doesn't have pointers because it doesn't need them for general purpose object-oriented programming.
Furthermore, adding pointers to Java would undermine the security and robustness and 1,e the language more complex.


11. Describe the usage of the final keyword in Java.
final keyword is used with variables when the value of the variable is not going to change and is going to remain constant.
When the final keyword is used with methods, then they can't be overridden in the derived class.
Final keyword is used with class to prevent the class from being subclasses when writing APIs or libraries so that the base behavior is not altered.


12. Why can't static methods call non-static methods?
A static method is not tied to any object/instance of the class, while a non-static method always refers to an actual object/instance of the class.
It is to be noted that non static methods can access any static method/variable without creating an instance of the class.


13.What do you mean by static?
static is a keyword used for a constant variable or a method that is the same for every instance of the class. 


14. Is Java 100% Object-oriented?
Java is not 100% object-oriented because it makes use of primitive data types such as Boolean, byte, char, int, float, double which are not objects.


15. Difference between HashMap and HashSet

HashMap

HashSet

Hashmap is the implementation of Map interface Hashset on other hand is the implementation of set interface.
Hashmap internally do not implements hashset or any set for its implementation. Hashset internally uses Hashmap for its implementation.
HashMap Stores elements in form of key-value pair i.e each element has its corresponding key which is required for its retrieval during iteration. HashSet stores only objects no such key value pairs maintained.
Put method of hash map is used to add element in hashmap. On other hand add method of hashset is used to add element in hashset.
Hashmap due to its unique key is faster in retrieval of element during its iteration. HashSet is completely based on object so compared to hashmap is slower.
Single null key and any number of null value can be inserted in hashmap without any restriction. On other hand Hashset allows only one null value in its collection,after which no null value is allowed to be added.


No comments:

Post a Comment

How to install Java on EC2

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

All Time Popular Post