AmbitionBox

Discover Best Places to work in India

C Programming

C Programming Questions and Answers

Showing 61 - 70 of 103 questions

61

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.

62

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]


63

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

64

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

65

What will the function rewind() do?

a

Reposition the file pointer to a character reverse.

b

Reposition the file pointer stream to end of file.

c

Reposition the file pointer to begining of that line.

d

Reposition the file pointer to begining of file.

correct answer d
Answer :

Option D

Explanation :

rewind() takes the file pointer to the beginning of the file. so that the next I/O operation will take place at the beginning of the file.
Example: rewind(FilePointer);

66

What will the function rewind() do?

a

Reposition the file pointer to a character reverse.

b

Reposition the file pointer stream to end of file.

c

Reposition the file pointer to begining of that line.

d

Reposition the file pointer to begining of file.

correct answer d
Answer :

Option D

Explanation :

rewind() takes the file pointer to the beginning of the file. so that the next I/O operation will take place at the beginning of the file.
Example: rewind(FilePointer);

67

What will be the output of the program?

#include < stdio . h >#includeint main()
{
    float i = 2.5;
    printf("%f, %d", floor(i), ceil(i));
    return0;
}
a

2, 3

b

2.000000, 3

c

2.000000, 0

d

2, 0

correct answer c

Answer :

Option C

Explanation :

Both ceil() and floor() return the integer found as a double.
floor(2.5) returns the largest integral value(round down) that is not greater than 2.5. So output is 2.000000.
ceil(2.5) returns 3, while converting the double to int it returns '0'.
So, the output is '2.000000,

68

What will be the output of the program?

#include < stdio . h >int main()
{
    int i;
    i = scanf("%d %d", &i, &i);
    printf("%d\n", i);
    return0;
}
a

1

b

2

c

Garbage value

d

Error: cannot assign scanf to variable

correct answer b
Answer :

Option B

Explanation :

scanf() returns the number of variables to which you are provding the input.
i = scanf("%d %d", &i, &i); Here Scanf() returns 2. So i = 2.
printf("%d\n", i); Here it prints 2.

69

What will be the output of the program?

#include < stdio . h >int main()
{
    int i;
    char c;
    for(i=1; i <= 5; i++)
    {
        scanf("%c", &c); /* given input is 'b' */
        ungetc(c, stdout);
        printf("%c", c);
        ungetc(c, stdin);
    }
    return0;
}
a

bbbb

b

bbbbb

c

b

d

Error in ungetc statement.

correct answer c
Answer :

Option C

Explanation :

The ungetc() function pushes the character c back onto the named input stream, which must be open for reading.
This character will be returned on the next call to getc or fread for that stream.
One character can be pushed back in all situations.
A second call to ungetc without a call to getc will force the previous character to be forgotten.

70

What will be the output of the program?

#include < stdio . h >#includeint main()
{
    char *i = "55.555";
    int result1 = 10;
    float result2 = 11.111;
    result1 = result1+atoi(i);
    result2 = result2+atof(i);
    printf("%d, %f", result1, result2);
    return0;
}

 


a

55, 55.555

b

66, 66.666600

c

65, 66.666000

d

55,55

correct answer c
Answer :

Option C

Explanation :

Function atoi() converts the string to integer.
Function atof() converts the string to float.

result1 = result1+atoi(i);
Here result1 = 10 + atoi(55.555);
result1 = 10 + 55;
result1 = 65;

result2 = result2+atof(i);
Here result2 = 11.111 + atof(55.555);
result2 = 11.111 + 55.555000;
result2 = 66.666000;
So the output is "65, 66.666000" .

Select a company to compare with

vs

Similar Companies