Pages

Wednesday, February 27, 2008

C Questions 5




Let's start today with something else. The English calendar of September 1752 contained only 19 days, and is the shortest month. Surprised. To know more please read this:
In September 1752 the Julian calendar was replaced with the Gregorian calendar in Great Britain and its American colonies. The Julian calendar was 11 days behind the Gregorian calendar, so 14 September got to follow 2 September on the day of the change. The result was that between 3 and 13 September, absolutely nothing happened!
To know more:
http://www.didyouknow.cd/calendar.htm

Q0. Do you know what happens when you type:
$cal 9 1752
September 1752
S M Tu W Th F S
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
And in this calendar days from 3rd till 13th are missing.

Q1. void *p++;
void *q; q++;
What p,q will point to?
Q2. DMA uses Logical address, or physical address?
Q3. What printf returns?
Q4. While in bottom half, what is the context, is it still the process context?
Q5. (MS) You are trying to use notepad, then while clicking on save, the application hangs. How will you go about debugging this? The complete source code of NOTEPAD is available with you. List down the scenarios and steps.

Q6. Tell me the o/p.
main(){
const int i=5;
i++;
printf("%d\n", i);
}
Ans:
Compilation:
warning: initialization discards qualifiers from pointer target type
warning: increment of read-only variable `i'
Result: i= 6

Q7. Tell me the o/p.
main() {
const int i =5;
int *p=&i;
p++;
printf("*p=%d", *p);
}
Ans:
Compilation:
warning message: warning: initialization discards qualifiers from pointer target type
Result:
*p=garbage value

Q8. Tell me the o/p.
main() {
const int i =5;
int *p=&i;
*p++;
printf("i=%d\n", i);
printf("*p=%d\n", *p);
}
Ans:
Compilation:
warning message: warning: initialization discards qualifiers from pointer target type
Result:
i=5
*p=garbage value

Q9. Tell me the o/p.
main() {
const int i =5;
int *p=&i;
(*p)++;
printf("i=%d\n", i);
printf("*p=%d\n", *p);
}
Ans:
Compilation:
warning message: warning: initialization discards qualifiers from pointer target type
Result:
i=6
*p=6

Q10.
main()
{
const i=9;
const int *p;
p=&i;
printf("*p=%d\n",*p);
p++;
printf("*p=%d\n",*p);
}

Ans:
*p=9
*p=garbage value

Q11. What are the differences in the following declarations:
a)
const i;
const int i;
b)
const *p;
const int *p;
int * const p;
const *int p;
int const *p;

Ans:
const int * p;
declares that p is variable pointer to a constant integer and
int const * p;
is an alternative syntax which does the same, whereas
int * const q;
declares that q is constant pointer to a variable integer and
int const * const p1;
declares that p1 is constant pointer to a constant integer. Basically ‘const’ applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right).
Q12.
int func()
{
printf("Hi");
func();
}
int main(){ func();}
What will be the o/p?

Ans:
Stack overflow, why?
The stack frame for func needs to store the entry and exit point each time func() is recursively called. This will fill up the stack, and hence overflow.
The func() is calling printf(), and in the printf's stack frame you will put the string argument "Hi", and not in the stack frame of func().

Q13.
main()
{
const int i=10;
i=20;
printf(i=%d\n",i);
}
Ans:
warning: assignment of read-only variable `i'
Result:
i=20
Now, tell me where the const variable is stored?

No comments: