Tuesday 13 February 2018

Arrays, Pointers and Strings



Arrays
1. What is the output
# include <stdio.h>
int main ()
{
      int a[] = { 0001, 0010, 0100, 01000 };
            int i;
            for ( i = 0 ; i < 4 ; i++)
                        printf("%o\t", a[i]);
            return 0;
}
            a) 1 10 100 1000                                   b) 1 8 64 512
            c) Compiler Error                     d) None

2. What is the output
#include<stdio.h>
int main()
{
      float arr[] = {12.4, 2.3, 4.5, 6.7};
      printf("%d\n", sizeof(arr)/sizeof(arr[0]));
      return 0;
}
a) 6                                                      b) 5
c) 4                                                      d) None

3. What is the output
#include<stdio.h>
int main()
{
      int  arr[] = {12, 13, 14, 15, 16};
      printf("%d\n", sizeof(arr)/sizeof(arr[0]));
      return 0;
}
            a) 6                                                      b) 5
            c) 4                                                      d) None

4. What is the output
#include<stdio.h>
int main()
{
         char *m[]={"jan","feb","mar"};
      char d[][10]={"sun","mon","tue"};

      printf("%s\t",m[1]);
      printf("%s\n",d[1]);
      return 0;
}
            a) feb mon                                            b) Compile time error
            c) Run time error                                  d) jan  sun

5. #include<stdio.h>
#include<stdio.h>
int main(void)
{
         int a[2]; a[1] = 3;
      printf("a[1] = %d",a[1]);
      printf("1[a] = %d",1[a]);
      return 0;
}
            a) Compile Time Error                          b) 1 1
            c) 3 3                                                    d) Garbage Value

6. What is the output?
 #include<stdio.h>
int main()
{
        static a[3]={1};
        int b[3]={1};
        printf("%d %d",a[2],b[2]);
}
            a) 0 0                                        b) 1 1              
            c) Compiler Error                     d) None

7. What will be the output?
#include <stdio.h>
   void size(int arr[10])
      {
        printf("%d\t", sizeof(arr));

      }
      int main()
      {
        int arr[10];
        size(arr);
           printf("%d\n", sizeof(arr));
        return 0;
      }
            a) 10 10                                               b) Compiler Error
            c) Runtime Error                                d) 4 10None

10. array[] = str[] is it possible if not what error it will give.
11. Array vs pointer
12. Array vs structure
        structure padding, packing
        indexing in array
        array elements accessing for loop, struct directly
13. *a[] -> what is this?
14. Convert a[i][j][k] in terms of pointers.
15. You have two arrays declared as int a[10],b[10];
            if you assign a=b what is the error and how can we overcome it?
16. int a[]={4};
            int b[] = { 1, 2, 3, 4}
            printf("%d %d ", sizeof(a), sizeof(b));

17. int a[] = { 1, 2, 3, 4};
            int *p = (&a + 1) ;
            printf ("ans = %d " , *(a + 1),  *(p - 1) );

19. Given an array of 100 integers.Find the greatest among them
            without using > , < than operators.
20. How to pass entire array to a function as call by value?
22. char *s = "char *s = %c%s%c;main(){printf(s,34,s,34);}";
            main(){printf(s,34,s,34);}
Answer : Prints its own code
23. About the arrays bound checking.
24. What is the difference between the pointer and array, which one is more advantages.
25. What is smart pointer?
27. When does the compiler not implicitly generate the address of the first element of an array?
Whenever an array name appears in an expression such as
- array as an operand of the sizeof operator
- array as an operand of & operator
- array as a string literal initializer for a character array
 Then the compiler does not implicitly generate the address of the address of the first element of an array.
28. What are the characteristics of arrays in C?
31. What is the difference between a string and an array?
32.Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?
33. Can the sizeof operator be used to tell the size of an array passed to a function?
34. Represent two dimentional array using pointer.
35. array[] = str[] is it possible if not what error it will give.
36. Find if there is any problem(s) in the code below:
int main()
{
    int a[] = {1,2};
    int *p = malloc(sizeof(int));
    p[1] = 3;
    p = a;
    a = p;
}

37. Take an array for example
 a[] = {3, -2, -1, 5, -5, 2, -3, 4, 2, -2, -3}
