Practice Question and Answer
8Q: 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) ); 1411 05b5cc7dce4d2b419777515c4
5b5cc7dce4d2b419777515c4- Show AnswerHide Answer
- Workspace
- SingleChoice
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 ) );
Q: Point out the error in the following program. main() { char mybuf[] = "Zanzibar" ; char yourbuf[] = " Zienckewiz"; char * const ptr = mybuf; *ptr = 'a'; ptr = yourbuf; } 1714 05b5cc7dce4d2b419777515c3
5b5cc7dce4d2b419777515c3- Show AnswerHide Answer
- Workspace
- SingleChoice
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 05b5cc7dce4d2b419777515be
5b5cc7dce4d2b419777515be- 15false
- 210true
- 3Errorfalse
- 4Garbage valuefalse
- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer : 2. "10"
Explanation :
Answer: B) 10 Explanation:
Q: How would you dynamically allocate a 2-D array of integers? 1017 05b5cc7dce4d2b419777515bd
5b5cc7dce4d2b419777515bd- Show AnswerHide Answer
- Workspace
- SingleChoice
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: 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 )); } 1959 05b5cc7dce4d2b419777515bc
5b5cc7dce4d2b419777515bc- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
for ( i=0; i < MAXROW ; i++) free (p[i]); free (p);
Q: Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how? 941 05b5cc7dce4d2b419777515bb
5b5cc7dce4d2b419777515bb- Show AnswerHide Answer
- Workspace
- SingleChoice
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 05b5cc7dce4d2b419777515ba
5b5cc7dce4d2b419777515ba- Show AnswerHide Answer
- Workspace
- SingleChoice
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 ));
Q: Point out the error, if any, in the following program. #include "stdarg.h" main() { display ( 4, 12.5, 13.5, 14.5, 44.3); } display(int num, ...) { float c; int j; va_list ptr; va_start (ptr, num); for ( j = 1; j <= num; j++) { c = va_arg ( ptr, float ); printf ("\n%f", c); } } 1478 05b5cc7dce4d2b419777515b9
5b5cc7dce4d2b419777515b9- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
While extracting a float argument using va_arg we should have useed c = va_arg (ptr, double)

