Simple Logical Program And Java Core Interview questions

Sanjay Singh
8 min readNov 14, 2019

interview questions.

1-WAP find factorial of a given number using java .

a -by using loop

public static int findFactorial(int number) {
int fact = 1;
for (int i = number; i > 0; i — )
fact = fact * i;
return fact;
}

b-find factorial of a given number using recursion

public static int recursionFact(int number) {
if (number == 0) {
return 1;
}
return number * recursionFact(number — 1);
}

2- WAP reverse the number in java.

public static int reverseNumber(int number) {
int rem;
int rev = 0;
while (number != 0) {
rem = number % 10;
rev = rev * 10 + rem;
number = number / 10;
}
return rev;
}

3-WAP reverse the String in java.

public static String reverseString(String name) {
String reverse = “”;
for (int i = name.length()-1; i >= 0; i — ) {
reverse = reverse + name.charAt(i);
}
return reverse;
}

4- WAP palindrome number program .

A palindromic number is a number that remains the same when its digits are reversed. Like 16461
16461 < — — →16461
121 < — — — ->121
public static String palindromeNumber(int number) {
int temp = number;
int rem = 0;
int rev = 0;
while (number != 0) {
rem = number % 10;
rev = rev * 10 + rem;
number = number / 10;
}
if (rev == temp) {
return “number is palindrome “;
}
return “number is not a palindrome “;
}

5- WAP find max number in array .

public static int max(int[] array) {
int max = array[0];
for (int i = 0; i < array.length — 1; i++)
if (max < array[i])
max = array[i];
return max;
}

6- WAP find min number in array .

public static int max(int[] array) {
int min= array[0];
for (int i = 0; i < array.length — 1; i++)
if (min >array[i])
min= array[i];
return min;
}

7-WAP to sort array in java .

We can sort array two type by using loop and Arrays.sort(array)
a-By using loop
public static int[] sortArray(int[] array) {
int temp = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;}}}
return array;
}
testing — — — — —
int[] array = { 10, 20, 50, 1, 5, 7, 90 };
int[] example = GratestNumber.sortArray(array);
for (int i : example) {System.out.println(i);}

B-Sort By using Arrays.sort(array)

public class Sorting {
public static void main (String [] args) {
int [] array = {45,12,85,32,89,39,69,44,42,1,6,8};
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
};}}

8-WAP Find Second largest number in array.

we need to sort array than we can find K’th element .

9 WAP to find duplicate in array .

public static void findDublicate(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[i] == array[j]) {
System.out.println(array[j]);
}}}

10- WAP How to create custom arraylist in java.

a- ArrayList is a re-sizable array

