Tuesday, September 16, 2008

ITSA- Programming & Debugging (Question Paper & Solutions)


Dear Juniors,
Below mentioned, is the Question Paper with Answers, of the Event ProDe(:-)!
Please get clarified all your doubts/clarifications!
Event Coordinators:(4/4 IT)
K Rajesh, PV Harsha Vardhan Reddy, JSV Ramya

Wish you Good Luck,
********
Vikash Madduri,
+919849474936
Question Paper:
PROGRAMMING & DEBUGGING
Name: Time: 1hr 30min
Regd no: Section/year:

Debugging:
· Write the output of the following programs. If any errors, write the error.
· Each question carries 1 mark (25x1=25).

1. void main()
{
unsigned int i=1;
signed int j=-1;
if(ij)
printf("greater");
else if(i==j)
printf("equal");
}
Output:

2. void main()
{
char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
char **p;
p=s;
printf("%s\n",++*p);
printf("%s\n",*p++);
printf("%s",++*p);
}
Output:

3. main()
{
int i=0;
for(i=0;i<20;i++)>
Output:
4. 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()); }
Output:
5. main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); printf("%s\n",p2); }
Output:
6. main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf("%d %d\n",x,y); }
Output:
7. main() { int i; i=1; i=i+2*i++; printf("%d",i); } Output: 8. char *f() { char *s=(char *)malloc(8); strcpy(s,"goodbye"); return s; } main() { char *f(); printf("%c",*f()='A'); }
Output:
9. #define MAX(x,y) (x)>(y)?(x):(y)
main()
{
int i=10;
int j=5;
int k=0;
k=MAX(i++,++j);
printf("%d %d %d ",i,j,k);
}
Output:

10. void main()
{
int const * p=5;
printf("%d",++(*p));
}
Output:

11. main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[i],*(s+i),*(i+s),i[s]);
}
Output:

12. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I agree");
else
printf("I disagree");
}
Output:

13. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j;
for(j=0;j<5;j++)>
Output:
14. main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++l++; printf("%d %d %d %d %d",i,j,k,l,m); }
Output:
15. main() { printf("%x",-1<<4);>
Output:
16. main() { int c=- -2; printf("c=%d",c); }
Output:
17. #define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }
Output:
18. main() { int i=10; i=!i>14;
printf("i=%d",i);
}
Output:

19. #define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}
Output:

20. #define square(x) x*x
main()
{
char *p="hai friends",*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s %s",p,p1);
}
Output:

21. main()
{
int i=400,j=300,z=500;
printf("%d..%d");
}
Output:
22. main()
{
int i=1;
while (i<=5) { printf("%d",i); if (i>2)
goto here;
i++;
}
}
fun()
{
here:
printf("PP");
}
Output:
23. enum {a,b,c};
main()
{
int i=1;
switch(i)
{
case a: printf("GOOD");
break;
case b: printf("BAD");
break;
}
}
Output:
24. main()
{
int i=-1;
for(;i++;printf("%d",i))
printf("%d\n",i);
printf("%d",i);
}
Output:

25. #define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
Output:

Programming:
· Write small snippets for any two of the following using c or cpp .
· Each program carries 10 marks (2 x 10=20).

1.a) Write a program to swap two variables without using temporary variable and without using arithmetic operators. (6 M)
b) Write a ‘C’ program that do not use the function main () (4M)

2.a)Write a program to print 1 to 100 numbers without using any loops(for,while,do while….) (5 M)
b) write a program to print “ITSA” without using semicolon. (2 M)
c) write a program to print a character on screen without using any functions...
(printf ,puts ,putch ……so on) (3 M)

3)a)#define fun(x,y) x##yvoid main(){int a=3,b=5;clrscr();printf(&a["ya!hellow!how is this?%s\n"],&b["junk/super"]);printf(&a["what%c%c%c %c%c %s\n"],1["this"],2["beauty"],0["tool"],0["is"],3["sensitive"],&b["TASTEIT"]);}
Output with explanation:

b)
int foo(int n){int i;int flag=n>1;for(i=2;flag&&i<=sqrt(n);++i)flag=n%i;return flag;}void main(){int i;clrscr();for(i=1;i<1000;++i)if(foo(i))printf("%d",i);} val="1;if(n">0){if(n%2==1)val*=x;val*=itrocks(x*x,n/2);}return val;}
main(){
itrocks(10,4);
}
Output with explanation:
ANSWERS:
1. Less
2. Harma
harma
ewlett-packard
3. 16, 21
4. The string is: oldstring.
5. garbage value.
6. 57 94
7. 4
8. A
9. 12, 6 ,11
10. Error ( cannot modify a constant object)
11. mmmm
aaaa
nnnn
12. I disagree
13. Error ( L value required)
14. 0 0 1 3 1
15. fff0
16. 2
17. sizeof(i)=1
18. i=0
19. 64
20. ibj!gsjfoet
21. 500.. 300
22. Error( labels have local scope)
23. BAD.
24. 0
01
25. 100

PROGRAMMING
1. #include#includemain(){int a=5, b=6;a=a^b;b=a^b;a=a^b;printf("%d\t %d", a, b);}
(or)
#include
#define deco(g,s,m,h) h##g##m##s
#define begin deco(a,n,i,m)
int begin()
{
printf("hello");
}
2)
a)void (*f[2])(int);
void printnum(int n)
{
printf("%d\t", n);
int i;
i = ++n <= 100; (*f[i])(n); } void done(int n) exit(0); } int main(void) { int n = 1; f[0] = &done; f[1] = &printnum; printnum(n); return 1} b) main() { if(printf("ITSA")){} } c) main() { char far *mem=(char far *)0xb8000000; *mem='A'; } 3) a) hellow! how is this super that is IT b) 2,3,5,7,11,.......(prime numbers between 1 to 1000) c) X^n 10^4=10000 But the output wont be printed

No comments: