5
Q:

How many times i value is checked in the below code?

       #include <stdio.h>
        int main()
        {
            int i = 0;
            do {
                i++;
                printf( "In while loopn" );
            } while (i < 3);
        }

A) 2 B) 3
C) 4 D) 1

Answer:   B) 3



Explanation:
Subject: Programming
Q:

How would you free the memory allocated by the following program?

#include "alloc.h"

#define MAXROW 3

#define MAXCOL 4

main()

{

     int **p, i;

     p = (int **) malloc (MAXROW * sizeof (int *));

      for ( i = 0; i < MAXROW ; i++)

            p[i] = (int *) malloc (MAXCOL * sizeof (int ));

}

Answer

for ( i=0; i < MAXROW ; i++)


free (p[i]);


free (p);

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3523
Q:

How would you dynamically allocate a 2-D array of integers?

Answer

#include "alloc.h"


#define MAXROW 3


#define MAXcol 4


main()


{


        int *p, i, J;


        p = (int *) malloc (MAXROW * MAXCOL * sizeof (int));


         for ( i=0; i < MaxROW ; i++)


         {


                for (j=0; j < MAXCOL ; j++)


                { 


                      p [ i * MAXCOL + j] = i;


                       printf ( "%d", p [i * MAXCOL + j] );


                 }


                  printf ("\n");


          }


}

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3112
Q:

What would be the output of the following program ?

main()

{

     const int x = 5; 

      int *ptrx;

      ptrx = &x;

      *ptr = 10;

       printf ("%d", x);

}

A) 5 B) 10
C) Error D) Garbage value
 
Answer & Explanation Answer: B) 10

Explanation:
Report Error

View Answer Report Error Discuss

Filed Under: Programming

2 11176
Q:

Point out the error in the following program.

main()

{

    char mybuf[] = "Zanzibar" ;

    char yourbuf[] = " Zienckewiz";

    char * const ptr = mybuf;

     *ptr = 'a';

     ptr = yourbuf;

}

Answer

ptr pointer is constant. In ptr = yourbuf the program is trying to modify it, hence an error.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

5 6849
Q:

Improve the following code using typedef.

struct node

{

      int data1;  float data2;

       struct node *left;

       struct node *right;

};

struct node *ptr;

ptr = (struct node *) malloc (sizeof (struct node) ); 

Answer

typedef struct node * treeptr


typedef struct node


{


         int data1;


         float data2;


         treeptr *left;


         treeptr *right;


}treenode;


treeptr ptr;


ptr = ( treeptr ) malloc ( sizeof ( treenode ) );

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2458
Q:

In the following code can we declare a new typedef name emp even though struct employee has not been completely defined while using typedef? < Yes / No>

typedef struct employee *ptr;

struct employee

{

       char name[20];

        int age;

        ptr next;

};

Answer

Yes

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2684
Q:

Answer the following Program

#define CHARSIZE 8

#define MASK(y) (1 << y % CHARSIZE)

#define BITSLOT (y) (y / CHARSIZE)

#define SET(x,y) ( x[BITSLOT(y)] = MASK(y) )

#define TEST(x,y) ( x[BITSLOT(y)] & MASK(y) )

#define NUMSLOTS(n) ((n + CHARSIZE - 1) / CHARSIZE)

 

Give the above macros how would you

1. declare an array arr of 50 bits

2. put the 20th bit on

3. test whether the 40th bit is on or off

Answer

1. char arr[NUMSLOTS(50)];


2. SET (arr, 20);


3. if (TEST (arr, 40))

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2298
Q:

What would be the output of the following program ?

main() 

{

       unsigned  int a = oxffff;

        ~a;

        printf ("%x", a);

}

A) ffff B) 0000
C) 00ff D) None of the above
 
Answer & Explanation Answer: A) ffff

Explanation:
Report Error

View Answer Report Error Discuss

Filed Under: Programming

0 10702