Docsity

Java Exceptions and Arrays: Q&A for OCA Java SE 7, Exams of Nursing

Toggle sidebar
bg1
OCA JAVA SE 7: EXCEPTIONS | OCA JAVA SE 7:
ARRAYS AND ARRAY LISTS – NEWEST 2025/2026
True or False: Trying to access an array element at a position that is greater than or equal to the
array's length is considered an exceptional condition. - ANSWER-True.
True or False: Trying to execute a method that throws a checked exception without handling the
thrown exception causes a compilation error. - ANSWER-True, as long as the throws is not part of the
method signature.
True or False: Trying to call a method recursively without defining an exit condition is not considered
an exceptional condition. - ANSWER-False.
Outline the basic Exception heirarchy, including Errors. - ANSWER-An exception is an object of the
class java.lang.Throwable. This is the top-level base class. It is a subclass of java.lang.Object. The
classes java.lang.Exception and java.lang.Error are subclasses of the class java.lang.Throwable. The
class java.lang.RuntimeException is a subclass of the class java.lang.Exception.
True of False: A try block must be followed by one or more catch blocks. - ANSWER-False. A try block
must have either a catch or a finally block.
True or False; Catch blocks must be followed by one or more finally blocks. - ANSWER-False. A catch
block(s) can have zero or one finally block follow them. There can not be more than one finally block.
True or False: The finally block executes regardless of whether the code in the try block throws an
exception. A finally block will execute even if a try or catch block defines a return statement. -
ANSWER-True.
Explain how the inheritance hierarchy of an exception affects how it may be handled when multiple
catch blocks exist. - ANSWER-The order in which the catch blocks are placed matters. If the caught
exceptions have an inheritance relationship, the base class exceptions can't be caught before the
derived class exceptions. An attempt to do this will result in compilation failure.
Will a finally block execute even if the try or catch block defines a return statement? - ANSWER-Yes.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Exceptions and Arrays: Q&A for OCA Java SE 7 and more Exams Nursing in PDF only on Docsity!

OCA JAVA SE 7: EXCEPTIONS | OCA JAVA SE 7:

ARRAYS AND ARRAY LISTS – NEWEST 2025/

True or False: Trying to access an array element at a position that is greater than or equal to the array's length is considered an exceptional condition. - ANSWER-True. True or False: Trying to execute a method that throws a checked exception without handling the thrown exception causes a compilation error. - ANSWER-True, as long as the throws is not part of the method signature. True or False: Trying to call a method recursively without defining an exit condition is not considered an exceptional condition. - ANSWER-False. Outline the basic Exception heirarchy, including Errors. - ANSWER-An exception is an object of the class java.lang.Throwable. This is the top-level base class. It is a subclass of java.lang.Object. The classes java.lang.Exception and java.lang.Error are subclasses of the class java.lang.Throwable. The class java.lang.RuntimeException is a subclass of the class java.lang.Exception. True of False: A try block must be followed by one or more catch blocks. - ANSWER-False. A try block must have either a catch or a finally block. True or False; Catch blocks must be followed by one or more finally blocks. - ANSWER-False. A catch block(s) can have zero or one finally block follow them. There can not be more than one finally block. True or False: The finally block executes regardless of whether the code in the try block throws an exception. A finally block will execute even if a try or catch block defines a return statement. - ANSWER-True. Explain how the inheritance hierarchy of an exception affects how it may be handled when multiple catch blocks exist. - ANSWER-The order in which the catch blocks are placed matters. If the caught exceptions have an inheritance relationship, the base class exceptions can't be caught before the derived class exceptions. An attempt to do this will result in compilation failure. Will a finally block execute even if the try or catch block defines a return statement? - ANSWER-Yes.

What happens if both a catch and a finally block define return statements? - ANSWER-If both catch and finally blocks define return statements, the calling method will receive the value from the finally block. What happens if a finally block modifies the value returned from a catch block? - ANSWER-If a catch block returns a primitive data type, a finally block can't modify the value being returned by it. If a catch block returns an object, a finally block can modify the value being returned by it. True or False: The finally block can appear before a catch block. - ANSWER-False. What are the three Exception categories? - ANSWER-Checked exceptions, runtime (or unchecked exceptions), and errors. These three categories share IS-A relation-ships (inheritance). How are runtime exception classes defined? - ANSWER-Subclasses of the class java.lang.RuntimeException are categorized as runtime exceptions. How are error classes defined? - ANSWER-Subclasses of the class java.lang.Error are categorized as errors. How are checked exceptions classes defined? - ANSWER-Subclasses of the class java.lang.Exception are categorized as checked exceptions if they are not subclasses of class java.lang.Runtime. True or False: FileNotFoundException is a checked exception. - ANSWER-True. This exception is thrown if the file that the code is trying to access can't be found. Give some examples of common RuntimeExceptions. - ANSWER-* ArrayIndexOutOfBoundsException is a runtime exception that's thrown when a piece of code tries to access an array position out of its bounds—when an array is accessed either at a position less than 0 or at a position greater than or equal to its length.

  • IndexOutOfBoundsException is a runtime exception that's thrown when a piece of code tries to access a list position that's out of its bounds—when the position is either less than 0 or greater than or equal to the list's size.

True or False: IOException is a runtime exception. - ANSWER-False. IOException extends Exception, it is a checked exception. True of False: FileNotFoundException directly extends IOException. - ANSWER-True, making it also a checked exception. True or False. A method must delcare throws on any thrown checked exceptions or their subclasses that are handled. - ANSWER-False. It must decalre throws on the undhandled checked Exceptions or their superclasses. True or False. BadStringOperationException is a runtime exception. - ANSWER-False. It directly extends Exception, so it is a checked exception. Name two sublclasses of IndexOutOfBoundsException. - ANSWER-ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException. True or False: Division by zero will result in a runtime DivisionByZeroException being thrown. - ANSWER-False. There is no DivisionByZeroException. The runtinme exception ArithmeticException is thrown for the divide by zero errors. True or False: Checked exceptions extend Throwable. - ANSWER-False. Checked exceptions extend Exception but are not subclasses of RuntimeException. True or False: An unhandled runtime exception will propagate through the stack and eventual result in an application exit. - ANSWER-True. True or False: classes that extend Error can be caught and handled. - ANSWER-True, but not recommended because typically these are problems with the JVM the programmer can do nothing to fix. If the exception hierarchy is A<-B<-C what should be the order of the catch blocks for each exception? - ANSWER-C, B, A. From bottom to top of the hierarchy. If the base exception is listed first, it will cause a compiler error. What exception will be thrown with the following code block:

String[][] arr = {{"foo", "bar"},null,{"help"}}; System.out.println(arr[2][1]); - ANSWER-ArrayIndexOutOfBoundsException. NullPointerException will not be thrown because the array exsits, the length is 0, so the index is out of bounds. Which of these exceptions are defined by the JRE: StackOverflowError OutOfMemoryError ArrayIndexOutOfBoundError ClassLoadingException CompilationError OutOfStorageError - ANSWER-StackOverflowError and OutOfMemoryError. If a super class method declares a checked throws and a subclass overrides the method with no throws, what does the compiler require regarding try/catch/throws? SuperClass superClass = new SubClass(); superClass.overriddenMethod(); - ANSWER-The compiler will assume the method throws the exception. Will reassigning a reference variable in a finally clause change the result returned in the try block? - ANSWER-No. The finally block variable works like an instance method parameter in this case. Can exceptions be thrown in catch or finally blocks? - ANSWER-Yes, but if checked exceptions are thrown, then they must be either handled in try-catch or declared with throws in method signature. True or False: A two-dimensional array refers to a collection of objects, in which each of the objects is a one-dimensional array. - ANSWER-True. True or False: A three-dimensional array refers to a collection of two-dimensional arrays, and so on. - ANSWER-True. What are the three steps in the creation of an array? - ANSWER-declaration of an array, allocation of an array, and initialization of array elements.

