Other Useful Links:
- Manual Testing Interview Questions (01-40)
- Manual Testing Interview Questions (41-80)
- Selenium With Java Interview Questions - (01-35)
- Selenium With Java Interview Questions - (36-70)
- Java Interview Questions for QA (01-50)
- Java Interview Questions for QA (51-100)
- TestNG Interview Questions for QA (01-20)
- TestNG Interview Questions for QA (21-40)
- Maven Interview Questions for QA Automation Interview (01-15)
- Maven Interview Questions for QA Automation Interview (16-30)
- Most Frequently Asked API Testing Interview Questions (01-20)
- Most Frequently Asked API Testing Interview Questions (21-40)
1. How to reverse a string in java?
public class Main{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String:");
String str = sc.nextLine();
int n = str.length();
System.out.print("Reverse is:");
for(int i=n-1;i>=0;i--){
System.out.print(str.charAt(i));
}
}
}
Output:
Enter a String: ABCD
Reverse is: DCBA
2.1 How to reverse a number in Java? (Convert int to string)
import java.util.Scanner;
public class Test{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number:");
int num = sc.nextInt();
String str = Integer.toString(num);
int n = str.length();
System.out.print("Reverse is:");
for(int i=n-1;i>=0;i--){
System.out.print(str.charAt(i));
}
}
}
Output:
Enter a Number:12345
Reverse is:54321
2.2 How to reverse a number in Java? (using % operator)
import java.util.Scanner;
public class Test{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number:");
int original = sc.nextInt();
int reverse = 0;
while(original!=0){
int rem = original % 10;
reverse = reverse * 10 + rem;
original = original /10;
}
System.out.println("Reverse Number:"+reverse);
}
}
Output:
Enter a Number:123456
Reverse is:654321
3.1 Check if string is Palindrome or not (Using equals method)
import java.util.*;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String:");
String original = sc.nextLine();
int n = original.length();
String reverse= "";
for(int i=n-1;i>=0;i--){
reverse=reverse+original.charAt(i);
}
if(original.equals(reverse)){
System.out.println("String is palindrome");
}else{
System.out.println("String is not palindrome");
}
}
}
Output:
Enter a String: abcba
String is palindrome
3.2 Check if string is Palindrome or not
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String:");
String original = sc.nextLine();
int length = original.length();
boolean isPalindrome = true;
for(int i=0;i<length;i++){
if(original.charAt(i)!=original.charAt(length-i-1)){
System.out.println("String is not palindrome");
isPalindrome = false;
break;
}
}
if(isPalindrome){
System.out.println("String is palindrome");
}
}
}
Output:
Enter a String:abcba
String is palindrome
4.1 Swap two numbers without using third variable
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int x=5,y=10;
x=x+y;
y=x-y;
x=x-y;
System.out.print("Values After Swapping = X:"+x+" Y:"+y);
}
}
Output:
Values After Swapping = X:10 Y:5
4.2 Swap two numbers using third variable
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int x=5,y=10;
int temp=x;
x=y;
y=temp;
System.out.print("Values After Swapping = X:"+x+" Y:"+y);
}
}
Output:
Values After Swapping = X:10 Y:5
5.1 Fibonacci Series (Starting from 0)
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int a=0,b=1,c;
for(int i=1;i<=10;i++){
System.out.print(a+" ");
c=a+b;
a=b;
b=c;
}
}
}
Output: 0 1 1 2 3 5 8 13 21 34
5.2 Fibonacci Series (Starting from 1)
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int a=0,b=1,c;
for(int i=1;i<=10;i++){
c=a+b;
a=b;
b=c;
System.out.print(a+" ");
}
}
}
Output: 1 1 2 3 5 8 13 21 34
6. Factorial of a Number
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int fact=1;
System.out.print("Enter a Number:");
int num = sc.nextInt();
for(int i=1;i<=num;i++){
fact = fact*i;
}
System.out.print("Factorial of a Number:"+fact);
}
}
Output:
Enter a Number:5
Factorial of a Number:120
7. Check if given number is prime number or not
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number:");
int num = sc.nextInt();
int m=num/2;
int flag=0;
if(num==0 || num==1){
System.out.print("Given number is not a prime number.");
flag=1;
}else{
for(int i=2;i<=m;i++){
if(num%i == 0){
System.out.print("Given number is not a prime number.");
flag=1;
break;
}
}
}
if(flag == 0){
System.out.print("Given number is a prime number.");
}
}
}
Output:
Enter a Number:23
Given number is a prime number.
8. Program to display the prime numbers from 1 to 100
public class Test
{
public static void main(String[] args) {
int a=1,b=100;
for(int i=a;i<=b;i++){
if(checkPrime(i)){
System.out.print(i+" " );
}
}
}
public static boolean checkPrime(int num){
// 0, 1 and negative numbers are not prime
if(num<2){
return false;
}
else{
// no need to run loop till num-1 as for any number x the numbers in
// the range(num/2 + 1, num) won't be divisible anyways.
// Example 36 wont be divisible by anything b/w 19-35
int x= num/2;
for(int i=2;i<x;i++){
if(num%i==0){
return false;
}
}
}
// the number would be prime if we reach here
return true;
}
}
Output:
2 3 4 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
9.1 Count Occurrence of each character in a String
import java.util.*;
class Test {
public static void main(String[] args) {
String str = "iamiball";
char arr[] = str.toCharArray();
HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
for(char c:arr){
if(hm.containsKey(c)){
hm.put(c,hm.get(c)+1);
}else{
hm.put(c,1);
}
}
for(Map.Entry <Character,Integer>e:hm.entrySet()){
System.out.println(e.getKey()+" : "+e.getValue());
}
}
}
Output:
a : 2
b : 1
i : 2
l : 2
m : 1
9.2 Count Occurrence of each character in a String
import java.util.Scanner;
public class EachCharacterCount {
public static void main(String args[]) {
String str;
int i, len, counter[] = new int[256];
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a string : ");
str = scanner.nextLine();
len = str.length();
// loop through string and count frequency of every char and store it in counter array
for (i = 0; i < len; i++) {
counter[(int) str.charAt(i)]++;
}
// Print Frequency of characters
for (i = 0; i < 256; i++) {
if (counter[i] != 0) {
System.out.println((char) i + " --> " + counter[i]);
}
}
}
}
Output:
Please enter a string :
iamiball
a --> 2
b --> 1
i --> 2
l --> 2
m --> 1
10. Count Occurrence of each word in a String
import java.util.*;
class Test {
public static void main(String[] args) {
String str = "India is my country. India is very beautiul";
String arr[] = str.split(" ");
HashMap<String,Integer> hm = new HashMap<String,Integer>();
for(String s:arr){
if(hm.containsKey(s)){
hm.put(s,hm.get(s)+1);
}else{
hm.put(s,1);
}
}
for(Map.Entry <String,Integer>e:hm.entrySet()){
System.out.println(e.getKey()+" : "+e.getValue());
}
}
}
Output:
very : 1
beautiul : 1
is : 2
country. : 1
my : 1
India : 2
11. Write program for:
Input: 1,3,5,7,9,3,5,11,13,9 | Output: 1 3 5 7 9 11 13
import java.util.*;
class Test {
public static void main(String[] args) {
int[] arr={1,3,5,7,9,3,5,11,13,9};
List<Integer> ls = new ArrayList<Integer>(arr.length);
for(int i:arr){
ls.add(i);
}
LinkedHashSet lhs = new LinkedHashSet(ls);
Object[] obj = lhs.toArray();
System.out.println("Printing numbers only once:");
for(Object o:obj){
System.out.print(o+ " ");
}
}
}
Output:
Printing numbers only once:
1 3 5 7 9 11 13
12. Remove duplicates from an integer array
Input: 1,3,5,7,9,11,13,3,5,7,15 | Output: 1 9 11 13 15
import java.util.*;
class Test {
public static void main(String[] args) {
int[] arr = {1,3,5,7,9,11,13,3,5,7,15};
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
for(int i:arr){
if(hm.containsKey(i)){
hm.put(i,hm.get(i)+1);
}else{
hm.put(i,1);
}
}
for(Map.Entry <Integer,Integer>e:hm.entrySet()){
if(e.getValue() < 2){
System.out.print(e.getKey()+" ");
}
}
}
}
Output:
1 9 11 13 15
13. Write program for:
Input: IND SL PAK SL NEP BAN SL | Output: IND, SL, PAK, NEP, BAN
import java.util.*;
class Test {
public static void main(String[] args) {
String arr[] = {"IND","SL","PAK","SL","NEP","BAN","SL"};
ArrayList<String> al = new ArrayList<String>(arr.length);
for(String s:arr){
al.add(s);
}
LinkedHashSet<String> lhs = new LinkedHashSet<String>(al);
Object[] obj = lhs.toArray();
for(Object o:obj){
System.out.print(o+" ");
}
}
}
Output:
IND SL PAK NEP BAN
14. Remove duplicates from string array
Input: IND SL PAK SL NEP PAK BAN | Output: NEP IND BAN
import java.util.*;
class Test {
public static void main(String[] args) {
String[] arr = {"IND","SL","PAK","SL","NEP","PAK","BAN"};
HashMap<String,Integer> hm = new HashMap<String,Integer>();
for(String s:arr){
if(hm.containsKey(s)){
hm.put(s,hm.get(s)+1);
}else{
hm.put(s,1);
}
}
for(Map.Entry <String,Integer>e:hm.entrySet()){
if(e.getValue() < 2){
System.out.print(e.getKey()+" ");
}
}
}
}
Output:
NEP IND BAN
15. Reverse words in a string array
Input: IND SL PAK NEP BAN | Output: BAN NEP PAK SL IND
import java.util.*;
class Test {
public static void main(String[] args) {
String[] arr = {"IND","SL","PAK","NEP","BAN"};
ArrayList<String> al = new ArrayList<String>(arr.length);
for(String s:arr){
al.add(s);
}
Collections.reverse(al);
Object[] obj = al.toArray();
for(Object o:obj){
System.out.print(o+" ");
}
}
}
Output: BAN NEP PAK SL IND
Next: Java Programs for QA Automation Interview (16-30)
Greetings, reader! Your input is highly important to us. Please share your thoughts in the comments section below.
Contact:
Email: piyush23agr@gmail.com
Follow on LinkedIn: Piyush Agrawal - LinkedIn
Follow on YouTube: Piyush Agrawal - Youtube
No comments:
Post a Comment