logo

CuriousTab

CuriousTab

Programming problems


  • 1. What would be the output of the following program? /* sample.c */ main ( int argc, char **argv ) { argc = argc - (argc -1); printf ("%s", argv[argc - 1]); }
  • Discuss
  • 2. If the following program (myprog) is run from the command line as myprog 1 2 3 what would be the output? main(int argc, char *argv[]) { int i, j = 0; for (i = 0; i < argc ; i++) j = j + atoi ( argv[i]); printf ("%d", j); }

  • Options
  • A. 123
  • B. 6
  • C. Error
  • D. "123"
  • Discuss
  • 3. What would be the output of the following program ? main() { unsigned int a = oxffff; ~a; printf ("%x", a); }

  • Options
  • A. ffff
  • B. 0000
  • C. 00ff
  • D. None of the above
  • Discuss
  • 4. 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
  • Discuss
  • 5. 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; };
  • Discuss
  • 6. 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) );
  • Discuss
  • 7. How would you dynamically allocate a 2-D array of integers?
  • Discuss
  • 8. 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 )); }
  • Discuss
  • 9. Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?
  • Discuss
  • 10. What is the difference between malloc() and calloc() functions?
  • Discuss

First 8 9 10 ... .. Last