Vidyalelo
Java Programming · Q51

Constructors and Methods

Programming · Java Programming · question 51

Q51

What is the output of the program? class MyClass MyClass() System.out.print("one"); public void myMethod() this(); System.out.print("two"); public class TestClass public static void main(String args[]) MyClass obj = new MyClass(); obj.myMethod();

A.
two one one
B.
one one two
C.
one Exception
D.
Compilation Error
Answer
E.
None of these

Answer: Option D

Solution

Answer: Option D
Solution:
The code will result in a compilation error because in the method myMethod, the use of this() is incorrect.

In Java, this() is used to call another constructor, but it can only be used in a constructor, not in a method.

Using this() in myMethod causes a compilation error.

Thus, the program will not compile, and the correct answer is a Compilation Error.