## Qantler Technologies Fresher Interview Questions with Answers – Part 3

### 1. Sort the given user input in descending order.

In order to sort in descending order we just need to change the logic array[j] > array[j+1] to array[j] < array[j+1] in the above program. Complete code as follows:

import java.util.Scanner;

class BubbleSortExample {

public static void main(String []args) {

int num, i, j, temp;

Scanner input = new Scanner(System.in);

System.out.println(“Enter the number of integers to sort:”);

num = input.nextInt();

int array[] = new int[num];

System.out.println(“Enter ” + num + ” integers: “);

for (i = 0; i < num; i++)

array[i] = input.nextInt();

for (i = 0; i < ( num – 1 ); i++) {

for (j = 0; j < num – i – 1; j++) {

if (array[j] < array[j+1])

{

temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;

}

}

}

System.out.println(“Sorted list of integers:”);

for (i = 0; i < num; i++)

System.out.println(array[i]);

}

}

Output:

Enter the number of integers to sort:6

Enter 6 integers:

89

12

45

9

56

102

Sorted list of integers:

102

89

56

45

12

9

### 2. Write a program to generate Fibonacci series (0,1,1,2,3,5,8…).

publicclassFibonacci{

publicstaticvoid main(String[] args){

int n =10, t1 =0, t2 =1;

System.out.print(“First “+ n +” terms: “);

for(int i =1; i <= n;++i)

{

System.out.print(t1 +” + “);

int sum = t1 + t2;

t1 = t2;

t2 = sum;

}

}

}

When you run the program, the output will be:

21.0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 +

### 3. Write a program to reverse a given number.

1. Reverse a Number using a while loop in Java
2. public class ReverseNumber {
3. public static void main(String[] args) {
4. int num = 1234, reversed = 0;
5. while(num != 0) {
6. int digit = num % 10;
7. reversed = reversed * 10 + digit;
8. num /= 10;
9. }
10. System.out.println(“Reversed Number: ” + reversed);
11. }
• }
1. When you run the program, the output will be:
• Reversed Number: 4321

### 4. Write a program to do 2 X 2 matrix multiplication:

Get input, generate and show the first 2 X 2 matrix.

Get input, generate and show the second 2 X 2 matrix.

Multiple the matrixes and show the output matrix.

 // Java program to multiply two square // matrices. import java.io.*;   class GFG {         static int N = 4;       // This function multiplies mat1[][]     // and mat2[][], and stores the result     // in res[][]     static void multiply(int mat1[][],                    int mat2[][], int res[][])     {         int i, j, k;         for (i = 0; i < N; i++)         {             for (j = 0; j < N; j++)             {                 res[i][j] = 0;                 for (k = 0; k < N; k++)                     res[i][j] += mat1[i][k]                                  * mat2[k][j];             }         }     }       // Driver code     public static void main (String[] args)      {         int mat1[][] = { {1, 1, 1, 1},                          {2, 2, 2, 2},                          {3, 3, 3, 3},                          {4, 4, 4, 4}};           int mat2[][] = { {1, 1, 1, 1},                          {2, 2, 2, 2},                          {3, 3, 3, 3},                          {4, 4, 4, 4}};           // To store result         int res[][] = new int[N][N] ;         int i, j;         multiply(mat1, mat2, res);           System.out.println(“Result matrix”                                 + ” is “);         for (i = 0; i < N; i++)         {             for (j = 0; j < N; j++)                 System.out.print( res[i][j]                                     + ” “);             System.out.println();         }     } }   // This code is contributed by anuj_67.

Output:

Result matrix is

10 10 10 10

20 20 20 20

30 30 30 30

40 40 40 40

### 5. Write a program to print pyramid using numbers:

1

232

3 4 5 4 3

4 5 6 7 6 5 4

5 6 7 8 9 8 7 6 5

Java Program :

1. importutil.Scanner;
2. public class MainClass
3. {
4. public static void main(String[] args)
5. {
6. Scanner sc = new Scanner(System.in);
7. //Taking rows value from the user
• System.out.println(&quot;How many rows you want in this pattern?&quot;);
• int rows = sc.nextInt();
• for (int i = 1; i <= rows; i++)
• {
• for (int j = 1; j <= i; j++)
• {
• System.out.print(j+&quot; &quot;);
• }
• System.out.println();
• }
• //Close the resources
• close();
• }
• }

Output :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7

### 6. Write a program to find the factorial of a given number using recursion.

/**

* @author: BeginnersBook.com

* @description: User would enter the 10 elements

* and the program will store them into an array and

* will display the sum of them.

*/import java.util.Scanner;

class FactorialDemo{

public static void main(String args[]){

//Scanner object for capturing the user input

Scanner scanner = new Scanner(System.in);

System.out.println(“Enter the number:”);

//Stored the entered value in variable

int num = scanner.nextInt();

//Called the user defined function fact

int factorial = fact(num);

System.out.println(“Factorial of entered number is: “+factorial);

}

static int fact(int n)

{

int output;

if(n==1){

return 1;

}

//Recursion: Function calling itself!!

output = fact(n-1)* n;

return output;

}

}

Output:

Enter the number:5Factorial of entered number is: 120

### 7. Write a program to find duplicates in an array.

Java Code:

public class Exercise13 {

public static void main(String[] args)

{

String[] my_array = {“bcd”, “abd”, “jude”, “bcd”, “oiu”, “gzw”, “oiu”};

for (int i = 0; i < my_array.length-1; i++)

{

for (int j = i+1; j < my_array.length; j++)

{

if( (my_array[i].equals(my_array[j])) && (i != j) )

{

System.out.println(“Duplicate Element is : “+my_array[j]);

}

}

}

}

}

Copy

Sample Output:

Duplicate Element is : bcd

### 8. Write a program to remove special characters from a give string.

public class App{

public static void main(String args[]) {

String text = “This – text ! has \\ /allot # of % special % characters”;

text = text.replaceAll(“[^a-zA-Z0-9]”, “”);

System.out.println(text);

String html = “This is bold”;

html = html.replaceAll(“[^a-zA-Z0-9\\s+]”, “”);

System.out.println(html);

}

}

Output

This text has a lot of special characters b.

This is bold b