Arrays and Loops - MCQ Practice
10 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the output of the following code? \nint[] numbers = {1, 2, 3, 4, 5}; \nint sum = 0; \nfor (int i = 0; i < numbers.length; i++) { \n if (i % 2 == 0) { \n sum += numbers[i]; \n } \n} \nSystem.out.println(sum);

  • 9 (correct)
  • 8
  • 12
  • 15
  • What is wrong with the following code? \nint[] arr = new int;

  • Array size must be defined. (correct)
  • The variable name is not valid.
  • Arrays cannot be initialized without values.
  • The data type is incorrect.
  • In the provided code, what is the purpose of the condition if (i % 2 == 0)?

  • To check if the element at index i is even.
  • To skip the iteration if the index is odd.
  • To check if the index is even. (correct)
  • To ensure every iteration is counted.
  • What would happen if the code used if (i % 2 != 0) instead of if (i % 2 == 0)?

    <p>The sum would include elements at odd indices.</p> Signup and view all the answers

    What would be the correct way to initialize an array with three elements in Java?

    <p>int[] arr = new int[3];</p> Signup and view all the answers

    What will be the final value of the variable 'sum' after executing the provided code?

    <p>8</p> Signup and view all the answers

    What is the primary issue with the code snippet int[] arr = new int;?

    <p>The syntax for creating an array is incorrect.</p> Signup and view all the answers

    If the loop in the first code example didn't include the condition if (i % 2 == 0), what would happen?

    <p>The sum would include all elements of the array.</p> Signup and view all the answers

    What is the effect of changing the condition to if (i % 2 != 0) in the initial code?

    <p>The sum would include only the elements at odd indices.</p> Signup and view all the answers

    What will happen if the array is initialized as int[] numbers = new int[5]; before the loop?

    <p>The array will contain default values of zero.</p> Signup and view all the answers

    Study Notes

    Arrays and Loops - MCQ Practice

    • Question 1: Calculates the sum of elements at even indices (0, 2, 4) in the numbers array: 1 + 3 + 5 = 9. Therefore, the correct answer is A) 9.

    • Question 2: The provided code snippet is incomplete and contains a syntax error. The line int[] arr = new int; is incorrect. To declare and initialize an integer array, specify the size using square brackets, e.g., int[] arr = new int[5]; The for loop is also incomplete, lacking a termination condition. A valid loop requires a comparison, such as i < arr.length.

    Question 1: Array Summation with Conditional Logic

    • The code iterates through an integer array numbers.
    • It adds elements at even indices (0, 2, 4) to the sum variable.
    • numbers contains {1, 2, 3, 4, 5}.
    • The loop adds 1 + 3 + 5.
    • The final sum is 9.
    • Therefore, the correct answer is A) 9.

    Question 2: Array Declaration Error

    • The code attempts to create an integer array but incorrectly omits the array size.
    • int[] arr = new int; is invalid syntax for array declaration in Java.
    • To declare an array, you need to specify its size using square brackets: int[] arr = new int[size]; or initialize it directly with values: int[] arr = {1,2,3};
    • The code's for loop is also incomplete, missing the termination condition. A correct example would be: for (int i = 0; i < arr.length; i++) { ... }

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    UPDATED MCQ PRACTICE PDF

    Description

    Test your knowledge on arrays and loops with this multiple-choice quiz. You'll encounter questions assessing your understanding of array manipulation and for loop syntax. Perfect for beginners and those looking to reinforce their programming skills.

    More Like This

    Use Quizgecko on...
    Browser
    Browser