61. which one will over flow given two programs
prog 1: prog2:
main() main()
{ {
int fact; int fact=0
long int x; for(i=1;i<=n;i++)
fact=factoral(x); fact=fact*i;
} }
int factorial(long int x)
{
if(x>1) return(x*factorial(x-1);
}
ans: program 1 (program 2 is always zero since fact =0)
62. main()
{
char str[5]="hello";
if(str==NULL) printf("string null");
else printf("string not null");
}
ans: string not null
63. void f(int value)
{
for (i=0;i<16;i++)
{
if(value &0x8000>>1) printf("1")
else printf("0");
}
}
ans: binary output of value
64. void f(int *p)
{
static val=100;
val=&p;
}
main()
{
int a=10;
printf("%d ",a);
f(&a);
printf("%d ",a);
}
ans: nonportable pointer conversion (we can’t store address in integer variable, we have to take pointer to store address)
65. main()
{
int x, *y;
x = y;
printf(“%d”,x);
}
ans: nonportable pointer conversion
66. # define f(a,b) a+b
#define g(c,d) c*d
find value of f(4,g(5,6))
ans: 34
67. main()
{
char a[10]="hello";
strcpy(a,'\0');
printf("%s",a);
}
ans: arguments must be a string constant or character array variable
here it is constat character not a string constant. Hence program error
68. char a[5][15];
int b[5][15];
address of a 0x1000 and b is 0x2000 find address of a[3][4] and b[3][4]
interger takes 32-bits and character takes 8-bits
ans: a[3][4] = 0x1031 b[3][4] = 0x20C4
(Note: addresses are in hexadecimal)
69. Given an interger in binary form,find the number of ones in
that number without counting each bit.(This questin is not
multiple choice question. This question carries more
marks. So please take care for this question.)
ans: K.Ritchie
70. main()
{
a=2;
b=3;
x=SUM(a,b)*2;
printf("x=%d\n",x);
}
ans: 8
71. number(int i)
{
number++;
printf("%d\n",number);
}
main()
{
static int i=0;
number(i);
}
ans: lvalue required (function name is an address. So ++ operator should not be applied)
72. main()
{
unsigned char i;
int sum;
for(i=0; i<300; i++)
sum+ = i;
printf("\nSum = %d\n", sum);
}
ans: infinite loop
73. void fn(int *p)
{
static int val = 100;
p = &val;
}
main()
{
int i=10;
printf("i=%d\n", i);
fn(&i);
printf("i=%d\n", i);
}
ans: i=10
i=10
74. Swapping without using a temporary variables. (3 methods)
ans:
x = x+y;
y = x-y;
x = x-y;
x = x^y;
y = x^y;
x = x^y;
x = x*y;
y = x/y;
x = x/y;
75. Code 1 :
for(i=0; i<1000; i++)
for(j=0; j<100; j++)
x = y;
Code 2 :
for(i=0; i<100; i++)
for(j=0; j<1000; j++)
x = y;
Which code will execute faster
ans: Code2 (Code 1 = 1,01000 increment operations)
(Code 2 = 1,00100 increment operations)
76. main()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, i, x=10, temp;
for(i=0; i
{
temp = a[i];
a[i] = a[x-i-1];
a[x-i-1] = temp;
}
ans: remains same
77. main()
{
int i = 1;
fork();
fork();
printf("\ni = %d\n", i+1);
}
ans: 4 printfs will occur and i = 2
78. #define MAX(a, b) a>b ? a:b
main()
{
int m, n;
m = 3 + MAX(2, 3);
n = 2 * MAX(3, 2);
printf("m = %d, n = %d\n", m, n);
}
ans: m = 2, n = 3
79. main()
{
int i=10;
fork();
fork();
fork();
printf("%d”,i);
}
ans: 8 printfs will occur and i = 10 (2 power no. of forks times printfs)
80. #define f(a,b) a+b
#define g(a,b) a*b
main()
{
int m;
m=2*f(3,g(4,5));
printf("\n m is %d",m);
}
ans: m is 26
81. main()
{
char a[10];
strcpy(a,"\0");
if (a==NULL)
printf("\a is null");
else
printf("\n a is not null");
}
ans: a is not null
82. main()
{
char a[5]="hello";
printf("%s",a);
}
ans: array size is small it should be 6
83. main()
{
unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");
}
ans: same (-1 is stored in 2’s complement form)
84. char *gxxx()
{
static char xxx[1024];
return xxx;
}
main()
{
char *g="string";
strcpy(gxxx(),g);
g = gxxx();
strcpy(g,"oldstring");
printf("The string is : %s",gxxx());
}
ans: The string is : oldstring
85. void myalloc(char *x, int n)
{
x= (char *)malloc(n*sizeof(char));
memset(x,'\0',n*sizeof(char));
}
main()
{
char *g="String";
myalloc(g,20);
strcpy(g,"Oldstring");
printf("The string is %s",g);
}
ans: The string is Oldstring
86. main()
{
char p[]="String";
int x=0;
if(p=="String")
{
printf("Pass 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
else
{
printf("Fail 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
ans: Fail 1Pass 2
87. A code which had some declarations of some data items. There were a couple of normal data items (char, int..) and some pointers as well and a malloc call. You have to find the total memory taken up in the stack (Hint: Pointers and all are allocated in heap, not in stack, so don’t count them).Also in most of these questions, they were specifying that the OS was 32 bit.
88. A structure was given and it contained normal data as well as some bit-wise data. You had to find the total size taken up by the structure
89. Pointer to a function which returned an array of char pointers
ans: char *((*x)() ) []
90. Value of 2 particular variables in C(MAXINT and some other constant)
91. What do you need to do to open more than 10 files simultaneously in Microsoft Operating System?
ans: change stdio.h/change CONFIG.SYS/compiler dependent
92. main()
{
int i=7;
i = i++*i++;
printf("%d\n",i);
i=7;
printf("%d %d\n",i ,i++*i++);
i=2;
printf("%d %d\n" ,i, i++*++i*i++*i++);
i=1;
printf("%d %d %d\n", i, i++*i++,
i++*i++*++i*i++);
i=1;
printf("%d %d %d\n" ,i, i++*i++, i++*i++*++i*i++*i++*++i);
}
ans: 51
9 56
6 160
7 30 32
9 56 1120
93. main()
{
int d ;
int i=10;
d =sizeof(++i);
printf("%d",d);
}
ans: 2
94. //char *f();
main()
{
char *a,*f();
a=(char*)malloc(20*sizeof(char));
a=f();
printf("%s",a);
}
char *f()
{
static char n[20];
strcpy(n,"Hello World");
return(n);
}
ans: Hello World
95. char *f();
main()
{
char*a,*f();
a=(char*)malloc(20*sizeof(char));
a=f();
printf("%s",a);
}
char *f()
{char n[20];
strcpy(n,"Hello World");
return(n);
}
ans: unpredictable output. auto variable address should not be returned. It will lose its scope when it comes out of the block.
96. char *f()
main()
{
char *a,*f();
a=f();
printf("%s",a);
}
char *f()
{return("Hello World");}
ans: Hello World
97. what is the error
main()
{
int j=10;
switch(j)
{
case 20:
pritnf("Less than 20");
break;
case 30:
printf("Less than 30");
break;
default:
printf("hello");
}
ans: printf not pritnf and one brace } is missing
98. which is valid :
(i)char arr[10];
arr="hello";
(ii) char arr[]="hello";
ans: second is correct. In first lvalue required.
99. main()
{
char *str;
str=(char*)malloc(20*sizeof(char));
strcpy(str,"test");
strcat(str,'!');
printf("%s",str);
}
ans: strcat function arguments should be either a character array variable or a string constant. Instead of ‘!’ give “!”
100. How many times main is get called
main()
{
printf("Jumboree");
main();
}
ans: till stack overflow
RSS Feed
Twitter
Orkut