limited time offer

Java Control Statements Interview Questions

Showing 1 - 10 of 28 results

What will be the output of below java code?

Example Code:

class ForWith0Comparison   
{
    public static void main (String args[])
    {
        for(int i=0; 0; i++)
        {
            System.out.println("Hello ProgramsBuzz");
        }  
    }  
} 

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to boolean

	at com.test.controlflowstatements.ForWith0Comparison.main(ForWith0Comparison.java:7)

Find out the difference between and output of below Java codes?

Example 1:

public Test
{
    public static void main(String[] args)
    {
        int a=20, b=10;
        
        while(b < a)
        {
            System.out.println("Hello");
        }
        System.out.println("Hi");
    }
}

Output: infinite times Hello

Example 2:

public Test
{
    public static void main(String[] args)
    {
        final int a=20, b=10;

        while(b<a)
        {
            System.out.println("Hello");
        }
        System.out.println("Hi");
    }
}

Output: unreachable code

What will be the output of below Java code?

Example Code: 

public Test
{
    public static void main(String[] args)
    {
        if(true)
            int a=10;
        
        System.out.println("Hello");
    }
}

Output:-Compile-time error: Variable declaration not allowed here.

If we are not using curly bracket then only one statement allows for if statement but that statement should not be declarative statement; In above example we are declaring int variable without curly bracket, this is not allowed because int a = 10 is a declarative statement. We can use any other statement except declarative statement.

What is an infinite loop in Java? Explain with an example.

An infinite loop is an instruction sequence in Java that loops endlessly when a functional exit isn't met. This type of loop can be the result of a programming error or may also be a deliberate action based on the application behavior. An infinite loop will terminate automatically once the application exits.

Example

public class InfiniteForLoopExample
{
    public static void main(String[] arg) 
    {
        for(;;)
        {
            System.out.println("ProgramsBuzz");
        }
    }
} 

Above program will keep on printing ProgramsBuzz.

Java program to print given Square Star Patten.

java program to print given star square pattern

package testjava.controlflow;

public class PrintSquareStar 
{
	public static void printSquareStar(int number)
	{
		if(number < 5)
		{
			System.out.println("Invalid Value");
		}
		else
		{
			for(int i = 1; i <= number; i++)
			{
				for(int j = 1; j <= number; j++) 
				{
					if((j == 1) || (j == number) || (i == 1) || 
					   (i == number) || (j == i) || (i == number - j + 1)) 
					{
						System.out.print("*");
					}
					else 
					{
						System.out.print(" ");
					}
				}
				System.out.println();
			}
		}
	}
	
	public static void main(String args[])
	{
		PrintSquareStar.printSquareStar(9);
	}
}

 

Java Program for Find largest prime factor of a number.

Below program will find largest prime factor of a number.

package testjava.controlflow;

public class LargestPrimeFactor
{
	public void largestPrimeFactor(long number)
	{
		int i;
		long originalNumber = number;
		
		for(i = 2; i <= number; i++)
		{
			if(number % i == 0)
			{
				number = number / i;
				i--;
			}
		}
		System.out.println("Largest Prime Factor of "+originalNumber+" is: "+i);	
	}
	
	public static void main(String args[])
	{
		LargestPrimeFactor lpf = new LargestPrimeFactor();
		
		lpf.largestPrimeFactor(6);
		lpf.largestPrimeFactor(21);
		lpf.largestPrimeFactor(45);
		lpf.largestPrimeFactor(217);
	}
}

OUTPUT

Largest Prime Factor of 6 is: 3
Largest Prime Factor of 21 is: 7
Largest Prime Factor of 45 is: 5
Largest Prime Factor of 217 is: 31

Java program to convert digits into its corresponding words. For Example: 12 = One Two

Below program will convert digits into its corresponding words:

package testjava.controlflow;

public class NumberToWords
{
	public void numberToWords(int number)
	{
		int reverseNumber = reverse(number);
		int digitcount = getDigitCount(number);
		
		System.out.print(number+":");
		while (digitcount > 0)
		{
			int test = reverseNumber % 10;
			
			switch (test)
			{
				case 0:
					System.out.print(" Zero");
					break;	
				case 1:
					System.out.print(" One");
					break;		
				case 2:
					System.out.print(" Two");
					break;	
				case 3:
					System.out.print(" Three");
					break;
				case 4:
					System.out.print(" Four");
					break;
				case 5:
					System.out.print(" Five");
					break;
				case 6:
					System.out.print(" Six");
					break;
				case 7:
					System.out.print(" Seven");
					break;
				case 8:
					System.out.print(" Eight");
					break;
				case 9:
					System.out.print(" Nine");
					break;
			}
	            
			reverseNumber /= 10;
			digitcount--;
		}
		System.out.println();
	}
	
	public static int getDigitCount(int number)
	{
		if(number < 0)
			return -1;
		else if(number == 0)
			return 1;
		
		int count = 0;
		
		while (number != 0)
		{
			number /= 10;
			count++;
		}
		return count;
	}
	
	public static int reverse(int number)
	{
		int result = 0;
		
		while(number != 0)
		{
			result = (result * 10) + number % 10;
			number /= 10;
		}
		return result;
	}
	
	public static void main(String args[])
	{
		NumberToWords ntw = new NumberToWords();
		
		ntw.numberToWords(345);
		ntw.numberToWords(012);
		ntw.numberToWords(456789);
	}		
}

OUTPUT

345: Three Four Five
10: One Zero
456789: Four Five Six Seven Eight Nine

Java program to find the perfect number.

A perfect number is a positive integer which is equal to the sum of its proper positive divisors. Proper positive divisors are positive integers that fully divide the perfect number without leaving a remainder. 

package testjava.controlflow;

public class PerfectNumber 
{
	public void isPerfectNumber(int number)
	{   
		int sum = 0;
		
		System.out.print("Proper positive Divisor of "+number+" are:");
		for(int i = 1; i<number; i++)
		{
			if(number % i == 0)
			{
				System.out.print(i + " ");
				sum += i;
			}
		}
		System.out.println();
		
		if(sum == number)
			System.out.println(number +" is perfect number.");
		else
			System.out.println(number +" is not a perfect number");
	}
	
	public static void main(String args[])
	{
		PerfectNumber pn = new PerfectNumber();
		pn.isPerfectNumber(6);
		pn.isPerfectNumber(12);
		pn.isPerfectNumber(14);
		pn.isPerfectNumber(28);
	}
}

OUTPUT

Proper positive Divisor of 6 are:1 2 3 
6 is perfect number.
Proper positive Divisor of 12 are:1 2 3 4 6 
12 is not a perfect number
Proper positive Divisor of 14 are:1 2 7 
14 is not a perfect number
Proper positive Divisor of 28 are:1 2 4 7 14 
28 is perfect number.

Java program to find all factors of a given integer number.

Below program will print all the factors of a given integer number.

package testjava.controlflow;

public class AllFactors 
{	
	public void printFactors(int number)
	{
		if(number < 1)
			System.out.print("Invalid Value");
		
		System.out.print("All Factors of "+number+" are: ");
		for(int i = 1; i <= number; i++)
		{
			if(number % i == 0)
				System.out.print(i + " "); 
		}
		System.out.println();
	}
	
	public static void main(String args[])
	{
		AllFactors af = new AllFactors();
		
		af.printFactors(6);
		af.printFactors(32);
		af.printFactors(10);
	}
}

OUTPUT

All Factors of 6 are: 1 2 3 6 
All Factors of 32 are: 1 2 4 8 16 32 
All Factors of 10 are: 1 2 5 10 

Subscribe to Java Control Statements Interview Questions