EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Java for EmSAT Achieve  >  Assignment: Loops in Java

Assignment: Loops in Java | Java for EmSAT Achieve PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which of the following statements correctly describes the need for loops in Java?
(a) Loops are used to iterate through arrays only.
(b) Loops help execute a set of statements repeatedly.
(c) Loops are used to declare variables in Java.
(d) Loops are required to implement conditional statements in Java.

Ans. (b)

Q.2. What is the output of the following code snippet?

int count = 1;

while (count <= 5) {

    System.out.print(count + " ");

    count++;

}

(a) 1 2 3 4 5
(b) 1 2 3 4
(c) 1 1 1 1 1
(d) 5 4 3 2 1

Ans. (a)

Q.3. Which loop in Java guarantees the execution of its body at least once?
(a) While loop
(b) Do-While loop
(c) For loop
(d) Enhanced for loop

Ans. (b)

Q.4. The ______ loop is commonly used when the number of iterations is known in advance.
(a) While
(b) Do-While
(c) For
(d) Enhanced for

Ans. (c)

Q.5. Which loop would you use to iterate through an ArrayList in Java?
(a) While loop
(b) Do-While loop
(c) For loop
(d) Enhanced for loop

Ans. (d)

Higher Order Thinking Skills (HOTS)

Q.1. Explain the difference between a while loop and a do-while loop in Java. Provide an example to illustrate their differences.

While loop:

  • The while loop checks the condition before executing the loop body.
  • It may not execute the loop body if the condition is initially false.

Example:

int count = 1;

while (count > 5) {

    System.out.print(count + " ");

    count++;

}

Output: (no output)

Do-while loop:

  • The do-while loop executes the loop body at least once, regardless of the condition.
  • It checks the condition after executing the loop body.

Example:
int count = 1;

do {

    System.out.print(count + " ");

    count++;

} while (count > 5);

Output: 1

Q.2. Write a program using a do-while loop to find the sum of all even numbers between 1 and 100 (inclusive).

Program to find the sum of all even numbers between 1 and 100 using a do-while loop:

int sum = 0;

int num = 2;

do {

    sum += num;

    num += 2;

} while (num <= 100);

System.out.println("Sum of even numbers between 1 and 100: " + sum);

Q.3. Write a program using a for loop to print the Fibonacci series up to a given number 'n'. (Assume 'n' is a positive integer)

Program to print the Fibonacci series up to a given number 'n' using a for loop:
int n = 10; // Example value of 'n'

int previous = 0;

int current = 1;

System.out.print("Fibonacci series up to " + n + ": ");

System.out.print(previous + " ");

System.out.print(current + " ");

for (int i = 2; i <= n; i++) {

    int next = previous + current;

    System.out.print(next + " ");

    previous = current;

    current = next;

}

Q.4. Discuss the advantages of using a for loop over a while loop in Java. Provide at least two advantages.

Advantages of using a for loop over a while loop:

  • A for loop provides a concise way to write loops, combining initialization, condition, and iteration in a single line.
  • It helps prevent infinite loops by ensuring that the loop control variable is automatically updated.

Q.5. Explain the term "loop control variable" in Java. Why is it important in loop constructs?

The loop control variable is a variable used in a loop to control the loop's execution and termination. It is usually initialized before the loop and updated within the loop body. The loop control variable is important as it determines the conditions for loop execution and termination, allowing precise control over the loop's behavior.

Fill in the Blanks

Q.1. The ________ statement is used to exit a loop prematurely.

Ans. break

Q.2. The ________ loop executes its body at least once, even if the condition is false initially.

Ans. do-while

Q.3. The ________ statement is used to skip the rest of the code in the current iteration of a loop.

Ans. continue

Q.4. In a ________ loop, the initialization, condition, and iteration statements are written within the loop itself.

Ans. for

Q.5. The ________ loop is useful when you need to iterate through the elements of an array or a collection.

Ans. enhanced for

True or False

Q.1. In Java, a while loop is guaranteed to execute at least once.

Ans. False

Q.2. A for loop can be used interchangeably with a while loop.

Ans. False

Q.3. A loop control variable in Java must be declared outside the loop. (True/False)

Ans. False

Q.4. The condition in a do-while loop is checked before executing the loop body. (True/False)

Ans. True

Q.5. The enhanced for loop is suitable for iterating through arrays and collections. (True/False)

Ans. True

Hands-On Questions

Q.1. Write a Java program to calculate the factorial of a given number using a for loop.

Java program to calculate the factorial of a given number using a for loop:
import java.util.Scanner;


public class Factorial {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int number = input.nextInt();

        int factorial = 1;

        for (int i = 1; i <= number; i++) {

            factorial *= i;

        }

        System.out.println("Factorial of " + number + ": " + factorial);

    }

}

Q.2. Write a Java program that takes an integer input from the user and prints whether it is a prime number or not. Use a while loop for the implementation.

Java program to check if a number is prime using a while loop:
import java.util.Scanner;

public class PrimeNumber {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int number = input.nextInt();

        int i = 2;

        boolean isPrime = true;

        while (i <= number / 2) {

            if (number % i == 0) {

                isPrime = false;

                break;

            }

            i++;

        }

        if (isPrime)

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

        else

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

    }

}

Q.3. Write a Java program to print the following pattern using nested loops:

*

**

***

****

*****

Java program to print the pattern using nested loops:

for (int i = 1; i <= 5; i++) {

    for (int j = 1; j <= i; j++) {

        System.out.print("*");

    }

    System.out.println();

}

Q.4. Write a Java program to find the sum of all odd numbers between 1 and 100 (inclusive) using a do-while loop.

Java program to find the sum of all odd numbers between 1 and 100 using a do-while loop:
int sum = 0;

int num = 1;

do {

    sum += num;

    num += 2;

} while (num <= 100);

System.out.println("Sum of odd numbers between 1 and 100: " + sum);

Q.5. Write a Java program to print the multiplication table of a given number using a do-while loop.

Java program to print the multiplication table of a given number using a do-while loop:
int number = 7; // Example value of 'number'

int i = 1;

System.out.println("Multiplication table of " + number + ":");

do {

    System.out.println(number + " x " + i + " = " + (number * i));

    i++;

} while (i <= 10);

The document Assignment: Loops in Java | Java for EmSAT Achieve is a part of the EmSAT Achieve Course Java for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
60 videos|37 docs|12 tests

Top Courses for EmSAT Achieve

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

mock tests for examination

,

Assignment: Loops in Java | Java for EmSAT Achieve

,

Extra Questions

,

MCQs

,

study material

,

Important questions

,

Viva Questions

,

Free

,

shortcuts and tricks

,

Previous Year Questions with Solutions

,

Assignment: Loops in Java | Java for EmSAT Achieve

,

Summary

,

Exam

,

Assignment: Loops in Java | Java for EmSAT Achieve

,

ppt

,

past year papers

,

video lectures

,

Objective type Questions

,

Semester Notes

,

pdf

,

practice quizzes

;