C string questions and answers with explanation






String questions in c interview

The answers and explanations provided are based on Turbo C 3.0 compiler. It's important to note that results and interpretations may vary with 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:
In C, the size of a character array should be one greater than the total number of characters in any string it stores. This extra space is reserved for the terminating null character, which represents the end of the string.

In the string 'Network,' there are 8 characters: 'N', 'e', 't', 'w', 'o', 'r', 'k', and '\0'. However, the size of array 'arr' is seven. As a result, the array 'arr' will store only the first seven characters and will not include the null character.

Since the %s in the printf statement prints a stream of characters until it encounters the first null character, and array 'arr' does not contain a null character, it will print garbage values.


2.
What 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:

The size of any character array assigned to a string in C cannot be less than the number of characters in that string. It must be equal (excluding the null character) or greater than the number of characters.
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:

In C, the size of an array cannot be a constant variable; it must be a constant expression. 

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:

The size of an array can be an enum constant. If the value of the enum constant 'Barack' is equal to 'Vladimir + 1', where 'Vladimir' is 3, then 'Barack' evaluates to 4. Therefore, the value of the enum variable 'p' is 4.

Considering the expression 'leader[p >> 1 + 1]':

  • 1. This simplifies to 'leader[4 >> 2]', as the + operator has higher precedence than the >> operator.
  • 2. Further simplification yields 'leader[1]', as '4 >> 2' equals 1.
  • 3. Finally, the value is 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 array size expression, a micro constant is introduced with the operation 'var + ~0'. Given the value of 'var' as 3, the expression evaluates to '3 + ~0', resulting in 3 + (-1), and thus, the final size of the array is 2.

Consider two strings, 'clarke' and 'kallis', stored at memory addresses 100 and 500, respectively, as depicted in the figure below. For the string 'clarke':



For string “kallis”:


In this program, 'cricket' is an array of character pointers with a size of 2, holding the memory addresses of the first characters of two strings: cricket[2] = {100, 500}. The pointer 'ptr' points to the first element of the 'cricket' array, so ptr = 100.

Considering the expression *++ptr:

Since ptr = 100, after ++ptr, ptr becomes 101. Therefore, *(++ptr) is equivalent to *(101), which holds the content of memory address 101. From the provided information, it is evident that the character at this address 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:
In a printf statement, the %o format specifier is used to print a number in 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:

Take a moment to contemplate and derive the solution on your own. Challenge your understanding and explore the concept independently.

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:

Array members cannot be the addresses of auto variables because arrays are allocated memory at load time, whereas auto variables are allocated 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:

When the size of the first member of an array is not specified during declaration, the size of the first dimension is determined by the maximum number of elements in the initialization of that array dimension. In the given case, the size of the first dimension is 3. Consequently, the size of the array becomes (size of int) * (total number of elements) = 2 * (3 * 3) = 18. The default initial values for the remaining elements are zero, resulting in the following array:
{
{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:

When an array is initialized at the time of declaration, the compiler treats it as a static variable, and any uninitialized members default to 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

In the expression !a + b, the ! operator always returns an integer type number. Therefore, the expression is equivalent to an integer type number + any character type number. The char data type is automatically type-casted into int in expressions. Consequently, sizeof(arr) = size of array * sizeof its data type = 2 * 2 = 4, making the size of the array 'arr' 4 bytes with an int data type.

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 stores each element of an assigned array, while a character pointer holds the memory address of the first element. The %s in printf prints characters until it encounters a null character ('\0'). Therefore, the first and second printf functions in the program will output the same result. However, the size of an array is the total number of its elements, i.e., 16 bytes, including the ending null character. On the other hand, the size of any type of pointer is 2 bytes (near pointer).


String questions with solution of c programming

Array tutorial in c

30 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....

Anonymous 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");
}
}

Anonymous 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!!

Unknown 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

Priyanka kumari said...

Can you tell exactly which thing are you know getting.

Unknown 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;

Unknown 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

Unknown said...

explain ques.15 answer

Unknown said...

answer this

Unknown 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.

Unknown said...

please explain all once

Unknown 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

Unknown said...

C Programming Tips And Tricks...

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

Unknown said...

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

Unknown 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

Aakanksha said...

Value of k will be undefined...
On different complier value of k will be diff

Aakanksha said...

The questions here are really conceptual based ,, and the explanation is also good