ITSC 1213 TEST 2 QUESTIONS AND CORRECT
ANSWERS
An exception can be handled with: - Answers - try/catch or try/catch/finally
Which of these regarding catch blocks are true? - Answers - You can have multiple
exceptions in several catch blocks
True or False: The finally clause is not required in a try/catch block - Answers - True
Catch blocks should catch exceptions in what order? - Answers - From most specific
exception to least specific
Why should you use exception handling? (Select all that apply) - Answers - To protect
your program from input errors, from mistakes you might make (like passing around
objects that haven't been initialized), badly formatted or missing files, and system level
issues that you can't predict
Your method has a try/catch block that handles 8 exceptions. An exception is caught by
the fifth catch block. When will the code in the remaining catch blocks be executed? -
Answers - They will not be executed
The keyword static when applied to methods and fields means what? - Answers - The
methods and fields may be accessed even when no class object has been created.
How would you access the printInfo method, given this documentation:
public class MediaClass - methods and fields for handling media files.
static void printInfo() - prints out information about the class and it's methods. - Answers
- MediaClass.printInfo();
Assume you have the following code.
public class MyClass {
private int count;
...
}
What is printed by the following code? Assuming this line of code is in a method within
the class.
, System.out.println("count = " + count); - Answers - count = 0
Assume you have the following code:
public class Hogwarts {
static final int NUM_STAFF = 7;
....
}
What is the output of the following code, assuming it is in another class in the same
package?
Hogwarts.NUM_STAFF--;
System.out.println("The number of Hogwarts staff is = " + Hogwarts.NUM_STAFF); -
Answers - Nothing, this code will not compile.
If you have a private method called getName() in MyNameClass, how would you call
this method on an object of MyNameClass called myName? - Answers - myName
cannot call the getName() method. Calling this method will result in a compiler error
Which of the following are NOT true regarding ArrayLists? - Answers - A single
ArrayList can hold objects of different classes
How would you allocate a new ArrayList to contain Double objects? (Multiple answers
allowed) - Answers - ArrayList<Double> myList = new ArrayList<Double>();
How should you allocate an ArrayList of 50 Cookie objects? (Multiple answers allowed)
- Answers - ArrayList<Cookie> myCookieList = new ArrayList<>(50);
or
ArrayList<Cookie> myCookieList = new ArrayList<Cookie>(50);
What is printed as a result of the following code?
ArrayList<String> avengers = new ArrayList<String>();
avengers.add("Ironman");
avengers.add("Hulk");
avengers.add(1, "Thor");
System.out.println(avengers.get(2)); - Answers - Hulk
Which of the following is NOT true regarding methods from the ArrayList package? -
Answers - "add(int i, Object x)" adds x at i and deletes the object previously in its place
ANSWERS
An exception can be handled with: - Answers - try/catch or try/catch/finally
Which of these regarding catch blocks are true? - Answers - You can have multiple
exceptions in several catch blocks
True or False: The finally clause is not required in a try/catch block - Answers - True
Catch blocks should catch exceptions in what order? - Answers - From most specific
exception to least specific
Why should you use exception handling? (Select all that apply) - Answers - To protect
your program from input errors, from mistakes you might make (like passing around
objects that haven't been initialized), badly formatted or missing files, and system level
issues that you can't predict
Your method has a try/catch block that handles 8 exceptions. An exception is caught by
the fifth catch block. When will the code in the remaining catch blocks be executed? -
Answers - They will not be executed
The keyword static when applied to methods and fields means what? - Answers - The
methods and fields may be accessed even when no class object has been created.
How would you access the printInfo method, given this documentation:
public class MediaClass - methods and fields for handling media files.
static void printInfo() - prints out information about the class and it's methods. - Answers
- MediaClass.printInfo();
Assume you have the following code.
public class MyClass {
private int count;
...
}
What is printed by the following code? Assuming this line of code is in a method within
the class.
, System.out.println("count = " + count); - Answers - count = 0
Assume you have the following code:
public class Hogwarts {
static final int NUM_STAFF = 7;
....
}
What is the output of the following code, assuming it is in another class in the same
package?
Hogwarts.NUM_STAFF--;
System.out.println("The number of Hogwarts staff is = " + Hogwarts.NUM_STAFF); -
Answers - Nothing, this code will not compile.
If you have a private method called getName() in MyNameClass, how would you call
this method on an object of MyNameClass called myName? - Answers - myName
cannot call the getName() method. Calling this method will result in a compiler error
Which of the following are NOT true regarding ArrayLists? - Answers - A single
ArrayList can hold objects of different classes
How would you allocate a new ArrayList to contain Double objects? (Multiple answers
allowed) - Answers - ArrayList<Double> myList = new ArrayList<Double>();
How should you allocate an ArrayList of 50 Cookie objects? (Multiple answers allowed)
- Answers - ArrayList<Cookie> myCookieList = new ArrayList<>(50);
or
ArrayList<Cookie> myCookieList = new ArrayList<Cookie>(50);
What is printed as a result of the following code?
ArrayList<String> avengers = new ArrayList<String>();
avengers.add("Ironman");
avengers.add("Hulk");
avengers.add(1, "Thor");
System.out.println(avengers.get(2)); - Answers - Hulk
Which of the following is NOT true regarding methods from the ArrayList package? -
Answers - "add(int i, Object x)" adds x at i and deletes the object previously in its place