Microsoft Interview Question
Front-end Software EngineersCountry: United States
Interview Type: In-Person
A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are self-replicating programs, self-reproducing programs, and self-copying programs. ( Wiki: en.wikipedia.org/wiki/Quine_(computing) )
Watch out for Quines here:
1. madore.org/~david/computers/quine.html
2. nyx.net/~gthompso/quine.htm
A complete Quine:
#include <stdio.h>
int
main (void)
{
char *s1="#include <stdio.h>%c%cint%cmain (void)%c{%c";
char *s2=" char *s%c=%c%s%c;%c char *s%c=%c%s%c;%c";
char *s3=" char n='%cn', q='%c', b='%c%c';%c";
char *sp=" printf(";
char *s4="%ss1,n,n,n,n,n);%c";
char *s5="%ss2,'1',q,s1,q,n,'2',q,s2,q,n);%ss2,'3',q,s3,q,n,'p',q,sp,q,n);%c";
char *s6="%ss2,'4',q,s4,q,n,'5',q,s5,q,n);%ss2,'6',q,s6,q,n,'7',q,s7,q,n);%c";
char *s7="%ss2,'8',q,s8,q,n,'9',q,s9,q,n);%ss2,'0',q,s0,q,n,'x',q,sx,q,n);%c";
char *s8="%ss3,b,q,b,b,n);%ss4,sp,n);%ss5,sp,sp,n);%c";
char *s9="%ss6,sp,sp,n);%ss7,sp,sp,n);%ss8,sp,sp,sp,n);%c";
char *s0="%ss9,sp,sp,sp,n);%ss0,sp,sp,n,n,n);%c return 0;%c}%c";
char *sx="--- This is an intron. ---";
char n='\n', q='"', b='\\';
printf(s1,n,n,n,n,n);
printf(s2,'1',q,s1,q,n,'2',q,s2,q,n); printf(s2,'3',q,s3,q,n,'p',q,sp,q,n);
printf(s2,'4',q,s4,q,n,'5',q,s5,q,n); printf(s2,'6',q,s6,q,n,'7',q,s7,q,n);
printf(s2,'8',q,s8,q,n,'9',q,s9,q,n); printf(s2,'0',q,s0,q,n,'x',q,sx,q,n);
printf(s3,b,q,b,b,n); printf(s4,sp,n); printf(s5,sp,sp,n);
printf(s6,sp,sp,n); printf(s7,sp,sp,n); printf(s8,sp,sp,sp,n);
printf(s9,sp,sp,sp,n); printf(s0,sp,sp,n,n,n);
return 0;
}
#include <iostream>
#include <fstream>
#include <string.h>
//#include <stdio.h>
int main()
{
std::ifstream fp(__FILE__, std::ifstream::in);
std::string line;
//FILE* fp = fopen(__FILE__, "r");
if(fp == NULL)
{
std::cout << "fp = NULL" << std::endl;
return 0;
}
else
{
//std::cout << "fp != NULL " << std::endl;
while(!fp.eof())
{
getline(fp, line, '\n');
for(int i=0; i<line.size(); i++)
{
char c = line[i];
std::cout << c;
}
std::cout << "\n";
}
}
return 0;
}
/This is a mirror program which prints its source code ;-)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
// to read one line from file
string line;
// this is our source file which is going to be the input file
ifstream f (__FILE__);
// read until the last line and print each line
while (getline(f, line))
cout << line << endl;
return 0;
}
key points:
1, use "printf" to keep the format
2, use ASCII code to replace "\n","\t" and quotation mark.
public class Quine
{
public static void main(String [] argus)
{
String input = "public class Quine %c{%c%c public static void main(String [] argus) %c{%c%c String s = %c%s%c; %c System.out.printf(input, 10, 10, 9, 10, 10, 9, 34, input, 34, 10, 10, 10); %c } %c}";
System.out.printf(input, 10, 10, 9, 10, 10, 9, 34, input, 34, 10, 10, 10);
}
}
The empty python script does the trick.
- Anonymous October 15, 2013