C Technical Interview Questions on Strings

This C Technical Interview Questions on strings post providing basic, intermediate and Hard level examples in C Strings. The Practice of these examples makes you good in C programming.


What is the Output of following Code?
#include<stdio.h>
void main()
{
char name[20] = “Hello” ;
printf(“%c”, name);
}

  1. Hello
  2. H
  3. Garbage character
  4. Hello and some garbage characters

Answer :

Ans:3 ; name holds address and it prints address corresponding garbage character

What is the Output of following Code?
#include<stdio.h>
void main()
{
char s[20] = “Hello”, t ;
int i=0, j=5 ;
while(i++ < –j)
{
t = s[i] ;
s[i] = s[j] ;
s[j] = t ;
}
printf(“%c”, s[4]);
}

  1. e
  2. l
  3. o
  4. Garbage character

Answer :

Ans : 1

What is the Output of following Code?
void main()
{
char str[] = “Java\02Python”;
printf(“%s”, str);
}

  1. Java2Python
  2. Java
  3. J2P
  4. Java\02Python

Answer :

Ans : 2 , printf function stop printing characters when it reaches \0 character

What is the Output of following Code?
#include<stdio.h>
void main()
{
char arr[11]=”Welcome to Java2Python”;
printf(“%s”,arr);
}

  1. Compile time error
  2. Welcome to Java2Python
  3. Welcome to
  4. Welcome to Ja

Answer :

Ans : 1 , Too many initializers

What is the Output of following Code?
#include<stdio.h>
void main()
{
char sport[]=”cricket”;
int x=1,y;
y=x++ + ++x;
printf(“%c”,sport[++y]);
}

  1. c
  2. t
  3. k
  4. e

Answer :

Ans : 4, the value of y is 4 and ++y prints 6th character

What is the Output of following Code?
#include<stdio.h>
void main()
{
char name[]=”Java2Python”;
printf(“%d,%d”,strlen(name), sizeof(name));
}

  1. 11,12
  2. 11,11
  3. 12,12
  4. Compile time error

Answer :

Ans : 1 , Length excludes \0 character where as size includes

What is output of following program?
void main()
{
char str1[] = “abcd”;
char str2[] = “abcd”;
if(str1==str2)
printf(“C is damn easy”);
else
printf(“Need to analyze”);
}

  1. C is damn easy
  2. Need to analyze
  3. Compile time error
  4. Blank output

Answer :

Ans : 2 , str1 and str2 gets different memory locations and here we are comparing memory locations only

What is the output of this C code?
#include <stdio.h>
int main()
{
char *str = “Java2Python”;
char *p = str;
printf(“%c\t%c”,*(p+2) ,*(str+2));
return 0;
}

  1. v, v
  2. v, garbage character
  3. garbage character, v
  4. compile time error

Answer :

Ans : 1 , Both are pointing to same location

Consider the following c program snippet and what is the output ?
#include <stdio.h>
int main()
{
char *s= “Java2Python”;
char *p = s;
printf(“%x\t%x”, *(p+2), s[2]);
return 0;
}

  1. 118 , 118
  2. v , v
  3. 76 , 76
  4. Garbage value, Garbage value

Answer :

Ans : 3, Hexa decimal value of character-v

Strings – Technical – Part 2

Share this