What are the three different types of loops?
For loops, While loops, and Do loops.
True or false: A method with return value void must print a result?
False!
What are the three types of errors in Java?
Syntax Errors, Logic Errors, Runtime Errors
What can you use to read data from a text file? How do you read one line from a file named "hello.txt"
Scanner.
File file = new File("hello.txt");
Scanner read = new Scanner(file);
String line = read.nextLine();
System.out.println(line);
What is the value of c after the following snippet is executed?
boolean a = true;
boolean b = false;
boolean c = !(a || b) && a
false
What does the following code snippet print?
2 4 8 16 32
Write a method header for a method that prints the balance of an account with a given initial balance and an annual interest rate over a given number of years
public static void printBalance(double initialBalance, double annualInterest, int years)
When is the finally block executed in a try-catch-finally statement?
The finally block is executed after the try (and catch) block is done executing no matter if an exception is thrown in either block.
What can you use to write data to a file (binary or text)?
PrintWriter or FileOutputStream
What is the value of the following:
6 mod 4
2
Rewrite the following for loop into a while loop:
int s = 0;
for (int i = 1; i <= 10; i++)
{
s = s + i;
}
int s = 0;
int i = 1;
while(i <= 10){
s = s + i;
i++;
}
What is the scope of s initialized on line 12?
Lines 12-17.
Can only be accessed within the mystery method.
How do you manually throw an exception?
throw new Exception();
What might happen if you don't close your files before the program exits?
Any changes made to the file may not be saved or the file may become corrupted.
What does the following code snippet print?
int a = 6;
int b = 3;
if (a == b){ System.out.println("Match"); }
else{ System.out.println("Failure"); }
Failure
Write a loop that computes the sum of all squares between 1 and 100 (inclusive)
int sum = 0;
for(int i = 1; i <= 100; i++){
sum =+ i * i;
}
System.out.println(sum);
What order are the lines of the following class executed starting with the first line of the main method (5)?
5, 18, 19, 6, 18, 19, 7, 8
If a method throws an unhandled exception, what do you need to add to the method header?
throws <Exception>
When is a FileNotFoundException thrown and how can you handle it?
FileNotFoundException occurs when you are trying to access a file that does not exist.
You can add "throws FileNotFoundException" to the method header or use a try-catch block as follows:
try{
PrintWriter write = new PrintWriter("file.txt");
write.println("Hello");
write.close();
}
catch(FileNotFoundException e){
System.out.println("File does not exist");
}
Use decision statements (if,elseif,else) to find the minimum value between 3 integers: a, b, and c.
if(b < smallest) { smallest = b; }
if (c < smallest) { smallest = c; }
System.out.println("Minimum: " + smallest);
Write a while loop that prints all positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90. Assume n is already initialized.
int i = 1;
while (i < n){
if ((i % 10) == 0){
System.out.print(i + " ");
}
}
Implement a method that computes the smallest of three floating-point numbers
public static double min(double a, double b, double c){
double minAB = Math.min(a,b);
double minBC = Math.min(b,c);
double min = Math.min(minAB, minBC);
return min;
}
What does the following code snippet print?
try{
int a = 1;
int b = 0;
int c = a/b;
System.out.println("Success");
catch(Exception e){
System.out.println("Failure");
}
finally{
System.out.println("Execution Complete");
}
Failure
Execution Complete
Write a code snippet to open a file called "hello.txt" for writing, write "Hello!" to the file, close the file, reopen it for reading, and read the first line of the file, then close it.
PrintWriter write = new PrintWriter("hello.txt");
write.println("Hello!");
write.close();
Scanner read = new Scanner(new File("hello.txt"));
System.out.println(read.nextLine());
read.close();
Simplify the following expression:
if (b < a){ result = false; }
else { result = true; }
result = !( b < a);