While traversing the above array, we need to find the last consecutive sume. In the above example (4+2 = 6) will give the largest sum. The output should be something like – a) largest sum b) the numbers taken to compute the same and b) the respective start index and end index
38. In case of array what is the differnce between a and &a. What is the output of below expressions. If both are same why they are same? If not  why?
                        a +1 =?, &a + 1 = ?

39. WAP to delete a member from array.
40. Difference in array and linked list.
41. Difference between Static array and Dynamic array?? What is their significance?
42. I have byte array, I need to set a bit at position 500 ?
43. I'm having array of 200 elements, the range of each value is 1 to 100, Now you should print values only once, (you should not print a value more than once).
44. int A[100];
    int B[100];
write a function to find out which all numbers are repeting?
47) array vs pointer

Pointers
1. Explain the type difference between the variables declared as follows:
int (**p)[10];
int *(*q)[10];

2. What is the output:
      int main( int argc , char **argv )
     {
            int k ;
            for ( k = 0 ; k < argc ; k++) 
printf( “%s”, argv[++k]);
     }
     In command Line   ./executable 1 2 3 4
            a) 2 4                                                    b) 1  3              
            c) 1 2  3 4                                             d) Compile time Error

3. int a[]={1,2,3,4,5} ;
     int *ptr , **ptr1 ;           
ptr = &a ;          ptr1 = & ptr ;
         How do you update a[2] to 10 using ptr1?

            a) *(ptr1+2) = 10                                  b) *(ptr1)+2 = 10          
c) *(*ptr1+2) =10                      d) (**(ptr1) +2) =10  

4. What is the output:
      int main( )
      {
 int k = 100 ;
 const int *p =&k ;
 *p = 200 ;
     printf(“\n %d”, k );
     }

            a) Compiler Error                     b) 100             
            c) 200                                                  d) 0

5. What is the Output
      # include <stdio.h>
      int main ()
{
      int *ptr = NULL;
      ptr++;
      printf("%d \n", ptr);
      return 0;
}
      a) Segmentation Fault               b) Compiler Error
            c) Runtime Error                                   d) 4

6. What is the output:
      const int *p ;
   int main( )
      {
     int k = 10 ;
     p =&k ;
     *p = 20 ;
     printf(“\n %d”, k );
     }

a) Compiler Error                     b) 10               
c) 20                                                    d) Run Time Error

7. What is the Output
      # include <stdio.h>
      int main ()
{
      int *ptr = 10;
      int x = 15
      printf("%d  %d \n", ptr+x, x + ptr);
      return 0;
}

      a) Runtime Error                                   b) Compiler Error
            c) 70 70                                                            d) None of the Above

8. What is the Output
      #include<stdio.h>
      int main(void)
      {
       char *v[] = {"abc","efg","jkl"};
       char **p = v; printf("%c\n",*++p[0]);
       printf("%c\n",(*++p)[0]);
       return 0;
}
 a) a e                                                   b) a f
             c) b f                                                   d) b e

9. What operation we cant do with pointers.
10. What is heap area?
11. Represent two dimentional array using pointer.
12. *p() -> what is this?
13.  Which operation on pointer is not valid, if p and q are two pointers  fun (int *p, int*q)
        a)  p + q
        b)  p * q
        c)  p / q
        d) None of these.
Here all the oprations are invalid . I Dont know whether it is wrong question or wrong answer.

14. int *( *test) (char*)  ---> what it represents;
15.for( i = 0 ; i < argc ; i++) {
    printf(" %s" , argv[i] );
}
what is output - if   input is ./a.out *
Ans - prints lists of files in directory.

16. what is pointer?

17. Program to print command line arguments and environment variables.
18. what does ptr++ and *ptr++ will do?
19. How to generate segmentation fault?
20. Why do we need pointers?
21. Can we return more than two values from a function without using Pointers?
22. What are the advantages & disadvantages of pointers?
23. Write a program equivalent to the below program using pointers?
main() {
int x;
x = 5 * 2;
printf(“%d”, x);
}

main() {
int *x;
x = (int *)malloc(sizeof(int));
*x = 5 *2;
printf(“%d”, *x);
free(x);
}