public class CustomList {

private Object[] object = new Object[10];

private int count = 0;

public void add(Object obj) {

if (count == object.length) {
incressCapicity();

}
object[count] = obj;
System.out.println(count);
count++;

}
public void incressCapicity() {

int capicity = object.length * 2;
Object[] NexObject = new Object[capicity];

for (int i = 0; i < object.length; i++) {
NexObject[i] = object[i];
}
object = NexObject;
}
@Override
public String toString() {
return “CustomList [object=” + Arrays.toString(object) + “, count=” + count + “]”;
}

Testing Class

public class ExampleTesting {
public static void main(String[] args) {
CustomList list = new CustomList();
for (int i = 0; i < 50; i++) {
list.add(i);
}
System.out.println(list);
}

B-It is used to fetch the element from the particular position of the list.

public Object getObject(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
return object[index];
}

11 -Can I add StringBuffer objects in TreeSet ?

if you want to add StringBuffer in TreeSet .we need follow some rule .
1- Object should be homogeneous
2- Object Should be comparable.

String is comparable ,But StringBuffer is not a comparable.

How to solve this problem ?

We need to create custom sorting logic by using comparator Interface and then use it in your TreeSet as follows:

Step-1
public class CustomExample implements Comparator<StringBuffer> {
@Override
public int compare(StringBuffer o1, StringBuffer o2) {
return o1.toString().compareTo(o2.toString());
}}
Step2 -
public class ExampleTesting {
public static void main(String[] args) {
TreeSet<StringBuffer> t1 = new TreeSet<>(new CustomExample());
t1.add(new StringBuffer(“Sanjay Singh”));
t1.add(new StringBuffer(“Singh”));
t1.add(new StringBuffer(“action”));
System.out.println(t1);
}
output — [Sanjay Singh, Singh, action]

12 -WAP to sort array accordingly ……

input array : -[90,10,40,20,30,60,50,70,80]
Output sequence : 90,10,80,20,70,30,60,40,50

Step 1 — we need to sort this array

int[] box = { 90, 10, 40, 20, 30, 60, 50, 70, 80 };
Arrays.sort(box);

Step 2

public class ExampleTesting {
public static void main(String[] args) {
int[] box = { 90, 10, 40, 20, 30, 60, 50, 70, 80 };
Arrays.sort(box);

int n = box.length;
int[] x = SortingOrder.sortAccording(box, n);
for (int i : x) {
System.out.print(“ “ + i);
}}}

Step 3 — Logic

public class SortingOrder {
public static int[] sortAccording(int[] array, int n) {
int[] temp = new int[n];
int small = 0;
int large = n — 1;
boolean flag = true;
for (int i = 0; i < n; i++) {
if (flag)
temp[i] = array[large — — ];
else
temp[i] = array[small++];
flag = !flag;}
return temp;}}

Program

13- WAP to Sort basic on two attribute.

Sort on the basic of salary but if two employee salary has been same then it sort on the basic of name .

Question 1

By using Java 8

Sort basic on two attribute

list.sort(Comparator.comparing(Student::getAge).thenComparing(Student::getName));

public class Example {
public static void main(String[] args) {
Student student1 = new Student(25, “Sanjay”, “TBC”);
Student student2 = new Student(28, “Ajay”, “AXY”);
Student student3 = new Student(55, “Ramesh”, “Delhi”);
Student student4 = new Student(55, “Karan”, “Vnc”);
Student student5 = new Student(55, “mustfa”, “Tata”);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
list.sort(Comparator.comparing(Student::getAge).thenComparing(Student::getName));
System.out.println(list);
}}

Example of Java 7 —

Employee class

public class Employee implements Comparable<Employee> {
private int sal;
private String name;
public Employee(int sal, String name) {
super();
this.sal = sal;
this.name = name;
}
@Override
public String toString() {
return “Employee [sal=” + sal + “, name=” + name + “]”;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Employee o) {
// TODO Auto-generated method stub
return o.getName().compareTo(getName());
}}

Custom Logic

public class CustomExample implements Comparator<Employee> {
@Override
public int compare(Employee o1, Employee o2) {
if (o1.getSal() > o2.getSal())
return 1;
if (o1.getSal() < o2.getSal())
return -1;
if (o1.getSal() == o2.getSal()) {
o1.getName().compareTo(o2.getName());}
return 0;
}}

Testing

package example.Core.testing;
public class ExampleTesting {
public static void main(String[] args) {
Employee emp1 = new Employee(1500, “SANJAY”);
Employee emp2 = new Employee(1900, “Raj”);
Employee emp3 = new Employee(1500, “Taj”);
Employee emp4 = new Employee(1900, “xyz”);
Employee emp5 = new Employee(1700, “SAN”);
Employee emp6 = new Employee(1600, “Zaj”);
List<Employee> list = new ArrayList<>();
list.add(emp1);
list.add(emp2);
list.add(emp3);
list.add(emp4);
list.add(emp5);
list.add(emp6);
Collections.sort(list, new CustomExample());
for (Employee employee : list) {
System.out.println(employee);
}}}

15- WAP Int to String in java .

Scanner in = new Scanner(System.in);
int n = in .nextInt();
in.close();
String s = Integer.toString(n);

Ques -How do I retrieve only string objects from heterogeneous ArrayList in Java?

public class ArraysDS {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
list.add(1);
list.add(“Sanjay”);
list.add(“ABX”);
list.add(25);
list.add(30);
list.add(“San”);
list.add(“AB”);
System.out.println(list);
List l1 = list.stream().filter(item -> item instanceof String).collect(Collectors.toList());
System.out.println(l1);
List l2 = list.stream().filter(item -> item instanceof Integer).collect(Collectors.toList());
System.out.println(l2);

}
Output -
[1, Sanjay, ABX, 25, 30, San, AB]
[Sanjay, ABX, San, AB]
[1, 25, 30]

16-WAP Capitalize the first letter in and and print them on a single line, separated by a space.

Example :

hello
java
Output
Hello Java

public class JavaLoops1 {
public static void main(String[] args) {
String A = “hello”;
String B = “java”;
System.out.println(A.substring(0, 1).toUpperCase() + A.substring(1) + “ “ + B.substring(0, 1).toUpperCase()
+ B.substring(1));
}

17 WAP nested if condition Condision

if number is odd print odd.
if number is even b/w 0–10 print “Hi”
if number is even b/w 10–20 print “Hello”
if number is even and grater then 20 print “Namste"

public class Example {
public static void main(String[] args) {
System.out.println(“Please Enter number”);
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
if (number % 2 != 0) {
System.out.println(“odd”);
} else if (number >= 0 && number <= 10) {
System.out.println(“Hi”);
} else if (number > 10 && number <= 20) {
System.out.println(“Hello”);
} else if (number > 20) {
System.out.println(“Namste”);
}}}

Ques -18 How to Read n number String

Sample Input
Hello world
I am a file
Read me until end-of-file.

Sample Output

1 Hello world
2 I am a file
3 Read me until end-of-file.

Program // For loop 
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
for(int i = 1; scan.hasNext()== true; i++){
System.out.println(i + " " + scan.nextLine());
}
}

// While loop System
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 0;
while(scan.hasNext()){
i++;
System.out.println(i + " " + scan.nextLine());
}
}

--

--

Sanjay Singh

Java||Spring-Boot|| Micro services Application developer|| INDIA