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

What are the differences between == and .equals() in Java?

==: compares object references (memory addresses) | .equals(): compares the content of the objects.

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

All Flashcards

What are the differences between == and .equals() in Java?

==: compares object references (memory addresses) | .equals(): compares the content of the objects.

What are the differences between && and ||?

&& (AND): Returns true only if both operands are true | || (OR): Returns true if at least one operand is true.

What are the differences between an if statement and an if-else statement?

if: Executes code only if the condition is true | if-else: Executes one block if true, another if false.

What are the differences between simple and compound boolean expressions?

Simple: single comparison | Compound: multiple comparisons combined with logical operators.

What are the differences between using nested if statements and compound boolean expressions?

Nested if statements can be more readable for complex logic, but compound boolean expressions can be more concise.

What are the differences between using multiple if statements vs. a single if-else if-else statement?

Multiple if statements will all be evaluated independently. An if-else if-else statement will stop evaluating after the first true condition.

What are the differences between a conditional statement and an iterative statement?

Conditional: Executes code based on a condition (if/else) | Iterative: Repeats code execution (for/while).

What are the differences between a boolean variable and a boolean expression?

Boolean variable: stores a boolean value (true/false) | Boolean expression: evaluates to a boolean value.

What are the differences between using ! and not using it?

! negates a boolean value. Using it can sometimes simplify code, while other times it can make it more confusing.

What are the differences between x > 0 || x < 10 and x > 0 && x < 10?

x > 0 || x < 10: true if x is greater than 0 OR less than 10 | x > 0 && x < 10: true if x is greater than 0 AND less than 10.

What is a Boolean expression?

An expression that evaluates to either true or false.

What is an if statement?

A conditional statement that executes a block of code only if a condition is true.

What is an if-else statement?

A conditional statement that executes one block of code if a condition is true, and another block if it's false.

What is an if-else if-else statement?

A conditional statement that creates multiple branches, allowing you to check a series of conditions.

What is the == operator?

Checks if two primitive values are equal.

What is the != operator?

Checks if two values are not equal.

What is the && operator?

The AND operator. Returns true only if both operands are true.

What is the || operator?

The OR operator. Returns true if at least one operand is true.

What is the ! operator?

The NOT operator. Negates a boolean value.

What does .equals() do?

Compares the content of objects for equality.

What does the following code output?

java
int x = 5;
if (x > 3) {
  System.out.println("A");
} else {
  System.out.println("B");
}

A

What does the following code output?

java
int x = 10;
if (x < 5 && x > 7) {
  System.out.println("True");
} else {
  System.out.println("False");
}

False

What does the following code output?

java
String str1 = "hello";
String str2 = new String("hello");
if (str1.equals(str2)) {
  System.out.println("Equal");
} else {
  System.out.println("Not Equal");
}

Equal

What does the following code output?

java
boolean a = true;
boolean b = false;
System.out.println(!a || b);

false

Identify the error in the following code:

java
if (x = 5) {
  System.out.println("x is 5");
}

The condition should use == for comparison, not = for assignment. It should be if (x == 5).

What does the following code output?

java
int score = 75;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else if (score >= 70) {
    System.out.println("C");
} else {
    System.out.println("D");
}

C

What does the following code output?

java
int num = 6;
if (num % 2 == 0 && num % 3 == 0) {
    System.out.println("Divisible by 2 and 3");
} else {
    System.out.println("Not divisible by 2 and 3");
}

Divisible by 2 and 3

What does the following code output?

java
int age = 20;
String message = (age >= 18) ? "Adult" : "Minor";
System.out.println(message);

Adult

What does the following code output?

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

false

What does the following code output?

java
boolean x = false, y = true;
if (x = y) {
    System.out.println("True");
} else {
    System.out.println("False");
}

True