Q1. How scanf will execute?
main(){
char name[10],s[12];
scanf(" \"%[^\"]\"",s);
}
Ans:
First it checks for the leading white space and discards it.Then it matches
with a quotation mark and then it reads all character upto another quotation mark.
with a quotation mark and then it reads all character upto another quotation mark.
Q2. What will be the position of the file marker?
a) fseek(ptr,0,SEEK_SET);
b) fseek(ptr,0,SEEK_CUR);
Ans:
a) The SEEK_SET sets the file position marker to the starting of the file.
b) The SEEK_CUR sets the file position marker to the current position of the file.
b) The SEEK_CUR sets the file position marker to the current position of the file.
Q3. What is the problem with the following code segment?
while ((fgets(receiving array,50,file_ptr)) != EOF)
Ans:
fgets returns a pointer. So the correct end of file check is checking for !=
NULL.
NULL.
Q4. Find the o/p:
main(){
char *cptr,c;
void *vptr,v;
c=10; v=0;
cptr=&c; vptr=&v;
printf("%c%v",c,v);
}
Ans:
Compiler error (at line number 4): size of v is Unknown.
Explanation:
You can create a variable of type void * but not of type void, since void is
an empty type. In the second line you are creating variable vptr of type void * and
v of type void hence an error.
Explanation:
You can create a variable of type void * but not of type void, since void is
an empty type. In the second line you are creating variable vptr of type void * and
v of type void hence an error.
Q5. Find the o/p:
#define max 5
#define int arr1[max]
main(){
typedef char arr2[max];
arr1 list={0,1,2,3,4};
arr2 name="name";
printf("%d %s",list[0],name);
}
Ans:
Compiler error (in the line arr1 list = {0,1,2,3,4})
Explanation:
arr2 is declared of type array of size 5 of characters. So it can be used to
declare the variable name of the type arr2. But it is not the case of arr1. Hence an
Rule of Thumb:
#defines are used for textual replacement whereas typedefs are used for
declaring new types.
Q6. Find the o/p:
int i=10;
main() {
extern int i; {
int i=20;{
int i=30;
printf("%d",i);
}
printf("%d",i);
}
printf("%d",i);
}
Ans:
30,20,10
Explanation:
'{' introduces new block and thus new scope. In the innermost block i is
declared as, const volatile unsigned which is a valid declaration. i is assumed of
type int. So printf prints 30. In the next block, i has value 20 and so printf prints
20. In the outermost block, i is declared as extern, so no storage space is allocated
for it. After compilation is over the linker resolves it to global variable i (since it
is the only variable visible there). So it prints i's value as 10.
Explanation:
'{' introduces new block and thus new scope. In the innermost block i is
declared as, const volatile unsigned which is a valid declaration. i is assumed of
type int. So printf prints 30. In the next block, i has value 20 and so printf prints
20. In the outermost block, i is declared as extern, so no storage space is allocated
for it. After compilation is over the linker resolves it to global variable i (since it
is the only variable visible there). So it prints i's value as 10.
Q7. Find the o/p:
main(){
int *j;{
int i=10;
j=&i;
}
printf("%d",*j);
}
Ans:
10
Explanation:
The variable i is a block level variable and the visibility is inside that
block only. But the lifetime of i is lifetime of the function so it lives upto the exit
of main function. Since the i is still allocated space, *j prints the value stored in i
since j points i.
Q8. Find the o/p:
# include < stdio.h >
main() {
const int i=4;
float j;
j = ++i;
printf("%d %f", i,++j);
}
Ans:
Compiler error
Explanation:
i is a constant. you cannot change the value of constant
Explanation:
i is a constant. you cannot change the value of constant
Q9. Find the o/p:
main() {
int i=_l_abc(10);
printf("%d\n",--i);
}
int _l_abc(int i) {
return(i++);
}
Ans:
9
Explanation:
return(i++) it will first return i and then increments. i.e. 10 will be
returned.
Explanation:
return(i++) it will first return i and then increments. i.e. 10 will be
returned.
Q10. Find the o/p of this:
main(int argc, char **argv) {
printf("enter the character");
getchar();
sum(argv[1],argv[2]);
}
sum(num1,num2)int num1,num2; {
return num1+num2;
}
Ans:
Explanation:
argv[1] & argv[2] are strings. They are passed to the function sum without
converting it to integer values.
Q11. Find the o/p:
int one_d[]={1,2,3};
main(){
int *ptr;
ptr=one_d;
ptr+=3;
printf("%d",*ptr);
}
Ans:
garbage value
Explanation:
ptr pointer is pointing to out of the array range of one_d.
Explanation:
ptr pointer is pointing to out of the array range of one_d.
Q12. Find the o/p:
main(){
FILE *ptr;
char i;
ptr=fopen("zzz.c","r");
while((i=fgetch(ptr))!=EOF)
printf("%c",i);
}
Ans:
contents of zzz.c followed by an infinite loop
Explanation:
The condition is checked against EOF, it should be checked against
NULL.
Explanation:
The condition is checked against EOF, it should be checked against
NULL.
Q13.Find the o/p:
int i;
main(){
int t;
for ( t=4;scanf("%d",&i)-t;printf("%d\n",i))
printf("%d--",t--);
}
// If the inputs are 0,1,2,3 find the o/p
Ans:
4--0
3--1
2--2
3--1
2--2
No comments:
Post a Comment