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

What are the differences between 'for' and 'while' loops?

For: Used when the number of iterations is known. While: Used when the number of iterations is unknown, and depends on a condition.

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

All Flashcards

What are the differences between 'for' and 'while' loops?

For: Used when the number of iterations is known. While: Used when the number of iterations is unknown, and depends on a condition.

What are the differences between == and .equals() when comparing strings?

==: Compares object references (memory locations). .equals(): Compares the content of the strings.

What are the differences between inheritance and polymorphism?

Inheritance: Allows code reuse and creates an 'is-a' relationship. Polymorphism: Allows objects of different classes to be treated as objects of a common type.

What does the following code output?

java
int x = 10;
if (x > 5) {
    x = x * 2;
}
System.out.println(x);

20

What does the following code output?

java
int i = 0;
while (i < 4) {
    System.out.print(i + " ");
    i++;
}

0 1 2 3

What does the following code output?

java
String str = "Hello";
System.out.println(str.substring(1, 4));

ell

What does the following code output?

java
int[] arr = {5, 10, 15};
System.out.println(arr[1]);

10

What does the following code output?

java
String s1 = "Java";
String s2 = "Java";
System.out.println(s1.equals(s2));

true

What does the following code output?

java
int sum = 0;
for (int i = 1; i <= 3; i++) {
    sum += i;
}
System.out.println(sum);

6

What does the following code output?

java
String str = "Computer";
System.out.println(str.length());

8

Identify the error in the following code:

java
int[] arr = new int[3];
System.out.println(arr[3]);

ArrayIndexOutOfBoundsException: Index 3 is out of bounds for an array of length 3.

What does the following code output?

java
int x = 3;
if (x % 2 != 0) {
    System.out.println("Odd");
} else {
    System.out.println("Even");
}

Odd

What does the following code output?

java
String str = "Hello World";
System.out.println(str.indexOf("World"));

6

What is control flow?

The order in which statements are executed in a program.

What is the purpose of conditional statements?

To allow programs to make decisions based on conditions.

Why is indentation important in code?

It improves code readability and helps identify code blocks.

What is the purpose of loops in programming?

To repeat a block of code multiple times.

Why are Strings immutable in Java?

To improve security and efficiency.

What is the significance of array indices starting at 0?

It's a convention in many programming languages for accessing array elements.

What is the relationship between classes and objects?

A class is a blueprint, and an object is an instance of that blueprint.

What is method overriding?

A subclass provides a specific implementation of a method already defined in its superclass.

What is the 'is-a' relationship?

A concept in inheritance where a subclass is a type of its superclass.

What is the purpose of the 'new' keyword?

To create a new object (instance) of a class.