Multiple Choice Questions
1 1. Which mechanism allows one class to inherit the properties of another class ?
a. Function b. Function Overloading
c. Inheritance d. Exception Handling
2 2. Which keyword is used to inherit a class ?
a. super b. extends
c. class d. void
3 3. Which one of the following options is also called a derived class ?
a. sub b. super
c. parent d. base
4 4. Which one of the following options is used to call the function of the base class in the derived
class ?
class ?
a. parent b. derived
c. extends d. super
5 5. Which one of the following options should be different to overload functions ?
a. Function name b. Function parameter list
c. Function return type d. Function call
True or False :-
11. A function cannot have the same name and different parameters. FALSE
22. A base class can also be called a parent class. TRUE
33. To overload functions, parameters should be different but the name should be the same. TRUE
44. A derived class cannot be called as a child class. FALSE
55. The extends keyword is used to call the function of a base class in a derived class. FALSE
Subjective Qustions :-
Qus. - 01 : What is inheritance ?
Ans. :- Inheritance is a mechanism that allows a class to inherit properties of another class.
Qus. - 02 : What is the difference between the extends and super keywords ?
Qus. - 03 : What are the derived and base classes ? Explain in detail.
Ans. :- Derived class :- The class that inherits property of another class is called the derived class. A derived class is also known as the Child class or the Sub class.
Base Class :- The class that gets inherited is called the base class. A base class is also known as the Parent class or the Super class.
Base Class :- The class that gets inherited is called the base class. A base class is also known as the Parent class or the Super class.
Qus. - 04 : What is function overloading ?
Ans. :- Function overloading :- The process of functions having the same name but different parameters is known as Function overloading.
Qus. - 05 : What is a parameter ?
Ans. :- The variables declared within the brackets of a function are known as parameters.
Lab Question :
Qus. - 01 : Write a program to display the menu to order a burger at a burger point. Write another class that contains information on beverages. The beverages class must be inherited by the burger class.
Ans:
Qus. - 02 : Write a program to find the area of square and triangle using function overloading.
Ans :-
class FunctionOverloading
{
public static void main(String args[])
{
FunctionOverloading fo = new FunctionOverloading();
fo.area(5);
fo.area(11,12);
}
void area(float s)
{
System.out.println("The area of Square is " + (s*s));
}
void area(float b, float h)
{
System.out.println("The area of the Triangle is " + (b*h)/2);
}
}
Ans :-
class FunctionOverloading
{
public static void main(String args[])
{
FunctionOverloading fo = new FunctionOverloading();
fo.area(5);
fo.area(11,12);
}
void area(float s)
{
System.out.println("The area of Square is " + (s*s));
}
void area(float b, float h)
{
System.out.println("The area of the Triangle is " + (b*h)/2);
}
}
Qus. - 03 : Write a program to use inheritance and function overloading.
Ans. :-
class FunctionOverloading
{
void area(float s)
{
System.out.println("The area of Square is " + (s*s));
}
void area(float b, float h)
{
System.out.println("The area of the Triangle is " + (b*h)/2);
}
}
class Inheritance extends FunctionOverloading
{
public static void main(String args[])
{
Inheritance i = new Inheritance();
i.area(5);
i.area(11,12);
}
}
class FunctionOverloading
{
void area(float s)
{
System.out.println("The area of Square is " + (s*s));
}
void area(float b, float h)
{
System.out.println("The area of the Triangle is " + (b*h)/2);
}
}
class Inheritance extends FunctionOverloading
{
public static void main(String args[])
{
Inheritance i = new Inheritance();
i.area(5);
i.area(11,12);
}
}
0 comments:
Post a Comment