C string questions and answers with explanation






String questions in c interview


Answer and explanation of questions are based on turbo c 3.0 compilers. Answer and explanation may vary in other compilers.
1.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char arr[7]="Network";
    printf("%s",arr);
}
Choose all that apply:
(A) Network
(B) N
(C) Garbage value
(D) Compilation error
(E) None of the above


Explanation:
Size of a character array should one greater than total number of characters in any string which it stores. In c every string has one terminating null character. This represents end of the string.


So in the string “Network” , there are 8 characters and they are ‘N’,’e’,’t’,’w’,’o’,’r’,’k’ and ‘\0’. Size of array arr is seven. So array arr will store only first sevent characters and it will note store null character.
As we know %s in prinf statement prints stream of characters until it doesn’t get first null character. Since array arr has not stored any null character so it will print garbage value. 

2.
WWhat will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char arr[11]="The African Queen";
    printf("%s",arr);
}

Choose all that apply:
(A) The African Queen
(B) The
(C) The African
(D) Compilation error
(E) None of the above


Explanation:

Size of any character array cannot be less than the number of characters in any string which it has assigned. Size of an array can be equal (excluding null character) or greater than but never less than.

3.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int const SIZE=5;
    int expr;
    double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
    expr=1|2|3|4;
    printf("%f",value[expr]);
}

Choose all that apply:
(A) 2.000000
(B) 4.000000
(C) 8.000000
(D) Compilation error
(E) None of the above


Explanation:

Size of any array in c cannot be constantan variable. 

4.
What will be output when you will execute following c code?

#include<stdio.h>
enum power{
    Dalai,
    Vladimir=3,
    Barack,
    Hillary
};
void main(){
    float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
    enum power p=Barack;
    printf("%0.f",leader[p>>1+1]);
}

Choose all that apply:
(A) 1
(B) 2
(C) 3
(D) Compilation error
(E) None of the above


Explanation:

Size of an array can be enum constantan.
Value of enum constant Barack will equal to Vladimir + 1 = 3 +1 = 4
So, value of enum variable p  = 4
leader[p >> 1 +1]
= leader[4 >> 1+1]
=leader[4 >> 2]   //+ operator enjoy higher precedence than >> operator.
=leader[1]  //4>>2 = (4 / (2^2) = 4/4 = 1
=2

5.
What will be output when you will execute following c code?

#include<stdio.h>
#define var 3
void main(){
    char *cricket[var+~0]={"clarke","kallis"};
    char *ptr=cricket[1+~0];
    printf("%c",*++ptr);
}

Choose all that apply:
(A) a
(B) r
(C) l
(D) Compilation error
(E) None of the above


Explanation:

In the expression of size of an array can have micro constant.
var +~0 = 3 + ~0 = 3 + (-1)  = 2
Let’s assume string “clarke” and “kallis” has stored at memory address 100 and 500 respectively as shown in the following figure:
For string “clarke”:


For string “kallis”:


In this program cricket is array of character’s pointer of size 2. So array cricket will keep the memory address of first character of both strings i.e. content of array cricket is:
cricket[2] = {100,500}
ptr is character pointer which is pointing to the fist element of array cricket. So, ptr = 100
Now consider on *++ptr
Since ptr = 100 so after ++ptr , ptr = 101
*(++ptr) = *(101) = content of memory address 101. From above figure it is clear that character is l.



6.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char data[2][3][2]={0,1,2,3,4,5,6,7,8,9,10,11};
    printf("%o",data[0][2][1]);
}

Choose all that apply:
(A) 5
(B) 6
(C) 7
(D) Compilation error
(E) None of the abov


Explanation:
%o in printf statement is used to print number in the octal format.

7.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    short num[3][2]={3,6,9,12,15,18};
    printf("%d  %d",*(num+1)[1],**(num+2));
}

Choose all that apply:
(A) 12 18
(B) 18 18
(C) 15 15
(D) 12 15
(E) Compilation error


Explanation:

*(num+1)[1]
=*(*((num+1)+1))
=*(*(num+2))
=*(num[2])
=num[2][0]
=15
And
**(num+2)
=*(num[2]+0)
=num[2][0]
=15

8.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char *ptr="cquestionbank";
    printf("%d",-3[ptr]);
}

Choose all that apply:
(A) Garbage value
(B) -300
(C) -101
(D) Compilation error
(E) None of the above


Explanation:

-3[ptr]
=-*(3+ptr)
=-*(ptr+3)
=-ptr[3]
=-103  //ASCII value of character ‘e’ is 103

9.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    long  myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
    printf("%ld\t",myarr[1][2]);
    printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
    printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);   
}

Choose all that apply:

(A) 5 66 776
(B) 6 77 667
(C) 6 66 776
(D) Compilation error
(E) None of the above


Explanation:

Think yourself.

10.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int array[2][3]={5,10,15,20,25,30};
    int (*ptr)[2][3]=&array;
    printf("%d\t",***ptr);
    printf("%d\t",***(ptr+1));
    printf("%d\t",**(*ptr+1));
    printf("%d\t",*(*(*ptr+1)+2));
}

Choose all that apply:
(A) 5 Garbage 20 30
(B) 10 15 30 20
(C) 5 15 20 30
(D) Compilation error
(E) None of the above


Explanation:

ptr is pointer to two dimension array.
***ptr
=***&array  //ptr = &array
=**array //* and & always cancel to each other
=*arr[0]  // *array = *(array +0) = array[0]
=array[0][0]
= 5
Rests think yourself.


11.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    static int a=2,b=4,c=8;
    static int *arr1[2]={&a,&b};
    static int *arr2[2]={&b,&c};
    int* (*arr[2])[2]={&arr1,&arr2};
    printf("%d %d\t",*(*arr[0])[1],  *(*(**(arr+1)+1)));
}

Choose all that apply:
(A) 2 4
(B) 2 8
(C) 4 2
(D) 4 8
(E) None of the above


Explanation:

Consider on the following expression:
*(*arr[0])[1]
=*(*&arr1)[1]  //arr[0] = &arr1
=*arr1[1]   //* and & always cancel to each other
=*&b
=b
=4
Consider on following expression:
*(*(**(arr+1)+1))
= *(*(*arr[1]+1))  //*(arr+1) = arr[1]
= *(*(*&arr2+1))  //arr[1] = &arr2
=*(*(arr2+1))  //*&arr2 = arr2
=*(arr2[1])  //*(arr2+1) = arr2[1]
=  *&c    //arr2[1] = &c
=  c
= 8

12.
What will be output when you will execute following c code?

#include<stdio.h>
#include<math.h>
double myfun(double);
void main(){
    double(*array[3])(double);
    array[0]=exp;
    array[1]=sqrt;
    array[2]=myfun;
    printf("%.1f\t",(*array)((*array[2])((**(array+1))(4))));   
}
double myfun(double d){
       d-=1;
       return d;
}

Choose all that apply:
(A) 3.5
(B) 7.0
(C) 2.7
(D) Compilation error
(E) None of the above


Explanation:

array is array of pointer to such function which parameter is double type data and return type is double.
Consider on following expression:
(*array)((*array[2])((**(array+1))(4)))
= (*array)((*array[2])((*array[1])(4)))
//*(array+1) = array[1]
= (*array)((*array[2])(sqrt(4)))) 
//array[1] = address of sqrt function
= (*array)((*array[2])(2.000000)))
= (*array)(myfun(2.000000)))
// array[2] = address of myfunc function
=(*array)(1.000000)
=array[0](1.000000)
=exp(1.000000)

13.
What will be output when you will execute following c code?

#include<stdio.h>
typedef struct{
    char *name;
    double salary;
}job;
void main(){
    static job a={"TCS",15000.0};
    static job b={"IBM",25000.0};
    static job c={"Google",35000.0};
    int x=5;
    job * arr[3]={&a,&b,&c};
    printf("%s  %f\t",(3,x>>5-4)[*arr]);
}
double myfun(double d){
       d-=1;
       return d;
}

Choose all that apply:
(A) TCS 15000.000000
(B) IBM 25000.000000
(C) Google 35000.000000
(D) Compilation error
(E) None of the above


