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

Enhanced for loop vs. Regular for loop: Modification?

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

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

All Flashcards

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

What is an enhanced for loop?

A simplified for loop for traversing data structures forward.

What does 'pass-by-value' mean?

A copy of the variable's value is passed to the method.

What is a mutator method?

A method that modifies the state of an object.

What is meant by traversing a data structure?

Visiting each element in a data structure.

What is an object reference?

A variable that points to the memory location of an object.

What does 'index' mean in the context of arrays?

The position of an element within an array.

Define 'data structure'.

A way of organizing and storing data.

What is the purpose of a loop?

To execute a block of code repeatedly.

What is an array?

A collection of elements of the same data type, stored in contiguous memory locations.

What is the role of the 'dataType' in enhanced for loop?

Specifies the data type of the elements in the array.