20 MCQ Questions on Output Based Questions on Looping Chapter ICSE X Computer Applications Subject

 

20 MCQ Questions on Output Based Questions on Looping Chapter ICSE X Computer Applications Subject 


1. What is the output of the following Java code?

     

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

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

   }

      

   a. 1 2 3 4 5  

   b. 1 2 3 4  

   c. 0 1 2 3 4  

   d. 0 1 2 3 4 5  


2. In Java, what is the purpose of the "continue" statement in a loop?

   a. Terminates the loop immediately  

   b. Skips the current iteration and proceeds to the next  

   c. Jumps to a specific label within the loop  

   d. Breaks out of the loop entirely  


3. How would you calculate the factorial of a number in Java using a for loop?

   a. Using a nested loop  

   b. Using a while loop  

   c. Using a do-while loop  

   d. Using a single for loop  


4. What will be the output of the following Java code?


   int num = 12345;

   int sum = 0;

   do {

       sum += num % 10;

       num /= 10;

   } while (num > 0);

   System.out.println("Sum of digits: " + sum);

      

   a. Sum of digits: 15  

   b. Sum of digits: 10  

   c. Sum of digits: 5  

   d. Sum of digits: 14  


5. Which loop in Java is best suited for iterating over the elements of an array?

   a. for loop  

   b. while loop  

   c. do-while loop  

   d. enhanced for loop  


6. What is the purpose of the "break" statement in a loop?

   a. Skips the current iteration and proceeds to the next  

   b. Terminates the loop immediately  

   c. Exits the loop only if a condition is met  

   d. Skips to a specific label within the loop  


7. What does the following Java code print?


   for (int row = 1; row <= 3; row++) {

       for (int col = 1; col <= row; col++) {

           System.out.print("* ");

       }

       System.out.println();

   }

      

   a. *

   b. * *

   c. * * *

   d. * * * *


8. Which loop is guaranteed to execute at least once in Java?

   a. for loop  

   b. while loop  

   c. do-while loop  

   d. enhanced for loop  


9. What does the following Java code output?


   int[] numbers = {10, 20, 30, 40, 50};

   for (int num : numbers) {

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

   }

      

   a. 10 20 30 40 50  

   b. 50 40 30 20 10  

   c. 1 2 3 4 5  

   d. 0 1 2 3 4  


10. In a for loop, what does the initialization part typically include?

    a. Loop condition  

    b. Loop body  

    c. Initial value  

    d. Increment or decrement  



11. What is the purpose of the "i++" in the for loop header?

    a. To decrement the loop variable  

    b. To increment the loop variable  

    c. To multiply the loop variable  

    d. To divide the loop variable  


12. Which loop construct is most suitable when the number of iterations is not known in advance?

    a. for loop  

    b. while loop  

    c. do-while loop  

    d. enhanced for loop  


13. How can you exit a loop prematurely in Java?

    a. Using the "exit" keyword  

    b. Using the "terminate" keyword  

    c. Using the "break" statement  

    d. Using the "end" statement  


14. What is the output of the following Java code?


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

        if (i % 2 == 0) {

            continue;

        }

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

    }

       

    a. 1 3 5 7 9  

    b. 2 4 6 8 10  

    c. 1 2 3 4 5  

    d. 1 2 3 4 5 6 7 8 9 10  



15. What is the purpose of the "row <= 3" condition in the outer loop of the pattern printing code?

    a. It controls the number of rows printed  

    b. It controls the number of columns printed  

    c. It determines the pattern shape  

    d. It has no effect on the output  


16. In the do-while loop for summing digits, what is the purpose of "num /= 10;"?

    a. It adds the last digit to the sum  

    b. It divides the number by 10  

    c. It multiplies the sum by 10  

    d. It checks if the number is greater than 10  


17. Which loop construct is used for traversing elements in an array without using an index?

    a. for loop  

    b. while loop  

    c. do-while loop  

    d. enhanced for loop  


18. What is the output of the following Java code?


    int number = 5;

    int factorial = 1;

    int i = 1;

    while (i <= number) {

        factorial *= i;

        i++;

    }

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

       

    a. Factorial of 5 is: 120  

    b. Factorial of 5 is: 15  

    c. Factorial of 5 is: 5  

    d. Factorial of 5 is: 1  


19. Which loop construct is used for executing a block of code repeatedly based on a condition?

    a. for loop  

    b. while loop  

    c. do-while loop  

    d. enhanced for loop  



20. What does the following Java code output?


    int[] numbers = {12, 45, 78, 23, 56, 89};

    int max_number = Arrays.stream(numbers).max().getAsInt();

    System.out.println("The largest number in the list is: " + max_number);

       

    a. The largest number in the list is: 12  

    b. The largest number in the list is: 89  

    c. The largest number in the list is: 78  

    d. The code will result in a compilation error  



Answers:

1. a, 2. b, 3. d, 4. a, 5. d, 6. b, 7. d, 8. c, 9. a, 10. c, 11. b, 12. c, 13. c, 14. a, 15. a, 16. b, 17. d, 18. a, 19. b, 20. b.


Comments