Explanation:

(3,5>>5-4)[*arr] 
=(3,5>>5-4)[*arr] //x=5
= (3,5>>1)[*arr] //- operator enjoy higher precedence than >>
= (3,2)[*arr]  //5>>1 = 5/(2^1) = 5 /2 = 2
= 2[*arr]  //In c comma is also operator.
= *(2 + *arr)
= *(*arr + 2)
=*arr[2]
=*(&c) //arr[2] = &c
=c   // *  and & always cancel to each other.
So,
printf("%s  %f\t",c);
=> printf("%s  %f\t", "Google",35000.0);

14.
What will be output when you will execute following c code?

#include<stdio.h>
union group{
    char xarr[2][2];
    char yarr[4];
};
void main(){
    union group x={'A','B','C','D'};
    printf("%c",x.xarr[x.yarr[2]-67][x.yarr[3]-67]);
}

Choose all that apply:
(A) A
(B) B
(C) C
(D) D
(E) Compilation error


Explanation:

In union all member variables share common memory space. 
So union member variable, array xarray will look like:
{
{‘A’,’B’},
{‘C’,’D’}
}
And union member variable, array yarray will look like:
{
{‘A’,’B’,’C’,’D’}
}
x.xarr[x.yarr[2]-67][x.yarr[3]-67]
= x.xarr[‘C’-67][‘D’-67]
= x.xarr[67-67][68-67]
//ASCII value of ‘C’ is 67 and ‘D’ is 68.
x.xarr[0][1]
=’B’

15.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=5,b=10,c=15;
    int *arr[3]={&a,&b,&c};
    printf("%d",*arr[*arr[1]-8]);
}

Choose all that apply:
(A) 5
(B) 10
(C) 15
(D) Compilation error
(E) None of the above


Explanation:

Member of an array cannot be address of auto variable because array gets memory at load time while auto variable gets memory at run time. 

16.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int arr[][3]={{1,2},{3,4,5},{5}};
    printf("%d %d %d",sizeof(arr),arr[0][2],arr[1][2]);
}

Choose all that apply:
(A) 12 3 5
(B) 18 0 5
(C) 12 0 5
(D) 18 3 5
(E) Compilation error


Explanation:

If we will not write size of first member of any array at the time of declaration then size of the first dimension is max elements in the initialization of array of that dimension.
So, size of first dimension in above question is 3.
So size of array = (size of int) * (total number of elements) = 2 *(3*3) = 18
Default initial value of rest elements are zero.  So above array will look like:
{
{1,2,0}
{3,4,5},
{5,0,0}
}          

17.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int xxx[10]={5};
    printf("%d %d",xxx[1],xxx[9]);
}

Choose all that apply:
(A) Garbage Garbage
(B) 0 0
(C) null null
(D) Compilation error
(E) None of the above


Explanation:

If we initialize any array at the time of declaration the compiler will treat such array as static variable and its default value of uninitialized member is zero.

18.
What will be output when you will execute following c code?

#include<stdio.h>
#define WWW -1
enum {cat,rat};
void main(){
    int Dhoni[]={2,'b',0x3,01001,'\x1d','\111',rat,WWW};
    int i;
    for(i=0;i<8;i++)
         printf(" %d",Dhoni[i]);
}

Choose all that apply:
(A) 2 98 4 513 28 73 1 -1
(B) 2 98 4 507 29 73 2 -1
(C) 2 99 3 513 29 73 2 -1
(D) 2 98 3 513 29 73 1 -1
(E) Compilation error


Explanation:

Dhoni[0]=2
Dhoni[1]=’b’ =98  //ASCII value of character ‘b’ is 98.
Dhoni[2]=  0x3  =  3  //0x represents hexadecimal number. Decimal value of hexadecimal 3 is also 3.
Dhoni[3]=01001 = 513 //Number begins with 0 represents octal number.
Dhoni[4]  = ‘\x1d’ = 29 //’\x1d’ is hexadecimal character constant.
Dhoni[5] = ‘\111’ = 73 //’\111’ is octal character constant.
Dhoni[6] =rat = 1  //rat is enum constant
Dhoni[7] = WWW = -1  //WWW is macro constant.

