Interview Questions & Answers

java

Collections:-

  • Collections are used to store and group the data in a particular data structure ike List, Set or Map.
  • You can add or remove elements from collections.
  • Collections have to be iterated externally (Using for loops).
  • Collections can be traversed multiple times.
  • Collections are eagerly constructed.

 

Streams:-

  • Streams are used to perform complex data processing operations like filtering, matching, mapping stored data such as arrays, collections or I/O resources.
  • You can’t add or remove elements from streams.
  • Streams are internally iterated (using forEach() method). Streams are traversable only once.
  • Streams are lazily constructed.

to be edited

ArrayList:-

  • ArrayList internally uses a dynamic array to store the elements
  • Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in memory.
  • ArrayList consumes less memory than LinkedList
  • An ArrayList class can act as a list only because it implements List only.
  • ArrayList is better for storing and accessing data.

 

LinkedList:-

  • LinkedList internally uses a doubly linked list to store the elements.
  • Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory.
  • A LinkedList consumes more memory than an ArrayList because it also stores the next and previous references along with the data.
  • LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
  • LinkedList is better for manipulating data.

List:-

  • List allows duplicates elements.
  • List is an ordered collection it maintains the insertion order.
  • List implementations: ArrayList, LinkedList, Vector and Stack.
  • List allows any number of null values.

Set:-

  • Set doesn’t allow duplicate elements.
  • Set is unordered collection, it doesn’t maintain any order.
  • Set implements: HashSet, LinkedHashSet, TreeSet etc.
  • Set can have only a single null value at most

String s1 = "obify"
String s2 = "obify";
System.out.println(s1 == s2);

Answer:
The above code snippet prints output as true because we have created 
both String objects using double quotes (Sting literals),
 then they will be part of the string pool and it will print true.

String s1 = new String("obify");
 String s2 = new String("obify");
System.out.println(s1 == s2);


Answer:
The above code snippet prints output as false because we are using a new operator to create String,
 so it will be created in the heap memory and both s1, s2 will have different references.
If we create them using double quotes, then they will be part of the string pool and it will print true.


                        

String s1 = new String("obify");

String s2 = "obify";


Answer:
Here, two string objects will be created.
 An object created using a new operator(s1) will be stored in the heap memory.
 The object created using a string literal(s2) is stored in the string constant pool.

String s1 = "obify";

String s2 = "obify";

Answer:

Only one object will be created and this object will be stored in the string constant pool.