This blog is about 10 most frequently asked java interview programs, As I live in northern India that's why I can say these are the most trending asked java programs in interviews at this place. And I am sure if you will have the knowledge of these java programs you can also do some complex programming on behalf of this logic.
1. Reverse Array
public class ReverseArray {
public static void main(String arg[]) {
int size, i, j, temp;
int arr[] = {1,2,3,4,5,6,7,8,9};
j = arr.length-1;
i=0;
while(i<j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
System.out.println("Reverse Array");
for(i=0; i<arr.length; i++) {
System.out.print(arr[i]);
}
}
}
OUTPUT
Reverse Array
987654321
2. Binary Search
public class BinarySearch {
int binarySearch(int arr[], int l, int r, int x) {
if (r>l) {
int mid = l+(r-l)/2;
if(arr[mid]==x)
return mid;
if(arr[mid]>x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
public static void main(String arg[]) {
BinarySearch obj= new BinarySearch();
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int n = arr.length;
int x = 6;
int result = obj.binarySearch(arr, 0, n-1, x);
if(result == -1)
System.out.println("Element not found.");
else
System.out.println("Element found at "+ result + "th index");
} }
OUTPUT
Element found at 5th index
3. Prime Number
public class PrimeNumber {
public static void main(String arg[]) {
int count = 0;
int n=17;
for(int i=2; i<=n/2; i++) {
if(n%i == 0) {
System.out.println("Not Prime");
count++;
break;
}
}
if(count == 0) {
System.out.println(n+" Is Prime Number");
}
} }
OUTPUT
17 Is Prime Number
4. Invert 0 to 1 and 1 to 0
/**
*
* @author Suraj Pratap Singh
* 5 ways to convert 1 to 0 and 0 to 1,
* Without using if, else, switch case.
*/
public class InvertNumber {
public static void main(String arg[]) {
System.out.println(invertMethod1(0));
System.out.println(invertMethod2(0));
System.out.println(invertMethod3(0));
System.out.println(invertMethod4(0));
System.out.println(invertMethod5(0));
}
public static int invertMethod1(int value) {
return 1-value;
}
public static int invertMethod2(int value) {
return 1-(value % 2);
}
public static int invertMethod3(int value) {
return 1-(value & 1);
}
public static int invertMethod4(int value) {
final int[] VALUES = {1, 0};
try {
return VALUES[value];
} catch(ArrayIndexOutOfBoundsException ex) {
return 0;
}
}
public static int invertMethod5(int value) {
return value ^ 1;
} }
OUTPUT
1
1
1
1
1
5. Reverse String
public class ReverseString {
public static void main(String arg[]){
String str = "Hello World";
String rev = "";
for(int i=str.length()-1; i>=0; i--) {
rev += str.charAt(i);
}
System.out.println(rev);
}
}
OUTPUT
dlroW olleH
6. Reverse Each Word In Sentence
public class ReverseEachWord {
public static void main(String arg[]) {
String str = "Java Is Object Oriented Programing";
String arr[] = str.split(" ");
String revstr = "";
for(int i=0; i<arr.length; i++) {
String word = arr[i];
String revwrd = "";
for(int j=word.length()-1; j>=0; j--) {
revwrd += word.charAt(j);
}
revstr = revstr + revwrd + " ";
}
System.out.println(str);
System.out.println(revstr);
} }
OUTPUT
Java Is Object Oriented Programing
avaJ sI tcejbO detneirO gnimargorP
7. Second Largest Number In Array
public class SecondLargestNumber {
public static void main(String arg[]) {
int num[] = {6,3,37,12,46,5,64,21};
int h1 = 0;
int h2 = 0;
for(int n: num) {
if(h1 < n) {
h2 = h1;
h1 = n;
} else if(h2 < n) {
h2 = n;
}
}
System.out.println("First Largest Number: "+ h1);
System.out.println("Second Largest Number: "+ h2);
}
}
OUTPUT
First Largest Number: 64
Second Largest Number: 46
8. Reverse String Without Moving Special Characters.
public class ReverseWithoutSpecialCharacter {
public static void main(String arg[]) {
System.out.println("suraj@pra!tap&Sing#h");
System.out.println(rev("suraj@pra!tap&Sing#h"));
}
public static String rev(String str) {
char arr[] = str.toCharArray();
int l = 0;
int r = arr.length-1;
while(l<r) {
if(!Character.isAlphabetic(arr[l])) {
l++;
} else if(!Character.isAlphabetic(arr[r])){
r--;
} else {
char temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
l++;
r--;
}
}
return new String(arr);
} }
OUTPUT
suraj@pra!tap&Sing#h
hgniS@pat!arp&jaru#s
9. Sort String
public class SortString {
public static void main(String arg[]) {
String str = "mlpnkobjivhucgyxftzdrsewaq";
char temp = 0;
char []arr = str.toCharArray();
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length; j++) {
if(arr[j]>arr[i]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int k = 0; k<arr.length; k++) {
System.out.print(arr[k]);
}
}
}
OUTPUT
abcdefghijklmnopqrstuvwxyz
10. Sum Without + Operator
public class SumWithoutUsingOperator {
public static void main(String arg[]) {
int a = 13, b = 34;
System.out.println(a+" + "+b+" = "+add(a,b));
}
public static int add(int a, int b) {
while(b!=0) {
int carry = (a & b);
a = a ^ b;
b = carry << 1;
}
return a;
}
}
Comments
Post a Comment