C/C++ interview quest

1. What would be the output of the following program ?

void main()
{
    char a[] ="Visual C++";
    char *b = "Visual C++";
    printf("\n%d %d",sizeof(a),sizeof(b));
    printf("\n%d %d",sizeof(*a),sizeof(*b));
}

Ans. 11  2   1    1


2. For the following statements would arr[3] and ptr[3] fetch the same character?  <yes/no>

char arr[] = "Surprised";
char *ptr = "Surprised";

Ans. Yes

3.  In the above example Q.2, would they also fetch in the same manner ?

arr[3] is three places past the start of the object named arr, whereas ptr[3] is three places past the object pointed to by ptr.

4. What would be the output of the following program, if the array begins at address 1200 ?

main()
{
    int arr[] = {2,3,4,1,6};
    printf("%d %d",arr,sizeof(arr));
}


Ans.  1200 10

5. Does mentioning the array name gives the base address in all the contexts ?
No.
Whenever mentioning the array name gives its base address it  is said that the array has decayed into a pointer.  This decaying doesn't take place in 2 situations :-

a)  When array name is used with sizeof operator.
b)  When the array name is an operand of the & operator.

6.  What would be output of the following program if the array begins at address 65486 ?

main()
{
    int arr[] = {12,14,15,23,45};
    printf("%u %u",arr,&arr);
}

Ans. arr = 65486,  &arr = 65486 (see the explanation in Q.7)


7.  Are the expressions arr and &arr same for an array of 10 integers ?
No
arr gives the address of the first int
&arr gives the address of array of ints

8. what would be the output of the following program, if the array begins at 65486 ?

main()
{
    int arr[] = {12,14,15,23,45};
    printf("%u %u",arr+1,&arr+1);
}

Ans. 65488  65496


9.  When are char a[] and char *a treated as same by the
compiler ?

When using them as formal parameters while defining a function.

10.  Would the following program compile successfully?

main()
{
    char a[]="Sunstroke";
    char *p = "Coldwave";
    a="Coldwave";
    p="Sunstroke";
    printf("\n%s %s",a,p);
}

Ans. No. because we may assign a new string to a pointer but
not to an array.

11.   what would be the output of the following program :-

main()
{
    float a[] = {12.4, 2.3, 4.5, 6.7};
    printf("\n%d",sizeof(a) / sizeof(a[0]));
}

Ans. 4

12. A pointer to a block of memory is effectively same as an array.  <true/false> ?   true

13. what would be the output of the following program if the array begins at 65472 ?

main()
{

    int a[3][4] = {

             1,2,3,4,       
             4,3,2,1,
             7,8,9,0
              };

    printf("\n%u %u",a+1,&a+1);
}

Ans. 65480  65496


14.  What does the following declaration mean ?
int (*ptr)[10];

Ans. ptr is a pointer to an array of 10 integers.

15.  If we pass the name of a 1-D int array to a function it decays into a pointer to an int.  If we pass the name of  a 2-D array of integers to a function what would it decay into ?

Ans. It decays into a pointer to an array and not
    a pointer to a pointer.

16.  How would you define the function f() in the following program ?

int arr[MAXROW][MAXCOL];
fun(arr);


Ans.  fun(int a[][MAXCOL]) { }


17. What would be the output of the following program :-

int a[3][4] = {

         1,2,3,4,
         4,3,2,8,
         7,8,9,0
          };
int *ptr;
ptr=&a[0][0];
fun(&ptr);
}

fun(int **p)
{
    printf("\n%d",**p);
}

Ans. 1

------------------------------------------------------------------------------------------------------------------------
1.  Can you combine the following two statements
into one ?
        char *p;
        p=malloc(100);

    Ans. char *p = malloc(100);

2. Are the expressions *ptr++ and ++*ptr same ?
*ptr++ increments the pointer and not the value pointed by it,
whereas ++*ptr increments the value being pointed to by ptr.

3. Can you write another expression which does the same job
as ++*ptr ?

Ans. (*ptr)++

4.  What would be the equivalent pointer expression for
referring the same element a[i][j][k][l] ?

Ans. *(*(*(*(a+i)+j)+k)+l)

5. What would be the output of the following program ?

main()
{
    int arr[] = {12,13,14,15,16};
    printf("\n%d %d %d",sizeof(arr),sizeof(*arr), sizeof(arr[0]));
}

Ans.  10  2  2
   
6. What would be the output of the following program assuming that the array
begins at location 1002 ?

main()
{
    int a[3][4] = {
            1,2,3,4,
            5,6,7,8,
            9,10,11,12
              };

    printf("\n%u %u %u",a[0]+1, *(*(a+0)+1));
}

output :- 1004 2 2

7. what would be the output of the following program assuming that the array
begins at location 1002 ?

int a[2][3][4] = {

            { 
                1,2,3,4,
                    5,6,7,8,
                9,1,1,2
            },
            {
                2,1,4,7,
                6,7,8,9,
                0,0,0,0
            }
        };

    printf("\n%u %u %u %d",a,*a,**a,***a);
}

output :-   1002  1002  1002  1


8. In the following program how would you print 50 using p ?

    main()
    {
        int a[] = {10,20,30,40,50};
        char *p;
        p = (char*)a;
   
    }

Ans. :-  printf("\n%d",*((int*)p+4));

9. Where can one think of using pointers ?

1.  Accessing array or string elements
2.  Dynamic Memory Allocation
3.  Call by reference
4.  Implementing linked lists, trees, graphs and many other data structures.


10.  In the following program add a statement in the function fun() such that address of a gets stored in j.

main()
{
    int *j;
    void fun(int **);
    fun(&j);
}

