Practice Question and Answer

  • Show AnswerHide Answer
  • Workspace

Answer :
Explanation :

typedef struct node * treeptr typedef struct node {          int data1;          float data2;          treeptr *left;          treeptr *right; }treenode; treeptr ptr; ptr = ( treeptr ) malloc ( sizeof ( treenode ) );

  • Show AnswerHide Answer
  • Workspace

Answer :
Explanation :

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

Q: What would be the output of the following program ? main() {      const int x = 5;        int *ptrx;       ptrx = &x;       *ptr = 10;        printf ("%d", x); } 1494 0

  • 1
    5
    Correct
    Wrong
  • 2
    10
    Correct
    Wrong
  • 3
    Error
    Correct
    Wrong
  • 4
    Garbage value
    Correct
    Wrong
  • Show AnswerHide Answer
  • Workspace

Answer : 2. "10"
Explanation :

Answer: B) 10 Explanation:

Q: How would you dynamically allocate a 2-D array of integers? 1017 0

  • Show AnswerHide Answer
  • Workspace

Answer :
Explanation :

#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");           } }

Q: Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how? 941 0

  • Show AnswerHide Answer
  • Workspace

Answer :
Explanation :

Yes, using the realloc() function as shown below: main() {         int *p;         p = ( int *) malloc (20) ;         t = p;         t = (int *) realloc ( p, 40);         if ( t == NULL )         Printf (" Cannot reallocate, leaves previous allocated region unchanged ");        else        {               if ( p ==t )               ;  / * the array expanded at the same region */              else             {                   free ( p ); / * deallocate the original array */                  p = t;  /* set p to newly allocated region */              }       } }

Q: What is the difference between malloc() and calloc() functions? 1886 0

  • Show AnswerHide Answer
  • Workspace

Answer :
Explanation :

As against malloc(), calloc() needs two arguments, the number of elements  to be allocated and the size of each element. For example,  p = (int *) calloc (10, sizeof (int)); would allocate space for a 10- integer array. Additionally, calloc() would also set each of this element with a value 0. Thus the above call to calloc() is equivalent to: p = (int *) malloc (10 * sizeof (int)); memset (p, 0, 10 * sizeof( int ));

      Report Error

    Please Enter Message
    Error Reported Successfully

      Report Error

    Please Enter Message
    Error Reported Successfully

      Report Error

    Please Enter Message
    Error Reported Successfully

      Report Error

    Please Enter Message
    Error Reported Successfully

      Report Error

    Please Enter Message
    Error Reported Successfully

      Report Error

    Please Enter Message
    Error Reported Successfully

      Report Error

    Please Enter Message
    Error Reported Successfully

      Report Error

    Please Enter Message
    Error Reported Successfully