MCQ
Single Best Answer
Moderate
Q
[Object Oriented Programming]
Software developers of Banayan High School have created a class to store
the name, age and height of students.
If the class shown below compiles correctly, which of the following will
be the correct statement to create an object and pass values as arguments?
class StudentBanyan
{
String name;
int age;
double height;
StudentBanyan(String n, int a, double h)
{
name = n;
age = a;
height = h;
}
}
Which one of the following is the correct statement of using a constructor?
If the class shown below compiles correctly, which of the following will be the correct statement to create an object and pass values as arguments?
{
String name;
int age;
double height;
StudentBanyan(String n, int a, double h)
{
name = n;
age = a;
height = h;
}
}
ID: #24943
Competency focused Practice Questions ISC Class XII Computer Science
4 views
Question Info
#24943Q ID
ModerateDifficulty
Competency focused Practice Questions ISC Class XII Computer ScienceTopic
Your Answer
Choose the Best Option
Click any option to instantly check if you're correct.
Correct Answer: Option B
Explanation
[Object Oriented Programming]
Software developers of Banayan High School have created a class to store
the name, age and height of students.
If the class shown below compiles correctly, which of the following will be the correct statement to create an object and pass values as arguments?
If the class shown below compiles correctly, which of the following will be the correct statement to create an object and pass values as arguments?
class StudentBanyan
{
String name;
int age;
double height;
StudentBanyan(String n, int a, double h)
{
name = n;
age = a;
height = h;
}
}
{
String name;
int age;
double height;
StudentBanyan(String n, int a, double h)
{
name = n;
age = a;
height = h;
}
}
Which one of the following is the correct statement of using a constructor?
Correct Answer: (b)
Understanding the Constructor:
The constructor of the class is:
StudentBanyan(String n, int a, double h)
This means the constructor expects arguments in the following order:
| Parameter | Data Type |
|---|---|
| n | String |
| a | int |
| h | double |
Checking Each Option:
| Option | Analysis |
|---|---|
| (a) | Wrong order of arguments. Constructor expects: String, int, double but here it is: String, double, int |
| (b) |
Correct order:
String → "Kylin" int → 23 double → 5.6 |
| (c) | Incorrect number and order of arguments. |
| (d) | Wrong class name. Constructor belongs to StudentBanyan, not StudentPine. |
Correct Object Creation:
StudentBanyan stud1 = new StudentBanyan("Kylin", 23, 5.6);
Step-by-Step Meaning:
- StudentBanyan → Class name
- stud1 → Object name
- new → Creates object in memory
- StudentBanyan(...) → Calls constructor
-
Arguments passed:
- "Kylin" → String
- 23 → int
- 5.6 → double
Final Conclusion:
Since the constructor requires:
(String, int, double)
only option (b) matches correctly.
Continue Practice
Share
Share This Question
Challenge a friend or share with your study group.
More from This Topic