Overview Lesson #2

Below is a list of all the lessons this overview lesson will cover.

1. The Scanner Class
2. Arrays
3. Foreach Loops
4. The Math Class
5. Errors/Exceptions
6. Methods

Lesson Quiz

Each question in this quiz corresponds to that lesson, so if you miss a specific problem, go back to that problem’s lesson to review.

1. What is the correct syntax of creating a variable s of type Scanner, reading input from the console?

a. `new Scanner s = Scanner(new File(“user.txt”));`
b. `Scanner s = new Scanner(System.in);`
c. `new Scanner(new File(“user.txt”)) = Scanner s;`
d. `Scanner s = new Scanner(new File(“user.txt”));`

2. Which signature line is for a method with three int parameters( a, b, c ), returns a String, has a name “method1”, and should not be accessed outside of the class.

a. `public (int a, int b, int c) method1 String {...}`
b. `private method1 String (int a, int b, int c) {...}`
c. `private String (int a, int b, int c) method1 {...}`
d. `private String method1(int a, int b, int c) {...}`

3. Which is the proper way of declaring a three dimensional int array of dimensions 5x5x5?

a. `int[[[]]] arr = new int[ 5 [ 5 [5] ] ];`
b. `int[][][] arr = new int[5][5][5];`
c. `int{} arr = new int{ { {5} } };`
d. `int[] arr = new int[5]int[5]int[5];`

4. What is the syntax of a foreach loop which traverses a one dimensional String array, “ar”, of length 7?

a. `for (ar : String s) {...}`
b. `for (String[7] ar : String s) {...}`
c. `for (String s : ar) {...}`
d. `for (String s : String[7] ar) {...}`

5. What is the result of the below statement?

``````    Math.abs(-1 * Math.sqrt(3 * Math.pow(3, 3)))
``````
a. 9
b. 9.0
c. 3
d. -81.0

6. What will occur from the following code?

``````    int a = 1;
while (a != 0) {
a *= 4;
}
``````
a. An infinite loop will occur.
b. ArrayIndexOutOfBoundsException will be thrown.
c. ClassCastException will be thrown.
d. NullPointerException will be thrown.

7. Will an exception be thrown from the following method declarations?

``````    public void method1() {...}
public void method1(int a) {...}
``````
a. Yes
b. No

Written by James Richardson

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.