What prints out from the following code?
int x = 5;
if (x <= 5) { System.out.println("hello"); }
else { System.out.println("world"); }
"hello"
Any for loop can be written as a while loop, T/F?
true
Declare an integer variable called "a" and initialize it to 5.
int a = 5;
the modulus operator
%
0
The next symbol in this code is:
if (x == 5)
{
Make the following into a for loop:
int i = 0;
while (i < 10) {
// code
i++;
}
for (int i = 0; i < 10; i++) {
// code
}
You can call methods on a String variable, T/F?
true
How to test if a number is even?
number % 2 == 0
How do you iterate through a 2D array?
Two nested for loops
Under what conditions does "orange" get printed?
if (x > 7) {
System.out.println("red");
} else {
System.out.println("orange");
}
when x <= 7
What is the syntax to declare a while loop that runs forever?
while (true) {
}
How do you print an array variable?
Iterate through it in a for loop, printing element by element
What is the difference between = and ==?
= is for assignment, == is for equality testing
How to reference the element at the 3rd row, 1st column of a 2D array?
array[2][0]
Name at least 2 syntax problems with this code.
if x == 6 {
System.out.println("apple")
parenthesis around condition, missing semicolon, missing closing bracket
Keyword that allows you to immediately halt execution of a loop
break
You can call methods on an int variable, T/F?
false
How to test two Strings for equality
.equals()
Declare and initialize a variable named "w" that is a new 2D array of Strings, with 4 rows and 5 columns.
String[][] w = new String[4][5];
Name at least 2 syntax problems with this code:
if (x = 4) {
System.out.println("bananas");
else (x % 4) {
System.out.println("oranges");
}
double equality operator, missing bracket to close if, x % 4 isn't a boolean condition
What is the sum after the loop runs?
int sum = 0;
for (int i = 0; i < 4; i++){
sum += i;
}
Name at least 2 problems with the following code that tries to declare an initialize a new 2D array of ints:
int[][] = q_array new String[4];
int vs String, 2D vs 1D, = in wrong place
What two things can the + operator do?
add two numbers, concatenate two Strings
Accessing array[5] in an array of length 5 results in ...
an out of bounds exception