Skillstorm Intro to Coding - Programming in Java Flashcards

1
Q

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?

A

if-then statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. In the applyBrakes method, what happens if the bicycle is in motion?
  2. And what happens if it is not in motion?

void applyBrakes() {

if (isMoving) {

currentSpeed–;

}

}

A
  1. The program jumps into the (isMoving) block which slows down due to the “currentSpeed–;” clause.
  2. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is the if-then-else statement different than the if-then statement?

A

It provides a secondary path of execution when an “if” clause evaluates to false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A message prints to the console saying, “The bicycle has already stopped!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is the switch statement different than the if-then and if-then-else statements?

A

The switch statement can have a number of paths and works with the byte, short, char and int primitive data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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);

}

}

A

The following output prints to console: Grade C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the “for” statement do in Java and how is it generally expressed?

A

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)

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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);

}

}

}

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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);

}

}

}

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the “while” statement do and what is its general syntax?

A

It continually executes a block of statements while a particular condition is true.

while (expression) {

statements(s)

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we use a “while” statement to print values from 1 to 10?

A

class WhileDemo {

public static void main(String[] args){

int count = 1;

while (count < 11) {

System.out.println(“Count is: “ + count);

count++;

}

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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?

A

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);

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the simple assignment operator do?

A

It assigns the value on its right to the operand on the left.

Examples:

int cadence = 0;

int speed = 0;

int gear = 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the common mathematical operators in Java?

A
    • ==> Additive operator
    • ==> Subtraction operator
  • * ==> Multiplication operator
  • / ==> Division operator
  • % ==> Remainder operator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are logical operators in Java?

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are all the relational operators in Java?

A
  • equal to ==> ==
  • not equal to ==> !=
  • greater than ==> >
  • greater than or equal to ==> >=
  • less than ==> <
  • less than or equal to ==> <=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are unary operators in Java?

A
  • 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 ==> !
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Can you have multiple catch blocks for one try block?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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);
 }

}

A

1

1

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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);
 }

}

A
  • 1
  • 1
  • 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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) {}

}

}

A

Day off

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How can you assign the value “var” to the String instance variable?

public class Assign {

}

A

