Skillstorm Intro to Coding - Programming in Java Flashcards
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
- In the applyBrakes method, what happens if the bicycle is in motion?
- And what happens if it is not in motion?
void applyBrakes() {
if (isMoving) {
currentSpeed–;
}
}
- The program jumps into the (isMoving) block which slows down due to the “currentSpeed–;” clause.
- If it test false for (isMoving), or in other words the bicycle is not in motion, then the program jumps to the end of the if-then statement.
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?
- ==> Additive operator
- ==> Subtraction operator
- * ==> Multiplication operator
- / ==> Division operator
- % ==> Remainder operator
What are logical operators in Java?
- AND operator ==> &
- Short-circuit AND operator ==> && (will return false if a false statement is encountered)
- OR operator ==> |
- Short-circuit OR operator ==> || (will return true if a true statement is encountered)
What are all the relational operators in Java?
- equal to ==> ==
- not equal to ==> !=
- greater than ==> >
- greater than or equal to ==> >=
- less than ==> <
- less than or equal to ==> <=
What are unary operators in Java?
- Unary plus operator; indicates positive value (numbers are positive without this, however) ==> +
- Unary minus operator; negates an expression ==> -
- Increment operator; increments a value by 1 ==> ++
- Decrement operator; decrements a value by 1 ==> –
- Logical complement operator; inverts the value of a boolean ==> !
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); }
}
- 1
- 1
- 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!
After a method throws an exception, the runtime system attempts to find something to handle and this something is an ordered list of methods known as what?
The call stack
The method that contains a block of code that can handle the exception is known as what?
Exception Handler.