Search Placementyogi.com
 

Objective Question on C Strings

  :: Topics ::

1

What is the output of the following program?

#include<stdio.h>
void main()
{
        printf(9+"PlacementYogi\t");
        printf("PlacementYogi"+9);
}
A.

Placement Yogi

B.

9PlacementYogi PlacementYogi9

C.

Yogi Yogi

D.

error: invalid operands to binary + operator

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

We know that string is an array of characters.

In the above program, We are passing the string "PlacementYogi" as argument to printf(), the string is printed from the 0th index till '\0' in encountered.

When we passed 9+"PlacementYogi" to the printf function. We passed the string from the index 0+9 i.e. Y.

Hence Only "Yogi" was printed and same is the case with the 2nd printf statement.

 

Let's make it more clear:

s="PlacementYogi"; // s=['P','l','a','c','e','m','e','n','t','Y','o','g','i','\0']

s is pointing to "P" initially.

9+s or s+9 will point to 'Y' at position 9.

the printf will start printing the array from inde 9 till it encounters the '\0' character.

Hence "Yogi" is printed.

 
2

What is the output of the following program?

#include<stdio.h>
void main()
{
        char c[]="gate2011";
        char *p=c;
        printf("%s",p+p[3]-p[1]);
}
A.

gate

B.

Garbage Value

C.

2011

D.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

c[] is an array of characters.

 

p[3] = 'e' i.e. ascii value of 'e'

p[1] = 'a' i.e. ascii value of 'a'

p[3]-p[1] has to be 4, whatever be the ascii value of 'e' and 'a'.

So the expression p + p[3]-p[1] becomes p+4.

p[4] is '2' in the string 'gate2011'.

Hence the string is printed from this index till '\0' is encountered.

So, "2011" is printed.

 

Note: This question was asked in GATE 2011 Computer Science Paper.

 
3

What is the output of the following program?

#include<stdio.h>
void main()
{
	printf("%c","PlacementYogi"[9]);
}
A.

Yogi

B.

Y

C.

t

D.

Error : indexing not allowed on constants.

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

We can use indexing on the string "PlacementYogi" directly 

and "PlacementYogi"[9] prints the character present at 9th index i.e. 'Y'.

 
4

What is the output of the following program?

#include<stdio.h>
void main()
{
	char str1[] = "Placement Yogi";
	char str2[] = "Placement Yogi";
	if(str1 == str2)
		printf("\n Equal");
	else
		printf("\nUnequal");
}
A.

Equal

B.

Unequal

C.

Compilation Error: string can not be compared using == operator

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

 

str1[] is an array of characters and str1 refers to the 0th location of the array.

Similarly, str[2] is an array and str2 refers to the 0th location of the array.

The str1 and str2 are stored at different locations and hence str1 and str2 are different arrays.

When we try to compare using 

str1==str2 ,

We are essentially comparing the base address of the two character arrays  and these addresses are not equal . So "Unequal" is printed.

 

 
5

What is the output of the following program?

#include<stdio.h>
void main()
{
        char str[13] = "PlacementYogi";
        char c='y';
        printf("%s", str);
}
A.

Compiler Error

B.

PlacementYogi

C.

Cannot predict

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

The character array has been declared of length 13 characters whereas the string "PlacementYogi" is of length 14 after adding the NULL character '\0'.

The string overwrites a memory location that is outside the range of the array and hence the result can be unpredictable on different machines.

C does not have an in-built check for this like other languages (Java) where arrayIndexOutofBound Exception is thrown.

 
 

Banner to LogicGuns.com

Hide Page Information

C Strings Objective Questions and Answers for Written Round Interview

Here you can find C Strings Objective Questions with answers and explanation. We have a vast collection of Objective Questions on Strings in C that will help you a lot!

Why learn C Strings?

Strings are an integral part of C and many Objective Questions in the written round are asked from this topic.In this section you can learn and practice C Strings (Objective Questions with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get C Strings Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved C Strings Objective Questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for C Strings, We have quiz Objective Questions with answers to help you test yourself..

\n

Where can I get C Strings Interview Objective Questions and Answers (objective type, multiple choice)?

Here you can find objective type C Strings Objective Questions and answers for interview and entrance examination.Multiple Answers and true or false type Objective Questions are also provided.

How to solve C Strings problems?

You can easily solve all kind of Objective Questions based on C Strings by practicing the exercises (including shortcut Strings to solve problems) given below.