/* */ Click to Join Live Class with Shankar sir Call 9798158723

C Strings MCQ


Q1) What is a String in C Language.?
  1. String is a new Data Type in C
  2. String is an array of Characters with null character as the last element of array.
  3. String is an array of Characters with null character as the first element of array
  4. String is an array of Integers with 0 as the last element of array.

Answer:- (B).
Explanations :none
Q2) Choose a correct statement about C String.
  char ary[]="Hello..!";
  1. Character array, ary is a string.
  2. ary has no Null character at the end
  3. String size is not mentioned
  4. String can not contain special characters.

Answer:- (A).
Explanations :It is a simple way of creating a C String. You can also define it like the below. \0 is mandatory in this version. char ary[] = {'h','e','l','l','o','\0'};
Q3) What is the Format specifier used to print a String or Character array in C Printf or Scanf function.?
  1. %c
  2. %C
  3. %s
  4. %w

Answer:- (C).
Explanations :char ary[]="Hello..!";
printf("%s",ary);
Q4) What is the output of C Program with Strings.?
    int main()
{
    char ary[]="Discovery Channel";
    printf("%s",ary);
    return 0;
}
   
  1. D
  2. Discovery Channel
  3. Discovery
  4. Compiler error

Answer:- (B).
Explanations :%s prints the while character array in one go.
Q5) What is the output of C Program with Strings.?
  int main()
{
    char str[]={'g','l','o','b','e'};
    printf("%s",str);
    return 0;
}
  
  1. g
  2. globe
  3. globe\0
  4. None of the above

Answer:- (D).
Explanations :Notice that you have not added the last character \0 in the char array. So it is not a string. It can not know the end of string. So it may print string with some garbage values at the end.
Q6) What is the output of C Program with Strings.?
 int main()
{
    char str[]={'g','l','o','b','y','\0'};
    printf("%s",str);
    return 0;
}
 
  1. g
  2. globe
  3. globe\0
  4. Compiler error

Answer:- (B).
Explanations :Adding a NULL or \0 at the end is a correct way of representing a C string. You can simple use char str[]="globy". It is same as above.
Q7) How do you convert this char array to string.?
  char str[]={'g','l','o','b','y'};
  
  1. str[5] = 0;
  2. str[5] = '\0'
  3. str[]={'g','l','o','b','y','\0'};
  4. All the above

Answer:- (D).
Explanations :none
Q8) What is the output of C Program.?
   int main()
{
    int str[]={'g','l','o','b','y'};
    printf("A%c ",str);
    printf("A%s ",str);
    printf("A%c ",str[0]);
    return 0;
}
  
  1. A A A
  2. A Ag Ag.
  3. A*randomchar* Ag Ag
  4. Compiler error.

Answer:- (C).
Explanations :Notice that STR is not a string as it is not a char array with null at the end. So STR is the address of array which is converted to Char by %c. If you use %s, it prints the first number converted to char.
Q9) What is the output of C Program with arrays.?
  int main()
{
    char str[]={"C","A","T","\0"};
    printf("%s",str);
    return 0;
}
  
  1. C
  2. CAT
  3. CAT\0
  4. Compiler error

Answer:- (D).
Explanations :Yes. You can not use Double Quotes " to represent a single character. Correct way is 'C' not "C". You should use Single Quotes around a single character constant.
Q10) What is the maximum length of a C String.?
  1. 32 characters
  2. 64 characters
  3. 256 characters
  4. None of the above

Answer:- (D).
Explanations :Maximum size of a C String is dependent on implemented PC memory. C does not restrict C array size or String Length.