Esfita Infotech Interview Questions

This blog explains about  Esfita Infotech Interview Questions and Answer is given below :

Esfita Infotech Pvt.Ltd:

Find the string count of upper case ,Lower case, special characters:

Given a string, write a program to count the occurrence of Lowercase characters, Uppercase characters, Special characters and Numeric values.

 Examples:

Input : #GeeKs01fOr@gEEks07
Output : 
Upper case letters : 5
Lower case letters : 8
Numbers : 4
Special Characters : 2

Input : *GeEkS4GeEkS*
Output :
Upper case letters : 6
Lower case letters : 4
Numbers : 1
Special Characters : 2

Java program to count the uppercase,

// lowercase, special characters 

// and numeric values

import java.io.*; 

class Count

{

    public static void main(String args[])

    {

        String str = "#GeeKs01fOr@gEEks07";

        int upper = 0, lower = 0, number = 0, special = 0;

        for(int i = 0; i < str.length(); i++)

        {

            char ch = str.charAt(i);

            if (ch >= 'A' && ch <= 'Z')

upper++;

            else if (ch >= 'a' && ch <= 'z')

                lower++;

            else if (ch >= '0' && ch <= '9')

                number++;

            else

                special++;

        }

        System.out.println("Lower case letters : " + lower);

        System.out.println("Upper case letters : " + upper);

        System.out.println("Number : " + number);

        System.out.println("Special characters : " + special);

    }

}

Output:

Upper case letters: 5
Lower case letters : 8
Number : 4
Special characters : 2

———————————————————————————————————————————————————————–

How to check if a string contains only dights :

The following is our string.

String str = "4434";

To check whether the above string has only digit characters, try the following if condition that uses matches() method and checks for every character.

Example:

if(str.matches("[0-9]+") && str.length() > 2) {
   System.out.println("String contains only digits!");
}
public class Demo {
   public static void main(String []args) {
      String str = "4434";
      if(str.matches("[0-9]+") && str.length() > 2) {
         System.out.println("String contains only digits!");
      }
   }
}


Output
String contains only digits!

---------------------------------------------------------------------------------------------

write a program to determine whether two matrices are equal or not ?

Two matrices are identical if their number of rows and columns are equal and the corresponding elements are also equal. An example of this is given as follows.

Matrix A = 1 2 3
   4 5 6
   7 8 9
Matrix B = 1 2 3
   4 5 6
   7 8 9
The matrices A and B are identical

A program that checks if two matrices are identical is given as follows.

Example:
public class Example {
   public static void main (String[] args) {
      int A[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} }; 
      int B[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} };
      int flag = 1;
      int n = 3;
      for (int i = 0; i < n; i++)
         for (int j = 0; j < n; j++)
            if (A[i][j] != B[i][j]) 
               flag = 0;
      if (flag == 1)
         System.out.print("Both the matrices are identical");
      else
         System.out.print("Both the matrices are not identical");
   }
}
Output
Both the matrices are identical

Now let us understand the above program.

The two matrices A and B are defined. The initial value of flag is 1. Then a nested for loop is used to compare each element of the two matrices. If any corresponding element is not equal, then value of flag is set to 0. The code snippet that demonstrates this is given as follows −

int A[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} };
int B[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} };
int flag = 1;
int n = 3;
for (int i = 0; i < n; i++)
   for (int j = 0; j < n; j++)
      if (A[i][j] != B[i][j])
         flag = 0;

If flag is 1, then matrices are identical and this is displayed. Otherwise, the matrices are not identical and this is displayed. The code snippet that demonstrates this is given as follows −

if (flag == 1) System.out.print(“Both the matrices are identical”); else System.out.print(“Both the matrices are not identical”);

———————————————————————————————————————————————————————–

How to remove duplicate elements from  Array List in Java:

To remove the duplicates from the arraylist, we can use the java 8 stream api as well. Use steam’s distinct() method which returns a stream consisting of the distinct elements comparing by object’s equals() method.

Collect all district elements as List using Collectors.toList().

Example:

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;
 
public class ArrayListExample
{
    public static void main(String[] args)
    {
        // ArrayList with duplicate elements
        ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
         
        System.out.println(numbersList);
 
        List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());
         
        System.out.println(listWithoutDuplicates);
    }
}
Output:
[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
———————————————————————————————————————————————————————–