Java program to print the string without modifying exiting main method?

This is very rarely asked Java interview question.

The question is as follows:

Java program to print the string without modifying the main method and without using the static block?

Suppose you have the following java code with the main method:



Now you have to print the Sting Like this ” Hello, Akshay How Are You?”. But the condition is that you can not modify the main method, and you can not also use the static block.

The question is the very tricky java interview question. As fresher in java interview question you may face thi question. However, this question may not be asked in your interview but you this will surely be helpful to understand the Java  SE concepts.

The solution of this question is as follows:



If you know what is the meaning of System.out.print() statement is in java. When you write System.out.println() it uses the object of the System class.  out is the instance of the OutputStream class. And the println()  is the method of the OutputStream class.

So to do that you have to rewrite the System class as per given in above class.

So you can write the code like this and you can achieve the result as wanted in the question.

I hope that you like this post. If you like our articles or you have any suggestion and doubt about the post and any thing related to technology and block you can comment below.

You will also get the Java interview questions post like this in your social media feed by following the Code Puran in social media platform.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *