Machine-translated by Google. Questions are authored in English.
Top

Library Functions Questions and Answers

51.

Which header file must be included to use the printf() and scanf() functions ?

Answer: C

Correct answer: stdio.h

The standard input and output declarations, including printf and scanf, live in stdio.h. stdlib.h provides memory allocation and conversion routines, string.h the string handling functions and math.h the mathematical ones.

Please log in or register to report this question.

52.

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);
    return 0;
}

 

Answer: B

Correct answer: 2

scanf returns the number of items successfully converted and assigned, not the values themselves. Two conversions succeed here, so it returns 2, and that is what is stored in i and printed - overwriting whatever the two conversions had put there.

Please log in or register to report this question.

53.

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);
    }
    return 0;
}

 

Answer: C

Correct answer: b

The first ungetc pushes the character back onto stdout, which is an output stream - the call fails and does nothing. The second pushes it back onto stdin, but only one character of pushback is guaranteed, and the behaviour of this loop is not reliable. On the usual implementations only a single b is printed.

Please log in or register to report this question.

54.

Which library function converts a string to a double-precision floating point value ?

Answer: C

Correct answer: atof()

atof() parses a string into a double, atoi() into an int and atol() into a long. strtok() is unrelated, splitting a string into tokens on a set of delimiters.

Please log in or register to report this question.

55.

What does the strcmp() function return when the two strings compared are identical ?

Answer: B

Correct answer: 0

strcmp() returns zero for identical strings, a negative value when the first sorts before the second and a positive value otherwise. The common mistake is to treat a zero return as false and so invert the test.

Please log in or register to report this question.

Answer: B

Correct answer: Convert floating-point number to a string

gcvt() converts a floating-point number to a string, in the shorter of fixed and exponential notation for the requested number of significant digits. It is a non-standard function of the old Unix and Turbo C libraries; sprintf is the portable way to do the same thing.

Please log in or register to report this question.

57.

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 'a' */
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}

 

Answer: B

Correct answer: aaaaa

ungetc pushes the character back onto the input stream, so the next scanf reads the same character again. The loop therefore reads and prints the same a on all five passes, giving aaaaa. Only one character of pushback is guaranteed, which is all this needs.

Please log in or register to report this question.

58.

Point out the error in the following program.

#include <stdio.h>

int main()
{
    fprintf("Compscibits");
    printf("%.ef", 2.0);
    return 0;
}

 

Answer: B

Correct answer: Error: in fprintf() statement

fprintf requires a stream as its first argument - fprintf(stdout, "..."). Called with only a string it is missing that argument, and the string is taken as the stream, which is a type error. Plain printf is the version that writes to stdout without being told.

Please log in or register to report this question.

59.

Point out the error in the following program.

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "Learn through Compscibits\0.com",  str2[120];
    char *p;
    p = (char*) memccpy(str2, str1, 'i', strlen(str1));
    *p = '\0';
    printf("%s", str2);
    return 0;
}

 

Answer: D

Correct answer: No error and prints "Learn through Compsci"

memccpy copies until the given character has been copied and returns a pointer just past it. The first 'i' in Learn through Compscibits is the one in sci, so the copy stops there and the null written through the returned pointer leaves Learn through Compsci.

Please log in or register to report this question.

60.

Point out the error in the following program.

#include <stdio.h>

int main()
{
    char str[] = "IndiaBIX";
    printf("%.#s %2s", str, str);
    return 0;
}

 

Answer: D

Correct answer: No error

There is no error. %.#s and %2s are not the specifiers the author probably intended, but printf is not obliged to reject them - the format string is parsed at run time, and undefined or unrecognised conversions do not stop the program compiling. The array declaration is perfectly ordinary.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Library Functions Questions and Answers || Library Functions MCQ Questions and Answers || Library Functions GK Questions and Answers || Library Functions GK MCQ Questions || Library Functions Multiple Choice Questions and Answers || Library Functions GK || GK on Library Functions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming