7/24/08

C Questions

[Q001]. Whether the following program is a LEGAL code or ILLEGAL code. If it is a LEGAL code then

what will be the output :
#include
main(t,_,a)
char *a;
{return!0 main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0 i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

Ans. As an extreme example the above C code snippet (mystery.c) is actually LEGAL C code.
It will compile and run and produce meaningful output. Try this program out. Try to compile and
run it yourself.

Clearly nobody ever writes code like or at least should never. This piece of code actually won an
international Obfuscated C Code Contest http://reality.sgi.com/csp/iocc The standard for
C programs was originally the features set by Brian Kernighan. In order to make the language more
internationally acceptable, an international standard was developed, ANSI C (American National
Standards Institute).

Please excuse for this.
_________________________________________________________________________________________________

[Q002]. What will be the output of the following program :
void main()
{
int i;
float a[5];
for (i=0; i<5; i++)
a[i] = (printf, ("%d",i/10.0));
for (i=0; i<5; i++)
printf("%.1f ",a[i]);
}

(a)Compile-Time Error (b)0.0 0.0 0.0 0.0 0.0 (c)0.0 0.1 0.2 0.3 0.4 (d)1.0 1.0 1.0 1.0 1.0
Ans. (c) Since Comma operator (,) is used, the expressions are evaluated from left to right and
the entire expression assumes the value of the last one evaluated. Also i/10.0 is the rightmost
expression hence it is evaluated and its value is assigned to a[i]. The first printf in the
program has no effect on the output (See next question [Q003]).
_________________________________________________________________________________________________

[Q003]. What will be the output of the following program :
void func()
{
printf("Testing...Done\n");
}
void main()
{
func;
func();
}
(a)Compile-Time Error (b)Testing...Done (c)Testing...Done (d)None of these
Testing...Done
Ans. (b) Since first statement 'func;' has no effect on the output and the second statement
'func();' is a normal function call which prints the message(One time) as specified.
_________________________________________________________________________________________________

[Q004]. A signed int bitfield 1-bit wide can only hold the values

(a)0 and 1 (b)0 and -1 (c)0, 1 and -1 (d)None of these
Ans. (b) Since any non-zero value will be interpreted as -1.
_________________________________________________________________________________________________

[Q005]. What will be the output of the following program :
void main()
{
int a=19,b=4;
float c;
c=a/b;
printf("%f",c);
}

(a)4.75 (b)4 (c)4.750000 (d)4.000000
Ans. (d) Since a and b are both of type int, so the result of a/b was of type int. That was
converted to type float when you assigned it to c, but the conversion took place after the
division, not before.
_________________________________________________________________________________________________

[Q006]. What will be the output of the following program :
void main()
{
int _;
_=70;
printf("%d",_);
}
(a)Compile-Time Error (b)Run-Time Error (c)70 (d)None of these
Ans. (c) Since a variable (an identifier) can start with an underscore.
_________________________________________________________________________________________________

[Q007]. In DOS environment, what is the maximum combined length of the command-line arguments
passed to main (including the space between adjacent arguments and the name of the program
itself).

(a)80 Characters (b)128 Characters (c)Until RETURN KEY (d)None of these
(i.e '\n') is encountered
Ans. (b) Since this is a DOS limit.
_________________________________________________________________________________________________

[Q008]. What will be the output of the following program :
void main()
{
int (*foo)(char *, ...) = printf;
(*foo)("hello, %s", "world!");
}
(a)Compile-Time error (b)hello, world! (c)Run-Time Error (d)None of these
Ans. (b) This is one of the method used for calling functions through pointers. Here foo is a
pointer to a function (printf) that accepts an argument which is a pointer to a character. Also
it accepts variable number of arguments and returns an integer value.
_________________________________________________________________________________________________

[Q009]. What will be the output of the following program :
void main()
{
int i=5,(*foo)(char *, ...);
foo=printf;
printf("%d",i=(*foo)("hello, %s\n", "world!"));
}
(a)Compile-Time error (b)hello, world! (c)hello, world! (d)hello, world!
5 13 14
Ans. (d) This is another method used for calling functions through pointers. Here foo is a
pointer to a function (printf) that accepts an argument which is a pointer to a character. Also
it accepts variable number of arguments and returns an integer value. Also 'printf' returns the
no. of bytes output which is stored in the variable 'i' i.e. i=14 (including '\n' character).
_________________________________________________________________________________________________

[Q010]. What will be the output of the following program :
void main()
{
int choice=2;
switch(choice)
{
default:
printf("Default1");

case 1:
printf("Case1");
break;

default:
printf("Default2");
}
}
(a)Compile-Time Error (b)Default1Case1 (c)Default2 (d)Default1
Ans. (a) Since Too many default cases i.e. the compiler encountered more than one default
statement in a single switch.
_________________________________________________________________________________________________

[Q011]. What is the MAXIMUM LIMIT for cases in a switch statement ?

(a)32767 cases (b)257 cases (c)127 (d)None of these
Ans. (b) Since a switch statement is limited to 257 cases.
_________________________________________________________________________________________________

[Q012]. What will be the output of the following program :
#define big(a,b) a > b ? a : b
#define swap(a,b) temp=a; a=b; b=temp;
void main()
{
int a=3,b=5,temp;
if ((3+big(a,b)) > b)
swap(a,b);
printf("%d %d",a,b);
}
(a)3 0 (b)5 3 (c)3 5 (d)5 0
Ans. (d) Since if ((3+3 > 5 ? 3 : 5) > 5) is FALSE. But a macro is expanded during the compilation
process itself. Thus temp=a; will not be executed and the remaining statements a=b; and b=temp;
are executed in the normal way.
_________________________________________________________________________________________________

[Q013]. What will be the output of the following program :
#define main main()
void main
{
#define END }
printf("First"
"Second"
"Third");
END

(a)Compile-Time Error (b)First (c)FirstSecondThird (d)None of these
Second
Third
Ans. (c) Preprocessor directives can be defined anywhere in the program but just before its use.
Turbo C will automatically do the concatenation for you on very long strings, resulting in nicer
looking programs.
According to K&R (applies to ALL C compilers), a string constant consists of exactly one string
unit, containing double quotes, text, double quotes ("like this"). You must use the backslash(\)
as a continuation character in order to extend a string constant across line boundaries.

Thus the above printf statement could be re-written (K&R Style) as follows :

printf("First" \
"Second" \
"Third");
_________________________________________________________________________________________________

[Q014]. What will be the output of the following program :
void main()
{
long double val;
printf("%d bytes",sizeof(val));
}
(a)Compile-Time Error (b)4 bytes (c)8 bytes (d)10 bytes
Ans. (d) Since sizeof long double is 80 bits i.e 10 bytes (1 byte = 8 bits)
_________________________________________________________________________________________________

[Q015]. What will be the output of the following program :
int * func()
{
int temp=50;
return &temp;
}
void main()
{
int *val;
val = func();
printf("%d",*val);
}
(a)Compile-Time Error (b)50 (c)Garbage Value (d)None of these
Ans. (c) Here the address of the variable 'temp' is returned to the variable 'val' in the main
program but the variable 'temp' is local to the function. As we know every function involves
built-in stack and whenever a function is called Stack Pointer (SP, which is a register pseudo-
variable) value changes [See ALT+W+R Register Window Screen]. Therefore it prints the garbage
value.
_______________________________________________________________

No comments:

ITUCU