Question

1.
2.
3.
4.
Answer (Detailed Solution Below)

Option 1 : `Student(String n) { System.out.println("hi"); this(n, 0); }` — calling this() as the second statement / `this()` को दूसरे statement के रूप में बुलाना

Advertisement

Detailed Solution

सही उत्तर `Student(String n) { System.out.println("hi"); this(n, 0); }` — calling this() as the second statement / `this()` को दूसरे statement के रूप में बुलाना है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

Which of the following Java constructors causes a COMPILATION error? / निम्नलिखित में से कौन-सा Java constructor कंपाइलेशन त्रुटि उत्पन्न करता है?

1.
`Student(String n) { System.out.println("hi"); this(n, 0); }` — calling this() as the second statement / `this()` को दूसरे statement के रूप में बुलाना
2.
`Student() { this("x", 0); }` / `Student() { this("x", 0); }`
3.
`Student(String n, int r) { this.name = n; }` / `Student(String n, int r) { this.name = n; }`
4.
`Student(int r) { this("x", r); }` / `Student(int r) { this("x", r); }`

Correct Answer: `Student(String n) { System.out.println("hi"); this(n, 0); }` — calling this() as the second statement / `this()` को दूसरे statement के रूप में बुलाना

A constructor's `this(...)` or `super(...)` call must be its very first statement; placing any statement before it is a compile-time error. The other three obey the rule. The trap is thinking constructor chaining can appear anywhere in the body. / constructor का `this(...)` या `super(...)` कॉल उसका पहला statement होना चाहिए; इससे पहले कोई statement रखना compile-time त्रुटि है। अन्य तीनों नियम का पालन करते हैं। जाल यह सोचना है कि constructor chaining body में कहीं भी आ सकती है।

Downloaded from PrepSnap (prepsnap.in)