C String Test 1
1) Which of the function is more appropriate for reading a multi-word string?
- puts()
- gets()
- printf()
- scanf()
The correct option is (b).
Explanation:
The function gets() is used for collecting a string of characters terminated by new line from the standard input stream stdin.
Therefore gets() is more appropriate for reading a multi-word string.
2) Which library function can change an unsigned long integer to a string?
- system()
- ltoa()
- ultoa()
- unsigned long can’t be change into a string
The correct option is (c).
Explanation:
The function ultoa() is used for converting an unsigned long integer to a string.
3) What is the value return by strcmp() function when two strings are the same?
- 2
- 1
- 0
- Error
The correct option is (c).
Explanation:
C library function strcmp() compares the two strings with each other and the value is return accordingly.
Comparison occurs between a first string (str1) with a second string (str2).
On comparing the two string, the values return by a function strcmp() are:
- If, str1 is equal to str2 then Return value = 0
- If, str1 is greater than str2 then Return value > 0
- If, str1 is less than str2 then Return value < 0
4) What is built in library function for comparing the two strings?
- strcmp()
- equals()
- str_compare()
- string_cmp()
The correct option is (a).
Explanation:
The strcmp() is a built-in function available in “string.h” header file. It is used for comparing the two strings. It returns 0 if both are same strings. It returns positive value greater than 0 if first string is greater than second string, otherwise it returns negative value.
5) What will be the output of the below program?
- b
- a
- A
- 65
The correct option is (c).
Explanation:
Step 1: char a[] = “%dn”; The variable ‘a’ is declared as an array of characters and initialized with string “%d”.
Step 2: a[1] = ‘b’; Here, we overwrite the second element of array ?a? by ‘b’. Hence the array ?a? becomes “%c”.
Step 3: printf(a, 65); becomes printf(“%c”, 65);
Therefore it will print the ASCII value of 65. Hence the output is ‘A’.