Sunday, December 8, 2019

What is the preferred datatype to store password in java and why?

Problem statement: what would be the better option to choose between char[] array and String.

Answer: Character array i.e. char[] over the String.
Reference

What will be the output of the below java code using volatile keyword?

Problem statement: Given code is
  1. public class Test extends Thread {
  2. private volatile int i = 0;
  3. public int getData() {
  4. return i + 1;
  5. }
  6. public void run() {}
  7. public static void main(String[] args) {
  8. Test t1 = new Test();
  9. t1.start();
  10. System.out.println("First thread: " + t1.getData());
  11. Test t2 = new Test();
  12. t2.start();
  13. System.out.println("Second thread: " + t2.getData());
  14. Test t3 = new Test();
  15. t3.start();
  16. System.out.println("Third thread: " + t3.getData());
  17. }
  18. }
Output:
First thread: 1
Second thread: 1
Third thread: 1

Example for compareTo() method?

How do you sort collection of StringBuffer object in java?

Problem statement: Given the collection of StringBuffer objects.
StringBuffer sb1 = new StringBuffer("Z");
StringBuffer sb2 = new StringBuffer("A");
StringBuffer sb3 = new StringBuffer("P");

You have to sort it either way, meaning natural order or custom order.

Answer:
// ASCENDING ORDER:
  1. import java.util.Comparator;
  2. import java.util.TreeSet;
  3. public class Test {
  4. public static void main(String[] args) {
  5. SortStringBufferObject s = new SortStringBufferObject();
  6. TreeSet<StringBuffer> t = new TreeSet<>(s);
  7. t.add(new StringBuffer("Z"));
  8. t.add(new StringBuffer("A"));
  9. t.add(new StringBuffer("P"));
  10. System.out.println(t);
  11. }
  12. }
  13. class SortStringBufferObject implements Comparator<StringBuffer> {
  14. public int compare(StringBuffer sb1, StringBuffer sb2) {
  15. return sb1.toString().compareTo(sb2.toString());
  16. }
  17. }
Output:
[A, P, Z] 

// DESCENDING ORDER:
  1. import java.util.Comparator;
  2. import java.util.TreeSet;
  3. public class Test {
  4. public static void main(String[] args) {
  5. SortStringBufferObject s = new SortStringBufferObject();
  6. TreeSet<StringBuffer> t = new TreeSet<>(s);
  7. t.add(new StringBuffer("Z"));
  8. t.add(new StringBuffer("A"));
  9. t.add(new StringBuffer("P"));
  10. System.out.println(t);
  11. }
  12. }
  13. class SortStringBufferObject implements Comparator<StringBuffer> {
  14. public int compare(StringBuffer sb1, StringBuffer sb2) {
  15. return sb2.toString().compareTo(sb1.toString());
  16. }
  17. }
Output:
[Z, P, A]
Reference

What is the internal working of Set / HashSet in Java ?

Problem statement: Give an explanation for internal working of Set / HashSet in java.
Answer: Reference

What will be the output when you add custom elements in HashSet?

Problem statement: Given code is
  1. import java.util.HashSet;
  2. class A {}
  3. class B {}
  4. class C {}
  5. public class Test {
  6. public static void main(String[] args) {
  7. HashSet set = new HashSet();
  8. set.add(new A());
  9. set.add(new B());
  10. set.add(new C());
  11. set.add(new A());
  12. System.out.println("Elements: \n" + set);
  13.   System.out.println("Size: " + set.size());
  14. }
  15. }
Answer:
Elements: 
[com.practice.core.C@7852e922, com.practice.core.A@15db9742, com.practice.core.B@6d06d69c, com.practice.core.A@4e25154f]
Size: 4

What will be the output when you add String object in HashSet?

Problem statement: Given code is 
  1. public class Test {
  2. public static void main(String[] args) {
  3. HashSet<String> s = new HashSet<>();
  4. s.add("A");
  5. s.add("B");
  6. s.add("C");
  7. s.add("A");
  8.   System.out.println("Elements: \n" + s);
  9.   System.out.println("Size: " + s.size());
  10.   System.out.println("Objects: " + s);
  11. }
  12. }
Answer:
Elements: 
[A, B, C]
Size: 3
Objects: [A, B, C]

What will be the output when we add elements in collection of ArrayList in java?

Problem statement: In given java code, can you please let me know, what will be the output.
  1. import java.util.ArrayList;
  2. class A {}
  3. class B {}
  4. class C {}
  5. public class Test {
  6. public static void main(String[] args) {
  7. ArrayList list = new ArrayList();
  8. list.add(new A());
  9. list.add(new B());
  10. list.add(new C());
  11.   list.add(new A());
  12. System.out.println(list);
  13.   System.out.println("Size: " + list.size());
  14. }
  15. }
Answer:
[com.practice.core.A@15db9742, com.practice.core.B@6d06d69c, com.practice.core.C@7852e922, com.practice.core.A@4e25154f]
Size: 4

Note: fully_qualified_class_name@hashcode_value

What is the benefits of generic in java?

Problem statement: List out the benefits of generic in java.
  1. type safety
  2. type erasure
  • type safety: what exactly mean of type safety, Compiler guarantee that if correct types are used in correct places means within angular braces then there should not be any ClassCastException in runtime. A use case can be a list of Integer i.e. List<Integer>.If we declare a list in java like List<Integer>, then java guarantees that it will detect and report us any attempt to insert any non-integer type into above list.
  • type erasure: what exactly mean of type erasure, It essentially means that all the extra information added using generics into sourcecode will be removed from bytecode generated from it. Inside bytecode, it will be old java syntax which you will get if you don’t use generics at all. This necessarily helps in generating and executing code written prior java 5 when generics were not added in language.
Let’s understand with an example.

List<Integer> list = new ArrayList<Integer>(); 
list.add(1000);     //works fine 
list.add("lokesh"); //compile time error 

When we write above code and compile it, we will get an error: “The method add(Integer) in the type List<Integer> is not applicable for the arguments (String)“. Compiler warned us. This is what exactly generics sole purpose i.e. Type Safety.

Second part is getting bytecode after removing second line from above example. If you compare the bytecode of above example with/without generics, then there will not be any difference. Clearly compiler removed all generics information. So, above code is very much similar to below code without generics.

List list = new ArrayList(); 
list.add(1000);

What is the correct and incorrect piece of code for generic representation in Java? [Onward Java 5]

Problem statement: We need to find out the correct pieces of java code.
  1. ArrayList<Integer> l = new ArrayList<>();
  2. ArrayList<Integer> l = new ArrayList<Integer>();
  3. ArrayList<int> l = new ArrayList<int>();
  4. ArrayList<int> l = new ArrayList<>();
  5. LinkedList<Integer> l = new LinkedList<Integer>();
  6. LinkedList<Integer> l = new LinkedList<>();
  7. LinkedList<int> l = new LinkedList<>();
  8. Map<String, Integer> m = new HashMap<String, Integer>();
  9. Map<String, Integer> m = new HashMap<>();
  10. Map<String, int> m = new HashMap<>();
Answer:
  1. Correct- we can keep right angular braces empty
  2. Correct- we can specify the right angular braces data type also
  3. Incorrect-generic support only wrapper class for auto boxing to Object type
  4. Incorrect-generic support only wrapper class for auto boxing to Object type
  5. Correct- we can specify the right angular braces data type also
  6. Correct- we can keep right angular braces empty
  7. Incorrect- generic doen't support primitive data type
  8. Correct - we can specify the right angular braces data type also
  9. Correct - we can keep right angular braces empty
  10. Incorrect- generic doen't support primitive data type

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...