0% found this document useful (0 votes)
231 views6 pages

Constructors and Class&Objects

The document discusses classes, objects, constructors, and related concepts in Java. It provides 17 questions about these topics, asking the reader to define classes with data members and methods, write code to create objects and call methods, differentiate between concepts like public and private access modifiers, and more. The questions cover topics like constructors, this keyword, inheritance, overloading, and calculating values like discounts, taxes, and pay.
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)
231 views6 pages

Constructors and Class&Objects

The document discusses classes, objects, constructors, and related concepts in Java. It provides 17 questions about these topics, asking the reader to define classes with data members and methods, write code to create objects and call methods, differentiate between concepts like public and private access modifiers, and more. The questions cover topics like constructors, this keyword, inheritance, overloading, and calculating values like discounts, taxes, and pay.
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/ 6

Class, Constructor and Objects

1. State the difference between Constructor and Method

2. Enter any two variables through constructor parameters and write a program to swap and
print the values.

3. What is the purpose of the new operator?

4. What is a default constructor?

5. Why do we need a constructor as a class member?

6. What does the following mean?


a. Employee staff = new Employee();

7. What is the use of the keyword ​this​?

8. Why is a class known as composite data type?


9.
10. Consider the following code and answer the questions that follow:
class academic
{
int x,y;
void access()
{
int a,b;
academic student =new academic();
System.out.println(“Object Created”);
}
}
a. What is the object name of the class academic?
b. Name the class variables used in the program.
c. Write the local variables used in the program.
d. Give the type of the function used and its name.

11. State two features of a constructor.

12. Explain the concept of constructor overloading with an example.

13. Create a class with one integer instance variable. Initialize the variable using
a. default constructor
b. parameterized constructor.

14. Diifferentiate between public and private modifiers for members of a class.

15. In the program given below, state the name and the value of the
a. method argument or argument variable
b. class variable
c. local variable
d. instance variable

class myClass
{
static int x=7;
int y=2;
public static void main(String args[])
{
myClass obj = new myClass();
System.out.println(x);
obj.sampleMethod(5);
int a=6;
System.out.println(a);
}
void sampleMethod(int n)
{
System.out.println(n);
System.out.println(y);
}
}
16. Name the Java keyword that Stores the address of the currently calling object

17. Write a Java statement to create an object ​mp4​ of class ​digital.


18. What is a constructor? When is it invoked?
19. Name the two types of constructors

1. Write a class with name ​employee ​and ​basic ​as its data member, to find the gross pay
of an employee for the following allowances and deduction. Use meaningful variables.
Dearness Allowance = 25% of Basic Pay
House Rent Allowance = 15% of Basic Pay
Provident Fund = 8.33% of Basic Pay
Net Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Gross Pay = Net Pay - Provident Fund.

2. A cloth showroom has announced the following festival discounts on the purchase of
items, based on the total cost of the items purchased:-
Total cost Discount (in Percentage)
Less than Rs. 2000 5%
Rs. 2001 to Rs. 5000 25%
Rs.5001 to Rs.10000 35%
Above Rs. 10000 50%
Write a program to input the total cost and to compute and display the amount
to be paid by the customer after availing the discount.

3. Define a class ​salary​ described as below:-


Data Members:
Name, Address, Phone, SubjectSpecialization, MonthlySalary, IncomeTax.
Member methods:
To accept the details of a teacher including the monthly salary.
To display the details of the teacher.
To compute annual Income Tax as 5% of annual salary above Rs.
1,75,000/-Write main method to create object and call the above
memeber methods.

4. Define a class ​employee​ having the following description:


Data members/Instance Variables:
int pan to store personal account number.
String name to store name.
double taxincome to store annual taxable income.
double tax to store tax that is calculated.

Member functions:
input() Store the pan number,name and taxable income
calc() Calculate tax for an employee
display() Output details of an employee

5. Write a program to compute the tax according to the given conditions and display the
output as per given format.
Total Annual Taxable Income Tax Rate
Upto Rs. 1,00,000 No tax
From 1,00,001 to 1,50,000 10% of the income exceeding 100000
From 1,50,001 to 2,50,000 5000+20% of the income exceeding 150000
Above Rs. 2,50,000 25000+30%of the income exceeding 150000
Output:​
Pan Number Name Tax-income Tax
- - - -

6. An electronics shop has announced the following discounts on the purchase of certain
items.
Purchase amount in Rs. Discount on Laptop Discount on Desktop PC
0 – 25000 0.0% 5.0%
25001 – 57000 5.0% 7.5%
57001 – 100000 7.5% 10.0%
More than 100000 10.0% 15.0%

7. Write a program based on the above criteria, to input name, address, amount of
purchase and type of purchase (L for Laptop and D for Desktop) by a customer.
Compute and print the discount and net amount to be paid by a customer along with
his name and address.
(Hint: discount = (discount rate/100)*amount of purchase
Net amount = amount of purchase - discount)

8. Define a class student described as below:


Data members/Instance variables:
name, age, m1,m2,m3 (marks in 3 subjects), maximum, average

Member methods:
A parameterized constructor to initialize the data members.
To accept the details of a student.
To compute average and maximum out of three marks.
To display the name, age, marks in three subjects, maximum and
average.
Write main () to create an object of the class and call the above member
methods.

9. Define a class called ​mobike​ with following description:


Instance variables/data members:
int bno to store the bikes number
int phno to store the phone number of the customer
String name to store the name of the customer
int days to store the number of days the bike is taken on rent
int charge to calculate and store the total charge

Member methods:
void input() to input and store the detail of the customer
void compute() to compute the rental charge

The rent is charged as follows


First five days Rs. 500 per day
Next five days Rs. 400 per day
Rest of the days Rs. 200 per day

void display() to display the details in the following format


Bike No Phone No Name No. of days Charge
------ -------- ----- ---- ---

10. Define a class Library with following description:


Instance Variables / Data Members:
into acc_num - stores the accession number of the book
String title - stores the title of the book
String author - stores the name of the author

Member methods:
void input() - To input and store the accession number, title and
author
void compute() - To accept the number of days late, calculate and
display the fine charged at the rate of Rs. 2 per day.
void display() - To display the details in the following format:
Accession​ Number​ Title​ Author

Write a main method to create an object of the class and call the above
methods.

11. Define a class named FruitJuice with the following description:


Instance variables/data members:
int product_code - stores the product code number
String flavour - stores the flavor of the juice(eg.orange,apple, etc)
String pack _type - stores the type of packaging(eg. Tetra pack, PET
bottle, etc)
int pack_size - stores package size(eg.200ml,400ml,etc)
int product_price - stores the price of the product
Member methods:
FruitJuice() - default constructor to initialize integer data members to
0 and string data members to “”.
void input() - to input and store the product code, flavour, pack type,
pack size and product price.
void discount() - To reduce the price by 10;
void display() - To display code, flavour, pack type, pack size and price.

12. 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 members to “”.
(ii) void accept() – To input and store year, title and rating
(iii) void display() – To display the title of the 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 methods.

13. Define a class ​ParkingLot​ with the following description :


Instance variables/data members :
int vno – to store the vehicle number
int hours – to store the number of hours the vehicle is parked
double bill – to store the bill amount
Member methods :
void input() – To input and store vno and hours
void calculate() – To compute the parking charge at the rate of Rs.3 for the
first hour or part thereof, and Rs.1.50 for each additional hour or part thereof.

void display() – To display the detail


Write a main method to create an object of the class and call the above methods

14.

You might also like