Sie sind auf Seite 1von 3

Skill Test in C Aptitude:

1) What is the output of the following code? 2) What is the output of the following program?
#include<stdio.h> #include<stdio.h>
void main( ) void main( )
{ {
char arr[ ] = "\0" ; int a [ ] = { 3, 0x14, 044, 9 };
if ( printf ("%s\n", arr ) ) printf ( "a[2]=%d\n a[1]=%d",a[2],1[a]) ;
printf ( "Nothing\n" ) ; }
else
printf ( "Something\n" ) ;
}

3) What is the output of the following program? 4) What is the output of the following code?
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
printf("%d",3|printf("hi\n")||printf("%s","HELLO\n") int var=5;
); printf("%d",var=++var==6);
} }

5) What is the output of the following code? 6) What is the output of the following code?
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
char prnt[]="%d\n"; int x;
prnt[1]='c'; x=~!printf;
printf(prnt,67); printf("%x",x);
} }

7) What is the output of the following code? 8) What will be printed as the result of the
#include <stdio.h> operation below?
void main() void main()
{ {
signed char i=0; int a=5;
for(;i>=0;i++); printf("%d,%d,%d\n",a,a<<2,a>>2);
printf("%d\n",i); }
}

9) What will be printed as the result of the 10) What will be printed as the result of the
operation below: operation below:
#define swap(a,b) a=a+b;b=a-b;a=a-b; void main()
void main() {
{ char *ptr = "SCSystems";
int m=5, n=10; *ptr++;printf("%s\n",ptr);
swap (m,n); ptr++;
printf("%d %d\n",m,n); printf("%s\n",ptr);
swap2(m,n); }
printf("%d %d\n",m,n);
}
int swap2(int x, int y)
{
int temp;
temp=x;
y=x;
x=temp;
return 0;
}

Solutions:
1) Output: Nothing 2) Output: a[2]=36
Explation: a[1]=20
printf ( ) function return integer value which Explanation:
related to how many total character pass to print. a[0]=3
In our example, a[1]=0x14 = 20 (Hexadecimal value start with 0x)
printf("%s\n",arr) we pass a "\0" string in place of a[2]=044 = 36 (Octal value start with 0)
%s and printf able to print character up to '\0' a[3]= 9
character (never print a null).
When it find a null character it stop to print
character.So, in our case we do not have a any
character before a null character So it is zero,
Then after we pass a '\n' character which is taken as
a single backslashcharacter so printf print a one
character.
So, over all in our case printf print total one
character and for that reason it return one.
So if statement becomes
if ( 1 )
{
}
now any non-zero value taken as a true so in our
example it execute a true block.

3) Output: hi 4) Output: 1
1 Explanation:
Explanation: The resolved expression is var=(++var==6) because
We know that bitwise 'OR' - '|' operator as high of assignment operator as lower precedence.
priority than logical 'OR'-'||' operator. In the inner expression, ++var is evaluated first
So in whole printf statement, which yields 6 and expression becomes 6==6 which
first calculate the 3|printf("hi\n") expression, yields true(1), so final expression becomes
In which printf("hi\n") print the "hi" words on the var=(1),and printf print the value of var which
moniter and change the line by '\n' character is becomes 1 after assignment operation performed.
printing. it also return 3 after printing because
overall it print a three character on the screen.
So our expression becomes a 3|3
And it generate a bitwise opeartion result 3 in plce
of 3|printf("hi\n").
So, overall our expression beacomes like,
printf("%d", 3 || printf("%s","HELLO\n"));
Now it's perform a logical || operator.
In first step 3 taken as true becise of on zero
value.
Logical operator finding first value is true so
overall expression also becomes true.So it is never
check a second value( it is not executed so HELLO is
not print).
now 3 || printf("%s","HELLO\n") expression replace
by true(1)
printf("%d", 1); and it is print a 1

5) Output: C 6) Output: ffff


Explanation: Explanation:
After assignment prnt[1]='c' the string in prnt Here printf returns TRUE value then ! negates that
becomes "%c\n". value into zero and zero's 1's compliment value(-
And in printf statement it is replace the prnt 1) is assigned to x. Here -1 is equal to FFFF in
variable, hexadecimal form.
So, printf statement becomes,
printf("%c\n",67);
And we know that 67 is an ascii value of 'C'
character.

7) Output: -128 8) Output: 5,20,1


Explanation: i is initialized to zero. for loop is
executed till 127 (the positive range of char) and
then it wrap to negative value -128.

9) Output: 10, 5 10) Output: CSystems


10, 5 Systems

Mehul
942845874

Das könnte Ihnen auch gefallen