The catch clause argument is always of type ________.
Correct Answer:
C
Because all exceptions in Java are the sub-class ofjava.lang.Exceptionclass, you can have a singlecatch blockthat catches an exception of typeExceptiononly. Hence the compiler is fooled into thinking that this block canhandle any exception.
See the following example:
try
{
// ...
}
catch(Exception ex)
{
// Exception handling code for ANY exception
}
You can also use the java.lang.Throwable class here, since Throwable is the parent class for the application-specificException classes. However, this is discouraged in Java programming circles. This is because Throwable happens to also be the parent class for the non-application specific Error classes which are not meant to be handled explicitly as they are catered forby the JVM itself.
Note: The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.
A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error.
View the exhibit:
public class Student {
public String name = "";
public int age = 0;
public String major = "Undeclared";
public boolean fulltime = true;
public void display() {
System.out.println("Name: " + name + " Major: " + major); }
publicboolean isFullTime() {
return fulltime;
}
}
Given:
Public class TestStudent {
public static void main(String[] args) {
Student bob = new Student ();
bob.name = "Bob";
bob.age = 18;
bob.year = 1982;
}
}
What is the result?
Correct Answer:
D
Given the code fragment:
interface SampleClosable {
public void close () throws java.io.IOException;
}
Which three implementations are valid?
Correct Answer:
ACE
A: Throwing the same exception is fine.
C: Using a subclass of java.io.IOException (here java.io.FileNotFoundException) is fine E: Not using a throw clause is fine.
Given the code fragment:
class Student {
int rollnumber; String name;
List cources = new ArrayList();
// insert code here public String toString() {
return rollnumber + " : " + name + " : " + cources;
}
}
And,
public class Test {
public static void main(String[] args) { List cs = newArrayList(); cs.add("Java");
cs.add("C");
Student s = new Student(123,"Fred", cs); System.out.println(s);
}
}
Which code fragment, when inserted at line // insert code here, enables class Test to print 123 : Fred : [Java, C]?
Correct Answer:
C
Incorrect:
Not A: Student has private access line: Student s = new Student(123,"Fred", cs);
Not D: Cannot be applied to given types.Line: Student s = new Student(123,"Fred", cs);
Given the code fragment:<>
Correct Answer:
A