C Programming :: String

Home > C Programming > String > General Questions

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

1 / 24

 What is the output of the following problem ?

#include
int main()
{
char *c;
c = "Hello";
printf("%s\n", c);
return 0;
}

AHello

BH

Cello

DCompilation Error

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

2 / 24

 What is the output of the following problem ?

#include
int main()
{
char *str = "12345";
printf("%c %c %c\n", *str, *(str++), *(str++));
return 0;
}

A3 2 1

B1 2 3

C3 4 5

DCompilation Error

ENone of these

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

3 / 24

 What is the output of the following problem ?

#include
int main()
{
int len=4;
char *st="12345678";
st = st + len;
printf("%c\n",*st);
return 0;
}

A5

B4

C12345678

DCompilation Error

ESegmentation fault

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

4 / 24

 int main()
{
char string[]="Hello World";
display(string);
return 0;
}
void display(char *string)
{
printf("%s",string);
}

AHello World

BHello

CWorld

DCompiler Error

ENone of these

Answer: Option E

Explanation:

In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

5 / 24

 #include
void main()
{
char a[]="12345\0";
int i=strlen(a);
printf("Value of i %d\n",++i);
}

AValue of i 6

BValue of i 5

CValue of i 7

DCompilation Error

ENone of these

Answer: Option A

Explanation:

The char array 'a' will hold the initialized string, whose length will be counted from 0 till the null character. Hence the 'I' will hold the value equal to 5, but after the pre- increment in the printf statement, value of I will be printed.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

6 / 24

 #include
void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf("Ok Done \n");
else
printf("Forget it\n");
}

AForget it

BOk Done

CCompilation Error

DNone of these

Answer: Option B

Explanation:

Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok
Done" will be printed.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

7 / 24

 What is the output of the following 'C' program ?

#include
void main()
{
char str1[] = "Hello";
char str2[] = "Hello";
if (str1 == str2)
printf("\nequal");
else
printf("\nUnequal");
}

AEqual

BError

CUnequal

DNone of these

Answer: Option C

Explanation:

The '==' operator is meant for reference comparison i.e, it returns true only if the 2 string objects are same, else it will return false. Here, since the 2 string objects are different, the else part gets executed, and the output is Unequal, i.e, option C. 


Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

8 / 24

 What is the output of the following C Program?

#include
#include
void main() {
int i, n;
char *x = "girl";
n = strlen(x);
*x = x[n];
for(i=0; i {
printf("%s\n",x);
x++;
}}

ASegmentation fault

Bgirl irl rl l

C(blank space) irl rl l

DCompilation Error

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

9 / 24

 #include
#include
void main()
{
while (strcmp("some","some\0"))
printf("Strings are not equal\n");
}

ANo Output No Error

BString are not equal

CCompilation Error

DNone of these

Answer: Option A

Explanation:

Ending the string constant with \0 explicitly makes no difference.
So "some" and "some\0" are equivalent. So, strcmp returns 0 .
So breaking out of the while loop.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

10 / 24

 #include
void main()
{
char str1[] = {'s','o','m','e'};
char str2[] = {'s','o','m','e','\0'};
while (strcmp(str1,str2))
printf("Strings are not equal\n");
}

ANo Output No Error

BString are not equal

Cstack overflow

DNone of these

Answer: Option C

Explanation:

If a string constant is initialized explicitly with characters, '\0' is not appended automatically to the string.
Since str1 doesn't have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a '\0'. So str1 and str2 are not the same.

Workspace