c Programming Question IV

Q1: Tell how to check whether a linked list is circular.

A: Create two pointers, each set to the start of the list. Update each as follows:

while (pointer1) {

pointer1 = pointer1->next;

pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;

if (pointer1 == pointer2) {

print (\"circular\n\");

}

}


Q2: OK, why does this work?

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet.

How can you quickly find the number of elements stored in a a) static array b) dynamic array ?

Why is it difficult to store linked list in an array?

How can you find the nodes with repetetive data in a linked list?

Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable but aac or bcd are unacceptable.

This is a C question that I had for an intern position at Microsoft: Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. After I wrote out my function, he asked me to figure out from the code how many times the printf statement is run, and also questions on optimizing my algorithm.

What’s the output of the following program? Why?

#include

main()

{

typedef union

{

int a;

char b[10];

float c;

}

Union;

Union x,y = {100};

x.a = 50;

strcpy(x.b,\"hello\");

x.c = 21.50;

printf(\"Union x : %d %s %f \n\",x.a,x.b,x.c );

printf(\"Union y :%d %s%f \n\",y.a,y.b,y.c);

}

Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively)

What is output equal to in

output = (X & Y) | (X & Z) | (Y & Z)

c Programming Questions III

1. What will print out?

main()
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%s\n”,p2);

}

Answer:empty string.

2. What will be printed as the result of the operation below:

main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%d\n”,x,y);

}

Answer : 5794

3. What will be printed as the result of the operation below:

main()
{
int x=5;
printf(“%d,%d,%d\n”,x,x< <2,x>>2);

}

Answer: 5,20,1

4. What will be printed as the result of the operation below:

#define swap(a,b) a=a+b;b=a-b;a=a-b;

void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %d\n”,x,y);
swap2(x,y);
printf(“%d %d\n”,x,y);
}

int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;

}

Answer: 10, 5
10, 5

5. What will be printed as the result of the operation below:

main()
{
char *ptr = ” Cisco Systems”;
*ptr++; printf(“%s\n”,ptr);
ptr++;
printf(“%s\n”,ptr);

}

Answer:Cisco Systems
isco systems

6. What will be printed as the result of the operation below:

main()
{
char s1[]=“Cisco”;
char s2[]= “systems”;
printf(“%s”,s1);
}

Answer: Cisco

7. What will be printed as the result of the operation below:

main()
{
char *p1;
char *p2;

p1=(char *)malloc(25);
p2=(char *)malloc(25);

strcpy(p1,”Cisco”);
strcpy(p2,“systems”);
strcat(p1,p2);

printf(“%s”,p1);

}

Answer: Ciscosystems

8. The following variable is available in file1.c, who can access it?:

9.static int average;

Answer: all the functions in the file1.c can access the variable.

10. WHat will be the result of the following code?

#define TRUE 0 // some code

while(TRUE)
{

// some code

}

Answer: This will not go into the loop as TRUE is defined as 0.

11. What will be printed as the result of the operation below:

int x;
int modifyvalue()
{
return(x+=10);
}

int changevalue(int x)
{
return(x+=1);
}

void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf("First output:%d\n",x);

x++;
changevalue(x);
printf("Second output:%d\n",x);
modifyvalue();
printf("Third output:%d\n",x);

}

Answer: 12 , 13 , 13

12. What will be printed as the result of the operation below:

main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %d\n”,x,y);

}

Answer: 11, 16

13. What will be printed as the result of the operation below:

main()
{
int a=0;
if(a==0)
printf(“Cisco Systems\n”);
printf(“Cisco Systems\n”);

}

Answer: Two lines with “Cisco Systems” will be printed.

c Programming Questions II

1. What does static variable mean?
2. What is a pointer?
3. What is a structure?
4. What are the differences between structures and arrays?
5. In header files whether functions are declared or defined?
6. What are the differences between malloc() and calloc()?
7. Difference between pass by reference and pass by value?
8. What is stat
9. What are macros? What are the advantages and disadvantages?
10. ic identifier?
11. Where are the auto variables stored?
12. Where does global, static, local, register variables, free memory and C Program instructions get stored?
13. Difference between arrays and linked list?
14. What are enumerations?
15. Describe about storage allocation and scope of global, extern, static, local and register variables?
16. What are register variables? What are the advantage of using register variables?
17. What is the use of typedef?
18. Can we specify variable field width in a scanf() format string? If possible how?
19. Out of fgets() and gets() which function is safe to use and why?
20. Difference between strdup and strcpy?


