Java

/> Why Strings are immutable in Java?
Java was defined from the start with considerations of concurrency. As often been mentioned shared mutables are problematic. One thing can change another behind the back of another thread without that thread being aware of it.

/> Deadlock?

Describes a situation where two or more threads are blocked forever, waiting for each other.

/> There are 3 ways to make an object thread-safe, They are

  1. Synchronize critical sections
  2. Make it immutable
  3. Use a thread-safe wrapper

/> is it bad practice to catch Throwable in java?

try {
    // try block content
} catch(Throwable e) {
    // handle the exception
}

Yes! we should be as specific as possible. Besides that Throwable can be an Error & we don’t need to catch/handle them. You would ideally terminate/kill the program immediately in such cases.

/> How to custom format Current Date & Time to String & vice-versa?

SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = formatter.format(new Date());
		
try {
	Date parsedDate = formatter.parse(dateInString);
	System.out.println(parsedDate);
	System.out.println(dateInString);
} catch (ParseException e) {
	e.printStackTrace();
}

/> Reverse a number in Java?

private static int reverse(int num) {
	int result = 0;
	while (num != 0) {
	    result = result * 10 + num % 10;
	    num /= 10;   
	}
	return result;
}

/> How to perform a bitwise multiplication in Java?

public static void bitMultiply(int n1, int n2) {
    int temp, i = 0, result = 0;

    while (n2 != 0) {
        if ((n2 & 1) == 1) {
            temp = n1;
            result += (temp<>=(1/i)); // only using Right shift
        }
        n2 >>= 1;   // Right shift n2 by 1.
        i++;
    }
    System.out.println(result);
}

/> How to write a bubble sort in Java?

public static void bubbleSort(int[] nums) {
	int temp=0;int n = nums.length;
	for (int i = 0; i < n; i++) {
	    for (int j = 1; j  nums[j]) {
			temp = nums[j - 1];
			nums[j - 1] = nums[j];
			nums[j] = temp;
		}
	    }
	}
}