java - Change output without modifying code -


This question was recently asked in an interview that I participated. Public class MagicOutput {public static zero main (last string argv []) {System.out.println ("Hello, World!"); }}

The main signature of the method (name, number and parameter type, return type) and implementation (body of method),

The program was told the standard console message "Magic Output!" To output to

I took about 2 minutes to answer. My solution was to put a static block there and essential string output.

  Fixed {System.out.println ("Magic Output!"); }   

But this works, it prints both magic outputs! and Hello, world!

How do I output it to only Magic String?

  static {System.out.println ("magic output!"); System.exit (0); }   

or fun (depending on string implementation - works with openjdk 8):

  static {System.out.println (" Magic output! "); Try {field f = string.clash.gateclined field ("value"); f.setAccessible (true); f.set ("hello, world!", new four [0]); f.set (System.lineSeparator (), new four [0]); } Hold (ignoring the exception) {}}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -