Saturday, 25 January 2014

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

No comments:

Post a Comment