Java-Programs-asked-in-Selenium-Interview

1. How to Print List of Even Numbers in Java?

 package JavaPrograms_InterviewQuestions;

 import java.util.Scanner;

 public class PrintEvenNumbers {

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 j = 1; j <= value; j++) {

// if the number is divisible by 2 then it is even

if (j % 2 == 0) {

System.out.print(j + ” “);

}

}

}

}

Output :

Enter Limit:

40

Print Even numbers between 1 and 40

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

2.How to Compare Two Numbers In Java ?

 package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class CompareTwoNumbersInJava {

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 :

Enter Number 1:

14

Enter Number 2:

12

14 is greater than 12

3.Access Modifiers in Java ?

package JavaPrograms_InterviewQuestions;

public class Java_AccessModifier {

public int a = 10;

protected int b = 20;

private int c = 30;

int d = 40;

public void test1() {

System.out.println(“public test1()”);

}

protected void test2() {

System.out.println(“protected  test2()”);

}

void test3() {

System.out.println(“void test3()”);

}

private void test4() {

System.out.println(“private  test4()”);

}

public static void main(String[] args) {

Java_AccessModifier obj = new Java_AccessModifier();

obj.test1();

obj.test2();

obj.test3();

obj.test4();

System.out.println(obj.a);

System.out.println(obj.b);

System.out.println(obj.c);

System.out.println(obj.d);

}

}

Output :

public test1()

protected  test2()

void test3()

private  test4()

10

20

30

40

HOW TO HANDLE FRAME IN SELENIUM WEBDRIVER USING JAVA?

4.Verify Year is Leap Year or Not in Java ?

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class VerifyLeapYear {

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”);

}

}

}

 

Note: Every fourth year is Leap Year.

Output :

Enter Year:

2008

Year 2008 is a leap year

 

5. To verify whether the no is palindrome or not in java?

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

class VerifyPalindrome {

public static void main(String args[]) {

int r, sum = 0, temp;

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Number to Check Palindrome or Not:”);

int n = sc.nextInt();

temp = n;

while (n > 0) {

r = n % 10;

sum = (sum * 10) + r;

n = n / 10;

}

if (temp == sum) {

System.out.println(“Given Number is Palindrome”);

} else {

System.out.println(“Given Number is Not Palindrome “);

}

}

}

 

Note: A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.

 

Output :

Enter Number to Check Palindrome or Not:

5

Given Number is Palindrome

STEPS TO CONFIGURE SELENIUM WEBDRIVER WITH JAVA TO DEVELOP TEST SCRIPTS

6. Find Factorial of a Number in Java?

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class FactorialNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Number: “);

int value = sc.nextInt();

int factorial = value;

for (int a = (value – 1); a > 1; a–) {

factorial = factorial * a;

}

System.out.println(“Factorial of number is ” + factorial);

}

}

 

Note : factorial of number 4!= 4*3*2*1=24.

Output :

Enter Number:

4

Factorial of number is 24

7.H ow to Display Fibonacci Series in java?

 

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class FibonacciSeries {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Number 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 :

Enter Number for Fibonacci series :

10

First 10 terms: 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

8. To Check Whether a Number is Prime or Not in java?

 package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class PrimeNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Prime Number: “);

int num = sc.nextInt();

boolean flag = false;

for(int i = 2; i <= num/2; ++i)

{

// condition for nonprime number

if(num % i == 0)

{

flag = true;

break;

}

}

if (!flag)

System.out.println(num + ” is a prime number.”);

else

System.out.println(num + ” is not a prime number.”);

}

}

 

Output :

Enter Prime Number:

29

29 is a prime number.

 

 

 

Leave a comment