RYTWAYS Fresher Interview Questions with Answers – Part 2

RYTWAYS Fresher Interview Questions with Answers – Part 2

This blog explains about RYTWAYS Fresher Interview Questions with Answers – Part 2 and is below :

    We have already discussed some questions in our previous blog RYTWAYS Fresher Interview Questions with Answers – Part 1 

6 . Write a Java program to swap two bits without using any variable . 

// C program to swap bits in an intger

#include<stdio.h>

 

// This function swaps bit at positions p1 and p2 in an integer n

int swapBits(unsigned int n, unsigned int p1, unsigned int p2)

{

    /* Move p1’th to rightmost side */

    unsigned int bit1 =  (n >> p1) & 1;

 

    /* Move p2’th to rightmost side */

    unsigned int bit2 =  (n >> p2) & 1;

 

    /* XOR the two bits */

    unsigned int x = (bit1 ^ bit2);

 

    /* Put the xor bit back to their original positions */

    x = (x << p1) | (x << p2);

 

    /* XOR ‘x’ with the original number so that the

       two sets are swapped */

    unsigned int result = n ^ x;

}

 

/* Drier program to test above function*/

int main()

{

    int res =  swapBits(28, 0, 3);

    printf(“Result = %d “, res);

    return 0;

}

Output:

Result = 21

7 . Write a java program to swap two strings using a temporary variable .

// Java program to swap two strings without using a temporary

// variable.

import java.util.*;

 

class Swap

{    

    public static void main(String args[])

    {

        // Declare two strings

        String a = “Hello”;

        String b = “World”;

 

        // Print String before swapping

        System.out.println(“Strings before swap: a = ” + 

                                       a + ” and b = “+b);

 

        // append 2nd string to 1st

        a = a + b;

 

        // store intial string a in string b

        b = a.substring(0,a.length()-b.length());

 

        // store initial string b in string a

        a = a.substring(b.length());

 

        // print String after swapping

        System.out.println(“Strings after swap: a = ” + 

                                     a + ” and b = ” + b);        

    }    

}

Output:

Strings before swap: a = Hello and b = World

Strings after swap: a = World and b = Hello

8. Write a Java program to convert Binary to Decimal .

Java Convert Binary to Decimal

We can convert binary to decimal in java using Integer.parseInt() method or custom logic.

Java Binary to Decimal conversion: Integer.parseInt()

The Integer.parseInt() method converts string to int with given redix. The signature of parseInt() method is given below:

  1. publicstatic int parseInt(String s,int redix)  

Let’s see the simple example of converting binary to decimal in java.

  1. publicclass BinaryToDecimalExample1{  
  2. publicstatic void main(String args[]){  
  3. String binaryString=”1010″;  
  4. intdecimal=Integer.parseInt(binaryString,2);  
  5. out.println(decimal);  
  6. }}  

 

Output:

10

9. Write a Java program to convert Decimal to Binary .

Java Convert Decimal to Binary

We can convert decimal to binary in java using Integer.toBinaryString() method or custom logic.

Java Decimal to Binary conversion: Integer.toBinaryString()

The Integer.toBinaryString() method converts decimal to binary string. The signature of toBinaryString() method is given below:

  1. publicstatic String toBinaryString(int decimal)  

Let’s see the simple example of converting decimal to binary in java.

  1. publicclass DecimalToBinaryExample1{  
  2. publicstatic void main(String args[]){  
  3. out.println(Integer.toBinaryString(10));  
  4. out.println(Integer.toBinaryString(21));  
  5. out.println(Integer.toBinaryString(31));  
  6. }}  

 

Output:

101010101

11111

10 .  Write a Java program for matrix multiplication .

Let’s see a simple example to multiply two matrices of 3 rows and 3 columns.

In case of matrix multiplication, one row element of first matrix is multiplied by all columns of second matrix.

  1. publicclass MatrixMultiplicationExample{  
  2. publicstatic void main(String args[]){  
  3. //creating two matrices  
  4. inta[][]={{1,1,1},{2,2,2},{3,3,3}};    
  5. intb[][]={{1,1,1},{2,2,2},{3,3,3}};    
  6. //creating another matrix to store the multiplication of two matrices  
  7. intc[][]=new int[3][3];  //3 rows and 3 columns  
  8. //multiplying and printing multiplication of 2 matrices  
  9. for(inti=0;i<3;i++){    
  10. for(intj=0;j<3;j++){    
  11. c[i][j]=0;      
  12. for(intk=0;k<3;k++)      
  13. {      
  14. c[i][j]+=a[i][k]*b[k][j];      
  15. }//end of k loop
  16. out.print(c[i][j]+” “);  //printing matrix element
  17. }//end of j loop
  18. out.println();//new line  
  19. }    
  20. }}  

 

Output:

6 6 6

12 12 12

18 18 18

REFERENCES : 

https://payilagam.com/blogs/rytways-fresher-interview-questions-with-answers-part-1/

https://www.geeksforgeeks.org/how-to-swap-two-bits-in-a-given-integer/

https://www.geeksforgeeks.org/swap-two-strings-without-using-third-user-defined-variable-in-java/

https://www.javatpoint.com/java-binary-to-decimal

https://www.javatpoint.com/java-decimal-to-binary

https://www.javatpoint.com/java-program-to-multiply-two-matrices