- Please download the standalone wiremock server from Direct download section at the bottom of the page. Download and installation
- Feel free to place the above standalone jar in a folder of your choice. Let say for this example, it is wiremock.
- Change the directory to this folder and Start the standalone jar based on the options provided in Running standalone
- open cmd to the path where wiremock jar is downloaded and use the commend: java -jar wiremock-standalone-3.2.0.jar --port 19180 --global-response-templating
Blog for coders !!
Every failure is a success, provided we learn from it !!
Friday, January 12, 2024
How to run standalone mock server on local laptop
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
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
First thread: 1
Second thread: 1
Third thread: 1
- public class Test extends Thread {
- private volatile int i = 0;
- public int getData() {
- return i + 1;
- }
- public void run() {}
- public static void main(String[] args) {
- Test t1 = new Test();
- t1.start();
- System.out.println("First thread: " + t1.getData());
- Test t2 = new Test();
- t2.start();
- System.out.println("Second thread: " + t2.getData());
- Test t3 = new Test();
- t3.start();
- System.out.println("Third thread: " + t3.getData());
- }
- }
First thread: 1
Second thread: 1
Third thread: 1
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:
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:
- import java.util.Comparator;
- import java.util.TreeSet;
- public class Test {
- public static void main(String[] args) {
- SortStringBufferObject s = new SortStringBufferObject();
- TreeSet<StringBuffer> t = new TreeSet<>(s);
- t.add(new StringBuffer("Z"));
- t.add(new StringBuffer("A"));
- t.add(new StringBuffer("P"));
- System.out.println(t);
- }
- }
- class SortStringBufferObject implements Comparator<StringBuffer> {
- public int compare(StringBuffer sb1, StringBuffer sb2) {
- return sb1.toString().compareTo(sb2.toString());
- }
- }
Output:
[A, P, Z]
// DESCENDING ORDER:
- import java.util.Comparator;
- import java.util.TreeSet;
- public class Test {
- public static void main(String[] args) {
- SortStringBufferObject s = new SortStringBufferObject();
- TreeSet<StringBuffer> t = new TreeSet<>(s);
- t.add(new StringBuffer("Z"));
- t.add(new StringBuffer("A"));
- t.add(new StringBuffer("P"));
- System.out.println(t);
- }
- }
- class SortStringBufferObject implements Comparator<StringBuffer> {
- public int compare(StringBuffer sb1, StringBuffer sb2) {
- return sb2.toString().compareTo(sb1.toString());
- }
- }
Output:
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
Answer: Reference
What will be the output when you add custom elements in HashSet?
Problem statement: Given code is
Elements:
[com.practice.core.C@7852e922, com.practice.core.A@15db9742, com.practice.core.B@6d06d69c, com.practice.core.A@4e25154f]
Size: 4
- import java.util.HashSet;
- class A {}
- class B {}
- class C {}
- public class Test {
- public static void main(String[] args) {
- HashSet set = new HashSet();
- set.add(new A());
- set.add(new B());
- set.add(new C());
- set.add(new A());
- System.out.println("Elements: \n" + set);
- System.out.println("Size: " + set.size());
- }
- }
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
Elements:
[A, B, C]
Size: 3
Objects: [A, B, C]
- public class Test {
- public static void main(String[] args) {
- HashSet<String> s = new HashSet<>();
- s.add("A");
- s.add("B");
- s.add("C");
- s.add("A");
- System.out.println("Elements: \n" + s);
- System.out.println("Size: " + s.size());
- System.out.println("Objects: " + s);
- }
- }
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.
[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
- import java.util.ArrayList;
- class A {}
- class B {}
- class C {}
- public class Test {
- public static void main(String[] args) {
- ArrayList list = new ArrayList();
- list.add(new A());
- list.add(new B());
- list.add(new C());
- list.add(new A());
- System.out.println(list);
- System.out.println("Size: " + list.size());
- }
- }
[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.
- type safety
- 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.
- ArrayList<Integer> l = new ArrayList<>();
- ArrayList<Integer> l = new ArrayList<Integer>();
- ArrayList<int> l = new ArrayList<int>();
- ArrayList<int> l = new ArrayList<>();
- LinkedList<Integer> l = new LinkedList<Integer>();
- LinkedList<Integer> l = new LinkedList<>();
- LinkedList<int> l = new LinkedList<>();
- Map<String, Integer> m = new HashMap<String, Integer>();
- Map<String, Integer> m = new HashMap<>();
- Map<String, int> m = new HashMap<>();
Answer:
- Correct- we can keep right angular braces empty
- Correct- we can specify the right angular braces data type also
- Incorrect-generic support only wrapper class for auto boxing to Object type
- Incorrect-generic support only wrapper class for auto boxing to Object type
- Correct- we can specify the right angular braces data type also
- Correct- we can keep right angular braces empty
- Incorrect- generic doen't support primitive data type
- Correct - we can specify the right angular braces data type also
- Correct - we can keep right angular braces empty
- Incorrect- generic doen't support primitive data type
Monday, August 26, 2019
How do you sort the keys in HashMap?
Problem statement:
Write the java code to sort the keys in HashMap.
[key12, key27, key39, key45]
Write the java code to sort the keys in HashMap.
- import java.util.HashMap;
- import java.util.Set;
- import java.util.TreeSet;
- public class Test {
- public static void main(String[] args) {
- HashMap<String, String> m = new HashMap<String, String>();
- m.put("key45", "aa");
- m.put("key12", "zz");
- m.put("key39", "cc");
- m.put("key27", "bb");
- Set<String> s = m.keySet();
- s = new TreeSet<String>(s);
- System.out.println(s);
- }
- }
[key12, key27, key39, key45]
How do you sort the values in HashMap?
Problem statement:
Write the java code to sort the values in HashMap.
Write the java code to sort the values in HashMap.
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.TreeSet;
- public class Test {
- public static void main(String[] args) {
- HashMap<String, String> m = new HashMap<String, String>();
- m.put("key45", "aa");
- m.put("key12", "zz");
- m.put("key39", "cc");
- m.put("key27", "bb");
- Collection<String> c = m.values();
- c = new TreeSet<String>(c);
- System.out.println(c);
- }
- }
Output:
[aa, bb, cc, zz]
Which of the following option will sort the keys in HashMap ?
Problem statement:
Given java code as follows, can you find out the correct option to sort the keys in HashMap?
- import java.util.HashMap;
- import java.util.Set;
- import java.util.TreeSet;
- public class Test {
- public static void main(String[] args) {
- HashMap<String, String> m = new HashMap<String, String>();
- m.put("key45", "aa");
- m.put("key12", "bb");
- m.put("key39", "cc");
- Set<String> s = m.keySet();
- // code goes here
- s = new TreeSet(s);
- }
- }
- Arrays.sort(s);
- s = new TreeSet(s);
- Collections.sort(s);
- s = new SortedSet(s);
Correct option:
s = new TreeSet(s);
Explanation:
Arrays.sort(s) // sorts for static array
Collection.sort(s) // sort the List type of object not Set type
s =new SortedSet(s); // SortedSet is an interface
Explanation:
Arrays.sort(s) // sorts for static array
Collection.sort(s) // sort the List type of object not Set type
s =new SortedSet(s); // SortedSet is an interface
Saturday, August 24, 2019
Write an algorithm to find N’th smallest element in an Unsorted Array.
Problem statement:
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the nth smallest element in the array.
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the nth smallest element in the array.
- public class NthSmallestElement {
- public static void main(String[] args) {
- int a[] = { 3, 4, 1, 105, 10, -10 };
- int thirdSmallest = 3;
- int min= nthSmallestElement(a, thirdSmallest );
- System.out.println(min);
- }
- public static int nthSmallestElement(int a[], int n) {
- // Sort the given array
- Arrays.sort(a);
- // Return n'th element in the sorted array
- return a[n - 1];
- }
- }
Output:
3
Write an algorithm to find N’th largest element in an Unsorted Array.
Problem statement:
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the nth largest element in the array.
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the nth largest element in the array.
- public class NthLargestElement {
- public static void main(String[] args) {
- int a[] = { 3, 4, 1, 105, 10, -10 };
- int thirdLargest = 3;
- int max = nthLargestElement(a, thirdLargest);
- System.out.println(max);
- }
- public static int nthLargestElement(int a[], int n) {
- // Sort the given array
- Arrays.sort(a);
- // Return n'th element in the sorted array
- return a[a.length - n];
- }
- }
Output:
4
Write an algorithm to find out the second smallest element in an array.
Problem statement:
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the second smallest element in the array.
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the second smallest element in the array.
- public class SecondMinElement {
- public static void main(String[] args) {
- int a[] = { 3, 4, 1, 105, 10, -10 };
- int firstMin = Integer.MAX_VALUE;
- int secondMin = Integer.MAX_VALUE;
- for (int i = 0; i < a.length; i++) {
- if (a[i] < firstMin) {
- secondMin = firstMin;
- firstMin = a[i];
- } else if (a[i] < secondMin) {
- secondMin = a[i];
- }
- }
- System.out.println("Second min element: " + secondMin);
- }
- }
Output:
Second min element: 1
Write an algorithm to find out the second largest element in an array.
Problem statement:
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the second largest element in the array.
Given an array of positive and negative unsorted integer. Can you write an algorithm to find out the second largest element in the array.
- public class SecondMaxElement {
- public static void main(String[] args) {
- int a[] = { 3, 4, 1, 105, 10, -10 };
- int firstMax = Integer.MIN_VALUE, // large -ve value
- int secondMax = Integer.MIN_VALUE; // large -ve value
- for (int i = 0; i < a.length; i++) {
- if (a[i] > firstMax) {
- secondMax = firstMax;
- firstMax = a[i]; // a[0] becomes first max
- } else if (a[i] > secondMax) {
- secondMax = a[i];
- }
- }
- System.out.println(secondMax);
- }
- }
Output:
10
What is the output of the given java code?
Problem statement:
Given java code, what will be the output of it?
Given java code, what will be the output of it?
- interface Car {
- public void startEngine();
- }
- class MySuzuki implements Car {
- public void startEngine() {
- System.out.println("MySuzuki");
- }
- }
- class MyFerrari implements Car {
- public void startEngine() {
- System.out.println("MyFerrari");
- }
- }
- public class OOPSExample {
- public static void main(String[] args) {
- MyFerrari obj = new MySuzuki();
- obj.startEngine();
- }
- }
Output:
Compile time error at line 16, due to Type mismatch: cannot convert from MySuzuki to MyFerrari
What is the output of the code?
Problem statement:
Given java code, what will be the output of it?
Given java code, what will be the output of it?
- interface Car {
- public void startEngine();
- }
- class MySuzuki implements Car {
- public void startEngine() {
- System.out.println("MySuzuki");
- }
- }
- class MyFerrari implements Car {
- public void startEngine() {
- System.out.println("MyFerrari");
- }
- }
- public class OOPSExample {
- public static void main(String[] args) {
- Car obj = new MySuzuki();
- obj.startEngine();
- }
- }
Output:
MySuzuki
Can you write an algorithm to check palindrome?
Problem statement:
Given a string. Can you check whether it is a palindrome or not?
Given a string. Can you check whether it is a palindrome or not?
- public class Palindrome {
- public static void main(String[] args) {
- String s = "abcba";
- char c[] = s.toCharArray();
- int p = c.length - 1;
- for (int i = 0; i < c.length / 2; i++) {
- if (c[i] == c[p]) {
- p--;
- } else {
- System.out.println("Not palindrome");
- return;
- }
- }
- System.out.println("Palindrome");
- }
- }
Output:
Palindrome
Time complexity: O(n/2)
How will you find smallest element in an array?
Problem statement:
Given an array of positive or negative integer. How will you write an algorithm to find out the largest element in the given array?
Given an array of positive or negative integer. How will you write an algorithm to find out the largest element in the given array?
- public class LargestElement {
- public static void main(String[] args) {
- int a[] = { 3, 4, 1, 5, 0, -10 };
- int max = Integer.MIN_VALUE; //-2147483648
- for (int i = 0; i < a.length; i++) {
- if (a[i] > max) {
- max = a[i];
- }
- }
- System.out.println("max: " + max);
- }
- }
Output:
max: 5
How will you find smallest element in an array?
Problem statement:
Given an array of positive or negative integer. How will you write an algorithm to find out the smallest element in the given array?
Given an array of positive or negative integer. How will you write an algorithm to find out the smallest element in the given array?
- public class SmallestElement {
- public static void main(String[] args) {
- int a[] = { 3, 4, 1, 5, 0, -10, -40 };
- int min = Integer.MAX_VALUE; // 2147483647
- for (int i = 0; i < a.length; i++) {
- if (a[i] < min) {
- min = a[i];
- }
- }
- System.out.println("min: " + min);
- }
- }
Output:
min: -40
Wednesday, August 21, 2019
Can we overload constructor in java?
Problem statement:
You are given a code for constructor overloading. What do you think, can we overload the constructor in java?
Ans: Yes.
You are given a code for constructor overloading. What do you think, can we overload the constructor in java?
Ans: Yes.
- class A {
- A() {
- System.out.println("default");
- }
- A(int id) {
- System.out.println("id");
- }
- A(int id, String name) {
- System.out.println("id & name");
- }
- A(String name, Double salary) {
- System.out.println("name & salary");
- }
- }
- public class Sample {
- public static void main(String[] args) {
- A a = new A();
- }
- }
Output:
default
Can we override constructor in java?
Problem statement:
You are given a code for constructor overriding. What do you think, can we override the constructor in java?
Ans: No.
You are given a code for constructor overriding. What do you think, can we override the constructor in java?
Ans: No.
- class A {
- A() {
- System.out.println("A of A");
- }
- }
- class B extends A {
- A() {
- System.out.println("A of B");
- }
- }
- public class Sample {
- public static void main(String[] args) {
- B b = new B();
- }
- }
Output:
Return type of method is missing, meaning compiler is treating A() as a method not as a constructor and also compiler is expecting return type for the method A(), because in code there is no return type.
Saturday, August 17, 2019
what will happen when you run the following code?
Problem statement:
Given the java code. what will be the output?
test
exception
runtime
end
Given the java code. what will be the output?
- public class Test {
- static void test() throws RuntimeException {
- try {
- System.out.println("test");
- throw new RuntimeException();
- } catch (Exception ex) {
- System.out.println("exception");
- }
- }
- public static void main(String[] args) {
- try {
- test();
- throw new RuntimeException();
- } catch (RuntimeException ex) {
- System.out.println("runtime");
- System.out.println("end");
- }
- }
- }
test
exception
runtime
end
Friday, August 16, 2019
What is the output of the following code?
Problem statement:
Given the code of java. Find out the output.
Given the code of java. Find out the output.
- public class Test {
- static void test() throws RuntimeException {
- try {
- System.out.println("test");
- throw new RuntimeException();
- } catch (Exception ex) {
- System.out.println("exception");
- }
- }
- public static void main(String[] args) {
- try {
- test();
- } catch (RuntimeException ex) {
- System.out.println("runtime");
- }
- System.out.println("end");
- }
- }
Output:
test
exception
end
end
Sunday, August 11, 2019
What is the output of the following code?
Problem statement:
Given the code of java. Can you tell me the output of this?
compilation error at line number 17 due to a.getData(). Here getData() is not a method of Annonymous interface.
Given the code of java. Can you tell me the output of this?
- interface Annonymous {
- public int getValue();
- }
- public class Sample1 {
- int data = 15;
- public static void main(String[] args) {
- Annonymous a = new Annonymous() {
- int data = 5;
- public int getValue() {
- return data;
- }
- public int getData() {
- return data;
- }
- };
- Sample1 s = new Sample1();
- System.out.println(a.getValue() + a.getData() + s.data);
- }
- }
compilation error at line number 17 due to a.getData(). Here getData() is not a method of Annonymous interface.
Subscribe to:
Posts (Atom)
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...
-
Cryptography or cryptology (from Ancient Greek: kryptós "hidden, secret"; graphein, "to write") is the practice and stu...
-
Problem statement: In a dark room,there is a box of 18 white and 5 black gloves. You are allowed to pick one and then you are allowed to k...
-
Problem statement: what would be the better option to choose between char[] array and String. Answer: Character array i.e. char[] over ...