Você está na página 1de 4

Al Akhawayn University School

of Sciences & Engineering


CSC2303 Spring13
Lab5.2: Inheritance

Answer the following questions

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

5. Which of the following statements are true?


a. Methods cannot be overloaded to be more private
b. An overloaded method cannot throw exceptions not checked in the base 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) { }
}

public class Scope extends Base{


public static void main(String argv[]){
}
//Method Here
}
a. void amethod(int i) throws Exception {}
b. void amethod(long i)throws Exception {}
c. void amethod(long i){}
d. public void amethod(int i) throws Exception {}

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
}
}

public class Sup extends Base{


public static void main(String argv[]){
Sup s= new Sup();
//One
}
Sup() {
//Two
}

public void derived(){


//Three
}
}
1) On the line After //One put Base(10);
2) On the line After //One put super(10);
3) On the line After //Two put super(10);
4) On the line After //Three put super(10);

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);
}

public void amethod(int iOver){


System.out.println("Over.amethod");
}
}
1) Compile time error
2) Runtime error
3) Output of "Base.amethod"
4) Output of "Over.amethod"

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 {

public static void main(String argv[]) {

int a = laBelleVie();

if (a >= 0) {

System.out.println("oh! la vie est dlicieuse ");

} else {

System.out.println("oh ! je meurs ");

public static int laBelleVie() {

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();

if (a >= 0) {

System.out.println("tout va bien, marquise");

return 0;

} else {

return -1;//error code

Você também pode gostar