Practice high-yield Programming & Software Development MCQs on Inheritence — Part 1 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass inherit constructors from its superclass?
Correct Answer: Option D — None of the above
Detailed Academic Explanation:
Option A: Only if the subclass is marked as "final" This option is incorrect. Marking a subclass as "final" means that it cannot be subclassed further, and it has no impact on the inheritance of constructors. Option B: Yes, a subclass inherits constructors from its superclass This option is incorrect. In Java, a subclass does not inherit constructors from its superclass. However, a subclass can call the constructors of its superclass using the super keyword. Option C: Only if the superclass is marked as "static" This option is incorrect. The "static" keyword cannot be applied to classes or constructors in a way that would affect inheritance. Option D: None of the above This option is correct. In Java, constructors are not inherited by subclasses. Subclasses must explicitly define their own constructors, although they can call their superclass's constructors using the super keyword. Conclusion: In Java, a subclass does not inherit constructors from its superclass, making Option D: None of the above the correct answer.
💡Key Conceptual Anchor:Inheritence
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass access protected members (fields and methods) of its superclass?
Correct Answer: Option D — Yes, regardless of the package
Detailed Academic Explanation:
In Java, protected members (fields and methods) of a superclass are accessible to its subclasses. Unlike private members, which are inaccessible outside the class, protected members can be accessed: 1. Within the same package: Any class within the same package can access protected members. 2. In subclasses (even if in a different package): A subclass can access protected members of its superclass, even if they are in different packages, provided they are accessed through inheritance (i.e., using super or via an instance of the subclass). This ensures that subclasses get access to important superclass members while still maintaining some level of encapsulation.
💡Key Conceptual Anchor:Inheritence
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the following code snippet? class Parent { private int x = 10; } class Child extends Parent { int x = 20; void display() { System.out.println(super.x + " " + x); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
Correct Answer: Option B — 10 20
Detailed Academic Explanation:
The given code snippet involves inheritance in Java. It defines two classes: Parent and Child . The Child class extends the Parent class, which means it inherits its fields and methods. Inside the Parent class, there is a private integer variable x with a value of 10. In the Child class, there is another integer variable x with a value of 20. Additionally, the Child class has a display() method that prints the values of super.x and x . In Java, when you access a variable with the super keyword, it refers to the variable of the superclass. When you access a variable without super , it refers to the variable of the current class. Now, let's analyze what happens in the display() method: - super.x refers to the x variable in the Parent class, which is 10. - x refers to the x variable in the Child class, which is 20. So, the display() method will print "10 20" because it first accesses the x variable of the superclass ( Parent ) using super.x and then accesses the x variable of the current class ( Child ) directly. Therefore, the correct answer is: Option B: 10 20
💡Key Conceptual Anchor:Inheritence
Question 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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(); } }
Correct Answer: Option C — BaseExamvedaDerived
Detailed Academic Explanation:
1. new Derived(); statement executes and invoke the non-parametrized constructor of derived class i.e. public Derived(); 2. As Derived class is a subclass of class Base so super(); executes and calls the super class constructor and prints "Base" . 3. After that this("Examveda"); executes and call the parametrized constructor public Derived(String s); of Derived class as this always refer to the current object. So, it prints "Examveda" . 4. Lastly the print statement executes and prints "Derived" Hence output is BaseExamvedaDerived .
💡Key Conceptual Anchor:Inheritence
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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(); } }
Correct Answer: Option C — Compilation Error
Detailed Academic Explanation:
It is important to understand that it is the type of reference variable - not the type of the object that it refers to - that which determines what members can be accessed. That is, when a reference to a subclass object is assigned to a super class reference variable, we will have access only to those parts of the object defined by the superclass. In the above program method method3() is defined in the class C which is a subclass of B and so A . Even the reference variable a refers to c , a can't access method3() as this method is unknown to class A .
💡Key Conceptual Anchor:Inheritence
Question 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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()); } }
Correct Answer: Option C — 40
Detailed Academic Explanation:
When object of new derived is called, the flow goes to Derived() first, by default super(); is present in Derived() as the first statement, so the flow now goes to Base. Here value is initialised to 0 and then addValue() is called. The addValue has been overridden in Derived() hence The Base's addValue() will perform value+20(0+20) .After this control flows back to Derived()'s addValue() where again value+20 is done (20+20). Hence Answer is 40
💡Key Conceptual Anchor:Inheritence
Question 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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(); } }
Correct Answer: Option C — Compile time error
Detailed Academic Explanation:
We cannot reduce the visibility of the inherited method from super class. If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs. If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs. If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.
💡Key Conceptual Anchor:Inheritence
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output? class One{ final int a = 15; } class Two extends One{ final int a = 20; } public class Test extends Two{ final int a = 30; public static void main(String args[]){ Test t = new One(); System.out.print(t.a); } }
Correct Answer: Option D — Compiler Error
Detailed Academic Explanation:
We can't store super class object in subclass reference But we can store subclass object in super class reference.
💡Key Conceptual Anchor:Inheritence
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: class A{ public void printName(){ System.out.println("Name-A"); } } class B extends A{ public void printName(){ System.out.println("Name-B"); } } class C extends A{ public void printName(){ System.out.println("Name-C"); } } public class Test{ public static void main (String[] args){ B b = new B(); C c = new C(); b = c; newPrint(b); } public static void newPrint(A a){ a.printName(); } }
Correct Answer: Option C — Compilation fails due to an error on lines 5
Detailed Academic Explanation:
Reference variable can refer to any object of the same type as the declared reference OR can refer to any subtype of the declared type. Reference variable "b" is type of class B and reference variable "c" is a type of class C. So Compilation fails.
💡Key Conceptual Anchor:Inheritence
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is inheritance in Java?
Correct Answer: Option A — The process of acquiring properties and behaviors of one class by another
Detailed Academic Explanation:
The process of acquiring properties and behaviors of one class by another is the correct answer to “What is inheritance in Java”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, which keyword is used to implement inheritance between classes?
Correct Answer: Option D — extends
Detailed Academic Explanation:
extends is the correct answer to “In Java, which keyword is used to implement inheritance between classes”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is a superclass in Java?
Correct Answer: Option C — The class that is inherited from
Detailed Academic Explanation:
The class that is inherited from is the correct answer to “What is a superclass in Java”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the "super" keyword used for in Java?
Correct Answer: Option A — To call a method or constructor of the superclass
Detailed Academic Explanation:
To call a method or constructor of the superclass is the correct answer to “What is the "super" keyword used for in Java”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass access private members (fields and methods) of its superclass?
Correct Answer: Option C — No, private members are not visible to subclasses
Detailed Academic Explanation:
No, private members are not visible to subclasses is the correct answer to “In Java, can a subclass access private members (fields and methods) of its superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 15 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the difference between "extends" and "implements" in Java?
Correct Answer: Option B — "Extends" is used for class inheritance, while "implements" is used for interface implementation
Detailed Academic Explanation:
"Extends" is used for class inheritance, while "implements" is used for interface implementation is the correct answer to “What is the difference between "extends" and "implements" in Java”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the following code snippet? class Parent { protected void display() { System.out.println("Parent"); } } class Child extends Parent { void show() { display(); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.show(); } }
Correct Answer: Option D — Parent
Detailed Academic Explanation:
Parent is the correct answer to “What is the result of the following code snippet? class Parent { protected void display() { System.out.println("Parent"); } } class Child extends Parent { void show() { display(); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.show(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass have more than one superclass?
Correct Answer: Option A — No, a subclass can only have one superclass
Detailed Academic Explanation:
No, a subclass can only have one superclass is the correct answer to “In Java, can a subclass have more than one superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 18 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the "final" keyword in Java inheritance?
Correct Answer: Option D — To indicate that a class cannot be further extended
Detailed Academic Explanation:
To indicate that a class cannot be further extended is the correct answer to “What is the purpose of the "final" keyword in Java inheritance”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass inherit private constructors from its superclass?
Correct Answer: Option C — No, constructors are not inherited, especially private constructors
Detailed Academic Explanation:
No, constructors are not inherited, especially private constructors is the correct answer to “In Java, can a subclass inherit private constructors from its superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the concept of method overriding in Java inheritance?
Correct Answer: Option B — Redefining a superclass method in a subclass with the same method signature
Detailed Academic Explanation:
Redefining a superclass method in a subclass with the same method signature is the correct answer to “What is the concept of method overriding in Java inheritance”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass inherit static methods from its superclass?
Correct Answer: Option C — Yes, a subclass inherits static methods from its superclass
Detailed Academic Explanation:
Yes, a subclass inherits static methods from its superclass is the correct answer to “In Java, can a subclass inherit static methods from its superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the following code snippet? class Parent { void display() { System.out.println("Parent"); } } class Child extends Parent { void display() { super.display(); System.out.println("Child"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
Correct Answer: Option D — ParentChild
Detailed Academic Explanation:
ParentChild is the correct answer to “What is the result of the following code snippet? class Parent { void display() { System.out.println("Parent"); } } class Child extends Parent { void display() { super.display(); System.out.println("Child"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass access package-private members (fields and methods) of its superclass defined in another package?
Correct Answer: Option A — No, package-private members are not accessible to subclasses from different packages
Detailed Academic Explanation:
No, package-private members are not accessible to subclasses from different packages is the correct answer to “In Java, can a subclass access package-private members (fields and methods) of its superclass defined in another package”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the following code snippet? class Parent { int x = 10; } class Child extends Parent { int x = 20; void display() { System.out.println(super.x + " " + x); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
Correct Answer: Option D — 10 20
Detailed Academic Explanation:
10 20 is the correct answer to “What is the result of the following code snippet? class Parent { int x = 10; } class Child extends Parent { int x = 20; void display() { System.out.println(super.x + " " + x); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass override a default method from its superclass?
Correct Answer: Option C — Yes, starting from Java 8, a subclass can override default methods from interfaces
Detailed Academic Explanation:
Yes, starting from Java 8, a subclass can override default methods from interfaces is the correct answer to “In Java, can a subclass override a default method from its superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the "protected" access modifier in Java inheritance?
Correct Answer: Option B — To allow access to members within the same package and subclasses in any package
Detailed Academic Explanation:
To allow access to members within the same package and subclasses in any package is the correct answer to “What is the purpose of the "protected" access modifier in Java inheritance”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass access public members (fields and methods) of its superclass defined in another package?
Correct Answer: Option C — Yes, public members are accessible to subclasses from different packages
Detailed Academic Explanation:
Yes, public members are accessible to subclasses from different packages is the correct answer to “In Java, can a subclass access public members (fields and methods) of its superclass defined in another package”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the following code snippet? class Parent { void display() { System.out.println("Parent"); } } class Child extends Parent { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
Correct Answer: Option D — Parent
Detailed Academic Explanation:
Parent is the correct answer to “What is the result of the following code snippet? class Parent { void display() { System.out.println("Parent"); } } class Child extends Parent { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass override a static method from its superclass?
Correct Answer: Option A — No, static methods cannot be overridden
Detailed Academic Explanation:
No, static methods cannot be overridden is the correct answer to “In Java, can a subclass override a static method from its superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the following code snippet? class Parent { static void display() { System.out.println("Parent"); } } class Child extends Parent { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
Correct Answer: Option D — Parent
Detailed Academic Explanation:
Parent is the correct answer to “What is the result of the following code snippet? class Parent { static void display() { System.out.println("Parent"); } } class Child extends Parent { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass inherit private fields from its superclass?
Correct Answer: Option C — No, private fields are not inherited by subclasses
Detailed Academic Explanation:
No, private fields are not inherited by subclasses is the correct answer to “In Java, can a subclass inherit private fields from its superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, can a subclass override a final method from its superclass?
Correct Answer: Option C — No, final methods cannot be overridden
Detailed Academic Explanation:
No, final methods cannot be overridden is the correct answer to “In Java, can a subclass override a final method from its superclass”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the following code snippet? class Parent { final void display() { System.out.println("Parent"); } } class Child extends Parent { void display() { System.out.println("Child"); } public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
Correct Answer: Option D — Compilation error
Detailed Academic Explanation:
Compilation error is the correct answer to “What is the result of the following code snippet? class Parent { final void display() { System.out.println("Parent"); } } class Child extends Parent { void display() { System.out.println("Child"); } public static void main(String[] args) { Child obj = new Child(); obj.display(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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.
Correct Answer: Option C — 3 and 5
Detailed Academic Explanation:
3 and 5 is the correct answer to “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.”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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(); } }
Correct Answer: Option C — 123
Detailed Academic Explanation:
123 is the correct answer to “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(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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.
Correct Answer: Option C — (II) and (III)
Detailed Academic Explanation:
(II) and (III) is the correct answer to “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.”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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(); } }
Correct Answer: Option C — Class Two method1
Detailed Academic Explanation:
Class Two method1 is the correct answer to “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(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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); } }
Correct Answer: Option A — prints "Mammal eats food"
Detailed Academic Explanation:
prints "Mammal eats food" is the correct answer to “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); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the result of compiling and executing the following program code? class Vehicle{ public void printSound(){ System.out.print("vehicle"); } } class Car extends Vehicle{ public void printSound(){ System.out.print("car"); } } class Bike extends Vehicle{ public void printSound(){ System.out.print("bike"); } } public class Test{ public static void main(String[] args){ Vehicle v = new Car(); Bike b = (Bike) v; v.printSound(); b.printSound(); } }
Correct Answer: Option B — ClassCastException exception is thrown at runtime.
Detailed Academic Explanation:
ClassCastException exception is thrown at runtime. is the correct answer to “What will be the result of compiling and executing the following program code? class Vehicle{ public void printSound(){ System.out.print("vehicle"); } } class Car extends Vehicle{ public void printSound(){ System.out.print("car"); } } class Bike extends Vehicle{ public void printSound(){ System.out.print("bike"); } } public class Test{ public static void main(String[] args){ Vehicle v = new Car(); Bike b = (Bike) v; v.printSound(); b.printSound(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: class Small{ public Small(){ System.out.print("a "); } } class Small2 extends Small{ public Small2(){ System.out.print("b "); } } class Small3 extends Small2{ public Small3(){ System.out.print("c "); } } public class Test{ public static void main(String args[]){ new Small3(); } }
Correct Answer: Option C — a b c
Detailed Academic Explanation:
a b c is the correct answer to “Determine output: class Small{ public Small(){ System.out.print("a "); } } class Small2 extends Small{ public Small2(){ System.out.print("b "); } } class Small3 extends Small2{ public Small3(){ System.out.print("c "); } } public class Test{ public static void main(String args[]){ new Small3(); } }”. This question is standard in competitive examination syllabi for Inheritence.
💡Key Conceptual Anchor:Inheritence
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.