ION idea Interview Question
Developer Program EngineersCountry: India
Interview Type: Written Test
this is the code that can print its source code as its output in java
import java.io.*
class Source
{
public static void main(String args[]) throws Exception
{
FileReader fr=new FileReader("Source.java");
BufferedReader br=new BufferedReader(fr);
String str=null;
while((str=br.readLine())!=null)
System.out.println(str);
}
}
this 1 from wiki:
public class Quine
{
public static void main( String[] args )
{
char q = 34; // Quotation mark character
String[] l = { // Array of source code
"public class Quine",
"{",
" public static void main( String[] args )",
" {",
" char q = 34; // Quotation mark character",
" String[] l = { // Array of source code",
" ",
" };",
" for( int i = 0; i < 6; i++ ) // Print opening code",
" System.out.println( l[i] );",
" for( int i = 0; i < l.length; i++ ) // Print string array",
" System.out.println( l[6] + q + l[i] + q + ',' );",
" for( int i = 7; i < l.length; i++ ) // Print this code",
" System.out.println( l[i] );",
" }",
"}",
};
for( int i = 0; i < 6; i++ ) // Print opening code
System.out.println( l[i] );
for( int i = 0; i < l.length; i++ ) // Print string array
System.out.println( l[6] + q + l[i] + q + ',' );
for( int i = 7; i < l.length; i++ ) // Print this code
System.out.println( l[i] );
}
}
Way too easy in C/C++,
use to do in my early programming days.. :)..
M nt sure its wat they expected but I will read a file whose file name is the program's File name . :)
Such a program is called as quine. For more information google with the keyword "quine".
- slater May 28, 2012