Support/FAQ

Top Ten Interview Coding Questions around Java Loops



  1. Write a method that uses a for loop to display numbers in descending order in the step of 5 starting from 100. Call your method from the main method.
    				
    e.g., The output should be:
    100
    95
    90
    ...
    ...
    5
    0
  2. Solution
    			
    	package com.training.firstprogram;
    
    	/**
    	 * uses a for loop to display numbers in descending order in the step of 5
    	 * starting from 100.
    	 */
    	public class BackwardCounting {
    
    		/**
    		 * This method counts backward
    		 * 
    		 */
    		public void backwardCounter() {
    			for (int i = 100; i >= 0; i = i - 5) {
    				System.out.println(i);
    			}
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			new BackwardCounting().backwardCounter();
    		}
    	}
    
     



  3. Write a method that uses a while loop. This method would replace every vowel in the String with X. Call your method from the main method.

  4. e.g., the String
    Java is Fun
    should become
    JXvX Xs FXn

    Solution
    			
    	package com.training.firstprogram;
    
    	/**
    	 * This class would replace every vowel in the String with X. Call your method
    	 * from the main method.
    	 * 
    	 */
    	public class ReplaceChar {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public String process(String str) {
    
    			String replacedString = "";
    			int i = 0;
    
    			while (i < str.length()) {
    				if (str.charAt(i) == ('a') || str.charAt(i) == ('e') || str.charAt(i) == ('i') || str.charAt(i) == ('o')
    						|| str.charAt(i) == ('u')) {
    					replacedString += 'X';
    				} else {
    					replacedString += str.charAt(i);
    				}
    				i++;
    			}
    
    			return replacedString;
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			String s = "Java is fun";
    			String replacedString = new ReplaceChar().process(s);
    			System.out.println(replacedString);
    		}
    	}			
    			 



  5. Write a method that uses a do while loop. This method would reverse a String. Call your method from the main method.

  6. e.g., the String
    Java
    should become
    avaJ

    Solution
    			
    	package com.training.firstprogram;
    
    	/**
    	 * This class would reverse a String.
    	 * 
    	 */
    	public class ReverseString {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public String process(String str) {
    
    			String reversedString = "";
    			int i = str.length() - 1;
    
    			do {
    				reversedString += str.charAt(i);
    				i--;
    			} while (i >= 0);
    
    			return reversedString;
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			String s = "Java";
    			String reversedString = new ReverseString().process(s);
    			System.out.println(reversedString);
    		}
    	}
    			 



  7. Write a method that checks if a String is a Palindrome. Call your method from the main method.

  8. e.g., the String
    ABCBA
    is a Palindrome, where as
    Java
    is not a Palidrome.
    Solution
    			
    	package com.training.firstprogram;
    
    	/**
    	 * This class would check if a String is a Palindrome.
    	 * 
    	 */
    	public class PalindromeChecker {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public boolean process(String str) {
    			boolean status = false;
    			String replacedString = "";
    			int i = str.length() - 1;
    
    			do {
    				replacedString += str.charAt(i);
    				i--;
    			} while (i >= 0);
    
    			if (str.equals(replacedString)) {
    				status = true;
    			}
    
    			return status;
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			String s1 = "Java";
    			boolean isPalnidrome = new PalindromeChecker().process(s1);
    			if (isPalnidrome) {
    				System.out.println("String " + s1 + " is a Palindrome");
    			} else {
    				System.out.println("String " + s1 + " is NOT a Palindrome");
    			}
    
    			String s2 = "geeksskeeg";
    			boolean isPalnidrome2 = new PalindromeChecker().process(s2);
    			if (isPalnidrome2) {
    				System.out.println("String " + s2 + " is a Palindrome");
    			} else {
    				System.out.println("String " + s2 + " is NOT a Palindrome");
    			}
    
    		}
    	}
    			
    			



  9. Write a method that prints the multiplication table of the user provided number. Call your method from the main method.

  10. e.g., If the user enters the value of
    9
    should become, the program prints
    				9
    				18
    				27
    				36
    				...
    				...
    				81
    				90

    Solution
    	package com.training.firstprogram;
    
    	import java.util.Scanner;
    
    	/**
    	 * This class would print a multiplication table
    	 * 
    	 */
    	public class MultiplicationTable {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public void process(int number) {
    			for (int i = 0; i <= 10; i++) {
    				System.out.println(number + " * " + i + " = " + (number * i));
    			}
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			System.out.println("Enter your number");
    
    			Scanner scanner = new Scanner(System.in);
    			int number = scanner.nextInt();
    			new MultiplicationTable().process(number);
    		
    			scanner.close();
    		}
    
    	}
    
    



  11. Write a method that checks if a number is a Prime number. Call your method from the main method.

  12. Solution
    	package com.training.firstprogram;
    
    	import java.util.Scanner;
    
    	/**
    	 * This class would check if a number is prime
    	 */
    	public class PrimeNumberChecker {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public boolean process(int number) {
    			boolean returnValue = true;
    			for (int i = 2; i <= number / 2; i++) {
    				if (number % i == 0) { //Check if divisible by any other number
    					returnValue = false;
    					break;
    				} else {
    					continue;
    				}
    			}
    			return returnValue;
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			System.out.println("Enter your number");
    
    			Scanner scanner = new Scanner(System.in);
    			int number = scanner.nextInt();
    			boolean isPrime = new PrimeNumberChecker().process(number);
    			if (isPrime) {
    				System.out.println("Number " + number + " is a Prime number");
    			} else {
    				System.out.println("Number " + number + " is NOT a Prime number");
    			}
    			scanner.close();
    		}
    
    	}
    			
    			
    			



  13. Write a method that accepts two numbers are returns the maximum of Two. Call your method from the main method.

  14. Solution
    	package com.training.firstprogram;
    
    	import java.util.Scanner;
    
    	/**
    	 * This class would accept two numbers and returns the maximum of Two
    	 */
    	public class MaxNumber {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public int process(int number1, int number2) {
    			int max = number1;
    			if (number1 < number2) {
    				max = number2;
    			} else {
    				max = number1;
    
    			}
    			return max;
    
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			System.out.println("Enter your first number");
    
    			Scanner scanner = new Scanner(System.in);
    			int number1 = scanner.nextInt();
    			System.out.println("Enter your second number");
    			int number2 = scanner.nextInt();
    
    			int max = new MaxNumber().process(number1, number2);
    
    			System.out.println("Max is " + max);
    
    			scanner.close();
    		}
    
    	}
    
    			
    			 



  15. Write a method that accepts two numbers and returns the minimum of Two. Call your method from the main method.

  16. Solution
    			
    	package com.training.firstprogram;
    
    	import java.util.Scanner;
    
    	/**
    	 * This class would accept two numbers and returns the minimum of Two
    	 */
    	public class MinNumber {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public int process(int number1, int number2) {
    			int min = number1;
    			if (number1 > number2) {
    				min = number2;
    			} else {
    				min = number1;
    
    			}
    			return min;
    
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			System.out.println("Enter your first number");
    
    			Scanner scanner = new Scanner(System.in);
    			int number1 = scanner.nextInt();
    			System.out.println("Enter your second number");
    			int number2 = scanner.nextInt();
    
    			int min = new MinNumber().process(number1, number2);
    
    			System.out.println("min is " + min);
    
    			scanner.close();
    		}
    
    	}
    
    



  17. Write a method that accepts two numbers and returns the average of Two. Call your method from the main method.

  18. Solution
    	package com.training.firstprogram;
    
    
    	import java.util.Scanner;
    
    	/**
    	 * This class would accept two numbers and returns the averageimum of Two
    	 */
    	public class AverageNumbers {
    
    		/**
    		 * This method does the actual work
    		 * 
    		 */
    		public float process(float number1, float number2) {
    
    			return (number1 + number2) / 2.0f;
    
    		}
    
    		/**
    		 * This is the main method
    		 * 
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			System.out.println("Enter your first number");
    
    			Scanner scanner = new Scanner(System.in);
    			float number1 = scanner.nextFloat();
    			System.out.println("Enter your second number");
    			float number2 = scanner.nextFloat();
    
    			float average = new AverageNumbers().process(number1, number2);
    
    			System.out.println("Average is " + average);
    
    			scanner.close();
    
    		}
    
    	}
    
    			
    			 



  19. Write a method that calls all the above methods. Log the time at the begining of this method and at the end. Print how many milliseconds elapsed in executing all the above methods Call your method from the main method.

  20. Solution
    
    package com.training.firstprogram;
    
    import java.util.Date;
    
    /**
     * This class would accept two numbers and returns the averageimum of Two
     */
    public class PerformanceTracker {
    
    	/**
    	 * This method does the actual work
    	 * 
    	 */
    	public int process(int number1, int number2) {
    
    		return (number1 + number2) / 2;
    
    	}
    
    	/**
    	 * This is the main method
    	 * 
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Date start = new Date();
    		System.out.println("Processing starting. Time now is " + start);
    
    		System.out.println("Processing first method");
    		new BackwardCounting().backwardCounter();
    		
    		System.out.println("Processing second method");
    		new ReplaceChar().process("Java is fun");
    
    		System.out.println("Processing third method");
    		new ReverseString().process("Java");
    
    		System.out.println("Processing fourth method");
    		new PalindromeChecker().process("geeksskeeg");
    
    		System.out.println("Processing fifth method");
    		new MultiplicationTable().process(9);
    		
    		System.out.println("Processing sixth method");
    		new PrimeNumberChecker().process(17);
    		
    		System.out.println("Processing seventh method");
    		new MaxNumber().process(2, 7);
    		
    		System.out.println("Processing eigth method");
    		new MinNumber().process(2, 7);
    		
    		System.out.println("Processing ninth method");
    		new AverageNumbers().process(2, 7);
    
    		Date end = new Date();
    		System.out.println("Processing completed. Time now is " + end);
    
    		System.out.println("Processing completed in " + (end.getTime() - start.getTime()) + " milli seconds!");
    	}
    
    }
    			
    			


Training highlights

Sansa Technology Professional Training Programs have the following highlights:

  • Proven training programs.
  • Excellent track record.
  • Courses tailored for job placements and Interview success.
  • Best instructors in the Bay Area.
  • Exceptional marketing staff.
  • Small Class Room for Personalized Attention.
  • H1 Sponsorship Available.
  • E-Verified Employer.
  • Equal Opportunity Employer.
  • Resume and Interview Preparation.
read more

Quality Commitment

We are committed to the Quality. Quality comes first for any service we provide.
Read Testimonials from our previous Trainees, to see it yourself.

Register Now

You can register using one of many options below:

1. Online Registration:
Click Here to Register!

2. To Register By Phone:
Call (510) 244-3953

3. To Register By Email:
Email contact@sansatechnology.com

Check Training Calendar

Our clients

"I was very satisfied with the contents of the course which covered a wide range of useful topics... I would like to attend more of his classes in the future." - Jaishree, J2EE Engineer

read more
Have you got any questions?
Call us now - Our expert Customer Service Professionals will help you choose the right course.

Contact us today!