0% found this document useful (0 votes)
5 views2 pages

Week 3 Java Practice questions_updated

Week 3

Uploaded by

niruthakur05
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)
5 views2 pages

Week 3 Java Practice questions_updated

Week 3

Uploaded by

niruthakur05
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/ 2

Week 3 Java Practice questions – Classes and Object Creation

Date:07-08-2025
1. Create a class named Car with the following attributes:
➢ model (String)
➢ year (int)
➢ color (String)
Create a method display CarInfo() to print the car's information. Create two Car
objects and display their information.
Viva questions:
1. What is an operator used for creating an object for the class?
2. What is the purpose of the displayCarInfo() method?
3. How do you create an object of a class in Java?
4. What happens if you don’t initialize the attributes before calling the display
method?
5. Can we create multiple objects for the same class? If yes, how?

2. Create a class named Rectangle with attributes width and height (both double). Create
methods to calculate the area and perimeter of the rectangle. Create a Rectangle
object, set its dimensions, and calculate and print its area and perimeter.
Viva questions:
1. What are the data types used for width and height in this program? Why?
2. What is the formula for calculating the area and perimeter of a rectangle?
3. How do you call a method of a class from the main method?
4. What is the difference between instance variables and local variables?

3. Create a class named Student with attributes name, rollNumber, and marks. Create a
method calculateGrade() to calculate the grade based on marks. Create a Student
object, set its details, calculate the grade, and print the student's information along
with the grade.
Viva questions:
1. How would you modify the class if there were 5 subjects?
2. What is the return type of the calculateGrade() method?

4.Create a class named Car with the following attributes:


➢ model (String)
➢ year (int)
➢ color (String)
Create a constructor to initialize these attributes. Create a method displayCarInfo() to
print the car's information. Create two Car objects and display their information.
Viva questions
1. What is a constructor?
2. How is a constructor different from a method?
3. What happens if you do not define any constructor in a class?
4. Can we have multiple constructors in the same class? What is this called?

5.Create a class named Person with attributes name, age, and address. Create a default
constructor and a parameterized constructor. Create a method displayPersonInfo() to
print the person's information. Create two Person objects, one using the default
constructor and the other using the parameterized constructor. Display information for
both objects.
Viva questions
1. What is a default constructor? When is it called?
2. How do you create a parameterized constructor?
3. What is constructor overloading?
4. Can a constructor be private?
5. How would you initialize object values using both constructors?

You might also like