Related Links 1. Spring DIP, DI & IoC in detail interview Q&As. … Read more ›...
Related Links 1. Spring DIP, DI & IoC in detail interview Q&As. … Read more ›...
Q1. What do you understand by automatic dirty checking in Hibernate? A1. Dirty checking is a feature of hibernate that saves time and effort to update the database when states of objects are modified inside a transaction. All persistent objects are monitored by hibernate.It detects which objects have been modified...
Q01: What are virtual threads in Java? A01: Virtual threads in Java are analogous to goroutines in the Go language. The virtual threads are a new type of thread that tries to overcome the resource limitation problem of platform threads by storing the stack frames in the heap, … Read...
This extends 06: JMockit interview Q&As & examples on Partial Mocking with MockUp. What is MockUp<T>? In the JMockit toolkit, the Faking API provides support for the creation of fake implementations. JMockit’s mockit.MockUp<T>, where T is the type to be faked. … Read more ›...
Q. Complete the method “int numberOfWays(int n)” where n is the distance to cover and the method should return the number of possible combinations if a frog can jump 1 or 2 steps at a time.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class JumpingFrog { public static int numberOfWays(int n) { throw new RuntimeException("To be completed"); } public static void main(String[] args) { System.out.println(numberOfWays(5)); } } |
The distance of 3 will have the following 3 combinations: 1) [1, …...
Java does not have a Tree class but you can define one. In my 10+ years as a Java developer, I have rarely used a Tree structure, but when I do use it I find it to be a bit more complicated than working with other data structure. … Read...
Q71. Can ETL in traditional data management (E.g. RDBMs) be migrated to EDH (i.e. Enterprise Data Hub) powered by Hadoop eco system? A71. Yes, it can be migrated, but it is not a direct & straight forward migration as there is a mismatch in underpinning concepts & … Read more...
Q. Can you review the following code and see if it has any issues?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import java.util.Iterator; import java.util.NoSuchElementException; public class WhatIsWrong { public static void main(String[] args) { MyStack<String> stack = new MyStack<String>(10000); for (int i = 0; i < 10000; i++) { stack.push("number=" + i); } while (!stack.isEmpty()) { stack.pop(); } } static class MyStack<E> implements Iterable<String> { private int index; private String[] backingArray; public MyStack(int capacity) { backingArray = new String[capacity]; } public void push(String item) { if (isFull()) { throw new RuntimeException("MyStack overflow"); } backingArray[index++] = item; } public String pop() { if (isEmpty()) throw new RuntimeException("MyStack underflow"); String item = backingArray[--index]; backingArray[index] = null; // null out to prevent memory leak return item; } public boolean isEmpty() { return index == 0; } public int size() { return index; } public boolean isFull() { return index == backingArray.length; } public String peek() { if (isEmpty()) throw new RuntimeException("MyStack underflow"); return backingArray[index - 1]; } @Override public Iterator<String> iterator() { return new StackIterator(); } class StackIterator implements Iterator<String> { private int i = index - 1; @Override public boolean hasNext() { return i >= 0; } @Override public String next() { if (!hasNext()) { throw new NoSuchElementException(); } return backingArray[i--]; } @Override public void remove() { throw new UnsupportedOperationException(); } } } } |
A. The “MyStack” class is not properly using “generics”. It only works with “String” class. Here is the revised solution using generics to work with any object type. … Read more ›...
A trivial coding example (i.e. a Calculator) tackled using the following programming paradigms in Java not only to perform well in coding interviews, but also to learn these programming paradigms.
Approach 1: Procedural Programming
Approaches 2 – 4: Object Oriented Programming
Approach 5: Functional Programming (Java 8)
Approach 1: Procedural
|
1 2 3 |
public interface Calculate { abstract int calculate(int operand1, int oerand2, Operator operator); } |
|
1 2 3 |
public enum Operator { ADD, SUBTRACT, DIVIDE, MULTIPLY; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class CalculateImpl implements Calculate { @Override public int calculate(int operand1, int operand2, Operator operator) { switch (operator) { case ADD: return operand1 + operand2; case SUBTRACT: return operand1 - operand2; case MULTIPLY: return operand1 * operand2; case DIVIDE: return operand1 / operand2; } throw new RuntimeException(operator + "is unsupported"); } } |
|
1 2 3 4 5 6 7 8 9 10 11 |
public class CalculatorTest { public static void main(String[] args) { Calculate calc = new CalculateImpl(); int result = calc.calculate(5,6,Operator.ADD); result = calc.calculate(result,6,Operator.MULTIPLY); result = calc.calculate(result,1,Operator.SUBTRACT); result = calc.calculate(result,5,Operator.DIVIDE); System.out.println("result=" + result); } } |
Output: result=13
Approach 2: OOP
|
1 2 3 |
public interface MathCommand<E> { abstract E execute(E operand1, E operand2); } |
|
1 2 3 4 5 6 7 |
public class AddCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 + operand2; } } |
|
1 2 3 4 5 6 7 |
public class SubtractCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 - operand2; } } |
|
1 2 3 4 5 6 7 |
public class MultiplyCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 * operand2; } } |
|
1 2 3 4 5 6 7 |
public class DivideCommand implements MathCommand<Integer>{ @Override public Integer execute(Integer operand1, Integer operand2) { return operand1 / operand2; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class CalculatorTest2 { public static void main(String[] args) { MathCommand<Integer> command = new AddCommand(); Integer result = command.execute(5, 6); command = new MultiplyCommand(); result = command.execute(result, 6); command = new SubtractCommand(); result = command.execute(result, 1); command = new DivideCommand(); result = command.execute(result, 5); System.out.println("result=" + result); } } |
When you have more mathematical operations,
…
SOAP Web Services interview Questions & Answers Links: 6 Java RESTful Web services Interview | 5 JAXB interview Questions & Answers | Java Web Services interview Questions & Answers Q1. What are the different approaches to developing a SOAP based Web service? … Read more ›...
Q. Complete the method “Set getAllCombinations(String input) ” where input is any string and the method should return all possible combinations of a given string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class WordPermutations { public static Set<String> getAllCombinations(String input) { throw new RuntimeException("Complete the given code"); } public static void main(String[] args) { Collection<String> words = getAllCombinations("abc"); for (String word : words) System.out.println(word); } } |
For example: “abc” will have a permutation of 3! … Read more ›...
There are certain generic columns that database tables have like Auto generated identity columns. Auditing columns like createdDtTm, createdBy, modifiedDtTm, and modifiedBy. Soft delete or logical delete flags like inactive ‘Y’ or ‘N’. Optimistic locking detection based on columns like Timestamp or version number. … Read more ›...
JMockit offers a set of utility fields for making argument matching more generic. 1) anyX fields like anyInt, anyBoolean, anyString, etc. There is one for each primitive type, and the corresponding wrapper class, … Read more ›...