In what Java SE package is the ArrayList class found? - ANSWER-Package java.util contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array). True or False: you may specify an initial size to create an ArrayList. - ANSWER-True. See the constructor "ArrayList(int initialCapacity)" True or False: ArrayList implements the interface List and allows null values to be added to it. - ANSWER-True. True or False: An iterator (Iterator or ListIterator) lets you remove elements as you iterate through an ArrayList. It's not possible to remove elements from an ArrayList while iterating through it using a for loop. - ANSWER-True. True or False: remove(int) removes the element at the specified position in an ArrayList. remove(Object o) removes the first occurrence of the specified element from the list, if it's present. - ANSWER-True. True or False: The ArrayList has a size() method that returns the number of elements in the list. - ANSWER-True. What is the behavior of the ArrayList method clone()? - ANSWER-It returns a shallow copy of the ArrayList instance. "Shallow copy" means that the method creates a new instance of the ArrayList to be cloned, but the ArrayList elements aren't copied. True or False: ArrayList objects provide better performance than arrays. - ANSWER-False. The collections ArrayList implementation reduces development time, but does not guarantee better performance over arrays. True or False: ArrayList objects require less low-level implementation than arrays. - ANSWER-True. The java.util package provides an ArrayList implementation that automatically resizes the list to a larger capacity, if needed when adding an element. With arrays, one would have to develop code to create a new array with the larger size, populate with the existing elements, then add the new element at the next index position. is int [] [] [] x = new int [3] [] []; a valid array declaration? - ANSWER-public class Example{

public static void main(String [] args) { int [] [] [] x = new int [3] [] []; int i, j; x[0] = new int[4][]; x[1] = new int[2][]; x[2] = new int[5][]; for (i = 0; i < x.length; i++) { for (j = 0; j < x[i].length; j++) { x[i][j] = new int [i + j + 1]; System.out.println("size = " + x[i][j].length); } } } } In the above program, how many lines of output will be produced? Compilation fails 7 9 11 Explanation: Answer option D is correct. The loops use the array sizes (length). It produces 11 lines of output as given below.

Using the equality operator: This only works properly if the two reference variables reference the same object. Using the array's equals method: This only works properly if the two reference variables reference the same object. Using the Array's class equals method: This will work for one-dimensional arrays. Using the Array's class deepEquals method: This performs a deeper comparison using the object's equals method. What are some different ways to code the copying of an array? - ANSWER-Simple element-by- element copy: Tedious but can implement either a shallow or deep copy Using the System.arraycopy method: Performs a shallow copy Using the Arrays.copyOf method: Performs a deep copy of the entire array Using the Arrays.copyOfRange method: Performs a deep copy of part of an array Using the clone method: Performs a shallow copy What package does the Arrays class belong to? - ANSWER-java.util What will be the output of the following ArrayList call? myArrayList.indexOf(NOT_PRESENT_ELEMENT); - ANSWER-- 1 True of False: the output of the following ArrayList call myArrayList.toString() will be an object reference like "[@13456" - ANSWER-False. It will be a human readable, square bracket delimited list of the toString() calls of the individual elements, like "[1,2,3]"

True of False: the ArrayList class has a method length() - ANSWER-False. It has a size() method that returns the number of elements in the list. Does this code compile and run: ArrayList list = new ArrayList<>(); list.add(1, "foo"); list.add(2, "bar"); list.remove(1); - ANSWER-It compiles, but throws IndexOutOfBoundsException since adding at position 1 will fail, only index 0 is available. There is a method remove(int) to remove the element at index int. Will the following code compile: String ejg6[][] = new String[][]{"A", "B"}; String ejg7[][] = new String[]{{"A"}, {"B"}}; String ejg2[][] = new String[][] { {}, {} }; - ANSWER-No, not the first and second lines. The first line needs to have individual elements enclosed with curly-braces. The second line is missing a pair of "[]" in the RHS. True or False: an ArrayList will accept primitives as elements. - ANSWER-False. Only Objects. What will be the output for each line: String multiStrArr[][] = new String[][] { { "A", "B" }, null, { "Jan", "Feb", null } }; System.out.println(multiStrArr[0][0]); // line System.out.println(multiStrArr[1][2]); // line System.out.println(multiStrArr[1]); // line System.out.println(multiStrArr[1].toString()); // line System.out.println(multiStrArr[2].toString()); // line5 - ANSWER-line 1: A line 2: NPE, there are no elements in element 1 of outer array, its null. line 3: null line 4: NPE, calling toString on null line 5: Reference location