public class Assign {

String instance = “var”;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is an exception in Java?

A

It occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What does throwing an exception entail?

A

In Java, it’s where an exception object is created and then handed to the runtime system!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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?

A

The call stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

The method that contains a block of code that can handle the exception is known as what?

A

Exception Handler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

The chosen exception handler is to do what?

A

Catch the exception.

28
Q

What are some exceptional events that occur in the code at runtime?

A
  • Reading a corrupted file
  • Network connection failure
  • Division by zero
  • Calling method without an object
29
Q

What type of exceptions are there?

A
  • IOException
  • ConnectException
  • ArithmeticException
  • NullPointerException
30
Q

Can Java recover from Errors and Exceptions?

A

No. Java can only recover from Exceptions

31
Q

What are Checked Exceptions and where must they be resolved before the code can compile?

A

IOException and ConnectException are Checked Exceptions and they are inherited from the Exception Class.

32
Q

What are unchecked Exceptions and where are they inherited from?

A

ArithmeticException and NullPointerException are unchecked exceptions that are inherited from RuntimeExceptions.

33
Q

Do unchecked Exceptions need to be handled at compile time (Exception Class)?

A

They do not need to be handled at compile time but they can still cause problems at runtime.

34
Q

What type of exception will this code throw?

public class Risky {

public static void main(String[] args) {
int x = 5 / 0;
Object obj = null;

}

}

A

It will throw an unchecked ArithmeticException as follows:

Exception in thread “main” java.lang.ArithmeticException: / by zero
at Risky.main(Risky.java:5)

35
Q

What type of Exception will this code throw?

public class Risky {

public static void main(String[] args) {
Object obj = null;
obj.toString();

}

}

A

It will throw an unchecked NullPointerException as follows:

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “Object.toString()” because “obj” is null
at Risky.main(Risky.java:7)

36
Q

What exception will the following code throw?

public class Risky {

public static void main(String[] args) {
int[] arr = new int[5];
arr[100] = 10;

}

}

A

It will throw an ArrayIndexOutOfBoundsException as follows:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 100 out of bounds for length 5
at Risky.main(Risky.java:9)

37
Q

What Exception will the following code throw?

public class Risky {

public static void main(String[] args) {

 FileInputStream file = new FileInputStream("file.txt");
 }

}

A

This will throw a checked exception inherited at the Exception class that prevents compiling and is as follows:

Exception in thread “main” java.lang.Error: Unresolved compilation problems:
FileInputStream cannot be resolved to a type
FileInputStream cannot be resolved to a type

at Risky.main(Risky.java:11)

38
Q

What does a basic try-catch block look like in a TryCatch Class (for learning purposes)?

List several types of procedures it can perform and highlight where you determine the type of exception it can handle.

A

import java.io.FileInputStream;

public class TryCatch {

static FileInputStream file;

public static void main(String[] args) {
 try {
 file = new FileInputStream("file.txt");
 } catch(FileNotFoundException e) { \<==You can switch the FileNotFound to whatever exception you need it to be
 // recovery procedures
 // create the file... then open it
 //log exception
 }

}

}

39
Q

What are the differences between throw and throws in Exceptions?

A
  1. The throw clause is telling us to create the deception object and stop execution.
  2. The throws clause is a declaration of your method that says this method has the potential of throwing a particular Exception.
40
Q

What are uses of the Finally Block?

A
  • Run after a try block - regardless if an exception is thrown
  • Useful for closing resources like files, network or database connections, etc.
  • Not to be confused with final keyword (Variable value cannot be changed, Methods cannot be overridden, and Classes cannot be extended )
  • Format for Finally Block is as follows:
  • try {
  • }catch(Exception e) {
  • }finally{}
41
Q

What are the main reasons for the finally block?

A
  1. It always executes when the try block exits and this ensures that finally block is executed even if an unexpected exception occurs
  2. The finally block also makes it possible to avoid cleanup code to be accidentally bypassed by a return, continue, or break.
42
Q

Add the code to print “Try Catch Finally”

_____ {

System.out.print(“Try “);

_____ new Exception();

} ______ (Exception e) {

System.out.print(“Catch “);

} ______ {

System.out.print(“Finally”);

}

A

try

throw

catch

finally

43
Q

Fill in the blank so that the code compiles and declares the exception:

void read() _____ FileNotFoundException{

FileInputStream file = new FileInputStream(“file.txt”);

}

A

throws

44
Q

Given the process method throws both AException and BException, select the code so that it compiles properly:

try{

process();

} catch(______ e) {

log.error(e);

} catch(______ e) {

log.error(e);

}

class AException extends Exception {}

class BException extends AException {}

A

BException

AException

45
Q

Fill in the blank to cause the DeploymentException to be thrown at runtime:

public class Parachute {

public static void main(String[] args) {

pull();

}

static void pull() throws DeploymentException{

_____ _____ _____

}

}

class DeploymentException extends RuntimeException {}

A

throw new DeploymentException():

46
Q

Select and drop the comment over the appropriate line of code where that particular exception will be thrown:

_____

int x = 5 /0;

_____

Object obj = null;

obj.toString();

_____

int[] arr = new int[5];

arr[100] = 10;

_____

FileInputStream file= new FileInputStream(“file.txt”);

A

// (unchecked) ArithmeticException

// (unchecked) NullPointerException

// (unchecked) ArrayIndexOutOfBoundsException

// (checked) FileNotFoundException

47
Q

Select the code so that it compiles and handles the exception:

______{

FileInputStream file = new FileInputStream(“file.txt”);

} _____ (_____ _____) {

log.error(e);

}

A

try

catch Exception e

48
Q

True or False:

Bubble Sort is a super-efficient sorting algorithm

A

False!!! It is not very efficient.

49
Q

In what order does Bubble Sort sort numbers?

A

From least to greatest

50
Q

Describe two mechanisms by which Bubble Sort sorts data:

A
  1. Sorts data by comparing adjacent elements
  2. Pushes higher numbers to the end
51
Q

How did Bubble Sort get its name?

A

Because the higher numbers “bubble” up!

52
Q

What is Big O-Notation?

A

An algebraic equation that represents the time complexity.

53
Q

Because each number (N) is compared to every other number once, what is the time complexity of Bubble Sort?

A

Because O(N * N) == O(N^2)

54
Q

Between runtime, unchecked, and checked exceptions, which one must be handled for the code to compile?

A

Checked Exceptions

55
Q

Convert to a Boolean expression:

if it’s raining outside or it’s dark

A

if(dark || raining)

if(raining | dark)

56
Q

The if statement will not evaluate a Boolean expression to decide if a block of code should execute?

A

False

57
Q

What is the time complexity of the Bubble Sort algorithm?

A

O(n^2)

58
Q

A do-while loop always executes at least once…

A

True

59
Q

Which is the appropriate syntax for a for loop declaration:

A

for(int i = 0; i < 10; i++)

60
Q

At which point does the while loop evaluate the Boolean expression to check if it should execute?

A

At the beginning

61
Q

When does a finally block execute?

A

both when exception is thrown and not thrown.

62
Q

Which of the following is a proper use of the assignment operator?

A

int a = 12

63
Q

The switch statement is combined with what type of statements to branch in multiple directions?

A

case statements

64
Q

The postfix increment operator evaluates the line of code and then performs the increment…

A

True

65
Q

You can have multiple catch blocks for the same try block?

A

True