Tuesday, 26 February 2013

C programs in Arrays,Pointers and Structures

1)
a) Write a C Program to find the LCM of two given numbers using Recursion
b) You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B.
Write a Function to merge B into A in sorted order.
void merge(int a[], int b[]);

2)
a) Write a C Program to sort the characters in a given string in ascending order
b) Write a C Program to print each word in a string in reverse order
Example : Input :- KMIT Engineering College
Output :- TIMK gnireenignE egelloC
3)
a) Write a C Program to Sort a Set of Student Records based on the Student Name in Descending
Order. Using Functions and Pointers

The Structure of the Student should consist of Sno, Sname and Marks

Sample Input :- Enter Student Details (Sno,Sname,Smarks):- 100 Raju 95
Enter Student Details (Sno,Sname,Smarks):- 95 Hari 90
Enter Student Details (Sno,Sname,Smarks):- 97 Vasu 85
Enter Student Details (Sno,Sname,Smarks):- 94 Rajesh 99

Output:- 97 Vasu 85
100 Raju 95
94 Rajesh 99
95 Hari 90

b)Write a C Program to Search a Given Element in an Array Using Recursion

4)
a) Write the Code for the following function
int * allocateMemory(int *a,int size)
The function should allocate memory for the variable a based on the size.
Read the elements into the array , return it and display it in main function.
b) Write a Macro to Swap two numbers without using third variable

ECE 2n-mid MCQ

1)
void fun(int x[])
{
x[0]=*x+100;
x[1]=*(x+1)+100;
}
void main( )
{ int a[2]={10,20};
fun(a);
printf("%d",a[1]);
}
A) 10 B) 20 C) 120 D) Error
Ans:- C
2)
#include
int func1(int m)
{
m++;
return m;
}

int func(int m)
{
++m;
return (m=func1(++m)); // Line 10
}

int main(int argc, char *argv[])
{
int k=35,z;
z=func(k);
printf(" %d ", z);
return 0;
}

A) 36
B) 37
C) 38
D) Compile Time Error 10
Ans:- C

3)
void main()
{
char p[20]; int i;
char *s="KMIT";
int length=strlen(s);
` for(i=0;i p[i]=s[length-i];
printf("%s",p);
}

A) gnirts
B) string
C) gnirt
D) None of these
Ans:- D

4) #include
#define MESS(m) printf("m")
void main()
{
MESS("KMIT");
MESS("Hyderabad");
}
A) KMIT Hyderabad
B) Compile Time Error
C) mm
D) Run Time error
Ans:- C

5) #include
void main()
{
int arr[]={0,1,2,3,4};
int *ptr,i;
for(ptr=arr+4;ptr>=arr;ptr--)
printf(" %d ", arr[ptr-arr]);
}

A) 0 1 2 3 4
B) 4 3 2 1 0
C) 1000 1004 1008 1012 1016
D) Compile Time error
E) Run Time Error
Ans:- B

6) #include
void main()
{
static char str[]={48,48,48,48,48,48,48,48,48};
char *s;
int i;
s=str;
for(i=0;i<9;i++)
{
if(*s)
printf("%c ",*s);
s++; //Line 10
}
}
a) 0 0 0 0 0 0 0 0 0
b) Prints Nothing
c) Compile Time error at Line 10
d) Run Time Error

Ans :- a
7) #include
void main()
{
struct s1
{
char *str;
struct s1 *ptr;
};
static struct s1 arr [] ={ {"Nikhil",arr+1},{"Aditya",arr+2},{"Sudheer",arr}, };
struct s1 *p[3];
int i;
for(i=0;i<=2;i++)
p[i]=arr[i].ptr;
printf(" %s ",p[0]->str);
printf(" %s ",(*p)->str);
printf(" %s ",(**p));
}
a) Compile Time Error
b) Run Time Error
c) Aditya Aditya Aditya
d) Aditya Sudheer Sudheer
e) Aditya Sudheer Nikhil
Ans :- C

8) #include
#include
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
printf("\n%d %d",ptr1,ptr2);
return 0;
}
A) NULL NULL
B) 0 0
C) Compile Time Error
D) Run Time Error
Ans:- B
9) #include
#include
int main(){
char *c="123\08";
printf("%s",c);
return 0;
}
A) 12308
B) 1238
C) 123
D) None of these
Ans:- C
10) #include
int main(){
int array[]={10,20,30,40};
printf("%d",2[-array]);
return 0;
}
(A) -60
(B) -30
(C) 60
(D) Garbage value
(E) Compiler error
Ans:- E
11)
#include
int main()
{ char *p="KMIT";
*p='H';
printf("%s",p);
return 0;
}
A) KMIT
B) HMIT
C) Compile Time Error
D) Run Time Error
Ans:- B
12)
int x;
void p(void)
{ int r=2; printf("%d",r); printf("%d",x); }
void r(void)
{ x=1; p(); }
void q(void)
{ int x=3; r(); p(); }
void main()
{ p(); q(); }
A) 2,0,2,1,3,1 B) 3,0,2,1,3,1
C) 2,0,2,1,2,1 D) 2,0,3,1,2,1
Ans:- C

13) #include
void main()
{
char c[]="KMIT2013";
char *p=c;
printf("%s",p+p[3]-p[1]);
}
A) MIT2013
B) 3
C) 2
D) IT2013
Ans:- B

14) #include
int main(){
int arr[]={6,12,18,24};
int x=0;
x=arr[1]+(arr[1]=2); //Line A
printf("%d",x);
return 0;
}
A) 14
B) 24
C) 4
D) Compile Time Error at Line A

Ans:- C
15) What will be output of following c code?
void main()
{
struct employee
{
unsigned id: 8;
unsigned sex:1;
unsigned age:7;
};
struct employee emp1={203,1,23};
clrscr();
printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age);
getch();
}
A) Compile Time Error
B) Run Time Error
C) 8 1 7
D) None of these

Ans:- D