aspose file tools*
The moose likes Programming Diversions and the fly likes Funny(deadly) Interview Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Funny(deadly) Interview Question" Watch "Funny(deadly) Interview Question" New topic
Author

Funny(deadly) Interview Question

Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 118
For my campus placements, Interviewer asked me one funny(deadly) question.
He said, "write a program whose output is the program itself".

I thought on it for while, Interviewer was smiling saying take your own time kid and started smoking.

After stretching my head i shouted, "Sir, within my code I will read the same code file and display"
"Amazing kid"..you are right, please write it and show me.
With victorious face, i wrote code in front of him in C considering standard's, indentation and other conventions.

Interviewer was very happy and started behaving like he is my uncle. . Asked me few questions about my family and some other technical stuff.
I got selected without any hassle.

He gave me few sample answers which my friends told him. Following are some which i rememebred
- We need to read RAM
- We need to print code as it is in "print" statements.

Well, This might be a joke, but the question he asked me can stand deadly when you are under pressure.

Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1012
    
    3
However often the executable (or the .class or .jar file for Java) is distributed without the source code file. If you had to solve the problem WITHOUT the source code available at run time, how would you do it.

Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 177
    
    6

Another take on this problem is something like...

and run the program with the following arguments:
java Test Test


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There is a word for such a program; it's called a "Quine", if you're interested in Googling. The solution you've described -- reading the source and printing it -- is considered a cheat, and is not a valid quine. It's entirely possible to create genuine quines that don't read their own source.


[Jess in Action][AskingGoodQuestions]
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Tina Smith wrote:Another take on this problem is something like...

and run the program with the following arguments:
java Test Test


I don't understand !!! how this is supposed to be Quine
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

Tina Smith wrote:Another take on this problem is something like...

and run the program with the following arguments:
java Test Test

This won't compile.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4463
    
    8

Ernest Friedman-Hill wrote:The solution you've described -- reading the source and printing it -- is considered a cheat, and is not a valid quine.

It's probably the sort of cheat that would go down well in an interview if you can't actually create a genuine quine, though .
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Matthew Brown wrote:
Ernest Friedman-Hill wrote:The solution you've described -- reading the source and printing it -- is considered a cheat, and is not a valid quine.

It's probably the sort of cheat that would go down well in an interview if you can't actually create a genuine quine, though .

Also a perfectly legitimate answer if the interviewer hasn't actually specified that you can't read the source file. Might as well go with a solution that's simple and easy to maintain.
Daniel Doboseru
Ranch Hand

Joined: Sep 26, 2011
Posts: 57
@Siddhesh: you said:
For my campus placements, Interviewer asked me one funny(deadly) question.

What do you mean be campus placements? You're student and for a place in the campus you had a technical interview as for hiring?
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
What do you mean be campus placements?


It refers to an event organized by the college management in which various companies visit the college campus and conduct written tests and technical interviews and give a job offer to the candidates who seem to fit their requirements.
Daniel Doboseru
Ranch Hand

Joined: Sep 26, 2011
Posts: 57
Oh, i see. Very nice thing. We have such a thing too: several companies come and present their offer, technologies etc and afterwards accept CVs from interested students, offering them the chance of a interview. We call them generically InfoTechs (ElectroTech, InstallTech, MechaniTech depending of the faculty).
simi selva
Greenhorn

Joined: Nov 18, 2011
Posts: 2
nice.. I have to learn more about quines...
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

from wikipedia:

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.

sounds a lot like a worm to me.

where did this interview take place?
viruses are us ?

SCJP
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2


However often the executable (or the .class or .jar file for Java) is distributed without the source code file. If you had to solve the problem WITHOUT the source code available at run time, how would you do it.

so much for my career as a virus writer
Bhanuprakash Sreenivas
Greenhorn

Joined: Jan 01, 2010
Posts: 8
Since Java is compiled and then interpreted , technically it is not feasible to get a exact copy of the
source code.
For example - The comments of a source will not be visible.

Directly interpreted languages like Javascript can be used to achieve this.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Bhanuprakash Sreenivas wrote:Since Java is compiled and then interpreted , technically it is not feasible to get a exact copy of the
source code.
For example - The comments of a source will not be visible.

Directly interpreted languages like Javascript can be used to achieve this.


Indeed, the many working examples above are all imaginary.
Bhanuprakash Sreenivas
Greenhorn

Joined: Jan 01, 2010
Posts: 8
Ernest Friedman-Hill wrote:
Bhanuprakash Sreenivas wrote:Since Java is compiled and then interpreted , technically it is not feasible to get a exact copy of the
source code.
For example - The comments of a source will not be visible.

Directly interpreted languages like Javascript can be used to achieve this.


Indeed, the many working examples above are all imaginary.


Oops.. jumped the gun
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

here is java Quine program

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

naveen yadav wrote:here is java Quine program



This doesn't work...I get a "File not found" error...Note that there are examples above that DO work, without requiring the source file.
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
an important question is - why do i need a quine ?

Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4463
    
    8

You don't. There's no practical use (that I can think of), it's just a puzzle. Some people find the challenge interesting.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
Quine (computing)
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
Andy Jack wrote:an important question is - why do i need a quine ?


George Mallory

Mallory is famously quoted as having replied to the question "Why do you want to climb Mount Everest?" with the retort "Because it's there",
which has been called "the most famous three words in mountaineering".

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2433
    
  28

Wait a minute. The discussion of Quine is interesting.. But the interviewer was smoking in the interview?!
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
naveen yadav wrote:here is java Quine program


For the record: it is actually not a Quine program:

A quine is a computer program which takes no input and produces a copy of its own source code as its only output.

 
Consider Paul's rocket mass heater.
 
subject: Funny(deadly) Interview Question