All Flashcards
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 are the steps to execute an 'if' statement?
- Evaluate the condition. 2. If true, execute the code block. 3. If false, skip the code block.
What are the steps to execute a 'for' loop?
- Initialize the loop variable. 2. Check the condition. 3. If true, execute the loop body. 4. Increment/decrement the loop variable. 5. Repeat steps 2-4 until the condition is false.
What are the steps to execute a 'while' loop?
- Check the condition. 2. If true, execute the loop body. 3. Update the loop variable (if necessary). 4. Repeat steps 1-3 until the condition is false.
What are the steps to create an object from a class?
- Declare a variable of the class type. 2. Use the 'new' keyword followed by the class name and parentheses. 3. Optionally, initialize the object's fields.
How are conditional statements applied in real-world scenarios?
Controlling traffic lights, validating user input, decision-making in games.
How are loops applied in real-world scenarios?
Iterating through data in a database, repeating actions in a game, processing elements in an array.
How are Strings applied in real-world scenarios?
Storing and manipulating text data, processing user input, representing names and addresses.
How are Arrays applied in real-world scenarios?
Storing lists of items, representing game boards, managing collections of data.
How are Classes and Objects applied in real-world scenarios?
Modeling real-world entities (e.g., cars, people, bank accounts) in software.
How is Inheritance and Polymorphism applied in real-world scenarios?
Creating a hierarchy of animal classes where each animal makes a different sound, but all are treated as animals.