Professional Documents
Culture Documents
Quiz4 Solution
Quiz4 Solution
Q1. Pick out the correct syntax of the malloc function from among
the following? c. malloc(100 * sizeof(float))
d. calloc(100, sizeof(float))
a. (int *) malloc(10*sizeof(int));
b. (int) malloc(10*sizeof(int)); Ans: d
c. malloc(10*sizeof(int));
d. (int*) malloc(10,sizeof(int)); Q7. If arr is an array of integers, which of the following
expressions is equivalent to the expression arr[0]
Ans: a
a. *arr
Q2. Have a look at the following piece of code and state the output. b. arr[0]
void main( ) c. *(arr+0)
{ d. All of these
int a[5]={1,2,3,4,5},*ptr=a;
printf("\n%d",*(ptr+3)); Ans: d
getch( );
}
Q8. Given an employee structure variable containing a name
a. Prints “3” on the screen. field, which of the following statements correctly references
b. Prints “4” on the screen. the name?
c. Prints a garbage value on the screen.
d. Results in a compiler error. a. employee
b. ptr->employee_name
Ans: b c. employee_name
d. employee.name
Q3. Have a look at the following piece of code and state the output.
void main( ) Ans: d
{
char str1[20]="ABCD",*str=str1; Q9. What will be output if you will compile and execute the
str++; following c code?
puts(str);
getch( ); void main( )
} {
int i=320;
a. Prints “ABCD” on the screen. char *ptr=(char *)&i;
b. Prints a garbage value on the screen. printf("%d",*ptr);
c. Prints an address on the screen. getch( )
d. Prints “BCD” on the screen. }
Ans: d a. 320
b. 1
Q4. Pick out the difference between malloc( ) and calloc( ) from the
following? c. 64
d. Compiler error
a. malloc( ) uses 2 parameters whereas calloc( ) uses only 1. Ans: c
b. malloc( ) uses 1 parameter whereas calloc( ) uses 2.
c. malloc( ) is used to reallocate memory whereas calloc( ) is used Q10. Which of the following is a correct way to declare two
to allocate memory. integer pointers a and b?
d. calloc( ) is used to reallocate memory whereas malloc( ) is used
to allocate memory.
a. int *a,b;
Ans: b b. int *a, int *b;
c. int *a, *b;
Q5. Which header file should be included to use functions like d. None of these
malloc( ) and calloc( )?
Ans: c
a. memory.h
b. string.h
Q11. Given the declaration statement const int * ptr; which of
c. stdlib.h
the following objects is a constant?
d. dos.h
Ans: c a. ptr
b. Both ptr and the object pointed by ptr
Q6. Which of the following statements allocates an array of 100 c. The object pointed to by ptr
floats and sets them to 0.0? d. The given declaration is not valid
a. calloc(sizeof(float))
b. calloc(100 * sizeof(float)) Ans: c
Q12. Given the declaration statement int *const ptr; which of the c. Allocated memory can be referred to only through
following objects is a constant? pointers; it does not have its own identifier.
d. Dynamic memory allocation can occur only with calloc
a. ptr and malloc.
b. Both ptr and the object pointed by ptr Ans: c
c. The object pointed to by ptr
d. The given declaration is not valid Q17. Have a look at the following piece of code and state the
output.
Ans: a
struct example
{
Q13. Determine the output of the following code:
char p;
}e1={'a'};
struct book void main( )
{ {
char title[20]; printf("%d",e1.p);
char author[20]; getch( );
int pages; }
float price;
a. Prints ‘a’ on the screen.
};
b. Prints the ASCII value of ‘a’ on the screen.
c. Prints a garbage value on the screen.
void main( )
d. Results in a compiler error.
{
struct book Cbook;
Ans: b
printf("%d ", sizeof(CBook));
getch( ); Q18. Have a look at the following piece of code and state
}
the output.
a. 46 struct example
b. 20 {
c. 8 char str[10];
d. Compiler error int ptr;
};
Ans: a void main()
{
struct example e1={abcd,12};
printf("\n%s",e1.str);
Q14. . Determine the output of the following code:
printf("\n%d",e1.ptr);
getch( );
void main( ) }
{
int i=3, *j, k; a. Prints “abcd” and “12” on the screen.
j=&i; b. Prints a garbage value on the screen.
printf("%d ", i**j*i**j+*j); c. Results in a compiler error.
getch ( ); d. Prints only 12 on the screen.
}
Ans: c
a. 81
b. 84 Q19. What is the return type of malloc?
c. 27
d. 30
a. void
b. void*
Ans: b c. int*
d. int
e. memory.h Q20. Given the declaration statement const int * ptr; which of
f. string.h the following objects is a constant?
g. dos.h
h. stdlib.h e. ptr
f. Both ptr and the object pointed by ptr
Ans: d g. The object pointed to by ptr
h. The given declaration is not valid
Q15. Which of the following statements allocates an array of 100
floats and sets them to 0.0? Ans: c
e. calloc(sizeof(float))
f. calloc(100 * sizeof(float))