# What is recursion?
# Differentiate between a for loop and a while loop? What are it uses?
# What are the different storage classes in C?
# Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?
# What is difference between Structure and Unions?
# What the advantages of using Unions?
# What are the advantages of using pointers in a program?
# What is the difference between Strings and Arrays?
# In a header file whether functions are declared or defined?
# What is a far pointer? where we use it?
# How will you declare an array of three function pointers where each function receives two ints and returns a float?
# What is a NULL Pointer? Whether it is same as an uninitialized pointer?
# What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?
# What does the error ‘Null Pointer Assignment’ mean and what causes this error?
# What is near, far and huge pointers? How many bytes are occupied by them?
# How would you obtain segment and offset addresses from a far address of a memory location?
# Are the expressions arr and *arr same for an array of integers?
# Does mentioning the array name gives the base address in all the contexts?
# Explain one method to process an entire string as one unit?
# What is the similarity between a Structure, Union and enumeration?
# Can a Structure contain a Pointer to itself?
# How can we check whether the contents of two structure variables are same or not?
# How are Structure passing and returning implemented by the complier?
# How can we read/write Structures from/to data files?
# What is the difference between an enumeration and a set of pre-processor # defines?
# What do the ‘c’ and ‘v’ in argc and argv stand for?
# Are the variables argc and argv are local to main?
# What is the maximum combined length of command line arguments including the space between adjacent arguments?
# If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?
# Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function?
# What are bit fields? What is the use of bit fields in a Structure declaration?
# To which numbering system can the binary number 1101100100111100 be easily converted to?
# Which bit wise operator is suitable for checking whether a particular bit is on or off?
# Which bit wise operator is suitable for turning off a particular bit in a number?
# Which bit wise operator is suitable for putting on a particular bit in a number?
# Which bit wise operator is suitable for checking whether a particular bit is on or off?
# Which one is equivalent to multiplying by 2?

* Left shifting a number by 1
* Left shifting an unsigned int or char by 1?

# Write a program to compare two strings without using the strcmp() function.
# Write a program to concatenate two strings.
# Write a program to interchange 2 variables without using the third one.
# Write programs for String Reversal. The same for Palindrome check.
# Write a program to find the Factorial of a number.
# Write a program to generate the Fibonacci Series?
# Write a program which employs Recursion?
# Write a program which uses command line arguments.
# Write a program which uses functions like strcmp(), strcpy(), etc.
# What are the advantages of using typedef in a program?
# How would you dynamically allocate a one-dimensional and two-dimensional array of integers?
# How can you increase the size of a dynamically allocated array?
# How can you increase the size of a statically allocated array?
# When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?
# Which function should be used to free the memory allocated by calloc()?
# How much maximum can you allocate in a single call to malloc()?
# Can you dynamically allocate arrays in expanded memory?
# What is object file? How can you access object file?
# Which header file should you include if you are to develop a function which can accept variable number of arguments?
# Can you write a function similar to printf()?
# How can a called function determine the number of arguments that have been passed to it?
# Can there be at least some solution to determine the number of arguments passed to a variable argument list function?
# How do you declare the following:

* An array of three pointers to chars
* An array of three char pointers
* A pointer to array of three chars
* A pointer to function which receives an int pointer and returns a float pointer
* A pointer to a function which receives nothing and returns nothing

