1>Given a number x, less than 100. How will you generate true with probability x/100. So if x = 65, how will you generate true with probability 65/100. You can represent true by 1 and false by 0.
SOL: We need to add the probability of finding a red pen in one jar and add it to the probability of finding it in the other. So the probability would b: 0.5x0.5 (probability of picking Jar A)x(probability of picking red pen in that jar) + 0.5x0.5(probability of picking Jar b)x(probability of picking red pen in that jar) = 0.5 But there exists a distribution in which the probability of picking one color pen can be increased and i.e. put 1 red pen in jar A and other 49 red and 50 blue in jar B. This gives us the probability of picking red pen as 0.5x1 (probability of picking Jar A)x(probability of picking red pen in that jar) + 0.5x(49/99) (probability of picking Jar b)x(probability of picking red pen in that jar) = .7474 ANS
sol: there is no predefined operator or any library function to calculate the size of any userdefined function:we can calculate size of fn by finding the size of each variables encountered in that function; example:
SOL: void foo( ) { int X=65,NUM; NUM=rand( )%100 + 1; if(NUM<=X) printf("true"); else printf("false"); }2> Given 50 red pens and 50 blue pens, two jars(A,B) initially empty. These 100 pens are to be distributed b/w the jars such that the probability of picking a red pen is maximum.
SOL: We need to add the probability of finding a red pen in one jar and add it to the probability of finding it in the other. So the probability would b: 0.5x0.5 (probability of picking Jar A)x(probability of picking red pen in that jar) + 0.5x0.5(probability of picking Jar b)x(probability of picking red pen in that jar) = 0.5 But there exists a distribution in which the probability of picking one color pen can be increased and i.e. put 1 red pen in jar A and other 49 red and 50 blue in jar B. This gives us the probability of picking red pen as 0.5x1 (probability of picking Jar A)x(probability of picking red pen in that jar) + 0.5x(49/99) (probability of picking Jar b)x(probability of picking red pen in that jar) = .7474 ANS
1> void main() { int i=320; char *ptr=(char *)&i; printf("%d",*ptr); } ANS-64
2>#define x 5+2 void main() { int i; i=x*x*x; printf("%d",i); ANS 27 }
3>void main(){ char c=125; c=c+10; printf("%d",c); ANS -121 }
4>void main(){ float a=5.2; if(a==5.2) printf("Equal"); else if(a<5.2) printf("Less than"); else printf("Greater than"); ANS- less than }
5>void main(){ int i=4,x; x=++i + ++i + ++i; printf("%d",x); ans-in gcc-18 in turbo c-21 }
6> void main(){ int a=2; if(a==2){ a=~a+2<<1; printf("%d",a); ans-compiler error } else{ break; } }
7>void main(){ int a=10; printf("%d %d %d",a,a++,++a);ans-12,11,11 }
8>void main(){ char *str="Hello world"; printf("%d",printf("%s",str)); ans-hello world11 }
9>void main(){ char *str=NULL; strcpy(str,"cquestionbank"); ans-turbo c-(null)i.e null ptr assignment printf("%s",str); gcc-runtime error }
10>void main(){ int i=0; for(;i<=2;) printf(" %d",++i); ans 1,2,3 }1> Explain the memory representation of int a=7;
ANS: if binary num is in form- ABCD 00000000|00000000|00000000|00000111 then memory representation is -CDBA 00000000|00000111|00000000|000000002>Explain floating point representation of real numbers:?
sol: float num: 1.46 by IEEE STD: sign bit: 1 expo bit: 8 significant bit:23 bias value:127 try urself3>find the size of function?
sol: there is no predefined operator or any library function to calculate the size of any userdefined function:we can calculate size of fn by finding the size of each variables encountered in that function; example:
void foo() { int arr[20]; int x=10; int *ptr=&x; } ANS>> SIZE= sizeof(arr)+ sizeof(x)+sizeof(ptr);4>convert integer to string??
main() { int num=6; char buf[10]; sprintf(buf,"%d",num); printf("%s",num); }5>compare two strings which are present in different memory location??
void cmp(char *str,char *ptr) { if(strcmp(str,ptr)==0) printf("equal") else printf("not equal"); }1>REVERSE THE NUMBER USING POINTER ex inp-109012 o/p 210901?
SOL: int rev(int *num) main() { int num=12345; pf("%d",rev(&num)); } { int r,sum=0; while(*num) { r=*num%10; sum=sum*10 + r; *num/=10; } return sum; }2>string uppercase to lowercase and vice versa?
sol:main() { char str[100],buff[100],ch; int i,j,k=0; printf("enter str:"); gets(str); for(i=0;str[i];i++) { ch=str[i]; if(ch>='A'&&ch<='Z') buff[k]=ch+32; else if(ch>='a'&& ch<='z') buff[k]=ch-32; k++; } puts(buff); }3>find the location of substring in a given string:if string is "synfosyssolution" and sub string is "sol" :then output will b 9 ?
main() { char str[]="synfosyssolution"; char sub[]="sol"; int i,j,cnt=0; for(i=0;str[i];i++) { if(str[i]==sub[0]) { for(j=1;sub[j];j++) { if(str[i+j]!=sub[j]) { break; } } } if(sub[j]!='\0') cnt++; } printf("sub str found at pos :%d\n",cnt+1); }5>Reverse word of a sentence??if input is "synfosys business soln ltd" than output will be "ltd soln business synfosys"??
void xrev(char *,char *) sol: main() { char str[]="synfosys business soln ltd"; char *ptr=0,*x=0,*y=0; for(ptr=str;*ptr;ptr++);//move ptr to last xrev(ptr-1,str) x=str-1; y=str; while(x++<ptr) { if(*x=='0'||*x==' ') { xrev(y,x-1) y=x+1; } } void rev(char *l,char *r) { char t; while(l<r) { t=*l; *l++=*r; *r--=t; } }