19.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    long double a;
    signed char b;
    int arr[sizeof(!a+b)];
    printf("%d",sizeof(arr));
}

Choose all that apply:
(A) 16
(B) 4
(C) 2
(D) Compilation error
(E) None of the above


Explanation:

Size of data type in TURBO C 3.0 compiler is:
S.N.
Data type
Size(In byte)
1
char
1
2
int
2
3
double
8

Consider on the expression: !a + b
! Operator always return zero if a is non-zero number other wisie 1. In general we can say ! operator always returns an int type number. So
!a +b
=! (Any double type number) + Any character type number
= Any integer type number + any character type number
= Any integer type number
Note: In any expression lower type data is always automatically type casted into the higher data type. In this case char data type is automatically type casted into the int type data.
So sizeof (!a +b) = sizeof(Any int type number)  = 2
So size of array arr is 2 and its data type is int. So
sizeof(arr) = size of array * sizeof its data type = 2* 2= 4

20.
What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char array[]="Ashfaq \0 Kayani";
    char *str="Ashfaq \0 Kayani";
    printf("%s %c\n",array,array[2]);
    printf("%s %c\n",str,str[2]);
    printf("%d %d\n",sizeof(array),sizeof(str));
}

Choose all that apply:
(A)


Ashfaq h
Ashfaq h
16 2
(B)

Ashfaq h
Ashfaq Kayani h
16 16
(C)

Ashfaq y
Ashfaq h
2 2
(D) Compilation error
(E) None of the above


Explanation:

A character array keeps the each element of an assigned array but a character pointer always keeps the memory address of first element.  
As we know %s in prints the characters of stream until it doesn’t any null character (‘\0’).  So first and second printf function will print same thing in the above program.  But size of array is total numbers of its elements i.e. 16 byte (including ending null character). While size of any type of pointer is 2 byte (near pointer). 


String questions with solution of c programming

Array tutorial in c

28 comments:

Anonymous said...

how can we convert a number to octal format which are less than 8.
for eg.,what is the octal format of 6?
can any1 giv ans....

Mithu_the reporter said...

Hi...I'm a new member here. I need a solution to this simple Q. :Write a program that initializes a 10*3 array so that the first

element of each row contains a number, the second element contains

its square, and the third element contains its cube.
Start with 1 and stop at 10. For example, the first few rows will

look like this:
1,1,1
2,4,8
3,9,27
.
.
10, 100,1000
Next, prompt the user for a cube, look up this value in the table,

and report the cube’s root and the root’s square.

muhammad altaf khattak said...

i have written a code for mithu_the reporter.

#include
void main()
{
int x[10][3]={0};
int f=1,n=1;
for(int i=0;i<10;i++)
{
for(int j=0;j<3;j++)
{
x[i][j]=f;
f=f*(i+1);
printf("%d ",x[i][j]);
}
n++;
f=n;
printf("\n");
}
}

Aregawi Gidey said...

write a program that compares two strings with out using strcmp().

Anonymous said...

plz explain 2nd prinf statement of Q:10,,,,,,,,,,,,,i hve understand rest of the printf statements.......a little bit confusion.........

sateeshnaidu said...

Morvelous site that i never seen any where.Thanks to rithesh kumar for providing the most useful data.

Anonymous said...

i want to say from my heart a Big Bravo to the guys working on this code it is wonderful.

master sonu said...

great job!!

Anil Kumar said...

hi ritesh u r website is too good,really great job man,can you explain the 10th question answer in detail,i have tried a lot but still i am not sure about the answer

ritesh kumar said...

Can you tell exactly which thing are you know getting.

Anil Kumar said...

printf("%d\t",***(ptr+1)); explain this
i am getting output as garbage value,

Anonymous said...

Numbers which are less than 8 will be remain same i.e. 6 in octal will remain 6.

kritka verma said...

can any1 tell to write a program that takes two strings as input passed as command line arguments within double quotes as shown below and outputs the concatenated string.
plzz rply fast

Anonymous said...

