Exercise: Strings

Questions for: Strings

Which of the following statements are correct ?
1: A string is a collection of characters terminated by '\0'.
2: The format specifier %s is used to print a string.
3: The length of the string can be obtained by strlen().
4: The pointer CANNOT work on string.
A:
1, 2
B:
1, 2, 3
C:
2, 4
D:
3, 4
Answer: B

Clearly, we know first three statements are correct, but fourth statement is wrong. because we can use pointer on strings. Eg. char *p = "ExamAdept".

Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";
1: There is no difference in the declarations and both serve the same purpose.
2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer.
3: The pointer p can be modified to point to another string, whereas the individual characters within array a can be changed.
4: In both cases the '\0' will be added at the end of the string "Sanjay".
A:
1, 2
B:
2, 3, 4
C:
3, 4
D:
2, 3
Answer: B
No answer description is available. Let's discuss.
Which of the following statements are correct about the program below?
#include<stdio.h>

int main()
{
    char str[20], *s;
    printf("Enter a string\n");
    scanf("%s", str);
    s=str;
    while(*s != '\0')
    {
        if(*s >= 97 && *s <= 122)
            *s = *s-32;
        s++;
    }
    printf("%s",str);
    return 0;
}
A:
The code converts a string in to an integer
B:
The code converts lower case character to upper case
C:
The code converts upper case character to lower case
D:
Error in code
Answer: B

This program converts the given string to upper case string.

Output:

Enter a string: ExamAdept

ExamAdept

What will be the output of the following program in 16 bit platform assuming that 1022 is memory address of the string "Hello1" (in Turbo C under DOS) ?
#include<stdio.h>

int main()
{
    printf("%u %s\n", &"Hello1", &"Hello2");
    return 0;
}
A:
1022 Hello2
B:
Hello1 1022
C:
Hello1 Hello2
D:
1022 1022
Answer: A

In printf("%u %s\n", &"Hello", &"Hello");.

The %u format specifier tells the compiler to print the memory address of the "Hello1".

The %s format specifier tells the compiler to print the string "Hello2".

Hence the output of the program is "1022 Hello2".

What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    printf("%c\n", "abcdefgh"[4]);
    return 0;
}
A:
Error
B:
d
C:
e
D:
abcdefgh
Answer: C

printf("%c\n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".

Hence the output is 'e'.

Ad Slot (Above Pagination)
Quiz