24. Where do we use single pointers and double pointers?
25. Diff between preincrement and post increment in case of pointers?

26. Output?
            int a[] = {1,2,3,4,5};
            p = a;
            pf(%d,*p++);

27. Explain
int * (*fun)(int (*a)[])
Answer : A function pointer which takes argument as pointer to an integer array and returns integer pointer.

28. what is size of following..
int *a[10];
int (*fun)();
void *fun();
Answer : 40 4 1

29. What is the function pointer? explained with an application.
30. How are pointer variables initialized?
31. Are pointers integers?
32. What is a pointer variable?
33. What is a pointer value and address?
34. When should a far pointer be used?
35. When would you use a pointer to a function?
36. How do you use a pointer to a function?
37. Can you add pointers together? Why would you?
38. What does it mean when a pointer is used in an if statement?
39. Is NULL always defined as 0?
40. What is a void pointer?
41. What is a null pointer?
42. How many levels of pointers can you have?
43. What is indirection?
44.       int main()
            {
                        unsigned char *c;
                        c = 'A';
             
                        while (c >= 0)
                                    printf("%d\n", (*c)++);
            }

            What will be the output of above program?

45. Why dereferencing null pointer is segmentaion fault?
46. Declare a structure variable is of pointer type and allocate memory for that.
47. What happens to the pointer p for the following code,
 int main()
 {
            int *p;
            p = p+12;
  }

48. Depict the usage of Function pointers.
49. void *p;
            p = p+ 12;
            What happens to p pointer?

50. String reverse function?
51. What is the use of function pointer (pointer to function)?
52. Char  *p = “abc”;
                        printf (“%c”, *p++);
                        printf (“%c”, ++*p);
                        printf (“%c”, (*p)++);

53. char p [] = “abc”;
            printf (“%c”, (*p)++);

54. a) const int a = 10;
      int main ( )
      {
            int *p = &a;
            *p = 5;
            printf ( " %d", a);
      }




      b) int main ()
       {
            int a= 10 ;
            *p = &a;
            (*p)++;
            printf ( "%d", a) ;
      }


Strings

1. Implement the following as recursive functions
    a. To check whether the given string is palindrome
    b. To reverse a given string
2. Search for keywords of C language in a source code and report the frequency of each keyword. (Use enumerated data type in your program)
3. Write a function
void written_amount ( unsigned int amount, char *buffer );
that converts the value in amount to words & stores them in the buffer.  This function might be used in a program that prints checks. For example value 16312 the string
SIXTEEN THOUSAND THREE HUNDRED TWELVE
should be stored in the buffer.

4. The standard I/O library does not provide a mechanism for putting commas in large integer when they are printed. Write a function that provide this capability for rupees amount. The function will convert a string digit to rupees as show below
Input : SIXTEEN THOUSAND THREE HUNDRED TWELVE
                        Output : 16,312/-
            Here is a prototype for the function:
            void rupees ( char *buffer, unsigned int amount );

5.  Implement the following functions as library functions using ar: strncpy, strncat, strncmp, strrchr, strspn, strcspn, strtok, strcasecmp, strstr, memcpy, memmove. (Hint: Write separate .c file and then use ar to combine them into one library)

6. Implement the following functions as library function using ar: isalnum, isalpha, isdigit, isprint, isupper, isxdigit, islower, iscntrl
7. Implement the Q6 functions as macro.
8. What is the output of the following code? Justify your answer.
#include<stdio.h>
int main()
{
            char *ptr="Hello"
             "'\0'"
               "world";
printf("%s",ptr);
}
9.  char *s = “Global”;
  int main(void)
  {
            strcpy (s, “Edge”);
            return 0;
  }
What is the output of the above code. Justify ?
10. Inserting comma between words in line.
11. Convert integer into string and string into integer.
12.  Arrange the words in a file in Increasing and Alphabetical order
13. Remove the common word's, and print the remaining word's and it presented line number's
14. Find the Output ?                                                                                                                                        
int main() {
char *str = “Hello World”;
str[0] = 'h';
printf(“%s”, str);
}  
Answer :Segmentation Fault


