Lab5.2 Inheritance Exceptions-2
Lab5.2 Inheritance Exceptions-2
1. Every object in Java knows its own class and can access this information through method
a. getClass
b. getInformation
c. objectClass
d. objectName
2. What will happen if you attempt to compile and run the following code?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
a. Compile and run without error
b. Compile time Exception
c. Runtime Exception
3. What will happen when you attempt to compile and run the following code?
class Craven{
Craven(){
System.out.println("Craven");
}
}
public class CilDig extends Craven{
public static void main(String argv[]){
CilDig _c = new CilDig();
}
public CilDig(){
System.out.println("CilDig");
}
}
a. Compile time error, an object reference cannot start with underscore
b. Compilation and output of "CilDig"
c. Compilation and output of "Craven" followed by "CilDig"
d. Compile time error, method CilDig has no return type
1
4. Which of the following statements are true?
a. Methods cannot be overriden to be more private
b. Private methods cannot be overriden
c. An overridden method cannot throw exceptions not checked in the super class
6. What happens when you attempt to compile and run these two classes in the same
directory?
//Class P1.java
package MyPackage;
public class P1{
private void afancymethod(){
System.out.println("What a fancy method");
}
}
//Class P2.java
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
7. Which of the following methods can be legally inserted in place of the comment
//Method Here ?
class Base{
public void amethod(int i) { }
}
8. Given the following code how could you invoke the Base constructor that will print out
the string "base constructor";
class Base{
Base(int i){
System.out.println("base constructor");
}
Base(){
2
}
}
9. What will happen when you attempt to compile and run the following code :
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Base b = new Base();
b.amethod(2);
}
10. The laBelleVie method below returns a negative number to signal an error. Define an
exception ExceptionOhJeMeurs and re-write the code below to use exceptions instead of
error codes.
3
class Marquise {
int a = laBelleVie();
if (a >= 0) {
} else {
int a = sc.nextInt();
if (a >= 0) {
return 0;
} else {