0% found this document useful (0 votes)
3 views3 pages

Constructor Functions and Java Programming Quiz

Practice java constructor progs and mcqs for class 10
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)
3 views3 pages

Constructor Functions and Java Programming Quiz

Practice java constructor progs and mcqs for class 10
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

Homework :

Tick the correct option.


1) Which of the following is not applicable for a constructor function?
a) It has the same name as the class. b) It has no return-type c) It is usually used for initialisation.
d) It can be invoked using an object like any other member function.
2) If the name of a class is ‘Number’, what can be the possible name for its constructor?
a) Number b) number c) No d) no
3) Which among the following is a type of constructor?
a) Parameterised constructor b) Non-parameterised constructor c) Both a and b d) None of these
4) If constructors are overloaded, what differentiates it?
a) Parameter list b) Return type c) Both a and b d) None of these
5) What access specifier for a constructor allows you to create an object only within the class?
a) public b) private c) protected d) default
6) Predict the output of the following program:
class T
{
int t = 20;
T()
{
t = 40;
}
public static void main(String args[])
{
T t1 = new T();
[Link](t1.t);
}
}
a) 20 b) 40 c) Compiler Error d) None of these
7) Which of the following is not true for static block?
a) It is used to initialise static variables. b) It gets executed when a class gets loaded in the memory.
c) It can print the content of instance variables. d) It begins with the static keyword)
8) A member method having the same name as that of the class is called __________
a) a friendly method b) constructor c) a protected method d) None
9) A constructor has _________ return type.
a) a void b) more than one c) String[] args d) no
10) A constructor is used when an object is _________
a) created b) destroyed c) assigned a value d) abstracted
11) A default constructor has _____________
a) no parameters b) one parameter c) two parameters d) multiple parameters
12) Pick the correct answer.
a) A constructor has exactly the same name as its defining class.
b) A constructor method does not have a return type.
c) A constructor can be invoked only via the new operator. d) All of the above
13) Default constructor requires how many parameters ?
a) 3 b) 2 c) 0 d) 1
Fill in the blanks :
1) A member function having the same name as that of its class is called _________ function.
2) A constructor function has _________ return type.
3) A private constructor allows object creation only inside _________ functions.
4) A _________ constructor takes no arguments.
5) A _________ constructor creates objects through values passed to it.
6) The keyword _________ refers to current object.

Solve the following problem :

1) Find the errors in the following program and rectify it:


class Myclass
{
int a,b;
void Myclass(int x, int y)
{
a=x;
b=y;
}
int Myclass(int x)
{
a=b=x;
}
void show( )
{
[Link](a+ " "+y);
}
public static void main(String args[ ])
{
Myclass ob1=new Myclass( );
Myclass ob2=new Myclass(12.3,14.6,15);
Myclass ob3=new Myclass(7);
[Link]( );
}
}

Solve the following programs :


a) Write a class program in Java with the following specifications:
Class name : Hcflcm
Data members/instance variables : int a, b
Member functions:
hcflcm (int x, int y) : constructor to initialize a to x and b to y.
void calculate() : to find and print hcf and lcm of both the numbers.
b) Design a class name ShowRoom with the following description:
Instance variables/data members:
String name: to store the name of the customer.
long mobno: to store customer’s mobile number.
double cost: to store the cost of the item purchased)
double dis: to store the discount amount.
double amount: to store the amount to be paid after discount.
Methods:
ShowRoom(): default constructor to initialize the data members
void input(): to input customer name, mobile number, cost
void calculate(): to calculate discount on the cost of purchased items, based on the following criteria

Cost Discount(In percentage)

Less than or equal to ₹ 10000 5%

More than ₹ 10000 and less than or equal to ₹ 20000 10 %

More than ₹ 20000 and less than or equal to ₹ 35000 15 %

More than ₹ 35000 20%

void display()- To display customer, mobile number, amount to be paid after discount.
Write a main() method to create an object of the class and call the above methods.

c) Define a class named movieMagic with the following description:


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 :
(i) movieMagic() Default constructor to initialize numeric data members to 0 and String data member to “”.
(ii) void accept() To input and store year, title and rating.
(iii) 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.

You might also like