# What do the functions atoi(), itoa() and gcvt() do?
# Does there exist any other function which can be used to convert an integer or a float to a string?
# How would you use qsort() function to sort an array of structures?
# How would you use qsort() function to sort the name stored in an array of pointers to string?
# How would you use bsearch() function to search a name stored in array of pointers to string?
# How would you use the functions sin(), pow(), sqrt()?
# How would you use the functions memcpy(), memset(), memmove()?
# How would you use the functions fseek(), freed(), fwrite() and ftell()?
# How would you obtain the current time and difference between two times?
# How would you use the functions randomize() and random()?
# How would you implement a substr() function that extracts a sub string from a given string?
# What is the difference between the functions rand(), random(), srand() and randomize()?
# What is the difference between the functions memmove() and memcpy()?
# How do you print a string on the printer?
# Can you use the function fprintf() to display the output on the screen?
# Gautam Pagedar adds this question: What is a linklist and why do we use it when we have arrays? - I feel the correct answer should be linklist is used in cases where you don’t know the memory required to store a data structure and need to allocate is dynamically on demand.
# How do you detect a loop in linked list?
# Sunil asks: What is the difference between main() in C and main() in C++?
# ajz at his interviews asks what will be printed out when the following code is executed:

main()
{
printf("%x",-1<<4);
}

C Programming questions I


2. For the following C program

#define AREA(x)(3.14*x*x)
main()
{float r1=6.25,r2=2.5,a;
a=AREA(r1);
printf("\n Area of the circle is %f", a);
a=AREA(r2);
printf("\n Area of the circle is %f", a);
}

What is the output?

3.
void main()
{
int d=5;
printf("%f",d);
}


4.
void main()
{
int i;
for(i=1;i<4,i++)
switch(i)
case 1: printf("%d",i);break;
{
case 2:printf("%d",i);break;
case 3:printf("%d",i);break;
}
switch(i) case 4:printf("%d",i);
}


5.
void main()
{
char *s="\12345s\n";
printf("%d",sizeof(s));
}


6.
void main()
{
unsigned i=1; /* unsigned char k= -1 => k=255; */
signed j=-1; /* char k= -1 => k=65535 */
/* unsigned or signed int k= -1 =>k=65535 */
if(iprintf("less");
else
if(i>j)
printf("greater");
else
if(i==j)
printf("equal");
}


7.
void main()
{
float j;
j=1000*1000;
printf("%f",j);
}

1. 1000000
2. Overflow
3. Error
4. None


8. How do you declare an array of N pointers to functions returning
pointers to functions returning pointers to characters?






9.
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}

What are the number of syntax errors in the above?



10.
void main()
{
int i=7;
printf("%d",i++*i++);
}



11.
#define one 0
#ifdef one
printf("one is defined ");
#ifndef one
printf("one is not defined ");




12.
void main()
{
int count=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=∑
*temp=count;
printf("%d %d %d ",count,*temp,sum);
}


13. what is alloca()


14.
main()
{
static i=3;
printf("%d",i--);
return i>0 ? main():0;
}



15.
char *foo()
{
char result[100]);
strcpy(result,"anything is good");
return(result);
}
void main()
{
char *j;
j=foo()
printf("%s",j);
}


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

17. Output of the following program is

main()
{int i=0;
for(i=0;i<20;i++)
{switch(i)
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default i+=4;
break;}
printf("%d,",i);
}
}

a) 0,5,9,13,17
b) 5,9,13,17
c) 12,17,22
d) 16,21
e) Syntax error

18. What is the ouptut in the following program

main()
{char c=-64;
int i=-32
unsigned int u =-16;
if(c>i)
{printf("pass1,");
if(cprintf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(iprintf("pass2");
else
printf("Fail2")
}

a) Pass1,Pass2
b) Pass1,Fail2
c) Fail1,Pass2
d) Fail1,Fail2
e) None of these

19. What will the following program do?

void main()
{
int i;
char a[]="String";
char *p="New Sring";
char *Temp;
Temp=a;
a=malloc(strlen(p) + 1);
strcpy(a,p); //Line number:9//
p = malloc(strlen(Temp) + 1);
strcpy(p,Temp);
printf("(%s, %s)",a,p);
free(p);
free(a);
} //Line number 15//

a) Swap contents of p & a and print:(New string, string)
b) Generate compilation error in line number 8
c) Generate compilation error in line number 5
d) Generate compilation error in line number 7
e) Generate compilation error in line number 1

20. In the following code segment what will be the result of the function,

value of x , value of y
{unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");
}

a) same, MAXINT, -1
b) not same, MAXINT, -MAXINT
c) same , MAXUNIT, -1
d) same, MAXUNIT, MAXUNIT
e) not same, MAXINT, MAXUNIT

21. What will be the result of the following program ?

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());
}

a) The string is : string
b) The string is :Oldstring
c) Run time error/Core dump
d) Syntax error during compilation
e) None of these

22.Find the output for the following C program

main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}

23.Find the output for the following C program

main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);
}

24.Find the output for the following C program

main()
{
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}

25 Find the output for the following C program

#define swap1(a,b) a=a+b;b=a-b;a=a-b;
main()
{
int x=5,y=10;
swap1(x,y);
printf("%d %d\n",x,y);
swap2(x,y);
printf("%d %d\n",x,y);
}
int swap2(int a,int b)
{
int temp;
temp=a;
b=a;
a=temp;
return;
}


26 Find the output for the following C program

main()
{
char *ptr = "Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n",ptr);
}


27 Find the output for the following C program

#include
main()
{
char *p1;
char *p2;
p1=(char *) malloc(25);
p2=(char *) malloc(25);
strcpy(p1,"Ramco");
strcpy(p2,"Systems");
strcat(p1,p2);
printf("%s",p1);
}


28.Find the output for the following C program

# define TRUE 0
some code
while(TRUE)
{
some code
}

29. struct list{
int x;
struct list *next;
}*head;

the struct head.x =100

Is the above assignment to pointer is correct or wrong ?

30.What is the output of the following ?

int i;
i=1;
i=i+2*i++;
printf(%d,i);

31. FILE *fp1,*fp2;

fp1=fopen("one","w")
fp2=fopen("one","w")
fputc('A',fp1)
fputc('B',fp2)
fclose(fp1)
fclose(fp2)
}

Find the Error, If Any?

32. #define MAN(x,y) (x)>(y)?(x):(y)
{int i=10;
j=5;
k=0;
k=MAX(i++,++j);
printf(%d %d %d %d,i,j,k);
}

33.void main()
{
int i=7;
printf("%d",i++*i++);
}


34.How will u terminate the statement?

35.select the wrong one
a.a+=1;
b.a*=2;
c.a**=1;
d.a>>=1;

36.pick the odd one
a.malloc
b.calloc
c.new

37.main()
{
char **p=="Hello";
printf("%s",**p);
}

38.main()
{
printf("%d%c\n");
printf("%d%c\n");
}

39.main()
{
int x==5;
printf("%d%d",x++,++x);
}

40.main()
{
int x==4;
printf("%d",printf(" %d %d ",x,x) );
}

41.main()
{
union
{
int i;
char p;
struct
{
int t;
char e;
char o;
};
};
printf("%d\n",sizeof(l) );
}

42.main()
{
int i==0,n==6;
while(n--0);
i+==n;
printf("%d\n",i);
}



Answers



1. B

2. Area of the circle is 122.656250
Area of the circle is 19.625000



3. Undefined

4. 1,2,3,4

5. 6

6. less

7. 4

8. char *(*(*a[N])())();

9. None.

10. 56

11. "one is defined"

12. 20 20 20

13. It allocates and frees memory after use/after getting out of scope

14. 321

15. anything is good.

16. "harma" (p->add(dharma) && (*p)->harma)
"harma" (after printing, p->add(hewlett-packard) &&(*p)->harma)
"ewlett-packard"

17. (d)

18. (c)

19. (b)

20. (a)

21. (b)

22. An empty string

23. 57 94

24. 5 20 1

25. 10 5

26. Samco Systems

27. RamcoSystems

28. This won't go into the loop as TRUE is defined as 0

29. Wrong

30. 4

31. no error. But It will over writes on same file.

32. 10 5 0

33. 56

34. ;

35. C

36. C

37. Garbage or nothing

38. Garbage Value

39. 6 6

40. 4 4 5

41. 4

42. -1

43. 0

44. 3

45. 16 21

46. UPPER CASE

47. 5 4 3 2 1

48. (newdictionary)-(dictionarynew)

49. (newdictionary)-(dictionarynew)



50. 50


C Questions Page 1(New)

C Questions Page 2

C Questions Page 3

C Questions Page 4

C Problems
Visual C++ Projects