Page 327 - CA_Blue( J )_Class10
P. 327
6. What is a constructor? When is it invoked? [2014]
Ans. A constructor is a member method with same name of the class that is used to initialize the instance variables of the objects. It
is invoked at the time of object creation.
7. Give a difference between a constructor and a method. [2013]
Ans. The name of the constructor is same as that of the class, whereas a method name is always different from class name.
8. Create a class with one integer instance variable. Initialize the variable using: [2012]
(i) default constructor (ii) parameterised constructor
Ans. class automorphic
{
int n;
public automorphic() // default Constructor
{
n=0;
}
public Automorphic(int a) // parameterised constructor
{
n = a;
}
}
SECTION B
9. Define a class named BookFair with the following description: [2016]
Instance variables/data members
String bName : stores the name of the book.
double price : stores the price of the book.
Member methods
BookFair() : default constructor to initialize data members.
void input() : to input and store the name and the price of the book.
void calculate() : to calculate the price after discount. Discount is calculated based on the following criteria:
Price Discount
Less than or equal to Rs. 1000 2% of price
More than Rs. 1000 and less than or 10% of price
equal to Rs. 3000
More than Rs. 3000 15% of price
void display() : to display the name and price of the book after discount.
Ans. Please refer chapter no. 10, Questions from ICSE 2016
10. Define a class named MovieMagic with the following description: [2014]
Instance variables/data members
int year : to store the year of release of a movie.
String title : to store the title of the movie.
float rating : to store the popularity rating of the movie (minimum rating = 0.0 and maximum rating = 5.0)
Member Methods
MovieMagic() : default constructor to initialize numeric data members to 0 and String data member to "".
void accept() : to input and store year, title and rating.
void display() : to display the title of a movie and a message based on the rating as per the table below:
Rating Message to be displayed
0.0 to 2.0 Flop
2.1 to 3.4 Semi-hit
3.5 to 4.5 Hit
4.6 to 5.0 Super Hit
Write a main method to create an object of the class and call the above member methods.
Ans. Please refer chapter no. 10, questions from ICSE 2014
325
Constructors 325

