Frequently Asked Java Programs For QA 1719045357
Frequently Asked Java Programs For QA 1719045357
com
9123820085
Frequently Asked
Java Programs for
QA
kasper-analytics
kasperanalytics.com
9123820085
kasper-analytics
kasperanalytics.com
9123820085
2. Armstrong number -
Armstrong number is a number that is equal to the sum of cubes of its digits.
3. Fibonacci Series –
kasper-analytics
kasperanalytics.com
9123820085
Input = 12321
Output =12321
5. Factorial Number
Factorial Program in Java: Factorial of n is the product of all positive descending
integers.
Input = 5!
Output = 5! = 5*4*3*2*1 = 120
kasper-analytics
kasperanalytics.com
9123820085
6. OddEvenNumbers
Input = 11
Output = Given number is odd number
public static void main(String[] args) {
// 1. Using Brute Forcew Approach
Scanner sc = new Scanner(System.in); System.out.println("Enter Number:-"); int
num = sc.nextInt();
if (num % 2 == 0)// Brute Forcew Approach
{
System.out.println("Given is even number");
} else {
System.out.println("Given number is odd number");
}
7. Prime Number
Prime number is a number that is greater than 1 and divided by 1 or itself only.
Input = 31, Output = The number is prime.
public static void main(String[] args) {
int num = 31;
int count = 0;
if (num <= 1) {
System.out.println("The number is not prime");
return;
}
for (int i = 2; i <= num / 2; i++) {
kasper-analytics
kasperanalytics.com
9123820085
if (num % i == 0) count++;
}
if (count > 1) {
System.out.println("The number is not prime");
} else {
System.out.println("The number is prime"); }
9. Sum of Digits
Sum of all given numbers.
Input = 987
Output = 24
kasper-analytics
kasperanalytics.com
9123820085
kasper-analytics
kasperanalytics.com
9123820085
1. Reverse a string
Input = mama
Output = mama
public static void main(String[] args) { String str = "mama";
String s2 = "";
// 1. by using the charAt() method
for (int i = str.length() - 1; i >= 0; i--) {
s2 = s2 + str.charAt(i);// extracts each character and store in string
}
System.out.println("Reversed word: " + s2);
// below is code to check weather given string is Palindrome or not
if (str.equalsIgnoreCase(s2)) {
System.out.println("String is Palindrome");
} else {
System.out.println("String is not Palindrome");
}
}
// 2. Using built in reverse() method of the StringBuilder class:
String input = "Welcome To Jave Learning"; StringBuilder input1 = new
StringBuilder();
input1.append(input); // append a string into StringBuilder
input1 input1.reverse();
System.out.println(input1);
// 3. Using StringBuffer:
String strText = "Java Learning";
// conversion from String object to StringBuffer
StringBuffer sbr = new StringBuffer(strText);
sbr.reverse();
System.out.println(sbr);
kasper-analytics
kasperanalytics.com
9123820085
Input =
array1 = { 4, 2, 3, 1, 6 }; array2 = { 6, 7, 8, 4 };
Output = 6,4
kasper-analytics
kasperanalytics.com
9123820085
Input = array1 = { 4, 2, 3, 1, 6 };
Output = First is:4, Last is: 6
kasper-analytics
kasperanalytics.com
9123820085
int num=arrayList.length;
Arrays.sort(arrayList);
System.out.println("Second Largest element is "+arrayList[num-2]);
//Display Second Smallest
System.out.println("Second Smallest element is "+arrayList[1]);
Output = 5 10 12 20 57 60 63 88
kasper-analytics
kasperanalytics.com
9123820085
kasper-analytics
kasperanalytics.com
9123820085
hashMap.put(word, 1);
}
System.out.println(hashMap);
Output = p = 1, a = 2, s = 2, T = 1, e = 2, h = 1, x = 1, i = 2, l = 1, m = 1, n = 1
kasper-analytics
kasperanalytics.com
9123820085
if (c != ' ') {
if (count.containsKey(c)) {
// if character already traversed, increment it
count.put(c, count.get(c) + 1);
} else {
// if character not traversed, add it to hashmap
count.put(c, 1);
}
}
}
// traverse the map and print the number of occurences of a character
for (Map.Entry entry : count.entrySet()) {
System.out.print( entry.getKey() + " = " + entry.getValue()+", ");
}
}
kasper-analytics
kasperanalytics.com
9123820085
{
if(!hs.add(arrayElement))
{System.out.println("HashSet :Duplicate Element is : "+arrayElement);
}}
13. String Anagrams: Determine if two strings are anagrams of each other
Input =
String str1 = "Army"; String str2 = "Mary";
Output = army and mary are anagram.
kasper-analytics
kasperanalytics.com
9123820085
if (str1.length() == str2.length()) {
// convert strings to char array
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
// sort the char array
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// if sorted char arrays are same, then the string is anagram
boolean result = Arrays.equals(charArray1, charArray2);
if (result) {
System.out.println(str1 + " and " + str2 + " are anagram.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
} else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
Output = p i
kasper-analytics
kasperanalytics.com
9123820085
}
}
}
}
// Initialize array
int[] arr = new int[] { 25, 11, 7, 75, 56 };
// Initialize max with first element of array.
int max = arr[0];
// Loop through the array
for (int i = 0; i < arr.length; i++) {
// Compare elements of array with max
if (arr[i] > max) max = arr[i];
}
System.out.println("Largest element present in given array: " + max);
16. Java program to split an alphanumeric digit without using split method
Input = "Welcome234To567Java89Programming0@#!!";
Output =
WelcomeToJavaProgramming
234567890
@#!!
kasper-analytics
kasperanalytics.com
9123820085
if (Character.isDigit(str.charAt(i)))
num.append(str.charAt(i));
else if (Character.isAlphabetic(str.charAt(i)))
alpha.append(str.charAt(i));
else
special.append(str.charAt(i));
}
System.out.println(alpha);
System.out.println(num);
System.out.println(special);
}
kasper-analytics