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?

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

Choose the Best Option

Click any option to instantly check if you're correct.

  • A StudentBanyan stud1 = new StudentBanyan("Kylin",5.6,23);
  • B StudentBanyan stud1 = new StudentBanyan("Kylin",23,5.6);
  • C StudentBanyan stud1 = new StudentBanyan(5.6,23,"Kylin",23);
  • D StudentPine stud1 = new StudentPine(5.6,23,"Kylin");
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?
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?
(a) StudentBanyan stud1 = new StudentBanyan("Kylin",5.6,23);
(b) StudentBanyan stud1 = new StudentBanyan("Kylin",23,5.6);
(c) StudentBanyan stud1 = new StudentBanyan(5.6,23,"Kylin",23);
(d) StudentPine stud1 = new StudentPine(5.6,23,"Kylin");
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.

Share This Question

Challenge a friend or share with your study group.