0% found this document useful (0 votes)
47 views11 pages

Inheritance by VXL Computer Classes 2023-24

Uploaded by

ishanbrave2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views11 pages

Inheritance by VXL Computer Classes 2023-24

Uploaded by

ishanbrave2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

124 K – BLOCK, KIDWAI NAGAR (in street of Balaji Park and Wedding Bell Banquet hall), Kanpur.

by : Vinod Khare(MCA ,M.Sc., B.Ed.)


phone : 9336061496

Java Inheritance MCQ Questions

1) What are the features of an Object Oriented Programming (OOPs)?


A) Inheritance
B) Encapsulation
C) Polymorphism
D) All the above

2) What are the features reused using Inheritance in Java?


A) Methods
B) Variables
C) Constants
D) All the above

2.1) Java language supports ___ type of inheritance.


A) Multiple Inheritance
B) Multi-Level Inheritance
C) Single Inheritance
D) Only B and C

1
3) What are the types of Inheritances (Whether Java supports or not) available in
Object-Oriented Programming Languages?
A) Single Inheritance
B) Multi-Level Inheritance, Hierarchical Inheritance
C) Multiple Inheritance, Hybrid Inheritance
D) All the above

4) In a Multi-Level Inheritance in Java, the last subclass inherits methods and


properties of ____.
A) Only one immediate Superclass
B) Few classes above it.
C) All classes above it
D) None

5) When a Class inherits two superclasses (not in Java), it is called ____ inheritance.
A) Multilevel inheritance
B) Single Inheritance
C) Multiple Inheritance
D) None

6) Which is the keyword used to implement inheritance in Java?


A) extends
B) implements
C) instanceof
D) None

7) What is the output of the below Java program with inheritance?


class Sweet
{ void price()
{ System.out.print("Sweet=$10 ");
}
}
class Sugar extends Sweet
{ void price()
{ super.price();
System.out.print("Sugar=$20");
}}
public class JavaInheritance1
{ public static void main( )
{
Sugar su = new Sugar();
su.price();
}}
A) Sweet=$10 Sugar=$20
B) Sweet=$10 Sugar=$10
C) Sweet=$20 Sugar=$20
D) Compiler error

8) To stop or block inheriting a given class, the ___ keyword is used before the class.
2
A) static
B) private
C) final
D) none of the above

9) What is the maximum number of levels possible in a Multilevel Inheritance in Java?


A) 8
B) 16
C) 32
D) No maximum level

10) What is the output of the below Java program with Constructors using Inheritance?
class Ant
{ Ant(String name)
{ System.out.print("Inside Ant(String) Constructor. ");}
}
class WildAnt extends Ant
{
WildAnt()
{ System.out.print("Inside WildAnt() Constructor. "); }}
public class Inheritance3
{
public static void main(String[] args)
{ WildAnt wa = new WildAnt(); } }
A) Inside WildAnt() Constructor.
B) Inside Ant(String) Constructor. Inside WildAnt() Constructor.
C) Inside WildAnt() Constructor. Inside WildAnt() Constructor.
D) Compiler error

11)What is inheritance in object-oriented programming?


A) Passing values between classes
B) Acquiring properties and behaviors of a class by another class
C) Using multiple classes simultaneously
D) None of the above

12)What is the main advantage of inheritance?


A) Code reusability
B) Code duplication
C) Code encapsulation
D) Code compilation

13) In Java, a class can inherit from:


A) Only one class
B) Multiple classes
C) No class at all
D) Only abstract classes

3
14) What is the access modifier used to declare a member so that it is accessible only
within its own class and its subclasses?
A) private
B) protected
C) public
D) default

15) In Java, which keyword is used to prevent a method from being overridden in a
subclass?
A) final
B) static
C) abstract
D) private

16) Which of the following is an example of hierarchical inheritance?


A) Class A extends Class B, Class C extends Class B
B) Class A extends Class B, Class B extends Class C
C) Class A extends Class B, Class B extends Class A
D) Class A extends both Class B and Class C

17) Which of the following is a disadvantage of multiple inheritance?


A) Code reusability
B) Ambiguity in method resolution
C) Simplicity in code design
D) Improved encapsulation

18)In Java, which keyword is used to call the parent class constructor?
A) parent()
B) super()
C) base()
D) this()

19) What is the purpose of the 'super' keyword in Java?


A) To call a method of the current class
B) To refer to the superclass object
C) To create an instance of the superclass
D) To initialize the subclass object

20) Inheritance is a mechanism to achieve:


D) Code duplication
B) Code encapsulation
C) Code reusability
D)Code compilation

4
Short Question

1. How is a subclass created in Java?


2. Differentiate between "single inheritance" and "multiple inheritance."
3. Why is the concept of "method overriding" important in inheritance?
4. What is the role of the super keyword in Java inheritance?
5. How is the final keyword used in the context of inheritance?
6. What is the significance of the protected access modifier in inheritance?
7. Can subclass access private members of its super class? Why or why not?
8. How can you achieve method overriding in Java? Provide an example.
9. State the difference between an Interface and a Class.
10. How can we override a method in inheritance?
11. Differentiate between the keywords extends and implements.
12. State two advantages of using the concept of inheritance in Java
13. Differentiate between this keyword and the super keyword.
14. From the class declaration given below, state the nature of the identifiers A, B, C and D:
class A extends B implements C, D

Important solved and unsolved programs to practice.

Note : Write comment in each program, write approx. 5 to 6 comments in each program
of 10 marks and 3 to 4 comments in 5 marks questions, no need to write variable
description.

Question 1. A class Demand contains the market demands of a product. Another class
Supply is derived from Demand which contains the production and supply details of the
product.
The details of the base class:
Class Name : Demand
Data member / instance variable:
PId : String to store the product id.
PName : String to store the product name.
PDemand : Integer to store the demand of the product.
Member functions / methods :
Demand(…) : parameterized constructor to initialize the data members
void display( ) : to display the member data.
The details of the derived class :
Class Name : Supply
Data member /instance variable:
PProduced : to store the amount of product produced.
Prate : to store the cost per unit.
Member functions / methods :
Supply(…) : parameterized constructor to initialize the data members of both the classes.
int amount( ) : returns the difference between the amount of demand [rate x demand] and
amount of produced [rate x produced].
void display( ) : to display all the details of a product with proper messages
(including the difference in amount) (apply method overriding).

5
Assume that the super class Demand has been defined. Using the concept of inheritance,
specify the class Supply giving details of the constructor, int amount( ) and void display( ). The
super class, main function and algorithm need NOT be written.

Answer.

class Supply extends Demand


{
int PProduced;
double Prate;
Supply(int id, String nm, int d, int pr, double rt)
{
super(id,nm,d);
PProduced=pr;
Prate=rt;
}
int amount( )
{
double d=PDemand*Prate;
double e=PProduced*Prate;
return Math.abs(d–e);
}
void display( )
{
super.display( );
System.out.println(PProduced);
System.out.println(Prate);
} }

Question 2. A super class VEHICLE stores the details of a vehicle. Another class CAR is
derived from VEHICLE which performs certain operations.
The details of the base class :
Class Name : VEHICLE
Data member/ instance variable :
String regnum : To store the registration number [alphanumeric].
char Permit : The national permit category [either N, S or L].
Member functions/ methods :
VEHICLE(…) : parameterized constructor to initialize the data members.
void Print( ) : to display the member data.
The details of the derived class :
Class Name : CAR
Data member/ instance variable :
long Price : to store the price of the car.
double Exc : to store the excise to be levied on the car.
Member functions/ methods :
CAR(…) : parameterized constructor to initialize the data members of the base and current
class. The excise amount (Exc) is initialized to null.

6
void CalEx( ) : calculates the excise amount as per the given chart.
Permit Excise Amount
N 22 % of Price
S 11% of Price
L 5% of Price
void Print( ) : to display all the details of both the classes with proper
messages (apply method overriding).
Specify the class CAR giving the details of its mentioned methods. Assume that class
VEHICLE is already present. The super class, main function and algorithm need NOT be
written.

Answer

class CAR extends VEHICLE


{
long Price;
double Exc;
CAR(Sring reg1, char permit1, long price1)
{
super(reg1, permit1);
Price = price1;
Exc = 0;
}
void CalEx( )
{
if(Permit == „N‟)
Exc = 22.0/100 * Price;
else if(Permit == „S‟)
Exc = 11.0/100 * Price;
else if(Permit == „L‟)
Exc = 5.0/100 * Price;
}
void Print( )
{
super.print( );
System.out.println(“ Price is ` ” + Price);
System.out.print(“ Excise is ` ” + Exc); } }

Question 3.
Create two classes named Library and Issue. The class library will have the following
structure to store detail of the books. Another class Issue will inherit class Library purchase
that will store the number of days late in returning the book and fine calculated.
Class name : Library
Member data:
bookno : as Integer to store book number.
Bookname : as String to store the name of the book.
Studentname : as String to store the name of the student.
Member function:
Library(….) : parameterized constructor to give initial values to data members.

