JAVA Programs Asked in Selenium Interview :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package package_name; public class AccessModifierInJava { public int i=1; protected int k=2; private int p=3; int d=4; public void test1() { System.out.println("public void test1()"); } protected void test2() { System.out.println("protected void test2()"); } void test3() { System.out.println("void test3()"); } private void test4() { System.out.println("private void test4()"); } public static void main(String[] args) { AccessModifierInJava obj = new AccessModifierInJava(); obj.test1(); obj.test2(); obj.test3(); obj.test4(); System.out.println(obj.i); System.out.println(obj.k); System.out.println(obj.p); System.out.println(obj.d); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void test1() protected void test2() void test3() private void test4() 1 2 3 4 |
Note: if the number is divisible by 2 then it is even
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.Scanner; public class ListEvenNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Limit:- "); int value = sc.nextInt(); System.out.println("Print Even numbers between 1 and " + value); for (int i = 1; i <= value; i++) { // if the number is divisible by 2 then it is even if (i % 2 == 0) { System.out.print(i + " "); } } } } |
Output:
1 2 3 4 5 6 7 | Enter Limit:- 15 Print Even numbers between 1 and 15 2 4 6 8 10 12 14 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.Scanner; public class CompareTwoNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Number 1:- "); int num1 = sc.nextInt(); System.out.println("Enter Number 2:- "); int num2 = sc.nextInt(); if (num1 > num2) { System.out.println(num1 + " is greater than " + num2); } else if (num1 < num2) { System.out.println(num1 + " is less than " + num2); } else { System.out.println(num1 + " is equal to " + num2); } } } |
Output:
1 2 3 4 5 6 7 8 9 | Enter Number 1:- 15 Enter Number 2:- 12 15 is greater than 12 |
Note: Every fourth year is Leap Year.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class DetermineLeapYear { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Year:- "); int year = sc.nextInt(); // if year is divisible by 4, it is a leap year if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) System.out.println("Year " + year + " is a leap year"); else System.out.println("Year " + year + " is not a leap year"); } } |
Output:
1 2 3 4 5 | Enter Year:- 2012 Year 2012 is a leap year |
Note: A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.Scanner; class PalindromeExample { public static void main(String args[]) { int r, sum = 0, temp; Scanner sc = new Scanner(System.in); System.out.println("Enter No to Check Palindrome or Not:"); int n = sc.nextInt(); temp = n; while (n > 0) { r = n % 10; // getting remainder sum = (sum * 10) + r; n = n / 10; } if (temp == sum) System.out.println("No is Palindrome"); else System.out.println("No is Not Palindrome "); } } |
Output:
1 2 3 4 5 | Enter No to Check Palindrome or Not: 131 No is Palindrome |
Note: A factorial is a function that multiplies a number by every number below it.
For example: 5!= 5*4*3*2*1=120.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner; public class NumberFactorial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Limit:- "); int value = sc.nextInt(); int factorial = value; for (int i = (value - 1); i > 1; i--) { factorial = factorial * i; } System.out.println("Factorial of number is " + factorial); } } |
Output:
1 2 3 4 5 | Enter Limit:- 5 Factorial of number is 120 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.BufferedReader; import java.io.InputStreamReader; public class GeneratePyramid { public static void main(String[] args) throws Exception { BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Number:"); int as = Integer.parseInt(keyboard.readLine()); System.out.println("Enter X:"); int x = Integer.parseInt(keyboard.readLine()); int y = 0; for (int i = 0; i <= as; i++) { for (int j = 1; j <= i; j++) { System.out.print(y + "\t"); y = y + x; } System.out.println(""); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Enter Number: 5 Enter X: 2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 |
Note: Square the radius and multiply by pi to find the area of the circle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; public class AreaOfCircle { public static void main(String ag[]) { int rad; double pie = 3.14, ar; Scanner s = new Scanner(System.in); System.out.print("Enter radius of circle:"); rad = s.nextInt(); ar = pie * rad * rad; System.out.println("Area of circle:" + ar); } } |
Output:
1 2 3 | Enter radius of circle:5 Area of circle:78.5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package package_name; import java.util.ArrayList; import java.util.List; public class ArrayListInJava { public static void main(String[] args) { List arrayList = new ArrayList<>(); arrayList.add(1); arrayList.add(1.2); arrayList.add(true); arrayList.add("Java"); arrayList.add(2); System.out.println(arrayList); List<Integer> arrayList1 = new ArrayList<Integer>(); arrayList1.add(1); arrayList1.add(2); arrayList1.add(3); arrayList1.add(4); System.out.println(arrayList1); } } |
Output:
1 2 3 | [1, 1.2, true, Java, 2] [1, 2, 3, 4] |
Note: The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. … F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter No for Fibonacci series :"); int n = sc.nextInt(); int t1 = 0, t2 = 1; System.out.print("First " + n + " terms: "); for (int i = 1; i <= n; ++i) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; } } } |
Output:
1 2 3 4 5 | Enter No for Fibonacci series : 10 First 10 terms: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package package_name; public class CalculateSumOfNumbersOfArray { public int calculateSumOfNumbers() { int[] a = { 10, 20, 30, 40 }; int sum = 0; for (int arr : a) { sum = sum + arr; } return sum; } public static void main(String[] args) { CalculateSumOfNumbersOfArray obj = new CalculateSumOfNumbersOfArray(); System.out.println(obj.calculateSumOfNumbers()); } } |
Output:
1 | 100 |
Note: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.Scanner; public class GeneratePrimeNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Prime No's Limit:- "); int limit = sc.nextInt(); // loop through the numbers one by one for (int i = 1; i < 50; i++) { boolean isPrime = true; // check to see if the number is prime for (int j = 2; j < i; j++) { if (i % j == 0) { isPrime = false; break; } } // print the number if (isPrime) System.out.print(i + " "); } } } |
Output:
1 2 3 4 5 | Enter Prime No's Limit:- 10 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.Scanner; class Reverse_Func { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter String to Reverse:- "); String str = sc.nextLine(); int i = str.length(); StringBuffer strb = new StringBuffer(); for (int j = i - 1; j >= 0; j--) { strb = strb.append(str.charAt(j)); } System.out.println(strb); } } |
Output:
1 2 3 4 5 | Enter String to Reverse:- sandeep peednas |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class NestedSwitch { public static void main(String[] args) { int i = 0; int j = 1; switch (i) { case 0: switch (j) { case 0: System.out.println("i is 0, j is 0"); break; case 1: System.out.println("i is 0, j is 1"); break; default: System.out.println("default case!!"); } break; default: System.out.println("No case found!!"); } } } |
Output:
1 | i is 0, j is 1 |
Note: The factorial of a number is defined is the product of natural numbers from one to that particular number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Scanner; class FactorialUsingRecursion { static int factorial(int n) { if (n == 0) return 1; else return (n * factorial(n - 1)); } public static void main(String args[]) { int i, fact = 1; Scanner sc = new Scanner(System.in); System.out.println("Enter Number to Factorial:- "); int number = sc.nextInt(); fact = factorial(number); System.out.println("Factorial of "+number+" is:"+fact); } } |
Output:
1 2 3 4 5 | Enter Number to Factorial:- 5 Factorial of 5 is: 120 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class SumofDigits { public static int sum(int n) { return n == 0 ? 0 : n % 10 + sum(n / 10); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number: "); int num = sc.nextInt(); sc.close(); System.out.println("Sum of digits" + num + " is: " + sum(num)); } } |
Output:
1 2 3 4 5 | Enter a number: 15 Sum of digits 15 is: 6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class WordCount { public static void main(String args[]) { String s = "Hello Java Selenium"; int count = 1; for (int i = 0; i < s.length() - 1; i++) { if ((s.charAt(i) == ' ') && (s.charAt(i + 1) != ' ')) { count++; } } System.out.println("No of words in a string = " + count); } } |
Output:
1 | No of words in a string = 3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Scanner; class InvertTriangle { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter No to Invert Triangle:- "); int num = sc.nextInt(); while (num > 0) { for (int j = 1; j <= num; j++) { System.out.print(" " + num + " "); } System.out.print("\n"); num--; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Enter No to Invert Triangle:- 6 6 6 6 6 6 6 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Scanner; class MultiplicationTable { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Limit of Multiplication Table:"); int num = sc.nextInt(); System.out.println("***MULTIPLICATION TABLE***"); for (int i = 1; i <= num; i++) { for (int j = 1; j <= num; j++) { System.out.print(" " + i * j + " "); } System.out.print("\n"); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Limit of Multiplication Table: 5 *****MULTIPLICATION TABLE***** 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.Scanner; class PrimeNo { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Check number is Prime or Not:- "); int num = sc.nextInt(); int flag = 0; for (int i = 2; i < num; i++) { if (num % i == 0) { System.out.println(num + " is not a Prime Number"); flag = 1; break; } } if (flag == 0) System.out.println(num + " is a Prime Number"); } } |
Output:
1 2 3 4 5 | Enter Number to check number is Prime or not:- 3 3 is a Prime Number |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Random; import java.util.Scanner; public class GenerateRandomNumber { public void generateRandomNumber(){ Random ran = new Random(); Scanner sc = new Scanner(System.in); System.out.println("How Random Numbers you want?:- "); int value = sc.nextInt(); for(int i=0; i<value;i++){ int number = ran.nextInt(200); System.out.println(number); } } public static void main(String[] args) { GenerateRandomNumber obj = new GenerateRandomNumber(); obj.generateRandomNumber(); } } |
Output:
1 2 3 4 5 | How Random Numbers you want?:- 2 65 175 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package package_name; public class Method_Overloading { public void Java1() { System.out.println("Java1()"); } public void Java2(int i) { System.out.println("Java2(int i)"); } public void Java3(int i, int j) { System.out.println("Java3(int i, int j)"); } public void Java4(int i, int j, int k) { System.out.println("Java4(int i, int j, int k)"); } public static void main(String[] args) { Method_Overloading obj = new Method_Overloading(); obj.Java1(); obj.Java2(7); obj.Java3(7,8); obj.Java4(7,8,10); } } |
Output:
1 2 3 4 5 6 7 | Java1() Java2(int i) Java3(int i, int j) Java4(int i, int j, int k) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; class OddOrEven { public static void main(String args[]) { int x; System.out.println("Enter Number:- "); Scanner sc = new Scanner(System.in); x = sc.nextInt(); if (x % 2 == 0) System.out.println("Number is Even."); else System.out.println("Number is Odd."); } } |
Output:
1 2 3 4 5 | Enter Number:- 15 Number is Odd. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; class Join { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter Number:- "); int num = sc.nextInt(); String result = " "; for (int i = 1; i <= num; i++) { result = result + i + " "; } System.out.println(result); } } |
Output:
1 2 3 | Enter Number:- 5 1 2 3 4 5 |
*
**
***
****
*****
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class JavaPyramid { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } // generate a new line System.out.println(""); } } } |
Output:
1 2 3 4 5 6 7 8 9 | * ** *** **** ***** |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Scanner; public class invertNumber { public long doInvert(long number) { long invert = 0; while (number != 0) { invert = (invert * 10) + (number % 10); number = number / 10; } return invert; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter Number to Reverse:- "); int lnum = sc.nextInt(); invertNumber input = new invertNumber(); System.out.println("Input value : " + lnum); System.out.println("Inverted value : " + input.doInvert(lnum)); } } |
Output:
1 2 3 4 5 6 7 | Enter Number to Reverse:- 123 Input value : 123 Inverted value : 321 |
12345
1234
123
12
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class JavaPyramid { public static void main(String[] args) { for (int i = 5; i > 0; i--) { for (int j = 0; j < i; j++) { System.out.print(j + 1); } System.out.println(""); } } } |
Output:
1 2 3 4 5 6 7 8 9 | 12345 1234 123 12 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package package_name; public class StaticAndNonStaticInJava { int i=1; int j=2; static int p=3; static int k=4; public void test1() { System.out.println("test 1 non static"); } public static void test2() { System.out.println("test 2 static"); } public void test3() { System.out.println("test 3 non static"); } public static void test4() { System.out.println("test 4 static"); } public static void main(String[] args) { StaticAndNonStaticInJava obj = new StaticAndNonStaticInJava(); obj.test1(); obj.test3(); System.out.println(obj.i); System.out.println(obj.j); StaticAndNonStaticInJava.test2(); StaticAndNonStaticInJava.test4(); System.out.println(StaticAndNonStaticInJava.k); System.out.println(StaticAndNonStaticInJava.p); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | test 1 non static test 3 non static 1 2 test 2 static test 4 static 4 3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.Scanner; class Swap { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter num1:- "); int num1 = sc.nextInt(); System.out.println("Enter num2:- "); int num2 = sc.nextInt(); System.out.println("\n***Before Swapping***"); System.out.println("Number 1 : " + num1); System.out.println("Number 2 : " + num2); // Swap logic num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("\n***After Swapping***"); System.out.println("Number 1 : " + num1); System.out.println("Number 2 : " + num2); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Enter num1:- 20 Enter num2:- 30 ***Before Swapping*** Number 1 : 20 Number 2 : 30 ***After Swapping*** Number 1 : 30 Number 2 : 20 |
Note: Armstrong number is a number that is equal to the sum of cubes of its digits.
e.g:- 153 = (1*1*1)+(5*5*5)+(3*3*3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.Scanner; class Armstrong { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Number is Armstrong or Not ?"); int num = sc.nextInt(); int n = num; int check = 0, remainder; while (num > 0) { remainder = num % 10; check = check + (int) Math.pow(remainder, 3); num = num / 10; } if (check == n) System.out.println(n + " is an Armstrong Number"); else System.out.println(n + " Not a Armstrong Number"); } } |
Output:
1 2 3 4 5 | Number is Armstrong or Not? 153 153 is an Armstrong Number |
- Java and Selenium are the best automation tools for QA. And these skills are essential for each QA engineer involved in test automation.
- Therefore, Above programs, we are presenting a set of Java coding programs to help test automation developers during job interviews.
- Visit : Selenium Programs for Interview
java programs asked in selenium interview access modifier in java, list of even numbers,compare two numbers using if-else,determine year is leap year or not,palindrome number,factorial of a number,pyramid of numbers using for loops,calculate area of the circle, array list,fibonacci series,calculate sum of numbers in array,prime numbers between 1 to given num,reverse string without reverse function,nested switch,factorial of a number using recursion,calculate ‘digit sum’,count the number of words in a string,display invert triangle,display multiplication table,find whether number is prime or not,generate random number in java,method overloading,odd or even number,print upto given number,pyramid of stars using nested for loops,reverse a number, reversed pyramid using for loop, static and non-static in java, swap two values, whether given no is armstrong or not java programming questions for testers and commonly asked java programs in selenium interview and java programs asked in interview for automation tester and java programs for automation testing interview
JAVA Programs Asked in Selenium Interview JAVA Programs Asked in Selenium Interview JAVA Programs Asked in Selenium Interview