C For Swimmers
If u don't know how to swim, learn it now. Otherwise participate in
the competition.
Topic : Playing with 'printf()' statement
***************************************************************************
* NOTE : All the programs are tested under Turbo C/C++ compilers.
*
* It is assumed that,
*
* *=> Programs run under DOS environment,
*
* *=> The underlying machine is an x86 system,
*
* *=> Necessary header files are included.
*
* *=> Program is compiled using Turbo C/C++ compiler.
*
* The program output may depend on the information based on this
assumptions.
* (For example sizeof(int) = 2 bytes may be assumed).
***************************************************************************
[Q001]. What will be the output of the following program :
void main()
{
printf();
}
(a)Run-Time Error (b)Compile-Time Error (c)No Output
(d)None of these
Ans. (b) Since there must be enough arguments for the format.
___________________________________________________________________________
[Q002]. What will be the output of the following program :
void main()
{
printf(NULL);
}
(a)Run-Time Error (b)Compile-Time Error (c)No Output
(d)None of these
Ans. (c) Since NULL is a constant value or NULL pointer value or a NULL
string.
___________________________________________________________________________
[Q003]. What will be the output of the following program :
void main()
{
printf("%%",7);
}
(a)7 (b)Compile-Time Error (c)%
(d)%%
Ans. (c) Since % is a format specifier & excess arguments (more than
required by the format) are
merely ignored.
___________________________________________________________________________
[Q004]. What will be the output of the following program :
void main()
{
printf("//",5);
}
(a)5 (b)Compile-Time Error (c)/
(d)//
Ans. (d) Since / is an escape sequence character & excess arguments (more
than required by the format) are merely ignored.
___________________________________________________________________________
______________________
[Q005]. What will be the output of the following program :
void main()
{
printf("d%",8);
}
(a)8 (b)Compile-Time Error (c)d%
(d)None of these
Ans. (c) Since excess arguments (more than required by the format) are
merely ignored.
___________________________________________________________________________
[Q006]. What will be the output of the following program :
void main()
{
printf("%d"+0,123);
}
(a)123 (b)Compile-Time Error (c)No Output
(d)None of these
Ans. (a) "%d"+0 has no effect on the output operation.
___________________________________________________________________________
[Q007]. What will be the output of the following program :
void main()
{
printf("%d"+1,123);
}
(a)123 (b)Compile-Time Error (c)d
(d)No Output
Ans. (c) "%d"+1 (or > 0) affects the program output by considering "%d" as
string and ignores 123
Where 1 refers to the index i.e. 2nd character in the array or string "%d".
___________________________________________________________________________
[Q008]. What will be the output of the following program :
void main()
{
printf("%d",printf("Hi!")+printf("Bye"));
}
(a)ByeHi!6 (b)Hi!Bye6 (c)Compile-Time Error
(d)None of these
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is
3+3=6
___________________________________________________________________________
______________________
[Q009]. What will be the output of the following program :
void main()
{
printf("%d",printf("Hi!")*printf("Bye"));
}
(a)ByeHi!6 (b)Hi!Bye9 (c)Hi!Bye
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is
3*3=9
___________________________________________________________________________
[Q010]. What will be the output of the following program :
void main()
{
printf("%d",printf("")+printf(""));
}
(a)0 (b)No Output (c)Compile-Time Error
(d)None of these
Ans. (a) Since L->R priority & the length of the 2 empty strings are :
0+0=0
___________________________________________________________________________
[Q011]. What will be the output of the following program :
void main()
{
printf("Hi Friends"+3);
}
(a)Hi Friends (b)Friends (c)Hi Friends3
(d)None of these
Ans. (b) Since (base adress)+0 points to the value 'H'. Now the NEW (base
address) equals
(base address)+3 that points to the character 'F'. Thus it prints the
string from 'F' onwards.
___________________________________________________________________________
[Q012]. What will be the output of the following program :
void main()
{
printf("C For ") + printf("Swimmers");
}
(a)Compile-Time Error (b)C For Swimmers (c)Run-Time Error
(d)None of these
Ans. (b) It is a VALID C statement. Change the operators but the output
remains same(NO EFFECT).
___________________________________________________________________________
[Q013]. What will be the output of the following program :
void main()
{
printf("\/\*\-*\/");
}
(a)Run-Time Error (b)\/*-*\/ (c)/*-*/
(d)None of these
Ans. (c) Since \ is an escape sequence character. Be careful while
analyzing such statements.
___________________________________________________________________________
[Q014]. What will be the output of the following program :
int main()
{
int main=7;
{
printf("%d",main);
return main;
}
printf("Bye");
}
(a)Compile-Time Error (b)Run-Time Error (c)7Bye
(d)7
Ans. (d) It is a VALID C statement. Prints 7 and returns the same to the
OS. NOTE: Last printf
statement will not be executed.
___________________________________________________________________________
[Q015]. What will be the output of the following program :
void main()
{
main();
}
(a)Compile-Time Error (b)Run-Time Error (c)Infinite Loop
(d)None of these
Ans. (c) It is a VALID C statement. It is like a recursive function & the
statements get executed
infinite number of times.
___________________________________________________________________________
[Q016]. What will be the output of the following program :
void main()
{
printf("Work" "Hard");
}
(a)Work (b)Hard (c)No Output
(d)WorkHard
Ans. (d) Since L->R priority. First it prints the word 'Work' & then
'Hard'.
___________________________________________________________________________
[Q017]. What will be the output of the following program :
void main()
{
char str[]="%d";
int val=25;
printf(str,val);
}
(a)Compile-Time Error (b)Run-Time Error (c)25
(d)None of these
Ans. (c) It is a VALID C statement. First parameter contains the format
specifier & the Second
parameter contains the actual value 25.
___________________________________________________________________________
[Q018]. What will be the output of the following program :
void main()
{
int val=75;
printf("%d",val,.,.);
}
(a)Compile-Time Error (b)Unpredictable (c)75
(d)None of these
Ans. (b) Output is Unpredictable B'coz there are not enough arguments for
the format. But it is a
VALID C statement.
___________________________________________________________________________
[Q019]. What will be the output of the following program :
void main()
{
int val=10;
printf("%d",val+1,"%d",val--);
}
(a)10 (b)11 10 (c)11 9
(d)10 9
Ans. (a) Since R->L priority. The second format specifier '%d' is an excess
argument and it is
ignored.
___________________________________________________________________________
[Q020]. What will be the output of the following program :
void main()
{
int val=5;
printf("%d %d %d %d",val,--val,++val,val--);
}
(a)3 4 6 5 (b)5 5 6 5 (c)4 4 5 5
(d)None of these
Ans. (c) Since R->L priority.
___________________________________________________________________________
[Q021]. What will be the output of the following program :
void main()
{
int val=5,num;
printf("%d",scanf("%d %d",&val,&num));
}
[NOTE : ASSUME 2 values are entered by the user are stored in the variables
'val' & 'num' respectively.]
(a)1 (b)2 (c)5
(d)None of these
Ans. (b) Since scanf statement returns the number of input fields
successfully scanned, converted
& stored.
___________________________________________________________________________
[Q022]. What will be the output of the following program :
#define Compute(x,y,z) (x+y-z)
void main()
{
int x=2,y=3,z=4;
printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z)));
}
(a)40 (b)30 (c)Compile-Time Error
(d)None of these
Ans. (b) Since it is macro function. NOTE : Be careful while doing such
type of calculations.
___________________________________________________________________________
[Q023]. What will be the output of the following program :
void main()
{
int m=10,n=20;
printf("%d %d %d",m/* m-value */,/* n-value */n,m*/* Compute m*n
*/n);
}
(a)Run-Time Error (b)10 20 200 (c)Compile-Time Error
(d)None of these
Ans. (b) Since comments /*...*/ are ignored by the compiler.
___________________________________________________________________________
[Q024]. What will be the output of the following program :
void main()
{
int m=10,n=20;
/* printf("%d",m*n);
}
(a)VALID but No Output (b)VALID : Prints 200 (c)Compile-Time Error
(d)None of these
Ans. (c) Since COMMENT statement not ended properly i.e */ is missing in
the above program.
___________________________________________________________________________
[Q025]. What will be the output of the following program :
void main()
{
int val=97;
"Printing..."+printf("%c",val);
}
(a)Printing...97 (b)97 (c)Compile-Time Error
(d)a
Ans. (d) Since alphabet 'a' is the ASCII equivalent of 97.
___________________________________________________________________________
[Q026]. What will be the output of the following program :
void main()
{
int val=5;
val=printf("C") + printf("Skills");
printf("%d",val);
}
(a)Skills5 (b)C1 (c)Compile-Time Error
(d)CSkills7
Ans. (d) VALID Since 'printf' function return the no. of bytes output.
___________________________________________________________________________
[Q027]. What will be the output of the following program :
void main()
{
char str[]="Test";
if ((printf("%s",str)) == 4)
printf("Success");
else
printf("Failure");
}
(a)TestFailure (b)TestSuccess (c)Compile-Time Error
(d)Test
Ans. (b) VALID Since 'printf' function return the no. of bytes output.
___________________________________________________________________________
[Q028]. What will be the output of the following program :
void main()
{
int val=5;
printf("%*d",val);
}
(a) 5 (b)5 (c)Compile-Time Error
(d)None of these
Ans. (a) VALID Since '*' specifies the precision (i.e. the next argument in
the precision). If no
precision is specified then the value itself will be the precision value.
Thus it prints 5 BLANK
SPACES & then the value 5.
___________________________________________________________________________
[Q029]. What will be the output of the following program :
void main()
{
int val=5;
printf("%d5",val);
}
(a)Compile-Time Error (b)5 (c)55
(d) 5
Ans. (c)
___________________________________________________________________________
[Q030]. What will be the output of the following program :
void main()
}
int val=5;
printf("%d",5+val++);
{
(a)Compile-Time Error (b)5 (c)10
(d)11
Ans. (a) Since incorrect usage of pair of braces } and {. Correct usage :
Each compound statement
should be enclosed within a pair of braces, i.e { and }.
___________________________________________________________________________
Thanx for using "C For Swimmers".
Regarding this material, you can send Bug Reports, Suggestions,
Comments,etc. to jayukudari@gmail.com
No comments:
Post a Comment