7
void display( ) : to display the member data
Class name : Issue
Member data :
daysLate : as Integer to store the number of days late to return the book.
fine : as Double to store the fine calculated.
Member function:
Issue(….) :parameterized constructor to give initial values to the Super class data members
void return_book( ): to receive date of return of the book from student and compute the fine if
no. of days are late according to the given rule otherwise no fine. Number of days late * 0.5
void display( ) : to display the detail of the student issued the book along with total amount as
fine (if applicable)
Assume that the super class Library has been defined. Using the concept of inheritance,
specify the class Issue giving details of the constructor, void return_book( ) and void display( ).
The super class, main function and algorithm need NOT be written.

Answer

class Issue extends Library


{
int daysLate;
double fine;
public Issue(int bno, String bname, String Sname, int dl)
{
super(bno, bname, Sname);
daysLate=dl;
fine=0;
}
public void return_book( )
{
fine=daysLate*0.5;
}
public void display( )
{
super.display( );
System.out.println(“Days late:”+daysLate);
System.out.println(“Fine:”+fine);
}}

Question 4.
A class Employee contains employee details and another class Salary calculates the
employee‟s net salary.
The details of the two classes are given below:
Class name : Employee
Data members :
empNo : stores the employee number.
empName : stores the employee name
empDesig : stores the employee‟s designation.
Member functions:
Employee(…) : parameterized constructor to assign values to data members.

8
void display( ) : display the employee details.
Class name : Salary Data members:
basic : float variable to store the basic pay.
Member functions:
Salary(…) : parameterized constructor to assign values to data members.
void calculate() : calculates the employee‟s net salary according to the following rules:
DA = 10% of basic
HRA = 15% of basic
Salary = basic + DA +HRA PF= 8 % of Salary
Net Salary = Salary –PF
Display the employee details and the Net salary.
Specify the class Employee giving details of the constructors and member function void
display(). Using the concept of inheritance specify the class Salary giving details of the
constructor and the member function void calculate (). The main function need not be written.

Question 5.
A super class Worker has been defined to store the details of a worker. Define a subclass
Wages to compute the monthly wages for the worker. The details/specifications of both the
classes are given below:
Class name : Worker
Data Members/instance variables:
Name : to store the name of the worker
Basic : to store the basic pay in decimals Member functions:
Worker (…) : Parameterized constructor to assign values to the instance variables.
void display ( ) : display the worker‟s details
Class name : Wages
Data Members/instance variables:
hrs : stores the hours worked
rate : stores rate per hour
wage : stores the overall wage of the worker
Member functions:
Wages (…) : Parameterized constructor to assign values to the instance variables of both the
classes
double overtime ( ) : Calculates and returns the overtime amount as (hours*rate)
void display ( ) : Calculates the wage using the formula
wage = overtime amount + Basic pay and displays it along with the other details.
Specify the class Worker giving details of the constructor () and void display ( ). Using the
concept of inheritance, specify the class Wages giving details of constructor ( ), double-
overtime () and void display (). The main () function need not be written.

INTERFACE
1. What is an interface in Java?
a. A class that cannot be instantiated
b. A blueprint for a class
c. A collection of methods without implementation
d. A keyword to define constants

2. Which keyword is used to declare an interface in Java?


a. interface
b. class
9
c. abstract
d. implements

3. An interface can have fields (variables) with which access modifier?


a. public
b. private
c. protected
d. All of the above

4. In an interface, all methods are by default:


a. final
b. abstract
c. static
d. private

5. Which of the following is a correct way to implement an interface in a class?


a. class MyClass implements MyInterface { }
b. class MyClass extends MyInterface { }
c. class MyClass : MyInterface { }
d. class MyClass extends interface MyInterface { }

6. Which of the following is true about interface variables?


a. They must be initialized at the time of declaration.
b. They are implicitly final and static.
c. They can be modified by the implementing class.
d. They are not allowed in interfaces.

7. Can an interface have constructors in Java?


a. Yes
b. No

8. Which of the following is not a type of inheritance in Java?


a. Single inheritance
b. Multiple inheritance
c. Multilevel inheritance
d. Interface inheritance

9. What is the purpose of the implements keyword in a class declaration?


a. It declares the class as abstract.
b. It declares the class as an interface.
c. It indicates that the class is implementing an interface.
d. It is not a valid keyword in Java.

10. In Java, can an interface have private methods?


a. Yes
b. No

10
11. Which of the following is a valid interface method declaration?
a. void method( );
b. private void method();
c. final void method();
d. static void method();

12. In Java, interfaces are used to achieve:


a. Code reusability
b. Code encapsulation
c. Code polymorphism
d. Code compilation

11

You might also like