i have written a code for string merge sort.its dry run is correct ,but it does not give correc output.please correct this program and please mention where i have done wrong.


#include
#include
#include
void main()
{
clrscr();
char str[5][5],str1[6][6],temp[40][40];
int i,j,k;
printf("enter 1st array of string \n");
for(i=0;i<5;i++)
{
gets(str[i]);
}
printf("the first array of strings are=\n");
for(i=0;i<5;i++)
{
printf("\n str[%d]=",i);
printf("%s\t",str[i]);
}
printf("\n");
printf("enter 2nd array of string =\n");
for(j=0;j<6;j++)
{
gets(str1[j]);
}
for(j=0;j<6;j++)
{
printf("\n str1[%d]=",j);
printf("%s\t",str1[j]);
}
printf("\n");
i=0;
j=0;
k=0;
while((i<5)&&(j<6))
{
if(strcmp(str[i],str1[j])<0)
{
strcpy(temp[k],str[i]);
i++;
k++;
}
else
{
strcpy(temp[k],str1[j]);

j++;
k++;
}
}
while(i<5)
{
strcpy(temp[k],str[i]);

i++;
k++;
}
while(j<6)
{
strcpy(temp[k],str1[j]);

j++;
k++;
}
printf("\n");
printf("after sorting=\n");
printf("\n");
for(k=0;k<11;k++)
printf("%s\t",temp[k]);





getch();





}


Anonymous said...

can u give me code for program..
In java
If User enters numeric value then add or if he enters string then concatinate.

Anonymous said...

(ptr+1) point next two-dimensional array;
*(ptr+1) give base address of (&array+1) i.e next 2-d array(say array1);
**(ptr+1)-array1[0];
***(ptr+1)-array1[0][0];
which contains garbage value;

Ram babu said...

can u give me code for following example in c using functions
see the example is consider the string "ram_uma_raju_ram_ravi_uma" this the input sting and the program it will give the output as find the duplicate words along with count number i.e how many times that word repeated
output:"ram2_uma2_raju1_ravi1"
please give the code for this i need it urgent

preethy said...

c prog to search a char & report the position

Anonymous said...

how to sort a structure without using library functions

Bhagyashri jha said...

explain ques.15 answer

sanjaie rajamanickam said...

answer this

sanjaie rajamanickam said...

Write an interactive c program that will encode or decode a
line of text. To encode a line of text, proceed as follows:

Convert each character, including blank spaces, to its
ASCII equivalent.
Generate a positive random integer. Add this integer to the
ASCII equivalent of each character. The same random integer
will be used for the entire line of text.
Suppose that N1 represents the lowest permissible value in
the ASCII code, and N2 represents the highest permissible
value. If the number obtained in step 2 above exceeds N2,
then subtract the largest possible multiple of N2 from this
number, and add the remainder to N1. Hence the encoded
number will always fall between N1 and N2, and will
therefore always represent some ASCII character.
Display the characters that correspond to the encoded ASCII
values.
The procedure is reversed when decoding a line of text. Be
certain, however, that the same random number is used in
decoding as was used in encoding.

Ravi Shanker Reddy said...

please explain all once

Rajesh Dasari said...

can you explain theses statements output
int a=5,k,b=5,t,c=5,r;
k=++a + ++a + a++;
printf("%i %i\n",a,k);
t=b++ + ++b + ++b;
printf("%i %i\n",b,t);
r=++c + ++c + ++c+ ;
printf("%i %i\n",c,r);

in gcc comipler
k=21
t=19
r=22
a=b=c=8
how can any body explain plzzzzzzzzz

Unknown said...

k=++a + ++a + a++;
in this first of all
a++ => a=a+1;
a++ => a=6;
++a => a=6;
++a => a=7 for third
so value of a will be 7
and 7+7+7=21

Zubair Baig said...

C Programming Tips And Tricks...

Vist This Link..
https://youtu.be/TBKVuM9W8RM

Aayush Jain said...

write a program in c to reverse a words starting with r in a sentence

Shwetha Kamath said...

Q2: There will not be compilation error because bound checking for array is not done by compiler. It will print garbage value because the array stores 11 characters(The African) without null character