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

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.

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

All Flashcards

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

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.

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.