Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Sunday, December 8, 2019

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

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...