Inheritence(Computer Science > Java Program ) Questions and Answers

Question 1. Which is true?
  1.    "X extends Y" is correct if and only if X is a class and Y is an interface
  2.    "X extends Y" is correct if and only if X is an interface and Y is a class
  3.    "X extends Y" is correct if X and Y are either both classes or both interfaces
  4.    "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces
Explanation:-
Answer: Option C. -> "X extends Y" is correct if X and Y are either both classes or both interfaces

Question 2. Which of the following is true?
1. A class can extend more than one class.
2. A class can extend only one class but many interfaces.
3. An interface can extend many interfaces.
4. An interface can implement many interfaces.
5. A class can extend one class and implement many interfaces.
  1.    1 and 2
  2.    2 and 4
  3.    3 and 5
  4.    3 and 4
  5.    2 and 5
Explanation:-
Answer: Option C. -> 3 and 5

Question 3. What is the result of compiling and running the following code?
class Base{
public Base(){
System.out.print("Base");
}
}
public class Derived extends Base{
public Derived(){
this("Examveda");
System.out.print("Derived");
}
public Derived(String s){
System.out.print(s);
}
public static void main(String[] args){
new Derived();
}
}
  1.    ExamvedaDerived
  2.    ExamvedaBaseDerived
  3.    BaseExamvedaDerived
  4.    ExamvedaDerivedBase
  5.    Compilation Error
Explanation:-
Answer: Option C. -> BaseExamvedaDerived

Question 4. The concept of multiple inheritance is implemented in Java by
I.   Extending two or more classes.
II.  Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
  1.    Only (II)
  2.    (I) and (II)
  3.    (II) and (III)
  4.    Only (I)
  5.    Only (III)
Explanation:-
Answer: Option C. -> (II) and (III)

Question 5. What is the output of the following program code?
abstract class C1{
public C1(){
System.out.print(1);
}
}
class C2 extends C1{
public C2(){
System.out.print(2);
}
}
class C3 extends C2{
public C3(){
System.out.println(3);
}
}
public class Test{
public static void main(String[] a){
new C3();
}
}
  1.    12
  2.    23
  3.    123
  4.    321
Explanation:-
Answer: Option C. -> 123

Question 6. What will be the output?
interface A{
public void method1();
}
class One implements A{
public void method1(){
System.out.println("Class One method1");
}
}
class Two extends One{
public void method1(){
System.out.println("Class Two method1");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method1();
}
}
  1.    Compilation Error
  2.    Class One method1
  3.    Class Two method1
  4.    Throws a NoSuchMethodException at runtime.
  5.    None of these
Explanation:-
Answer: Option C. -> Class Two method1

Question 7. Determine output:
class A{
public void method1(){
System.out.print("Class A method1");
}
}
class B extends A{
public void method2(){
System.out.print("Class B method2");
}
}
class C extends B{
public void method2(){
System.out.print("Class C method2");
}
public void method3(){
System.out.print("Class C method3");
}
}
public class Test{
public static void main(String args[]){
A a = new A();
C c = new C();
c.method2();
a = c;
a.method3();
}
}
  1.    Class B method2 Class C method3
  2.    Class C method2 Class C method3
  3.    Compilation Error
  4.    Runtime exception
  5.    None of these
Explanation:-
Answer: Option C. -> Compilation Error

Question 8. What is the result of compiling and running this program?
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay");
}
}
class Horse extends Cattle{
void eat(Horse h){
System.out.println("Horse eats hay");
}
}
public class Test{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();
c.eat(h);
}
}
  1.    prints "Mammal eats food"
  2.    prints "Cattle eats hay"
  3.    prints "Horse eats hay"
  4.    Class cast Exception at runtime.
  5.    None of these
Explanation:-
Answer: Option A. -> prints "Mammal eats food"

Question 9. What will be printed after executing following program code?
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test{
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
  1.    30
  2.    10
  3.    40
  4.    20
  5.    None of these
Explanation:-
Answer: Option C. -> 40

Question 10. What will be the output?
class Parent{
public void method(){
System.out.println("Hi i am parent");
}
}
public class Child extends Parent{
protected void method(){
System.out.println("Hi i am Child");
}
public static void main(String args[]){
Child child = new Child();
child.method();
}
}
  1.    Compiles successfully and print
  2.    Compiles successfully and print
  3.    Compile time error
  4.    Run Time error
  5.    None of This
Explanation:-
Answer: Option C. -> Compile time error