Most Frequently Asked Java Interview Programs. PART-1

    Hi friends, 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. Compare Two String Anagram


import java.util.Arrays;
import java.util.Scanner;
public class Anagram {
public static void main(String arg[]) {
String str1 = "suraj";
String str2 = "arjus";
if (isAnagram(str1, str2)) {
System.out.println(str1 + " is anagram of "+ str2);
} else {
System.out.println(str1 + " is not anagram of "+ str2);
}
}
private static boolean isAnagram(String str1, String str2) {
if(str1.length() != str2.length()) {
return false;
}
str1 = sortChar(str1);
str2 = sortChar(str2);
return str1.equals(str2);
}
private static String sortChar(String str) {
char [] arr = str.toLowerCase().toCharArray();
Arrays.sort(arr);
return String.valueOf(arr);
}
}

2. Count Vowel From Text File

import java.io.File;
public class CountVowel {
    public static void main(String arg[]) throws Exception{
 
        File file = new File("First.txt");
        Scanner sc = new Scanner(file);
        String str = "";
        while(sc.hasNext()) {
            str += sc.next() + "";
        }
        sc.close();
        char []arr = str.toCharArray();
        int count = 0;
        for(char c: arr) {
            if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
                count++;
        }
        System.out.println("Vowels in file: "+ count);
    }
}

3. Area Of Circle


import java.util.Scanner;
public class AreaOfCircle {

public static void main(String arg[]) {
int r=12;
double area = Math.PI * r * r;
double prm = 2 * Math.PI * r;
System.out.println("Area of circle: "+ area);
System.out.println("Parameter of circle: "+ prm);
}
}

4. Armstrong Number In Range

public class Armstrong {
public static void main(String arg[]) {
int r, n, s1;
for (int i=1; i <= 1000; i++) {
n=i;
s1=0;
while (n>0) {
r = n % 10;
s1 = s1 + (r*r*r);
n /= 10;
} if (s1 == i) {
System.out.println(s1);
}
}
}
}

5. Number Is Binary Or Not


public class BinaryOrNot {
public static void main(String arg[]) {
System.out.println(isBinary(10100));
}
public static boolean isBinary(int number) {
while(number != 0) {
if (number % 10 > 1) {
return false;
}
number /= 10;

return true; 
}
}

6. Common Values Between Two Array

public class CommonInArray {

public static void main(String arg[]) {
int arr1[] = {4,7,3,9,2};
int arr2[] = {3,2,12,9,40,32,4};
for(int i=0; i<arr1.length; i++) {
for(int j=0; j<arr2.length; j++) {
if(arr1[i] == arr2[j]) {
System.out.println(arr1[i]);
}
}
}
}}

7. Largest Number In Array

public class LargestNumberInArray {
public static void main(String arg[]) {
 int arr[]= {1,25,34,66,78,41,57};
 int l = arr[0];
 int index = 0;
 for(int i=1;i<arr.length; i++) {
     if(arr[i]>l) {
     l = arr[i];
     index = i;
    }
  }
System.out.println("largest number: "+ l + " at possition: "+ index);
}
}


8. Copy Text File Content into Other Text File.

import java.io.FileReader;
import java.io.FileWriter;
public class CopyContentInFile {
public static void main(String arg[]) {
 try {
 FileReader fr = new FileReader("Armstrong.java");
 FileWriter fw = new FileWriter("Test.txt");
 int c = fr.read();
 while(c != -1) {
 fw.write(c);
 c = fr.read();
 }
 fr.close();
 fw.close();
 } catch(Exception e) {
 e.printStackTrace();
 }
}
}

9. Count Words In Sentence.

public class CountWordInSentence {
public static void main(String arg[]) {
  String str = "India is best country";
System.out.println(countWord(str));
}             
 
public static int countWord(String str) {
  int count = 1;
  for (int i=0; i<str.length(); i++) {
  if(str.charAt(i) == ' ' && str.charAt(i+1) != ' ') {
  count++;
  }
}
return count;
}
              
}

10. Current Date & Time

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurentDateTime {
public static void main(String arg[]) {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

Date date = new Date();
System.out.println(dateFormat.format(date));
    }
}


Read below posts.

Most Frequently Asked Java Interview Programs PART-2

Most Frequently Asked Java Interview Programs. PART-3

Comments

  1. Nice java program collection, please add java program for Graph Data Structure.

    ReplyDelete
    Replies
    1. Thanks for your valuable comment.
      I will consider this too.

      Delete

Post a Comment

Popular Posts