Q1. Which three are advantages of the Java exception mechanism?
A. Improves the program structure because the error handling code is separated from the
normal program function
B. Provides a set of standard exceptions that covers all the possible errors
C. Improves the program structure because the programmer can choose where to handle
exceptions
D. Improves the program structure because exceptions must be handled in the method in
which they occurred
E. allows the creation of new exceptions that are tailored to the particular program being
Answer: ACE
Q2. Which statement will empty the contents of a StringBuilder variable named sb?
A. sb.deleteAll();
B. sb.delete(0, sb.size());
C. sb.delete(0, sb.length());
D. sb.removeAll();
Answer: C
Q3. Given:
A. Ym
Xm2
B. Ym
Xm1
C. Compilation fails
D. A Class Cast Exception is thrown at runtime
Answer: B
Q4. Given:
Which approach ensures that the class can be compiled and run?
A. Put the throw new Exception() statement in the try block of try – catch
B. Put the doSomethingElse() method in the try block of a try – catch
C. Put the doSomething() method in the try block of a try – catch
D. Put the doSomething() method and the doSomethingElse() method in the try block of a try – catch
Answer: A
Q5. Given the code fragment:
Which code fragment prints blue, cyan, ?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: B
Q6. Which two actions will improve the encapsulation of a class?
A. Changing the access modifier of a field from public to private
B. Removing the public modifier from a class declaration
C. Changing the return type of a method to void
D. Returning a copy of the contents of an array or ArrayList instead of a direct reference
Answer: AD
Q7. 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); }
public boolean 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?
A. year is set to 1982.
B. bob.year is set to 1982
C. A runtime error is generated.
D. A compile time error is generated.
Answer: D
Q8. Which code fragment is illegal?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: D
Q9. Given:
What will be the output?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: D
Q10. Given the code fragment:
A. 20
B. 25
C. 29
D. Compilation fails
E. An Array Index Out Of Bounds Exception is thrown at runtime
Answer: B