Java Control Statements Interview Questions in a single program


  

Program 1:

here's an example Java program that demonstrates the use of different control statements:


import java.util.Scanner;

public class ControlStatementsExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // If-else statement
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        if (number % 2 == 0) {
            System.out.println(number + " is even.");
        } else {
            System.out.println(number + " is odd.");
        }

        // Switch statement
        System.out.print("Enter a day of the week (1-7): ");
        int day = scanner.nextInt();
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Invalid day");
        }

        // While loop
        int i = 1;
        while (i <= 5) {
            System.out.println("Count: " + i);
            i++;
        }

        // Do-while loop
        i = 1;
        do {
            System.out.println("Count: " + i);
            i++;
        } while (i <= 5);

        // For loop
        for (int j = 1; j <= 5; j++) {
            System.out.println("Count: " + j);
        }

        scanner.close();
    }
}


This program uses the following control statements:

if-else statement to check if a number is even or odd. switch statement to print the day of the week based on a user input. while loop to print the numbers from 1 to 5. do-while loop to print the numbers from 1 to 5. for loop to print the numbers from 1 to 5.

Program 2 :

Here's an example Java program that includes several different control statements:


public class ControlStatementsExample {

  public static void main(String[] args) {
  
    int x = 5;
    int y = 10;
    
    // If statement
    if (x > y) {
      System.out.println("x is greater than y");
    } else {
      System.out.println("y is greater than or equal to x");
    }
    
    // Switch statement
    switch (x) {
      case 1:
        System.out.println("x is 1");
        break;
      case 2:
        System.out.println("x is 2");
        break;
      case 3:
        System.out.println("x is 3");
        break;
      default:
        System.out.println("x is not 1, 2, or 3");
    }
    
    // While loop
    int i = 0;
    while (i < 5) {
      System.out.println("i is " + i);
      i++;
    }
    
    // For loop
    for (int j = 0; j < 5; j++) {
      System.out.println("j is " + j);
    }
    
    // Do-while loop
    int k = 0;
    do {
      System.out.println("k is " + k);
      k++;
    } while (k < 5);
    
  }
  
}


In this program, we use an if statement to compare two variables and print a message based on their relative values. We also use a switch statement to check the value of x and print a message based on the case that matches.

We then use three different types of loops: a while loop that iterates while a condition is true, a for loop that iterates a fixed number of times, and a do-while loop that iterates at least once and then checks a condition to see if it should continue.

These are just a few examples of the different control statements available in Java. There are many more, and they can be combined in many different ways to create complex programs.

Program 3:

Here's an example program in Java that uses various control statements:


import java.util.Scanner;

public class ControlStatementsExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        // if-else statement
        if (num % 2 == 0) {
            System.out.println(num + " is even.");
        } else {
            System.out.println(num + " is odd.");
        }

        // switch statement
        switch (num) {
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            default:
                System.out.println("Number is not between 1 and 3.");
        }

        // for loop
        for (int i = 1; i <= num; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

        // while loop
        int i = 1;
        while (i <= num) {
            System.out.print(i + " ");
            i++;
        }
        System.out.println();

        // do-while loop
        i = 1;
        do {
            System.out.print(i + " ");
            i++;
        } while (i <= num);
        System.out.println();
    }
}

This program asks the user to enter a number and then uses various control statements to perform different actions based on the input. The if-else statement checks whether the input is even or odd, the switch statement prints the corresponding word for the input number (1, 2, or 3), the for loop prints the numbers from 1 to the input number, the while loop also prints the numbers from 1 to the input number, and the do-while loop does the same.

Post a Comment

0 Comments