As the most basic of all the control flow statements, which one tells your program to execute a certain section of code ONLY if a particular test evaluates to true?
if-then statement
void applyBrakes() {
if (isMoving) {
currentSpeed–;
}
}
How is the if-then-else statement different than the if-then statement?
It provides a secondary path of execution when an “if” clause evaluates to false.
In the following code, what happens if the Bicycle is not moving?
void applyBrakes() {
if (isMoving) {
currentSpeed–;
} else {
System.err.println(“The bicycle has already stopped!”);
}
}
A message prints to the console saying, “The bicycle has already stopped!”
How is the switch statement different than the if-then and if-then-else statements?
The switch statement can have a number of paths and works with the byte, short, char and int primitive data types.
In the following code what will print to console?
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = ‘A’;
} else if (testscore >= 80) {
grade = ‘B’;
} else if (testscore >= 70) {
grade = ‘C’;
} else if (testscore >= 60) {
grade = ‘D’; } else {
grade = ‘F’;
}
System.out.println(“Grade = “ + grade);
}
}
The following output prints to console: Grade C
What’s the “for” statement do in Java and how is it generally expressed?
It provides a compact way to iterate over a range of values in a loop until a particular condition is satisfied.
for (initialization; termination; increment) {
statement(s)
}
What’s the output of the following program:
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println(“Count is: “ + i);
}
}
}
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
How does the Enhanced “for” differ from the regular for and what’s the output for this program with an Enhanced “for”?
class EnhancedForDemo {
public static void main(String[] args){
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println(“Count is: “ + item);
}
}
}
It is designed for iteration through Collections and arrays and can be used to make your loops more compact and easy to read.
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
What does the “while” statement do and what is its general syntax?
It continually executes a block of statements while a particular condition is true.
while (expression) {
statements(s)
}
How do we use a “while” statement to print values from 1 to 10?
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println(“Count is: “ + count);
count++;
}
}
}
What’s the difference between the “while” statement and Java’s “do-while” statement and what’s its syntax with an example for a program that counts to 10?
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top, which means the statements within the do block are always executed at least once.
class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println(“Count is: “ + count);
count++;
} while (count < 11);
}
}
What does the simple assignment operator do?
It assigns the value on its right to the operand on the left.
Examples:
int cadence = 0;
int speed = 0;
int gear = 1;
What are the common mathematical operators in Java?
What are logical operators in Java?
What are all the relational operators in Java?
What are unary operators in Java?
Can you have multiple catch blocks for one try block?
Yes.
What will the following code print out?
public class Increment {
public static void main(String[] args) {
int x = 0;
// prefix increment
System.out.println(++x);
// postfix increment
System.out.println(x++);
// x is 2
System.out.println(x);// x = x + 2 x %= 2;
// even elements for (int i=0; i\<10; i+=2); }
}
1
1
2
What will the following code print out?
public class Increment {
public static void main(String[] args) {
int x = 0;
// prefix decrement
System.out.println(--x);
// postfix decrement
System.out.println(x--);
// x is 2
System.out.println(x);// x = x + 2 x %= 2;
// even elements for (int i=0; i\<10; i+=2); }
}
What will the following code print?
public class Logical {
public static void main(String[] args) {
boolean holiday = false;
boolean weekend = true;
boolean work = false;
// short-circuit
if(holiday || weekend && !work) {
// day off
System.out.println("Day off");
}int x = 10;
if(x > 10 || x < 0) {}
}
}
Day off
How can you assign the value “var” to the String instance variable?
public class Assign {
}
public class Assign {
String instance = “var”;
}
What is an exception in Java?
It occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
What does throwing an exception entail?
In Java, it’s where an exception object is created and then handed to the runtime system!