zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

Why can't enhanced for loops modify primitive array elements?

Java uses pass-by-value, so changes to the loop variable don't affect the original array.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

Why can't enhanced for loops modify primitive array elements?

Java uses pass-by-value, so changes to the loop variable don't affect the original array.

How does an enhanced for loop work?

It iterates through each element of an array or collection, providing access to the element's value.

Can enhanced for loops traverse backwards?

No, enhanced for loops can only traverse in the forward direction.

When are enhanced for loops inappropriate?

When you need to modify the original array elements directly or need to traverse in a non-sequential manner.

How do enhanced for loops handle object references?

They provide a copy of the object reference, allowing modification of the object's state.

What is the scope of the loop variable in an enhanced for loop?

The loop variable is only accessible within the body of the enhanced for loop.

Why use enhanced for loops?

They simplify array/collection traversal, reducing code and potential errors.

What is the relationship between enhanced for loops and regular for loops?

Any enhanced for loop can be rewritten as a for loop, but not vice versa.

What are the limitations of enhanced for loops?

Cannot modify array elements directly and cannot control the traversal order or indices.

Explain the concept of 'object reference'.

A pointer to the memory location where an object is stored; multiple references can point to the same object.

Enhanced for loop vs. Regular for loop: Modification?

Enhanced: Cannot directly modify original array. Regular: Can modify original array.

Enhanced for loop vs. Regular for loop: Traversal control?

Enhanced: No control over indices. Regular: Full control over indices and traversal order.

Enhanced for loop vs. Regular for loop: Readability?

Enhanced: More concise for simple traversals. Regular: More verbose.

Enhanced for loop vs. Regular for loop: Use cases?

Enhanced: Iterating over all elements. Regular: Conditional traversal or modification.

Enhanced for loop vs. Regular for loop: Flexibility?

Enhanced: Less flexible. Regular: More flexible.

Enhanced for loop vs. Regular for loop: Direction of traversal?

Enhanced: Forward only. Regular: Forward or backward.

Enhanced for loop vs. Regular for loop: Access to index?

Enhanced: No direct access. Regular: Direct access.

Enhanced for loop vs. Regular for loop: Complexity?

Enhanced: Simpler syntax. Regular: More complex syntax.

Enhanced for loop vs. Regular for loop: When to use?

Enhanced: When you need to iterate through all elements without needing the index. Regular: When you need the index or need to modify the array.

Enhanced for loop vs. Regular for loop: Performance?

Enhanced: Generally similar performance. Regular: Can be optimized for specific scenarios.

What is wrong with this code?

java
int[] nums = {1, 2, 3};
for (int num : nums) {
  num = num * 2;
}

The array 'nums' is not modified because 'num' is a copy.

What does this code do?

java
String[] names = {"Alice", "Bob"};
for (String name : names) {
  System.out.println(name);
}

Prints each name in the 'names' array on a new line.

Predict the output:

java
int[] arr = {5, 10, 15};
for (int val : arr) {
  val += 5;
  System.out.print(val + " ");
}

5 10 15

Identify the error:

java
int[] numbers = {1, 2, 3};
for (double num : numbers) {
  System.out.println(num);
}

Incompatible types: cannot convert from int to double in the loop declaration.

What is the output?

java
String[] words = {"hello", "world"};
for (String word : words) {
  word = word.toUpperCase();
  System.out.println(word);
}

hello world

What is the output of this code?

java
int[] numbers = {10, 20, 30};
for (int i : numbers) {
  i = i + 5;
}
for (int i = 0; i < numbers.length; i++) {
  System.out.print(numbers[i] + " ");
}

10 20 30

What is the output of this code?

java
String[] names = {"Alice", "Bob"};
for (String name : names) {
  name = "Charlie";
}
for (String name : names) {
  System.out.print(name + " ");
}

Alice Bob

What is the output of this code?

java
int[] numbers = {1, 2, 3};
for (int number : numbers) {
  number *= 2;
  System.out.print(number + " ");
}
System.out.println();
for (int number : numbers) {
  System.out.print(number + " ");
}

2 4 6 1 2 3

What is the output of this code?

java
Integer[] numbers = {1, 2, 3};
for (Integer number : numbers) {
  number = number * 2;
}
for (Integer number : numbers) {
  System.out.print(number + " ");
}

1 2 3

What is the output of this code?

java
class Student {
    String name;
    public Student(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
}

public class Main {
    public static void main(String[] args) {
        Student[] students = {new Student("Alice"), new Student("Bob")};
        for (Student student : students) {
            student.setName("Charlie");
        }
        for (Student student : students) {
            System.out.print(student.getName() + " ");
        }
    }
}

Charlie Charlie