AmbitionBox

Discover Best Places to work in India

C Programming

C Programming Questions and Answers

Showing 1 - 9 of 9 questions

1

Which header file should be included to use functions like malloc() and calloc()?

a

memory.h

b

stdlib.h

c

string.h

d

dos.h

correct answer b
Answer :

Option B

2

What will be the output of the program?

#include < stdio . h >#include < stdlib . h >int main()
{
    int *p;
    p = (int *) malloc (20); /* Assume p has address of 1314 */free(p);
    printf("%u", p);
    return0;
}
a

1314

b

Garbage value

c

1316

d

Random address

correct answer a
Answer :

Option A

3

What will be the output of the program (16-bit platform)?

#include < stdio . h >#include < stdlib . h >int main()
{
    int *p;
    p = (int *) malloc (20);
    printf("%d\n", sizeof (p));
    free(p);
    return0;
}
a

4

b

2

c

8

d

Garbage value

correct answer b
Answer :

Option B

4

What will be the output of the program?

#include < stdio . h >#includeint main()
{
    char *s;
    char *fun();
    s = fun();
    printf("%s\n", s);
    return0;
}
char *fun()
{
    char buffer[30];
    strcpy(buffer, "RAM");
    return (buffer);
}
a

0xffff

b

Garbage value

c

0xffee

d

Error

correct answer b
Answer :

Option B

Explanation :

The output is unpredictable since buffer is an auto array and will die when the control go back to main. Thus s will be pointing to an array , which not exists.

5

Assume integer is 2 bytes wide. What will be the output of the following code?

#include < stdio . h >#include < stdlib . h >#define MAXROW 3#define MAXCOL 4int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL]) malloc( MAXROW *sizeof (*p));
    printf("%d, %d\n", sizeof(p), sizeof(*p));
    return0;
}
a

2, 8

b

4, 16

c

8, 24

d

16, 32

correct answer a
Answer :

Option A

6

How many bytes of memory will the following code reserve?

#include < stdio . h >#include < stdlib . h >int main()
{
    int *p;
    p = (int *)malloc(256 * 256);
    if(p == NULL)
        printf("Allocation failed");
    return0;
}
a

65536

b

Allocation failed

c

Error

d

No output

correct answer b
Answer :

Option B

Explanation :

Hence 256*256 = 65536 is passed to malloc() function which can allocate upto 65535. So the memory allocation will be failed in 16 bit platform (Turbo C in DOS).

If you compile the same program in 32 bit platform like Linux (GCC Compiler) it may allocate the required memory.

7

Point out the error in the following program.

#include#include < stdlib . h >int main()
{
    int *a[3];
    a = (int*) malloc ( sizeof (int) *3);
    free(a);
    return0;
}
a

Error: unable to allocate memory

b

Error: We cannot store address of allocated memory in a

c

Error: unable to free memory

d

No error

correct answer b
Answer :

Option B

Explanation :

We should store the address in a[i]


8

Point out the error in the following program.

#include#include < stdlib . h >int main()
{
    char *ptr;
    *ptr = (char)malloc(30);
    strcpy(ptr, "RAM");
    printf("%s", ptr);
    free(ptr);
    return0;
}
a

Error: in strcpy() statement.

b

Error: in *ptr = (char)malloc(30);

c

Error: in free(ptr);

d

No error

correct answer b
Answer :

Option B

Explanation :

ptr = (char*)malloc(30);

9

How will you free the memory allocated by the following program?

#include#include#define MAXROW 3#define MAXCOL 4int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return0;
}
a

memfree(int p);

b

dealloc(p);

c

malloc(p, 0);

d

free(p);

correct answer d

Answer:Option D

Select a company to compare with

vs

Similar Companies