string interview questions

string interview questions

1. What will be the output of the following program. ?

char* p = “string1”;
printf(“%c”, *++p);

a. No output
b. compile time Error
c. Run time Error
d. Strings Equal

Show Answer

The correct option is (a)
Explanation:
In this program we comparing the base address of ‘a’ and ‘b’ and that are not same. Therefore the program has No output.

2. Which of the following C language code snippet is not valid?

a. char q[] = “string1”; printf(“%c”, *++q);
b. char* p = “string1”; printf(“%c”, *++p);
c. char* r = “string1”; printf(“%c”, r[1]);
d. None of the above

Show Answer

The correct option is (b)
Explanation:
as p is a pointer pointing to character ‘s’ of “string1”. Using ++p, p will point to character ‘t’ in “string1”. Therefore, *++p will print ‘t’.

3. What is the output of C Program with String.?

int main()
{
char str[]={‘H’,’a’,’p’,’p’,’y’};
printf(“%s”,str);
return 0;
}

a. H
b. Happy
c. Happy\0
d. None of these

Show Answer

The correct option is (a)
Explanation:
Adding a NULL or \0 at the end is a correct way of representing a C string. You can simple use char str[]=”Happy”. It is same as above.

4. How do you convert char array to string.?

char strg[]={‘S’,’m’,’i’,’l’,’e’};

a. strg[5]=’\0′;
b. strg[]={“S”,”m”,”i”,”l”,”e”,’\0′};
c. strg[5]=0;
d. All of above

Show Answer

correct option d(All of above)

5. What is the output of the following program ?

int main()
{
int strg[]={‘g’,’o’,’o’,’d’,’b’,’y’};
printf(“N%c “,strg);
printf(“N%s “,strg);
printf(“N%c “,strg[0]);
return 0;
}

a) N N N
b) N Ng Ng
c) N*randomchar* Ng Ng
d) Compiler error

Show Answer

Explanation:
Notice that strg is not a string as it is not a char array with null at the end. So strg is the address of array which is converted to Char by %c. If you use %s, it prints the first number converted to char.

6. What is the output of C program with strings.?

int main()
{
char strg1[]=”Lovely”;
char strg2[20];
strg2= strg1;
printf(“%s”,strg2);
return 0;
}

a) Lovely
b) L
c) Lovely\0
d) Compiler error

Show Answer

correct option d
Explanation:
You can not assign one string to the other. It is an error. ”

7. What is the output of C Program with arrays.?

int main()
{
char str[25];
scanf(“%s”, str);
printf(“%s”,str);
return 0;
}
//input: New Delhi

a. Delhi
b. New Delhi
c. New
d. Compiler error

Show Answer

correct option C
Explanation:
SCANF can not accept a string with spaces or tabs. So SCANF takes only New into STR.