1. Find the output.
void main()
{
int i=5;
do
{
putchar(i+100);
printf("%d",i--);
}while(i);
}
Ans: i5h43f2el
Explanation: The loop will continue till 5 times and the characters corresponding to that ASCII value get printed with the value of i.
2. Find the output.
void main()
{
int i=3;
while(i--)
{
int i=100;
i--;
printf("%d",i);
}
}
Ans: 999999
Explanation: Here i is an auto variable and the scope of the auto variabnle is local to a block. So every time the control gets into the loop one new i will be created. i with in the while expression is local to the first block.
3. What is the problem in the following code?
static int i=50;
main()
{
int sum=0;
do
{
sum+=(1/i);
printf("Sum of series is %d",i);
}while(0<i--)
printf("Sum of series is %d",sum);
}
Ans: Divided Error
Explanation: Here i is declared as static so it has file scope. The value of i is decremented. So when the i vlaue is zero the expression inside body of the loop will be sum=sum+(1/0); so here the divide error occurs.
4.How many times the loop will continue and find the output?
int main()
{
int i=5,j=0;
while(i--||j++)
printf("\n%d%d",i,j);
return 0;
}
Ans: 40 30 20 10 00
Explanation: The loop will continue five times and the output is 40 30 20 10 00 consider the expression a||b, if a evaluates as true then b will not be executed. Same thing is happening here. i-- is true so j++ is not executed and the value of j remains 0.
5. How many times the loop will continue and find the putput?
void main()
{
int a,b,c,d,e;
a=b=c=d=e=12345.5;
if(d=a==b==c)
printf("Good reply');
while(d>0)
{
d--;
if(e=1)
printf("continue");
if (e==1)
printf("I have learnt c
from lakshya");
}
}
Ans: No output
Explanation: First, a==b is ecaluated which returns 1 then 1==c will get evaluated which returns 0 and stored in d. That's why in the if and while statement the condition is false and hence no output
6. What is the problem in the followinr code?
void main()
{
int i;
for(i=1;i<=10;i++)
printf("%d",i);
if(i==5)
break;
}
Ans: No output
Explanation: First the value of as [i] is '\\' so the first case gets satisfied and prints A in the screen, In the second iteration the value in as [i] is '0' i.e. 48, so none of the case get satisfied and the default case get executed which prints C, the condition in the while loop becomes false and the loop terminates.
7.Find the output.
void main()
{
char as[]="\\0\0";
int i=0;
do
{
switch(as[i++])
{
case '\\' : printf("A");
break;
case 0 : printf("B");
break;
default : printf("C");
}
}while(as[i]!='\0');
}
Ans: AC
Explanation: First the value of as [i] is'\\' so the first case gets satisfied and prints A in the screen, In the second iteration the value in as [i] is '0' i.e. 48, so none of the case get satisfied and the default case get executed which prints C, the condition in the while loop becomes false and the loop terminates.
8. What is the problem in the following code?
void main()
{
int k=1;
while(k<=5)
{
printf("%d",k);
if(k>2)
goto here;
}
}
fun()
{
here:
printf("If it works dot's fix it.");
}
Ans: Compilation error
Explanation: goto has functional scope. so the label should be present inside the same function where goto statement is used.
9.Find the output.
void main()
{
int i;
for(i=0;i<=4;i++)
{
if(i)
continue;
else
printf("Lakshya");
}
}
Ans: Lakshya
Explanation: continue keyword takes the control to the beginning of the loop. First i value is 0 so else part gets executed and prints lakshya. After first eteration the value of i is incremented and if condition will be always true.
10. Find the output.
void main()
{
char i;
for(i=1;i<=150;i++)
printf("%d",i);
}
Ans: Infinite loop
Explanation: As i is the signed character (default is signed) its maximum limit is 127. so the value of i will be always less than 150. Hence the condition is always true.
11. Do you find anything on the screen? Why dose this happens?
void main()
{
int k=263,p=0;
while(p<5)
{
putchar(k);
p++;
}
}
Ans: It makes beep sound 5 times
Explanation: putchar() puts the character on the screen. As k value is greater than 255, due to the presence of cycle in chat data type it will wrap around to 7 which makes beep sound. Here while loop will be executed 5 timed, hence make beep sound 5timed.
12. Find the output.
void main()
{
int k=1;
;akshya: if (k<=3)
{
putchar(k);
k++;
goto lakshya;
}
}
Ans: Garbage output
Explanation: goto takes the control to the label specified. This loop will continue 3 times and the characters having ASCII value 1,2,3 gets printed.
13.Find the output.
void main()
{
goto start;
{
int a;
a=5+5;
start: printf("%d',a);
}
}
Ans: Garbage value
Explanation: goto takes the control to the label specified. Here the instruction a=5*5 will not be executed. Hence the output is garbage.
14.How maney times the following loop will continue?
void main()
{
while('a'<'b')
printf("Malayalam is palindrom");
}
Ans: Infinite loop
Explanation: The ASCII values of character a,b are 48 and 49 respectively. Character constants are stored as the ASCII value format. So the condition of the while expression is always true, hence enter to infinite loop.
15. How many times the loop will run and find the output?
void main()
{
for(putchar('c');putchar(''a);
putchar('r'))
putchar('t');
}
Ans: Infinite loop
Explanation: The return value of putchar(int c ) is the character give by c. The step value of for loop is putchar('a'); which always return 97 which evaluates as true value hence result is infinite loop.
16. Find the output.
void main()
{
int i=50,j=0;
for(;i<2,j<5;i+=10,j+=4);
printf("i=%d,j=%d",i++,j++);
}
Ans: i=70 j=8
Explanation:There is semicolon at the end of the for loop so there is no statement inside the for loop. The condition in for loop is valuated for j value not fot i value and after the loopterminates the values of i j are 70 and 8 respectively.
17. Find how many times d will be incremented.
void main()
{
int i,j,k,d=0;
for(i=1;i<31;++j)
for(j=1;j<31;++j)
for(k=1;k<31;++k)
if (((i+j+k)%3)==0)
d=d+1;
printf("%d",d);
}
Ans: 9000
Explanation: The if statement gets executed 2700 times but the condition (i+j+k)%3 will be true for 9000 times and d is incremented 9000 times.
18. What is the problem in the following code?
void main()
{
int i;
char ch 'a';
do
{
printf("%c",ch);
i++;
}while(i<=5 ||ch<='f');
}
Ans: prints a infinite times.
Explanation: Here, only i is incrementing and ch is not incrementing. The condition i<=5||ch<'f' is always true, since the ASCII value of 'a' is always less than 'f'
19. Find the output.
void main()
{
char *str="hello";
char *ptr=str;
char least=127;
while(*ptr++)
{
least=(*ptr<least)?*ptr:lesat;
}
printf("%d",least);
}
Ans: 0
Explanation: First the value of least is 127 and it is compared with *ptr i.e. 101(ASCII value of e), condition is true, so 101 is assigneed to least. Then never the condition satisfies and always the least value is again assigned to least. When the value of *ptr is NULL then only the condition in the ternary operator gets satisfied and the value of *ptr i.e. 0 is assigned to least. Now thewhile loop terminates as now the condition of while loop is false and the output is 0.
20.Find the output.
void main()
{
intx,y;
for(y=1;(x=y)<10;x++)
printf("&d %d",x,y);
}
Ans: prints 1 infinite time.
Explanation: Here in the condition part of the loop, y is first assigned to x and then the condition checking is done. only x is incrementing, y value is always 1 so always 1 is assigned to x which is smaller then 10. This results in infinite printing of 1.
21. What is the problem in the following code?
#define loop i<5;
void main()
{
int i;
for(i=0;loopi++)
printf("%d",i);
}
Ans: Undefined symbol loopi.
Explanation: Here the preprocessor searches for the identifier loop to replace it with i<5; but as there is no space between loop and i++ the compiler finds a new identifier loopi and doesn't replace it with i<5; as there is no declaration for the variable loopi compiler flags an error message.
22. How to implement loop to add btween two numbers?
Explanation: Here is the program to add any two numbers using loop.
void main()
{
int a,b,c1=1,c2=2,c3=3;
printf("Enter any two number");
scanf("%d %d",&a,&b);
c3=b;
while (c1++<=a)
{
if(c2++<=b)
c3++;
}
printf("Addition is %d",c3);
}
23.Write the missing statement so that the output will be 2 2 2 2 2 2 3 4 6 5.
void main()
{
/*add here*/
int j,*p=c,*q=c;
for(j=0;j<=5;j++)
{
printf("%d",*c);
++q;
}
for(j=0;j<5;j++)
{
printf("%d",*p);
++p;
}
}
Ans: int c[]={2,3,4,5,6};
Explanation: The first for loop prints the value present at base address of array c (i.e prints 2 five times). The second for loop is printing the value pointed by p which is an integer pointer and every times the loop continues it get incremented so the whole array elements get perinted.
24. Usually loop(do-while, while and for) runs or stops only after checking a condition after every step. But can we make such loop that, the condition will be check when we will want.
Explanation:
while (1)
{
if (kbhit()) //if any key is pressed, then control entres into the loop
{
if(getch()==27) //ASCII value of esc is 27, if Escape character is
return(1); //pressed it goes out of the loop.
}
}
25. Write the missing statement to reverse any number.
void main()
{
int n,r=0;
printf("\nEnter a number");
scanf("%d",&n);
while(n>=1)
{
r=r*10+n%10;
}
printf("%d",r);
}
Ans: n=n/10
Explanation: in the body of the while loop, add n=n/10 after this (r=r*10+n%10) expression.
26. Write the missing statement that the loop will continue 5 time.
void main()
{
int i;
for(i=1;i<=10;i++)
printf("helio");
if(i==5)
break;
}
Ans:There should be curly braces of the for loop enclosing the three statements after the loop.
Explanation:
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf("hello");
if(i==5)
break;
}
}
27. Find the output.
#define TRUE 0/1
void main()
{
while(TRUE)
{
printf("%d",TRUE);
}
}
Ans: no output
Explanation: TRUE in the while condition expression is replaced by 0/1. So it evaluates to false. That's why it will go out of the loop and nothing will be displayed.
28. Find the output.
void main()
{
short int i;
for(i=1;i<=35560;i++)
{
if (i==33000)
break;
else
printf("Hello");
}
}
Ans: prints Hello infinite times.
Explanation: the maximum value of a signed integer is 32767. When it will be incremented beyond 32767, it will enter to the integer cycle as(32768 will be -1). So the value of i will be always less than 35560, hence infinite loop.
29. Find the output.
void main()
{
int a=5;
do
{
printf("%d %d",a,a=0);
}while(a>0);
}
Ans: 0 0
Explanation: when there is multiple statements inside the printf function. It will be executed from right to left. When compiler will execute the printf function, a assigned to 0, so first time print 0 0. After that the condition in the while expression will be evaluates as false and go out of the loop.
30. Find the output.
void main()
{
int loop=0;
switch(loop)
{
case 0:
do
{
case 1: loop++;
case 2: ++loop;
}while(-loop==0);
}
printf("%d",loop);
}
Ans: 1
Explanation: Inside the switch case structure, control will jump to case 0: After that inside the do-while loop, it will execute both case 1 and 2. Now the value of loop is 2.When it test the condition in the while exptession it will be false, hence go out of the loop and print 1
31. Find the output.
void main()
{
char *p="abc";
char *q="abc123";
while(*p++=*q++)
printf("%c %c",*p,*q);
}
Ans: bbcc 1a2b3c
Explanation: String "abd" is stored as abc\0 and q will be stored as abc 123 \0 immediately after p as both of the strings are created in contiguous memory location. When the control goes to the condition part in the while loop *p++=*q++, 1st *p will be assigned with *q and then both will get incremented by 1 position. So 1st a will be overwritten with a and the printf statement will print b. The loop continues until *p is assigned with NULL. As both the strings saved in contiguous memory location the output is bbcc 1a2b3c
32.Find the output.
main()
{
char n[]="lakshya";
int y;
y=0;
while(n[y])
{
printf("%c%c%c",n[y],
*(n+y),*(Y+n),y[n]);
y++;
}
}
Ans: llll
aaaa
kkkk
ssss
hhhh
yyyy
aaaa
Explanation: Here n[y], *(n+y),*(y+n),y[n] all of these refer to the same element. The loop will continue 7 times and all the characters gets printed four times each.
33. Find the output.
main()
{
char str [10]={0,0,0,0,0};
char *s;
int x;
s=str;
for(x=0;x<5;x++)
{
if(*s)
{
printf("%c",*s);
s++;
}
}
}
Ans: No output
Explanation: All the elements of the string str is initialized with 0. The for loop will continue 5 times, but if condition will always fail and the control will never go inside if statement, hence no output.
34. Write the missing statement of the following program.
void main()
{
int show();
int (*p)();
(*p)();
}
int show()
{
int i;
for(i=1;i<=5;i++)
printf("Hello\n");
}
Ans: p=show;
Explanation: Here p is a pointer to function returning an int. p is called before assigned the address of function show. so to get the desired output p should be initialized show. So to get the desired output p should be initialized with the address of function show.
35. Write the missing statement of the following program.
void main()
{
int a[]={4,67,2,6,8};
int i,j,t;
for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
if(a[i]>a[j])
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<5;i++)
printf("%d",a[i]);
}
Ans: Curly braces
Explanation: t=a[i];a[i]=a[j];a[j]=t;
These three statements should be inside curly braces to get the array sorted. if no curly braces are given then inside if only one statement gets executed i.e. t=a[i];
36. Make the following program more effcient.
void main()
{
int i;
for(i=1;i<=5;i++)
show();
}
int show()
{
printf("C Marathon");
}
Ans:
void main()
{
show ();
}
int show()
{
int i;
for(i=1;i<=5;i++)
printf("\nc marathon");
}
Explanation: More you call a function slower the execution of the program. Here in the above program show function is repeatedly called 5 times. To overcome this problem write the following code, it will be more efficient.
37. Convert the following program into function recursion.
void main()
{
int x;
x=unknown(5);
printf("%d",x);
}
int unknow (int n)
{
static int f=1;
while (n>=1)
{
f=f*n;
n-;
}
return f;
}
Explanation:
void main()
{
int x;
x=unknown(5);
printf("%d",x)'
}
int unknown(int n)
{
static int f=1;
if (n==1)
return 1;
else
f=n*unknown (n-1);
return f;
}
38. Find the output.
void main()
{
char ch;
for(ch='0';ch<='\377';ch++)
printf("%d",ch);
}
Ans: No output
Explanation:The decimal value of '\377'is 255. But in the for loop. the value of ch i.e. 48 is compared with '\377',it will be converted to signed value i.e. -1. so the condition in the for loop is not satisfied, hence no output.
39. How many times the following loop will execute?
void main()
{
short int x=1;
unsigned long int i=1;
while(x)
{
printf("\nc Marathon");
x++;
}
}
Ans: 65535
Explanation: As x is a short interger it will increment fot 1 to 32767 and then from -32768 to 0.When x becomes 0 condition becomes false and the loop terminates. So c marathan gets printed 655535 times.
40. Find the output.
void main()
{
unsigned int k=987,i=0;
char trans[10];
do
{
trans[i++]=(k%16-'0');
}while(k/=16);
printf("%s",trans);
}
Ans: b d garbage
Explanation: Here first k% 16 is evaluated that is 987%16 which equals to 11.As 11 is greater than 9 condition becomes true and expression (k%16-10+'a')is evaluated which result in 66 and 66 gets stored in trans[0]. Then in condition part of the do while loop k/=16 is evaluated which results in 61. condition is true so again k%16 is evaluated which results 13 which is also greater than 9 again the condition is true and the expression(k%16-10+'a') is evaluated which results in 68 and it gets stored in trans [1]. then condition part k/=16 is evaluated which results in 3. In the do while loop k%16 is evaluated which also result in 3 now condition is false and the expression (k%16-'0') gets evaluated and results in -45 and -45 gets stored in trans[2]. Then k/=16is evaluated which result in 0 and the loop terminates. The printf statement prints the contents of the array i.e. b d and garbage.
41. Find the output.
void main()
{
int i=6,j=4;
printf("N");
switch(i)
{
do
{
case 1: printf("y");
case 2:
case 3:
case 4:
case 5:
case 6: j-;
}while(j);
}
}
Ans: NYYY
Explanation: First Ngets printed and when it execute the switch statement, it directly jumps to case 6(as i=6) gets satisfied so j is decremented and the value of j becomes 3. Now the condition in the do-while expression is evaluated true 3 times for (j=3,2,1)so y will get printed 3 times. After that it will go out of the do-while loop and also from switch expression.
42.Find the output.
void main()
{
char input[]="SSSWILTECH1\1\1";
int i,c;
for(i=2;(c=input[i])!='\0';i++)
{
switch(c)
{
case 'a' :putcher ('i');
continue;
case '1' : break;
case 1: while((c=input[++i])
!='\1'&&c!='\0');
case 9: putchar('s');
case 'E': putchar(c);
continue;
}
putchar(' ');
}
putchar('\n');
}
Ans:SWITCH S
Explanation: Here continue is not working for switch case it is working with for loop. The for loop is running and whenever the case is not matching in the switch it executes the default case that prints the character in the screen. And whenever the case containing default is executed then it takes the control to the starting of the for loop.
43. If A is an array with n elements and procedure swap exchanges its arguments. then the following code segment sorts A in descending order.
void main()
{
int j,k;
for(j=0;j<n-1;j++)
for(k=0;k<n-j-1;k++)
if (A[k]<a[k+1])
counter++;
printf("%d",counter);
swap(A[k],A[k+1]);
}
How many calls to swap are made if initially, A[i] = i, for i= 0,1,2,.....,n-1?
Explanation: for n is odd: FLOOR (n/2)*n //hints: FLOOR returns next lowest integer below number
n is even: n*(n-1)/2
44.Find the output.
void main()
{
int n=0;
while(+(+n-)!=0)
n-=n++;
n-=n++;
printf("%d",n);
}
Ans: 1
Explanation: Here the value of n is 0, in the while loop condition is false as there is post increment operator so the control will not enter the while loop. Now the value of n is -1 the expression n-=n++ expands as n=n-n++, here n is assigned with 0 (-1-(-1)) but just after the instruction the post increment operator come into the picture and it increments the value of i to 1. So the output is 1.
45.Find the output.
void main()
{
static a[]={1,2,3,4,5,6,7,8};
int i;
for(i=2;i<6;++i)
a[a[i]]=a[i];
for(i=0;i<8;++i)
printf("%d",a[i]);
}
Ans: 1 2 3 3 5 5 7 8
Explanation: First the value of i is 2 so the expression a[a[i]=a[i] evaluates to a[a[2]]=a[2]; that is a [3]=a[2]; a[3]=3; so 3 is stored in a[3]. similarly a[3]=3;a[5]=5;a[5]=5; The next for loop prints the value of the array i.e. 1 2 3 3 5 5 7 8
46. Find the output.
void main()
{
char i=13;
while(i)
{
i++;
}
printf("%d",i);
}
Ans: 0
Explanation: Here the value of i will be incremented until it reaches 0. So the loop will continue until the value of i becomes 0.
47.Find the output.
void main()
{
int i=0;
for(;;)
{
i++;
while(1)
{
i++;
if(i==4)
break;
}
if(i==5)
break;
}
}
Ans: Infinite loop
Explanation: Here the control will get loop and then into the while loop when the value of i becomes 4 then it comes out of the while loop the condition in the if is checked which is false then in the for loop the value of i gets incremented and again it enters into the while loop. i value is greater than 4 so condition is false and it becomes an infinite loop.
48.What is the problem in the following code?
void main()
{
static int i;
for(;;)
{
if(i!=printf(9876))
{
i++;
}
else
{
while(1)
{
break;
break;
}
}
}
printf("%d",i);
}
Ans: output will be garbage value infinite times.
Explanation: The problem is the control gets struck in the outer loop that is the for loop. There is no terminator condition in the for loop.
49. Find the output when input is 25.
void main()
{
int su=0;
for(scanf("%d",&su);printf
("%d",su)-1;printf("%d"))
{
su=su/10;
}
}
Ans: 2522
Explanation: Here the statemint scanf will input the value 25 to variable su. The condition part contains a printf statement that will print the value 25 and returns the value 2. so condition is true and the su is now modified to 2, the statement printf("%d"); will print the value present in the stack which is 2 and then condition is again checked, the printf statement in condition part willprint the value 2 and returns 1 which is subtracted by 1, so the condition becomes false and the loop terminates resulting output 2522.
50.Find the output.
void main()
{
int i=0;
switch(0)
{
printf("%d",i);
i++;
}
for(;i;)
{
printf("%d",i);
}
while(i)
{
printf("%d",i);
}
do
{
printf("%d",i);
}while(i);
}
Ans : 0
Explanation: since there is no case inside switch statement, control will not enter inside the switch. in the for loop, the condition part is 0, so also not enter into the for loop. In the while loop the condition part is also false, that's why it will not also enter inside the while loop. But for the do-while loop, at least it will execute once, hence output is 0. when it test condition of the do-while loop, again it goes out of the loop.
void main()
{
int i=5;
do
{
putchar(i+100);
printf("%d",i--);
}while(i);
}
Ans: i5h43f2el
Explanation: The loop will continue till 5 times and the characters corresponding to that ASCII value get printed with the value of i.
2. Find the output.
void main()
{
int i=3;
while(i--)
{
int i=100;
i--;
printf("%d",i);
}
}
Ans: 999999
Explanation: Here i is an auto variable and the scope of the auto variabnle is local to a block. So every time the control gets into the loop one new i will be created. i with in the while expression is local to the first block.
3. What is the problem in the following code?
static int i=50;
main()
{
int sum=0;
do
{
sum+=(1/i);
printf("Sum of series is %d",i);
}while(0<i--)
printf("Sum of series is %d",sum);
}
Ans: Divided Error
Explanation: Here i is declared as static so it has file scope. The value of i is decremented. So when the i vlaue is zero the expression inside body of the loop will be sum=sum+(1/0); so here the divide error occurs.
4.How many times the loop will continue and find the output?
int main()
{
int i=5,j=0;
while(i--||j++)
printf("\n%d%d",i,j);
return 0;
}
Ans: 40 30 20 10 00
Explanation: The loop will continue five times and the output is 40 30 20 10 00 consider the expression a||b, if a evaluates as true then b will not be executed. Same thing is happening here. i-- is true so j++ is not executed and the value of j remains 0.
5. How many times the loop will continue and find the putput?
void main()
{
int a,b,c,d,e;
a=b=c=d=e=12345.5;
if(d=a==b==c)
printf("Good reply');
while(d>0)
{
d--;
if(e=1)
printf("continue");
if (e==1)
printf("I have learnt c
from lakshya");
}
}
Ans: No output
Explanation: First, a==b is ecaluated which returns 1 then 1==c will get evaluated which returns 0 and stored in d. That's why in the if and while statement the condition is false and hence no output
6. What is the problem in the followinr code?
void main()
{
int i;
for(i=1;i<=10;i++)
printf("%d",i);
if(i==5)
break;
}
Ans: No output
Explanation: First the value of as [i] is '\\' so the first case gets satisfied and prints A in the screen, In the second iteration the value in as [i] is '0' i.e. 48, so none of the case get satisfied and the default case get executed which prints C, the condition in the while loop becomes false and the loop terminates.
7.Find the output.
void main()
{
char as[]="\\0\0";
int i=0;
do
{
switch(as[i++])
{
case '\\' : printf("A");
break;
case 0 : printf("B");
break;
default : printf("C");
}
}while(as[i]!='\0');
}
Ans: AC
Explanation: First the value of as [i] is'\\' so the first case gets satisfied and prints A in the screen, In the second iteration the value in as [i] is '0' i.e. 48, so none of the case get satisfied and the default case get executed which prints C, the condition in the while loop becomes false and the loop terminates.
8. What is the problem in the following code?
void main()
{
int k=1;
while(k<=5)
{
printf("%d",k);
if(k>2)
goto here;
}
}
fun()
{
here:
printf("If it works dot's fix it.");
}
Ans: Compilation error
Explanation: goto has functional scope. so the label should be present inside the same function where goto statement is used.
9.Find the output.
void main()
{
int i;
for(i=0;i<=4;i++)
{
if(i)
continue;
else
printf("Lakshya");
}
}
Ans: Lakshya
Explanation: continue keyword takes the control to the beginning of the loop. First i value is 0 so else part gets executed and prints lakshya. After first eteration the value of i is incremented and if condition will be always true.
10. Find the output.
void main()
{
char i;
for(i=1;i<=150;i++)
printf("%d",i);
}
Ans: Infinite loop
Explanation: As i is the signed character (default is signed) its maximum limit is 127. so the value of i will be always less than 150. Hence the condition is always true.
11. Do you find anything on the screen? Why dose this happens?
void main()
{
int k=263,p=0;
while(p<5)
{
putchar(k);
p++;
}
}
Ans: It makes beep sound 5 times
Explanation: putchar() puts the character on the screen. As k value is greater than 255, due to the presence of cycle in chat data type it will wrap around to 7 which makes beep sound. Here while loop will be executed 5 timed, hence make beep sound 5timed.
12. Find the output.
void main()
{
int k=1;
;akshya: if (k<=3)
{
putchar(k);
k++;
goto lakshya;
}
}
Ans: Garbage output
Explanation: goto takes the control to the label specified. This loop will continue 3 times and the characters having ASCII value 1,2,3 gets printed.
13.Find the output.
void main()
{
goto start;
{
int a;
a=5+5;
start: printf("%d',a);
}
}
Ans: Garbage value
Explanation: goto takes the control to the label specified. Here the instruction a=5*5 will not be executed. Hence the output is garbage.
14.How maney times the following loop will continue?
void main()
{
while('a'<'b')
printf("Malayalam is palindrom");
}
Ans: Infinite loop
Explanation: The ASCII values of character a,b are 48 and 49 respectively. Character constants are stored as the ASCII value format. So the condition of the while expression is always true, hence enter to infinite loop.
15. How many times the loop will run and find the output?
void main()
{
for(putchar('c');putchar(''a);
putchar('r'))
putchar('t');
}
Ans: Infinite loop
Explanation: The return value of putchar(int c ) is the character give by c. The step value of for loop is putchar('a'); which always return 97 which evaluates as true value hence result is infinite loop.
16. Find the output.
void main()
{
int i=50,j=0;
for(;i<2,j<5;i+=10,j+=4);
printf("i=%d,j=%d",i++,j++);
}
Ans: i=70 j=8
Explanation:There is semicolon at the end of the for loop so there is no statement inside the for loop. The condition in for loop is valuated for j value not fot i value and after the loopterminates the values of i j are 70 and 8 respectively.
17. Find how many times d will be incremented.
void main()
{
int i,j,k,d=0;
for(i=1;i<31;++j)
for(j=1;j<31;++j)
for(k=1;k<31;++k)
if (((i+j+k)%3)==0)
d=d+1;
printf("%d",d);
}
Ans: 9000
Explanation: The if statement gets executed 2700 times but the condition (i+j+k)%3 will be true for 9000 times and d is incremented 9000 times.
18. What is the problem in the following code?
void main()
{
int i;
char ch 'a';
do
{
printf("%c",ch);
i++;
}while(i<=5 ||ch<='f');
}
Ans: prints a infinite times.
Explanation: Here, only i is incrementing and ch is not incrementing. The condition i<=5||ch<'f' is always true, since the ASCII value of 'a' is always less than 'f'
19. Find the output.
void main()
{
char *str="hello";
char *ptr=str;
char least=127;
while(*ptr++)
{
least=(*ptr<least)?*ptr:lesat;
}
printf("%d",least);
}
Ans: 0
Explanation: First the value of least is 127 and it is compared with *ptr i.e. 101(ASCII value of e), condition is true, so 101 is assigneed to least. Then never the condition satisfies and always the least value is again assigned to least. When the value of *ptr is NULL then only the condition in the ternary operator gets satisfied and the value of *ptr i.e. 0 is assigned to least. Now thewhile loop terminates as now the condition of while loop is false and the output is 0.
20.Find the output.
void main()
{
intx,y;
for(y=1;(x=y)<10;x++)
printf("&d %d",x,y);
}
Ans: prints 1 infinite time.
Explanation: Here in the condition part of the loop, y is first assigned to x and then the condition checking is done. only x is incrementing, y value is always 1 so always 1 is assigned to x which is smaller then 10. This results in infinite printing of 1.
21. What is the problem in the following code?
#define loop i<5;
void main()
{
int i;
for(i=0;loopi++)
printf("%d",i);
}
Ans: Undefined symbol loopi.
Explanation: Here the preprocessor searches for the identifier loop to replace it with i<5; but as there is no space between loop and i++ the compiler finds a new identifier loopi and doesn't replace it with i<5; as there is no declaration for the variable loopi compiler flags an error message.
22. How to implement loop to add btween two numbers?
Explanation: Here is the program to add any two numbers using loop.
void main()
{
int a,b,c1=1,c2=2,c3=3;
printf("Enter any two number");
scanf("%d %d",&a,&b);
c3=b;
while (c1++<=a)
{
if(c2++<=b)
c3++;
}
printf("Addition is %d",c3);
}
23.Write the missing statement so that the output will be 2 2 2 2 2 2 3 4 6 5.
void main()
{
/*add here*/
int j,*p=c,*q=c;
for(j=0;j<=5;j++)
{
printf("%d",*c);
++q;
}
for(j=0;j<5;j++)
{
printf("%d",*p);
++p;
}
}
Ans: int c[]={2,3,4,5,6};
Explanation: The first for loop prints the value present at base address of array c (i.e prints 2 five times). The second for loop is printing the value pointed by p which is an integer pointer and every times the loop continues it get incremented so the whole array elements get perinted.
24. Usually loop(do-while, while and for) runs or stops only after checking a condition after every step. But can we make such loop that, the condition will be check when we will want.
Explanation:
while (1)
{
if (kbhit()) //if any key is pressed, then control entres into the loop
{
if(getch()==27) //ASCII value of esc is 27, if Escape character is
return(1); //pressed it goes out of the loop.
}
}
25. Write the missing statement to reverse any number.
void main()
{
int n,r=0;
printf("\nEnter a number");
scanf("%d",&n);
while(n>=1)
{
r=r*10+n%10;
}
printf("%d",r);
}
Ans: n=n/10
Explanation: in the body of the while loop, add n=n/10 after this (r=r*10+n%10) expression.
26. Write the missing statement that the loop will continue 5 time.
void main()
{
int i;
for(i=1;i<=10;i++)
printf("helio");
if(i==5)
break;
}
Ans:There should be curly braces of the for loop enclosing the three statements after the loop.
Explanation:
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf("hello");
if(i==5)
break;
}
}
27. Find the output.
#define TRUE 0/1
void main()
{
while(TRUE)
{
printf("%d",TRUE);
}
}
Ans: no output
Explanation: TRUE in the while condition expression is replaced by 0/1. So it evaluates to false. That's why it will go out of the loop and nothing will be displayed.
28. Find the output.
void main()
{
short int i;
for(i=1;i<=35560;i++)
{
if (i==33000)
break;
else
printf("Hello");
}
}
Ans: prints Hello infinite times.
Explanation: the maximum value of a signed integer is 32767. When it will be incremented beyond 32767, it will enter to the integer cycle as(32768 will be -1). So the value of i will be always less than 35560, hence infinite loop.
29. Find the output.
void main()
{
int a=5;
do
{
printf("%d %d",a,a=0);
}while(a>0);
}
Ans: 0 0
Explanation: when there is multiple statements inside the printf function. It will be executed from right to left. When compiler will execute the printf function, a assigned to 0, so first time print 0 0. After that the condition in the while expression will be evaluates as false and go out of the loop.
30. Find the output.
void main()
{
int loop=0;
switch(loop)
{
case 0:
do
{
case 1: loop++;
case 2: ++loop;
}while(-loop==0);
}
printf("%d",loop);
}
Ans: 1
Explanation: Inside the switch case structure, control will jump to case 0: After that inside the do-while loop, it will execute both case 1 and 2. Now the value of loop is 2.When it test the condition in the while exptession it will be false, hence go out of the loop and print 1
31. Find the output.
void main()
{
char *p="abc";
char *q="abc123";
while(*p++=*q++)
printf("%c %c",*p,*q);
}
Ans: bbcc 1a2b3c
Explanation: String "abd" is stored as abc\0 and q will be stored as abc 123 \0 immediately after p as both of the strings are created in contiguous memory location. When the control goes to the condition part in the while loop *p++=*q++, 1st *p will be assigned with *q and then both will get incremented by 1 position. So 1st a will be overwritten with a and the printf statement will print b. The loop continues until *p is assigned with NULL. As both the strings saved in contiguous memory location the output is bbcc 1a2b3c
32.Find the output.
main()
{
char n[]="lakshya";
int y;
y=0;
while(n[y])
{
printf("%c%c%c",n[y],
*(n+y),*(Y+n),y[n]);
y++;
}
}
Ans: llll
aaaa
kkkk
ssss
hhhh
yyyy
aaaa
Explanation: Here n[y], *(n+y),*(y+n),y[n] all of these refer to the same element. The loop will continue 7 times and all the characters gets printed four times each.
33. Find the output.
main()
{
char str [10]={0,0,0,0,0};
char *s;
int x;
s=str;
for(x=0;x<5;x++)
{
if(*s)
{
printf("%c",*s);
s++;
}
}
}
Ans: No output
Explanation: All the elements of the string str is initialized with 0. The for loop will continue 5 times, but if condition will always fail and the control will never go inside if statement, hence no output.
34. Write the missing statement of the following program.
void main()
{
int show();
int (*p)();
(*p)();
}
int show()
{
int i;
for(i=1;i<=5;i++)
printf("Hello\n");
}
Ans: p=show;
Explanation: Here p is a pointer to function returning an int. p is called before assigned the address of function show. so to get the desired output p should be initialized show. So to get the desired output p should be initialized with the address of function show.
35. Write the missing statement of the following program.
void main()
{
int a[]={4,67,2,6,8};
int i,j,t;
for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
if(a[i]>a[j])
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<5;i++)
printf("%d",a[i]);
}
Ans: Curly braces
Explanation: t=a[i];a[i]=a[j];a[j]=t;
These three statements should be inside curly braces to get the array sorted. if no curly braces are given then inside if only one statement gets executed i.e. t=a[i];
36. Make the following program more effcient.
void main()
{
int i;
for(i=1;i<=5;i++)
show();
}
int show()
{
printf("C Marathon");
}
Ans:
void main()
{
show ();
}
int show()
{
int i;
for(i=1;i<=5;i++)
printf("\nc marathon");
}
Explanation: More you call a function slower the execution of the program. Here in the above program show function is repeatedly called 5 times. To overcome this problem write the following code, it will be more efficient.
37. Convert the following program into function recursion.
void main()
{
int x;
x=unknown(5);
printf("%d",x);
}
int unknow (int n)
{
static int f=1;
while (n>=1)
{
f=f*n;
n-;
}
return f;
}
Explanation:
void main()
{
int x;
x=unknown(5);
printf("%d",x)'
}
int unknown(int n)
{
static int f=1;
if (n==1)
return 1;
else
f=n*unknown (n-1);
return f;
}
38. Find the output.
void main()
{
char ch;
for(ch='0';ch<='\377';ch++)
printf("%d",ch);
}
Ans: No output
Explanation:The decimal value of '\377'is 255. But in the for loop. the value of ch i.e. 48 is compared with '\377',it will be converted to signed value i.e. -1. so the condition in the for loop is not satisfied, hence no output.
39. How many times the following loop will execute?
void main()
{
short int x=1;
unsigned long int i=1;
while(x)
{
printf("\nc Marathon");
x++;
}
}
Ans: 65535
Explanation: As x is a short interger it will increment fot 1 to 32767 and then from -32768 to 0.When x becomes 0 condition becomes false and the loop terminates. So c marathan gets printed 655535 times.
40. Find the output.
void main()
{
unsigned int k=987,i=0;
char trans[10];
do
{
trans[i++]=(k%16-'0');
}while(k/=16);
printf("%s",trans);
}
Ans: b d garbage
Explanation: Here first k% 16 is evaluated that is 987%16 which equals to 11.As 11 is greater than 9 condition becomes true and expression (k%16-10+'a')is evaluated which result in 66 and 66 gets stored in trans[0]. Then in condition part of the do while loop k/=16 is evaluated which results in 61. condition is true so again k%16 is evaluated which results 13 which is also greater than 9 again the condition is true and the expression(k%16-10+'a') is evaluated which results in 68 and it gets stored in trans [1]. then condition part k/=16 is evaluated which results in 3. In the do while loop k%16 is evaluated which also result in 3 now condition is false and the expression (k%16-'0') gets evaluated and results in -45 and -45 gets stored in trans[2]. Then k/=16is evaluated which result in 0 and the loop terminates. The printf statement prints the contents of the array i.e. b d and garbage.
41. Find the output.
void main()
{
int i=6,j=4;
printf("N");
switch(i)
{
do
{
case 1: printf("y");
case 2:
case 3:
case 4:
case 5:
case 6: j-;
}while(j);
}
}
Ans: NYYY
Explanation: First Ngets printed and when it execute the switch statement, it directly jumps to case 6(as i=6) gets satisfied so j is decremented and the value of j becomes 3. Now the condition in the do-while expression is evaluated true 3 times for (j=3,2,1)so y will get printed 3 times. After that it will go out of the do-while loop and also from switch expression.
42.Find the output.
void main()
{
char input[]="SSSWILTECH1\1\1";
int i,c;
for(i=2;(c=input[i])!='\0';i++)
{
switch(c)
{
case 'a' :putcher ('i');
continue;
case '1' : break;
case 1: while((c=input[++i])
!='\1'&&c!='\0');
case 9: putchar('s');
case 'E': putchar(c);
continue;
}
putchar(' ');
}
putchar('\n');
}
Ans:SWITCH S
Explanation: Here continue is not working for switch case it is working with for loop. The for loop is running and whenever the case is not matching in the switch it executes the default case that prints the character in the screen. And whenever the case containing default is executed then it takes the control to the starting of the for loop.
43. If A is an array with n elements and procedure swap exchanges its arguments. then the following code segment sorts A in descending order.
void main()
{
int j,k;
for(j=0;j<n-1;j++)
for(k=0;k<n-j-1;k++)
if (A[k]<a[k+1])
counter++;
printf("%d",counter);
swap(A[k],A[k+1]);
}
How many calls to swap are made if initially, A[i] = i, for i= 0,1,2,.....,n-1?
Explanation: for n is odd: FLOOR (n/2)*n //hints: FLOOR returns next lowest integer below number
n is even: n*(n-1)/2
44.Find the output.
void main()
{
int n=0;
while(+(+n-)!=0)
n-=n++;
n-=n++;
printf("%d",n);
}
Ans: 1
Explanation: Here the value of n is 0, in the while loop condition is false as there is post increment operator so the control will not enter the while loop. Now the value of n is -1 the expression n-=n++ expands as n=n-n++, here n is assigned with 0 (-1-(-1)) but just after the instruction the post increment operator come into the picture and it increments the value of i to 1. So the output is 1.
45.Find the output.
void main()
{
static a[]={1,2,3,4,5,6,7,8};
int i;
for(i=2;i<6;++i)
a[a[i]]=a[i];
for(i=0;i<8;++i)
printf("%d",a[i]);
}
Ans: 1 2 3 3 5 5 7 8
Explanation: First the value of i is 2 so the expression a[a[i]=a[i] evaluates to a[a[2]]=a[2]; that is a [3]=a[2]; a[3]=3; so 3 is stored in a[3]. similarly a[3]=3;a[5]=5;a[5]=5; The next for loop prints the value of the array i.e. 1 2 3 3 5 5 7 8
46. Find the output.
void main()
{
char i=13;
while(i)
{
i++;
}
printf("%d",i);
}
Ans: 0
Explanation: Here the value of i will be incremented until it reaches 0. So the loop will continue until the value of i becomes 0.
47.Find the output.
void main()
{
int i=0;
for(;;)
{
i++;
while(1)
{
i++;
if(i==4)
break;
}
if(i==5)
break;
}
}
Ans: Infinite loop
Explanation: Here the control will get loop and then into the while loop when the value of i becomes 4 then it comes out of the while loop the condition in the if is checked which is false then in the for loop the value of i gets incremented and again it enters into the while loop. i value is greater than 4 so condition is false and it becomes an infinite loop.
48.What is the problem in the following code?
void main()
{
static int i;
for(;;)
{
if(i!=printf(9876))
{
i++;
}
else
{
while(1)
{
break;
break;
}
}
}
printf("%d",i);
}
Ans: output will be garbage value infinite times.
Explanation: The problem is the control gets struck in the outer loop that is the for loop. There is no terminator condition in the for loop.
49. Find the output when input is 25.
void main()
{
int su=0;
for(scanf("%d",&su);printf
("%d",su)-1;printf("%d"))
{
su=su/10;
}
}
Ans: 2522
Explanation: Here the statemint scanf will input the value 25 to variable su. The condition part contains a printf statement that will print the value 25 and returns the value 2. so condition is true and the su is now modified to 2, the statement printf("%d"); will print the value present in the stack which is 2 and then condition is again checked, the printf statement in condition part willprint the value 2 and returns 1 which is subtracted by 1, so the condition becomes false and the loop terminates resulting output 2522.
50.Find the output.
void main()
{
int i=0;
switch(0)
{
printf("%d",i);
i++;
}
for(;i;)
{
printf("%d",i);
}
while(i)
{
printf("%d",i);
}
do
{
printf("%d",i);
}while(i);
}
Ans : 0
Explanation: since there is no case inside switch statement, control will not enter inside the switch. in the for loop, the condition part is 0, so also not enter into the for loop. In the while loop the condition part is also false, that's why it will not also enter inside the while loop. But for the do-while loop, at least it will execute once, hence output is 0. when it test condition of the do-while loop, again it goes out of the loop.
No comments:
Post a Comment