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
Post a Comment