Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CSE101 ( Computer Programming) AT #4 (Quiz Solution) SET A

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

Q15. Which of the following statements successfully releases an


Ans: b
array ptr of 100 integers?
Q20. Which of the following statements about initializing
a. free(ptr);
b . free(ptr[100]; structures is false?
c. free(ptr + 100;
d. for(i = 0; i < 100; i++) free(ptr + i); a. Initializers are enclosed in a set of braces.
b. The initializer values must match their
Ans: a corresponding types in the structure.
c. The initializer values must be separated by
Q16. Which of the following statements about memory allocation is commas.
true? d. Structure initialization must be complete; that is,
a.calloc (change allocation) is used to change the allocation to if one field is initialized all fields must be
memory previously allocated through malloc. initialized.
b.malloc (memory allocation) is used to allocate blocks of
memory for arrays. Ans: d
CSE101 ( Computer Programming) AT #4 (Quiz Solution) SET B
Q1. Given the declaration statement int *const ptr; which of the
following objects is a constant? c. Dynamic memory allocation can occur only with calloc
and malloc
d. Allocated memory can be referred to only through
e. Both ptr and the object pointed by ptr
pointers; it does not have its own identifier.
f. The object pointed to by ptr
g. ptr Ans: d
h. The given declaration is not valid
Q6. Have a look at the following piece of code and state the
Ans: c output.
struct example
Q2. Determine the output of the following code: {
struct book char p;
{ }e1={'a'};
char title[20]; void main( )
char author[20]; {
int pages; printf("%d",e1.p);
int price; getch( );
}; }
e. Prints ‘a’ on the screen.
void main( ) f. Prints a garbage value on the screen.
{ g. Prints the ASCII value of ‘a’ on the screen.
struct book Cbook; h. Results in a compiler error.
printf("%d ", sizeof(CBook));
getch( ); Ans: c
}
Q7. Have a look at the following piece of code and state the
e. 44 output.
f. 46
g. 8 struct example
h. Compiler error {
char str[10];
Ans: a int ptr;
};
void main()
Q3. Determine the output of the following code:
{
void main( )
struct example e1={“abcd”,12};
{
printf("%s",e1.str);
int i=3, *j, k;
printf("%d",e1.ptr);
j=&i;
getch( );
printf("%d ", i**j*i**j-*j);
}
getch ( );
}
e. Prints abcd and 12 on the screen.
f. Prints a garbage value on the screen.
e. 81 g. Results in a compiler error.
f. 84 h. Prints only 12 on the screen.
g. 27
h. 78 Ans: a

Q8. What is the return type of malloc?


Ans: d e. void *
f. void
Q4. Which of the following statements successfully releases an g. int*
array ptr of 100 integers? h. int
Ans: a
a. free(ptr + 100;
b . free(ptr[100]; Q9. Which of the following statements about initializing
c. free(ptr); structures is false?
d. for(i = 0; i < 100; i++) free(ptr + i);
e. Initializers are enclosed in a set of braces.
Ans: c f. Structure initialization must be complete; that is,
if one field is initialized all fields must be
Q5. Which of the following statements about memory allocation is initialized.
true? g. The initializer values must match their
a.calloc (change allocation) is used to change the allocation to corresponding types in the structure.
memory previously allocated through malloc. h. The initializer values must be separated by
b. malloc (memory allocation) is used to allocate blocks of commas.
memory for arrays.
Ans: b
Q10. Pick out the correct syntax of the malloc function from among g. malloc(100 * sizeof(float))
the following? h. calloc(100, sizeof(float))
e. (int) malloc(10*sizeof(int)); Ans: d
f. (int *) malloc(10*sizeof(int));
g. malloc(10*sizeof(int));
h. (int*) malloc(10,sizeof(int)); Q16. If arr is an array of integers, which of the following
expressions is equivalent to the expression arr[0]
Ans: b
e. *arr
f. arr[0]
Q11. Have a look at the following piece of code and state the
g. *(arr+0)
output.
h. All of these
void main( )
{
int a[5]={1,2,3,4,5},*ptr=a; Ans: d
printf("\n%d",*(ptr+2));
getch( );
} Q17. Given an employee structure variable containing a name
field, which of the following statements correctly references
e. Prints “3” on the screen. the name?
f. Prints “4” on the screen.
g. Prints a garbage value on the screen. e. employee.name
h. Results in a compiler error. f. employee
g. ptr->employee_name
h. employee_name
Ans: a
Ans: a
Q12. Have a look at the following piece of code and state the output.
void main( ) Q18. What will be output if you will compile and execute the
{ following C code?
char str1[20]="ABCD",*str=str1;
str++;
void main( )
puts(str);
{
getch( );
int i=320;
}
char *ptr=(char *)&i;
printf("%d",*ptr);
e. Prints “BCD” on the screen.
getch( )
f. Prints a garbage value on the screen.
}
g. Prints an address on the screen.
h. Prints “ABCD” on the screen.
a. 320
Ans: a b. 64
c. 1
Q13. Pick out the difference between malloc( ) and calloc( ) from d. Compiler error
the following?
Ans: b
e. malloc( ) uses 2 parameters whereas calloc( ) uses only 1.
f. malloc( ) is used to reallocate memory whereas calloc( ) is used Q19. Which of the following is a correct way to declare two
to allocate memory. integer pointers a and b?
g. malloc( ) uses 1 parameter whereas calloc( ) uses 2.
h. calloc( ) is used to reallocate memory whereas malloc( ) is used e. int *a,*b;
to allocate memory. f. int *a, * int b;
Ans: c g. int *a, b;
h. None of these
Q14. Which header file should be included to use functions like
malloc( ) and calloc( )? Ans: a

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))

You might also like