15. What is the Output
# include <stdio.h>
int main (void)
{
char *str = "Globaledge";
*str = 'g';
printf("%s", str);
return 0;
 }
            a) globaledge                                        b) Globaledge
            c) Runtime Error                                   d) Compiler Error

16. What is the output of this.
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "Global\0\Edge\0";
printf("%s\n", str);
return 0;
}
            a) Global Edge                         b) Edge
            c) Global                                              d) None

17.  What is the output of this?
#include<stdio.h>
int main()
{
            char c='a';
char *c_ptr=&c;
printf("%d & %d", sizeof(char), sizeof c_ptr);
return 0;
}

18. #include <stdio.h>
      #include <string.h>
      int main ( )
{
      char *str=”Helloworld”;
      printf(“%d %d”, sizeof(str), strlen(str));
      return 0;
      }
                        a) 10 4                                      b) 4 10
            c) 11 11                                                d) 10 10

19. What will be the output?
int main ( )
{
      char *src=”Helloworld” ;
      char *dst ;
      while ( *dst++ = *src++) ;
      printf(“%s”, dst);
      return 0;
 }

             a) Helloworld                           b) Compiler Error
            c) Runtime Error                                  d) No output

20. #include <stdio.h>
      #include <string.h>
      int main ( )
{
      char *str=”Hello\0world”;
      printf(“%d %d”, sizeof(str), strlen(str));
      return 0;
      }
            a) 10 4                                      b) 4 10
            c) 4 5                                        d) 5 4   

21. What is the output
      #include<stdio.h>
int main()
{
    char src[] = "Hi";
    char dest[] = "Hello";
      dest = src;
      printf("%s\n", src);
      return 0;
}
                        a) Hi                                                     b) HelloHi
                        c) Hello                                    d) None
22. Impliment Optimized strcpy.
23. Output?
            int main()
                        {
                                    char *str="hello";
                                    char *str1 ="world";
                                    char *p,*q;
                                    p=&str;
                                    q=&str1;
                                    while(*str1++ = *p++)
                                                ;
                                                while(*str1++ = *q++);
                                    printf("%s" star1);
                        }

25. char str[7]="ABC";
            char str2[4]="yxz" ;
            char *s, *t;
            *s = str;           // wrong
            *t = str2;          // wrong
            while(*s) s+;
            while (*s++=*t++) ;
            printf("str = %s", str);
Ans ---- IT is strcat function . But they are assigning to *s , IT is not  correct , we have to assign to s only .. There is no answer like  any Error .
      So I marked - "ABCyxz" .
26. Write strcat and strcpy  fuctions.
27.  Write a program to get a string from the console and return to the calling function without return value?
28. int main()
{
char *str="Sony is Japanese based company"
char *tmp;
tmp=str;
str=strstr(str,"Japanese ");
printf("%d %d\n",&tmp, &str);
strcat(tmp,str);
printf("%s",str);
}
29. How do you print only part of a string?
30. How can I convert a string to a number?
31. How can I convert a number to a string?
32. What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
33. Finding duplicate characters in a stream of characters.
35.Write a function to check whether the given string is palindrome or not.


36. Find the output for the following code,
int main()
{
char *s =  “Hello”;
s[0] = 'h';
printf(“%s”,s);
return 0;
}

37. Find the output of following code,
int main()
{
char s[10] = “abcde”;
int a = 3;
printf(“%c”,s[a]);
printf(“%c”,a[s]);
}
38. Find the output for the following,
int main()
{
char c ;
for(c = 120; c <= 128; c++)
{
printf(“%d ”,c);
}
return 0;
}

39. Reverse the given string on the same string.
40. Why we can't change char *s="hello" this value?
41. Write a program for string comparision?
42. int NoOftime(char *sentence,char *word),write function it should return in a sentence how many time the given word is present?
    sentence = tamiltamizh tamizh tamizh
    word = tamizh 
    return 2


44. Int *ptr = malloc (100);
suppose we have writen upto 50 bytes in ptr, and we want to remove remaining 50 bytes.
How you will do it and Write a function for the same.

45. what is the difference between strcpy and memcpy?
46. prm for strcpy and memcpy
47. memset int arr[100] to zero



No comments:

Post a Comment