The document contains 25 Java programs that demonstrate various programming concepts like:
- Printing output and accepting user input
- Conditional statements like if-else
- Loops like for, while, nested loops, break and continue statements
- String, array and other data types operations
- Mathematical and other functional programs like prime number check, Armstrong number check, etc.
The programs are designed to help learn and practice Java programming interview questions and concepts.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
65 views
Main Out Println: 1) Write A Simple Java Program?
The document contains 25 Java programs that demonstrate various programming concepts like:
- Printing output and accepting user input
- Conditional statements like if-else
- Loops like for, while, nested loops, break and continue statements
- String, array and other data types operations
- Mathematical and other functional programs like prime number check, Armstrong number check, etc.
The programs are designed to help learn and practice Java programming interview questions and concepts.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10
Java for Testers – Interview Questions and Answers Part-1
1) Write a simple Java program?
public class Simple{ public static void main(String[] args){ System.out.println(“Hello World”); } } 2) Write a Java program to print an integer value? public class PrintInteger{ public static void main(String[] args){ int i = 1; System.out.println(i); } } 3) Write a Java program to print the command line arguments? public class Sample { public static void main(String[] args){ if (args.length > 0) { System.out.println("The command line"+ " arguments are:"); for(String val:args) System.out.println(val); } else System.out.println("No command line "+ "arguments found."); } } Command Line: Bijans-MacBook-Pro:java bijan$ javac Sample.java Bijans-MacBook-Pro:java bijan$ java Sample Java Interview Questions The command line arguments are: Java Interview Questions 4) Write a Java program to print the input from scanner? public class PrintScannerInput { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.nextLine(); System.out.println("Name: "+name); } } 5) Write a Java program to convert from Fahrenheit to Celsius? public class ConvertToCelsius { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Input temperature in Fahrenheit: "); double fahrenheit = sc.nextDouble(); double celsius = ((5 * (fahrenheit - 32.0)) / 9.0); System.out.println(fahrenheit + " degree fahrenheit is equal to " + celsius + "in celsius"); } } 6) Write a Java program to swap two numbers? public class SwapNumbers { public static void main(String[] args) { int x, y, z; Scanner sc = new Scanner(System.in); x = sc.nextInt(); y = sc.nextInt(); System.out.println("Before swapping\n x = " + x + "\n y = " + y); z = x; x = y; y = z; System.out.println("After swapping\n x = " + x + "\n y = " + y); } } 7) Write a Java program to swap two numbers without using third variable? public class SwapNumberWithoutVariable { public static void main(String[] args) { int x = 10; int y = 5; x = x + y; y = x - y; x = x - y; System.out.println("After swapping: " + " x is " + x + " and " + "y is "+ y); } } 8) Write a Java program to add two numbers? public class AddNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int sum = a + b; System.out.println("Sum of two numbers is: " + sum); } } 9) Write a Java program to find the largest number? public class FindLargestNumberInArray { static int array[] = {21,98,13,9,34}; public static void main(String[] args){ int maxNumber = findLargestNumber(); System.out.println("Maximum number in the array: "+ maxNumber); } private static int findLargestNumber(){ int max = array[0]; for(int i =0;i<array.length;i++){ if(array[i]>max) max = array[i]; } return max; } } 10) Write a Java program to demonstrate if..else statement ? public class FindNumberIsPrime { public static void main(String[] args){ boolean flag = false; System.out.println("Enter input number"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); for(int i=2;i<=num/2;i++){ 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"); } } } 11) Write a Java program to demonstrate nested if … else if .. statement? public class IfElseDemo { public static void main(String[] args){ int i =20; if(i == 10) System.out.println("i is 10"); else if(i == 15) System.out.println("i is 15"); else if(i == 20) System.out.println("i is 20"); else System.out.println("i is not present"); } } 12) Write a Java program to demonstrate nested if …else statement? class Ladder { public static void main(String[] args) { int number = 0; if (number > 0) { System.out.println("Number is positive."); } else if (number < 0) { System.out.println("Number is negative."); } else { System.out.println("Number is 0."); } } } 13) Write a Java program to find odd and even numbers ?
public class FindOddEvenNumbers {
public static void main(String[] args){ Scanner sc= new Scanner(System.in); System.out.println("Enter a number: "); int inputNumber = sc.nextInt(); findNumber(inputNumber); } public static void findNumber(int number){ if(number%2!=0){ System.out.println("Number is odd"); } else { System.out.println("Number is even"); } } } 14) Write a Java program to compare two strings? public class CompareTwoStrings { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter first string"); String first = sc.next(); System.out.println("Enter second string"); String second = sc.next(); compare(first,second); } public static void compare(String s1, String s2){ if(s1.compareTo(s2)==0) { System.out.println("Strings are equal"); } else { System.out.println("Strings are not equal"); } } } 15) Write a Java program to demonstrate for loop ? public class ReverseSentenceWordByWord { public static void main(String[] args){ String sentence = "Java Interview Questions"; String reversedSentence = reverseSentence(sentence); System.out.println(reversedSentence); } public static String reverseSentence(String sentence){ String reverse = ""; String[] words = sentence.split("\\s"); for(int i=words.length-1;i>=0;i--){ reverse = reverse + words[i] + " "; } return reverse; } } 16) Write a Java program to print stars using for loop, where the number of stars printed should be equal to the row number? public class PrintStarPatternInRow { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the number of rows"); int rows = sc.nextInt(); printStars(rows); } public static void printStars(int n){ for(int i=0;i<n;i++){ for(int j=0;j<=i;j++){ System.out.print("* "); } System.out.println(); } } } 17) Write a Java program to demonstrate while loop? class WhileLoopExample { public static void main(String args[]){ int i=10; while(i>1){ System.out.println(i); i--; } } } 18) Write a Java program to print the entered number in reverse? public class ReverseNumber { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the number"); int num = sc.nextInt(); reverseNumber(num); } public static void reverseNumber(int number){ int reverse = 0; while(number!=0){ int digit = number % 10; reverse = reverse * 10 + digit; number = number/10; } System.out.println("Reversed number " + reverse); } } 19) Write a Java program to demonstrate the usage of break statement inside while loop? class BreakExample { public static void main(String[] args) { Double number, sum = 0.0; Scanner input = new Scanner(System.in); while (true) { System.out.print("Enter a number: "); number = input.nextDouble(); if (number < 0.0) { break; } sum += number; } System.out.println("Sum = " + sum); } } 20) Write a Java program to demonstrate the usage of break and continue statements inside while loop? public class ContinueExample { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 5}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print( x ); System.out.print("\n"); } } } 21) Write a Java program to print the alphabets using for loop? public class PrintAlphabets { public static void main(String[] args) { char i; System.out.printf("The Alphabets from A to Z are: \n"); for (i = 'A'; i <= 'Z'; i++) { System.out.printf("%c ", i); } } } 22) Write a Java program to demonstrate for each loop? public class FindHighestMarks { public static void main(String[] arg) { int[] marks = { 125, 132, 95, 116, 110 }; int highest_marks = maximum(marks); System.out.println("The highest score is " + highest_marks); } public static int maximum(int[] numbers) { int max = numbers[0]; // for each loop for (int num : numbers) { if (num > max) { max = num; } } return max; } }
23) Write a Java program for printing the Multiplication table?
public class MultiplicationTable { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter number"); int number = sc.nextInt(); System.out.println("Enter multiplication range"); int range = sc.nextInt(); for(int i=1;i<=range;i++){ System.out.printf("%d * %d = %d \n", number, i, number * i); } } } 24) Write a Java program for printing the prime numbers? public class FindNumberIsPrime { public static void main(String[] args){ boolean flag = false; System.out.println("Enter input number"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); for(int i=2;i<=num/2;i++){ 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"); } } } 25) Write a Java program to check whether a given number is Armstrong ? public class ArmstrongNumber { public static void main(String[] args){ int c=0,a,temp; Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); temp = num; while(num>0){ a=num%10; num=num/10; c=c+(a*a*a); } if(temp==c) { System.out.println(temp + " is an Armstrong number"); } else System.out.println(temp + " is not an armstrong number"); } }