Overloading, Overriding written test Samples

Question 1)

Given the following class definition, which of the following methods could be legally placed after the comment with the commented word "//Here"?
public class Rid{
public void amethod(int i, String s){}
//Here
}
1) public void amethod(String s, int i){}
2) public int amethod(int i, String s){}
3) public void amethod(int i, String mystring){}
4) public void Amethod(int i, String s) {}

Question 2)

Given the following class definition which of the following can be legally placed after the comment line
//Here ?
class Base{
    public Base(int i){}

}



public class MyOver extends Base{
public static void main(String arg[]){
                MyOver m = new MyOver(10);
                }

        MyOver(int i){
                super(i);
        }

        MyOver(String s, int i){
                this(i);
                 //Here
        }
}

1) MyOver m = new MyOver();
2) super();
3) this("Hello",10);
4) Base b = new Base(10);

Question 3)

Given the following class definition
class Mammal{
        Mammal(){
                System.out.println("Mammal");
        }
}

class Dog extends Mammal{
        Dog(){
                System.out.println("Dog");
        }
}



public class Collie extends Dog {
public static void main(String argv[]){
        Collie c = new Collie();
}

        Collie(){
             this("Good Dog");
             System.out.println("Collie");
        }

        Collie(String s){
        System.out.println(s);
        }
}
What will be output?
1) Compile time error
2) Mammal, Dog, Good Dog, Collie
3) Good Dog, Collie, Dog, Mammal
4) Good Dog, Collie

Question 4)

Which of the following statements are true?
1) Constructors are not inherited
2) Constructors can be overriden
3) A parental constructor can be invoked using this
4) Any method may contain a call to this or super

Question 5)

What will happen when you attempt to compile and run the following code?
class Base{
        public void amethod(int i, String s){
        System.out.println("Base amethod");
        }
        Base(){
        System.out.println("Base Constructor");
        }
}

public class Child extends Base{
int i;
String Parm="Hello";
public static void main(String argv[]){
        Child c = new Child();
        c.amethod();
}

void amethod(int i, String Parm){
        super.amethod(i,Parm);
        }
    public void amethod(){}
}
1) Compile time error
2) Error caused by illegal syntax super.amethod(i,Parm)
3) Output of "Base Constructor"
4) Error caused by incorrect parameter names in call to super.amethod

Question 6)

What will be output if you attempt to compile and run this code?
class Mammal{

Mammal(){
        System.out.println("Four");
        }

public void ears(){
        System.out.println("Two");
        }
}


class Dog extends Mammal{
        Dog(){
        super.ears();
        System.out.println("Three");
        }
}



public class Scottie extends Dog{
public static void main(String argv[]){
        System.out.println("One");
        Scottie h = new Scottie();
        }
}
1) One, Three, Two, Four
2) One, Four, Three, Two
3) One, Four, Two, Three
4) Compile time error

Answers

Answer 1)

1) public void amethod(String s, int i){}
4) public void Amethod(int i, String s) {}

The upper case A on Amethod means that this is a different method.


Answer 2)

4) Base b = new Base(10);
Any call to this or super must be the first line in a constructor. As the method already has a call to this, no more can be inserted.


Answer 3)

2) Mammal, Dog, Good Dog, Collie


Answer 4)


1) Constructors are not inherited

Parental constructors are invoked using super, not this.


Answer 5)

1) Compile time error

This will cause an error saying something like "you cannot override methods to be more private". The base version of amethodwas specifically marked as public whereas the child had no specifier. OK so this was not a test of your knowledge of constructors overloading but they don't tell you the topic in the exam either. If it were not for the omission of the keywordpublic this code would output "Base constructor", option 3.


Answer 6)

3) One, Four, Two, Three

The classes are created from the root of the hierarchy downwards. Thus One is output first as it comes before the instantiation of the Scottie h. Then the JVM moves to the base of the hierarchy and runs the constructor for the grandparent Mammal. This outputs "Four". Then the constructor for Dog runs. The constructor for Dog calls the ears method in Mammal and thus "Two" is output. Finally the constructor for Dog completes and outputs "Three".

Comments

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

Core Java -----Question and Answers