Core Java Interview Questions & Programs - Yash-777/LearnJava GitHub Wiki
Input:
int array[] = { 0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 20, 21, 23 };
Output:
Missing number(s): 5, 16, 17, 19, 22.
List list = new ArrayList();
// To get least and highest value sort array
Arrays.sort( arr );
int least = array[0], highest = arrar[ array.length - 1 ]
list.add( ... ); // For each 0 - 23
list.remove( array[0] ); // For each of array and remove from list.
list.removeAll(Arrays.asList( array ));
//Java 8
list.removeIf(s -> s.equals( 1 )); // removes all instances, not just the 1st one
int[] numbers = { 1, 5, 23, 2, 1, 6, 3, 1, 8, 12, 3 };
Arrays.sort(numbers);
for(int i = 1; i < numbers.length; i++) {
if(numbers[i] == numbers[i - 1]) {
System.out.println("Duplicate: " + numbers[i]);
}
}
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (char c : str.toCharArray()) {
map.merge(c, 1, (prevValue, newValue) -> prevValue + newValue );
}
int maxValueInMap = Collections.max( map.values() );
for (Entry<Character, Integer> entry : map.entrySet()) {
if ( entry.getValue() == maxValueInMap ) {
System.out.format("Char ['%s'] repeated %d times\n", entry.getKey(), maxValueInMap);
}
}
- Factorial Function [n! = n Ă (nâ1)!], Print 1-50 numbers with out using any loops.
Example: 5! = 5 * 4 * 3 * 2 * 1
public static int factorial(int n) {
if( n==0 || n==1 ) {
return 1;
} else {
return n * factorial(n-1);
}
}
public static void recurciveMethod( int val ) {
System.out.println( val );
if( val < 100 ) {
recurciveMethod( ++val );
}
}
- Fibonacci series Sum of the two preceding numbers. The simplest is the series 0, 1, 1, 2, 3, 5, 8, etc.
int x = 0, y = 1;
for(int i=0; i<10; i++) {
z = x + y;
System.out.println(z);
x = y;
y = x;
}
- Perfect Number and Armstrong Number.
perfect number, a positive integer that is equal to the sum of its proper divisors.
The smallest perfect number is 6, which is the sum of 1, 2, and 3
-
Collections HashMap, ConcurentHashMap, SynchronisedHashMap, UnmodifiableHashMap
HashTable
Push and Pop method classes in List