Posts

Showing posts from July, 2017

Important Java Interview Questions - Part 7

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. Where have u used abstraction and encapsulation in your project? We use encapsulation everywhere in java like when we create a class in java we are using encapsulation. Abstraction we use when we create abstract class or interface using abstraction in java. " Encapsulation  is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing" Using Encapsulation we achieve  “Data Hiding, Information Hiding”  in java. Encapsulation binds data (attributes and methods) and the code in the form of a class. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. Encapsulation provides “ access control ” through which we can control

Important Java Interview Questions - Part 5

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. What is difference between Linkedlist & Arraylist? LinkedList is an implementation of List interface in java. The LinkedList Java class is a concrete implementation of this abstract concept. LinkedList is based on double linked List data structure.  LinkedList<E>  allows for constant-time insertions or removals  using iterators , but only sequential access of elements and maintains insertion order. LinkedList is non synchronized. ArrayList is an implementation of List interface in java. ArrayList is based on an Array data structure. ArrayList implements it with a dynamically re-sizing array and maintains insertion order. It is non synchronized. ArrayList LinkedList 1) ArrayList internally uses dynamic array to store the elements. LinkedList internally uses doubly linked list to store the elements. 2) ArrayList

Important Java Interview Questions - Part 6

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. Why linkedlist is good for insertion in java? A LinkedList looks like a chain, consisting of people who hold each other's hand. You can insert people or node into that chain or remove.  LinkedList is actually very flexible extension of an ArrayList, For example if you need to add new element to the end of an ArrayList you have to look at the size of the collection and then add that new element at  n+1  place. In LinkedList you do it directly by  addLast(E e)  method. In a LinkedList there are three parts. First part stores the pointer to previous element, second stores the data & third stores pointer to next element. So when insertion & deletion happens in linkedlist these pointers get updated. Thats why linkedlist is fast.  Whereas, ArrayList internally uses array & when you run an “ add ” command a new Array of size more tha

Aggregation and Composition in Java

Image
The distinction between aggregation and composition depends on context. The composition is stronger than Aggregation. In Short, a relationship between two objects is referred as an association, and an association is known as composition when one object  owns other while an association is known as aggregation when one object uses another object. In the case of Composition, One object is  OWNER  of another object, while in the case of aggregation one object is just a  USER  or another object. 1. Composition is a  strong  Association whereas Aggregation is a  weak  Association. 2. Aggregation relation is  “has-a”  and composition is  “part-of”  relation. Take the example of Team in which Player can stand “on its own” so may not be in composition with a Team. Player which is a part of Team can exist without a team and can become part of other team as well. Just like Car example, it is true that a car exhaust can stand "on its own" so may not be in composition w

Important Java Interview Questions - Part 4

Image
I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. What is the impelementation of linkedlist in java? Singly Linked Lists are a type of data structure. It is a type of List. In a singly linked list each node in the list stores the contents of the node and a pointer or reference to the next node in the list. Here is the pictorial view of singly linked list: A doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. This structure allows for efficient insertion or removal of elements from any position in the sequence. In a doubly linked list each node has two links one pointing to the next node in the list and one pointing to the previous node in the list. Here is the pictorial view of doubly l

Important Java Interview Questions - Part 3

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. Why we need Linkedhashmap? 1.       LinkedHashMap will iterate in the order in which the entries were put into the map. LinkedHashMap maintain insertion order of keys, i.e the order in which keys are inserted into LinkedHashMap. On the other hand HashMap doesn't maintain any order or keys or values. 2.       Null Values are allowed in LinkedHashMap. 3.       In terms of Performance there is not much difference between HashMap and LinkedHashMap but yes LinkedHashMap has more memory foot print than HashMap to maintain doubly linked list which it uses to keep track of insertion order of keys. 4.       A HashMap has a better performance than a LinkedHashMap because a LinkedHashMap needs the expense of maintaining the linked list. 5.       The LinkedHashMap implements a normal hashtable, but with the added benefit of the keys of the hashtable

Abstraction in Java

Abstraction Abstraction  on the other hand, can be explained as the capability to use the same  interface  for different objects. It  involves the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system. Different implementations of the same interface can exist. Details are hidden by  encapsulation . To explain you about abstraction in java we can take three different entities (People in real world). You:  As a user you know how to start a washing machine. We just need to press a button and all other operations behind the scene are happening and are abstracted from user. Now you are facing some problem with your machine when you are pressing start button it is not turning on there might be some major issue with your hardware which needs to be corrected. Then you call local mechanic to check the problem. Local Mechanic:  Our local mechanic kn

Encapsulation in java

Encapsulation Using Encapsulation we achieve “Data Hiding, Information Hiding” in java. Encapsulation binds data (attributes and methods) and the code in the form of a class. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. Encapsulation provides “ access control ” through which we can control the access of class in accessing methods or members of class. Encapsulation is performed by constructing the class. Abstraction is achieved by creating either Abstract Classes or Interfaces on top of your class. Real world examples of Encapsulation: Washing Machine and its button on machine which turns the machine “ON”:  Function of button is to start the functioning of machine but did you ever think that what happens inside machine when button was pressed like inside mechanism of machine. Actually we do not need to worry about that internal functionality of washing machine till the time our machine

Hashmap vs Hashtable - Part 2

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. 5 differences between Hashmap vs Hashtable with explanation? There are several differences between HashMap and Hashtable in Java: 1.       Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones. 2.       HashMap does not guarantee that the order of the map will remain constant over time and HashMap is much faster uses less memory than Hashtable because former is unsynchronized. 3.       Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values. 4.       One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a  LinkedHashMap.  This would