590 views
0 votes
0 votes
  • P1: A program can print its source code once.
  • P2: A program can print its source code twice.
  • P3: A program takes an input n and prints its source code n number of times.

Which of these programs are possible in C++/Java without the use of File I/O ?

Options:

A. P1 possible
B. P1 and P2 possible.
C. None Possible.
D. All are possible

Option can be different

3 Answers

Best answer
3 votes
3 votes

First you should notice that File Input/Output is not allowed. If its allowed then option (D) would be the correct answer. But as its not allowed hence it will not be the answer.

Correct answer is (B). Here is the explanation. A computer program which print it's own source code is called quine.

quine is a non-empty 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".

It can print twice or thrice. But number of times should be fixed. It can not take input from user. Hence P3 will not be possible. 

selected by
0 votes
0 votes
The question does not make any sense due to options :O

To print the content of the program we have the macro __FILE__ in C/C++ and this returns the source code as a string. So, all options should be true.

Related questions

1 votes
1 votes
1 answer
2
bad_engineer asked Mar 13, 2016
8,678 views
void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); return 0; }
0 votes
0 votes
0 answers
4