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:
- publicstatic int parseInt(String s,int redix)
Let’s see the simple example of converting binary to decimal in java.
- publicclass BinaryToDecimalExample1{
- publicstatic void main(String args[]){
- String binaryString=”1010″;
- intdecimal=Integer.parseInt(binaryString,2);
- out.println(decimal);
- }}
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:
- publicstatic String toBinaryString(int decimal)
Let’s see the simple example of converting decimal to binary in java.
- publicclass DecimalToBinaryExample1{
- publicstatic void main(String args[]){
- out.println(Integer.toBinaryString(10));
- out.println(Integer.toBinaryString(21));
- out.println(Integer.toBinaryString(31));
- }}
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.
- publicclass MatrixMultiplicationExample{
- publicstatic void main(String args[]){
- //creating two matrices
- inta[][]={{1,1,1},{2,2,2},{3,3,3}};
- intb[][]={{1,1,1},{2,2,2},{3,3,3}};
- //creating another matrix to store the multiplication of two matrices
- intc[][]=new int[3][3]; //3 rows and 3 columns
- //multiplying and printing multiplication of 2 matrices
- for(inti=0;i<3;i++){
- for(intj=0;j<3;j++){
- c[i][j]=0;
- for(intk=0;k<3;k++)
- {
- c[i][j]+=a[i][k]*b[k][j];
- }//end of k loop
- out.print(c[i][j]+” “); //printing matrix element
- }//end of j loop
- out.println();//new line
- }
- }}
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