void fun(int **k
{
    int a = 10;
    /* add statement here */
}


Ans.  *k = &a;


11.  How would you declare an array of three function pointers where each function receives two ints and returns a float ?

Ans. :-   float (*arr[3])(int, int);

12.  Would the following program give a compilation error
    or warning ? <yes/no>

main()
{
    float i = 10, *j;
    void *k;
    k=&i;
    j=k;
    printf("\n%f",*j);
}

Ans. No. Here no typecasting is required while assigning the value to and from k because conversions are applied automatically when other pointer types are assigned to and from
void *.

13.  Would the following program compile ?

    main()
    {
        int a = 10, *j;
        void *k;
        j=k=&a;
        j++;
        k++;
        printf("\n%u %u",j,k);
    }

Ans. :-  No. An error would be reported in the statement k++ since arithmetic on void pointers is not permitted unless the void pointer is appropriately typecasted.


14.Would the following code compile successfully ?

main()
{
    printf("%c",7["Sundaram"]);
}

Ans. It would print m of Sundaram

15.  Is the NULL pointer same as uninitialized pointer ?
    <yes/no>
    No

16.  In which header file is the NULL macro defined ?
Ans.  In files "stdio.h" and "stddef.h"

17. Why is it for large memory models NULL has been defined as 0L and for small memory models as just 0 ?

Because in small memory models the pointer is two bytes long whereas in large memory models it is 4 bytes long.

18. what is a NULL pointer ?
For each pointer type (like say a char pointer) C defines a special pointer value which is guarenteed not to point to any
object or function of that type.  Usually the null pointer
constant used for representing a null pointer is the integer 0.

19. What's the difference between a null pointer, a NULL macro, the ASCII NUL character and a null string ?

A null pointer is a pointer which doesn't point anywhere.

A NULL macro is used to represent the null pointer in source
code. It has a value 0 associated with it.

The ASCII NUL character has all its bits as 0 but doesn't have any relationship with the null pointer.

The null string is just another name for an empty string "".


20.  What would be the output of the following program :-

main()
{
    int a,b=5;
    a=b+NULL;
    printf("%d",a);
}

Ans. 5


21.  what would be the output of the following program ?

main()
{
    printf("%d %d",sizeof(NULL),sizeof(""));
}

Ans. 2 1

22. Would the following program give any warning on compilation ?
main()
{
    float *p1,i=25.50;
    char *p2;
    p1=&i;
    p2=&i;
}

Yes.  Suspicious pointer conversion in function main
------------------------------------------------------------------------------------------------------------------------
1. what is the similarity between a structure, union and an enumeration ?

All of them let you define new data types.


2. Would the following declaration work ?
   (typedef defines a new name for a type)
    typedef struct s
    {
        int a;
        float b;
    }s;

Ans. Yes

3.Can a structure contain a pointer to itself ?

Certainly, such structures are known as self-referential structures.

4. Point out the error in the following code :-

    typedef struct
    {
        int data;
        NODEPTR link;
    }*NODEPTR;


Ans. error becos typedef declaration cannot be used until it is defined. To fix this code, follow the 3 examples (any method)

Method 1:
typedef struct node
{
    int data;
    struct node *link;
}*NODEPTR;


Method 2:
typedef struct node *NODEPTR

struct node
{
    int data;
    NODEPTR next;
};


Method 3:
struct node
{
    int data;
    struct node *link;
};
typedef struct node *NODEPTR;

5. Point out the error, if any,in the following code:-

void modify(struct emp *);
struct emp
{
    char name[20];
    int age;
};
main()
{
    struct emp e = {"Sanjay",35};
    modify(&e);
    pritf("\n%s %d",e.name,e.age);
}

void modify(struct emp *p)
{
    strupr(p->name);
    p->age=p->age+2;
}


Ans.  function prototype must be after the struct definition.

6. What would be the output of the following program ?

main()
{
    struct emp
    {
        char *n;
        int age;
    };
    struct emp e1 = {"Dravid",23};
    struct emp e2 = e1;
    strupr(e2.n);
    printf("\n%s",e1.n);
}

Ans. DRAVID
Copies of any pointer fields will point to the same place as
the original. Anything pointed to is not copied.

7.  Point out the error, if any, in the following code:-

main()
{
    struct emp
    {
        char n[20];
        int age;
    };
    struct emp e1 = {"Dravid",23};
    struct emp e2 = e1;
    if(e1==e2)
       printf("The structures are equal");
}

Ans. Structures cannot be compared using the built-in
     == operators. We have to write our own function to do so.


8. How to solve the problem of Q.7 ?

struct emp
{
    char n[20];
    int age;
}
main()
{
    struct emp e1 = {"Dravid",23};
    struct emp e2;
    scanf("%s %d",e2.n, &e2.age);
    if(structcmp(e1,e2)==0)
        printf("Structures are equal");
    else
        printf("Structures are not equal");
}

structcmp(struct emp x, struct emp y)
{
    if(strcmp(x.n,y.n)==0)
        if(x.age==y.age)
            return(0);
    return(1);
}


9. Point out the error, if any, in the following code.

main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a z1={512};
    union a z2={0,2};
}


Ans.  ANSI C standard allows an initializer for the first member
of a union. There is no standard way of initializing any other
member, hence the error in initializing z2.

solution:-
==========
union a
{   
    int i;
    char ch[2];
};

union b
{
    char ch[2];
    int i;
};

main()
{
    union a z1 = {512};
    union b z2 = {0,2};
}


10.  What is the difference between Structure and Union ?

Ans.  A union is essentially a structure in which all of the fields overlay        each other.  You can see only one field at a time.

11.  Is it necessary that size of all elements in a Union should be the same?

No. Union elements can be of different sizes.  If so, size of the union is size of the longest element in the union.  As against this the size of a structure is the sum of the size of
its members.  In both cases, the size may be increased by padding.