C-Pointers Question and Answer
C-Pointers Question and Answer
91. main ( )
{
char x [25], y[25], *p = y;
strcpy (x, "BIRTHDAY");
strcpy (y, "HAPPY");
p=x;
strcpy (x, "Lover");
*p = 'd';
printf("p=%s\n", p);
}
What will be the output when the sample code above is executed ?
{
char x [25], y[25], *p = y;
strcpy (x, "BIRTHDAY");
strcpy (y, "HAPPY");
p=x;
strcpy (x, "Lover");
*p = 'd';
printf("p=%s\n", p);
}
What will be the output when the sample code above is executed ?
- p=DOVERDAY
- p=LOVERDAY
- p=DOVER
- p=BIRTHDAY
92. int *x;x = (int *) 15;
Is the above code legal?
Is the above code legal?
- Yes. A new memory space will be alocated to hold the number 15.
- No. This would assign the number 15 to an unallocated space in memory.
- Yes. Upon initialization, the number 15 will be stored in a special pointer memory address space.
- Yes. The pointer x will point at the integer in memory location 15.
93. Which statement correctly defines a character string with a length of 4 plus 1 for the terminating NUL?
- char string [ ] ;
- char * string;
- char * string [5];
- char string [5];
94. Given that a is an arrray of elements of type t, val is a 1value expression of type "pointer to t" that points to elements in a then * ++val.
- sets val to point to the next element in a.
- increments val and then references the value in a, that val points to.
- increments val and then references the value in a, that val points to.
- references the element of a that val points to.
95. int Y[4] = {6, y, 8, 9};
int *ptr= Y +2;
printf ("%d\n", ptrl[1]);
What is printed when the sample code above is executed ?
int *ptr= Y +2;
printf ("%d\n", ptrl[1]);
What is printed when the sample code above is executed ?
- 7
- 8
- 9
- The code will not compile.
96. printf ("%s\n", string);
Which of the following initializations for the string variable will cause the code above to print the string "First grade actor" when executed without memory leads or memory overwrite ?
Which of the following initializations for the string variable will cause the code above to print the string "First grade actor" when executed without memory leads or memory overwrite ?
- char *string = malloc (100); string = "First grade actor";
- char string [ ]= "Hellow again"; string = "First grade actor";
- char *string = "Hellow again"; string = "First grade actor";
- char string [100]; string = "First grade actor";
97. # include <ctype.h>
char s [ ] = "Photo Flash";
char *ptr = s;
Referring to the code above, which of the following code is the best way to convert the string s to all lower case letters?
char s [ ] = "Photo Flash";
char *ptr = s;
Referring to the code above, which of the following code is the best way to convert the string s to all lower case letters?
- for ( ; *ptr; ptr++) { if (isalpha (*ptr) && isupper (*ptr)) *ptr = tolower (*ptr); }
- tolower ( ptr );
- for ( ; *ptr; ptr++ ) { if (isupper (*ptr)) *ptr = tolower (*ptr); }
- for ( ; *ptr; ptr ++) { *ptr = tolower ( *ptr); }
98. exern void *ptr1;
extern void *ptr2;
int compare ( int n )
{
return ????;
}
What should replace the ???? in the code above to compare the first n bytes of the memory pointed to by ptrl and ptr2 and return a non-zero value if they are equal ?
extern void *ptr2;
int compare ( int n )
{
return ????;
}
What should replace the ???? in the code above to compare the first n bytes of the memory pointed to by ptrl and ptr2 and return a non-zero value if they are equal ?
- memcmp (ptr1, ptr2, n)
- strncmp ( ptr1, ptr2, n)
- strncmp (ptr1, ptr2, n)
- !memcmp (ptr1, ptr2, n)
99. What memory function should be used to allocate memory in which all bits are initialized to 0 ?
- calloc
- malloc
- alloc
- memalloc
100. What is the primary differrence between the maolloc and calloc functions ?
- Memory allocated by calloc doesnot need to be freed and memory allocated by malloc does.
- calloc returns a pointer to char and malloc returns a void pointer.
- calloc initializes the memory returned and malloc does not.
- calloc can allocate memory for an array and malloc cannot.