c-style字符字符串
C programming String Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Strings, String is the set of characters and String related Aptitude Questions and Answers you will find here.
C编程String Aptitude问答:在本节中,您将找到有关字符串的C Aptitude问答,String是字符集,与String相关的Aptitude问答在这里可以找到。
1) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main()
{
int val=0;
char str[]="IncludeHelp.Com";
val=strcmp(str,"includehelp.com");
printf("%d",val);
return 0;
}
Answer
Correct Answer - 3
-1
Strings are not equal, hence strcmp will return -1.
-1
Strings are not equal, hence strcmp will return -1.
2) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main()
{
char str[];
strcpy(str,"Hello");
printf("%s",str);
return 0;
}
Answer
Correct Answer - 2
Error: 'str' Unknown Size
At the time of str declaration, size is empty, it may be possible when you are initializing string with declaration (like char str[]="Hello";
Error: 'str' Unknown Size
At the time of str declaration, size is empty, it may be possible when you are initializing string with declaration (like char str[]="Hello";
2)以下程序的输出是什么?
回答
正确答案-2
错误:“ str”未知大小
在str声明时,size为空,当您使用声明初始化字符串时(例如char str [] =“ Hello”;
错误:“ str”未知大小
在str声明时,size为空,当您使用声明初始化字符串时(例如char str [] =“ Hello”;
3) What will be the output of following program ?
#include <stdio.h>
int main()
{
char str[8]="IncludeHelp";
printf("%s",str);
return 0;
}
Answer
Correct Answer - 3
Error: Too many initializers/ array bounds overflow.
Error: Too many initializers/ array bounds overflow.
4) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main()
{
char str1[]="IncludeHelp",str2[]=".Com";
printf("%s",str1+strlen(str2));
return 0;
}
Answer
Correct Answer - 2
udeHelp
Look the expression str1+strlen(str2)=> str1+4, since str1 is a character array, and str1+4 will point 4th index of str1, then udeHelp will print.
udeHelp
Look the expression str1+strlen(str2)=> str1+4, since str1 is a character array, and str1+4 will point 4th index of str1, then udeHelp will print.
4)以下程序的输出是什么?
回答
正确答案-2
udeHelp
查看表达式str1 + strlen(str2)=> str1 + 4,因为str1是一个字符数组,并且str1 + 4将指向str1的第4个索引,然后将输出udeHelp。
udeHelp
查看表达式str1 + strlen(str2)=> str1 + 4,因为str1是一个字符数组,并且str1 + 4将指向str1的第4个索引,然后将输出udeHelp。
5) What will be the output of following program ?
#include <stdio.h>
int main()
{
char str[]="Hello%s%dFriends";
printf(str);
printf("\n");
printf("%s",str);
return 0;
}
Answer
Correct Answer - 3
Hello(null)0Friends
Hello%s%dFriends
printf("%s",str); prints all string, but printf(str) prints the value instead of %s, %d .. etc (default value for %s is null and %d is 0.
Hello(null)0Friends
Hello%s%dFriends
printf("%s",str); prints all string, but printf(str) prints the value instead of %s, %d .. etc (default value for %s is null and %d is 0.
5)以下程序的输出是什么?
回答
正确答案-3
您好(空)0个朋友
你好%s%dFriends
printf(“%s”,str); 打印所有字符串,但printf(str)打印该值,而不是%s,%d等。(%s的默认值为null,%d为0。
您好(空)0个朋友
你好%s%dFriends
printf(“%s”,str); 打印所有字符串,但printf(str)打印该值,而不是%s,%d等。(%s的默认值为null,%d为0。
6) What will be the output of following program ?
#include <stdio.h>
int main()
{
int i;
char str[]="IncludeHelp";
for(i=0;str[i]!='\0';i++)
{
putchar(str[i]);
putchar('*');
}
return 0;
}
Answer
I*n*c*l*u*d*e*H*e*l*p*
.
.
6)以下程序的输出是什么?
回答
I * n * c * l * u * d * e * H * e * l * p *
。
。
7) What will be the output of following program ?
#include <stdio.h>
int main()
{
char str[]="value is =%d";
int a='7';
str[11]='c';
printf(str,a);
return 0;
}
Answer
Correct Answer - 4
value is =7
We can assign '7' into integer variable a, a will be 55, but str[11]='c' statement will replace 11th character of str 'd' to 'c', hence str will be "value is =%c.
and statement printf(str,a); will print "value is=7".
value is =7
We can assign '7' into integer variable a, a will be 55, but str[11]='c' statement will replace 11th character of str 'd' to 'c', hence str will be "value is =%c.
and statement printf(str,a); will print "value is=7".
7)以下程序的输出是什么?
回答
正确答案-4
值= 7
我们可以将'7'分配给整数变量a,a将为55,但是str [11] ='c'语句会将str'd'的第11个字符替换为'c',因此str将为“ value is =%c 。
和语句 printf(str,a); 将显示“值is = 7”。
值= 7
我们可以将'7'分配给整数变量a,a将为55,但是str [11] ='c'语句会将str'd'的第11个字符替换为'c',因此str将为“ value is =%c 。
和语句 printf(str,a); 将显示“值is = 7”。
8) What will be the output of following program ?
#include <stdio.h>
int main()
{
char result,str[]="\0IncludeHelp";
result=printf("%s",str);
if(result)
printf("TRUE");
else
printf("FALSE");
return 0;
}
Answer
Correct Answer - 4
FALSE
str[]="\0IncludeHelp", str will be terminated by \0.
FALSE
str[]="\0IncludeHelp", str will be terminated by \0.
8)以下程序的输出是什么?
回答
正确答案-4
假
str [] =“ \ 0IncludeHelp”,str将以\ 0终止。
假
str [] =“ \ 0IncludeHelp”,str将以\ 0终止。
9) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main()
{
if( printf("Hello") == strlen("Hello") )
printf("...Friends");
else
printf("...Enemies");
return 0;
}
Answer
Correct Answer - 1
Hello...Friends
Statement printf("Hello") will print "Hello" and return 5, and statement strlen("Hello") will return total number of characters (5) , hence condition is true.
Hello...Friends
Statement printf("Hello") will print "Hello" and return 5, and statement strlen("Hello") will return total number of characters (5) , hence condition is true.
9)以下程序的输出是什么?
回答
正确答案-1
你好朋友
语句 printf(“ Hello”)将打印“ Hello”并返回5,语句 strlen(“ Hello”)将返回字符总数(5),因此条件为true。
你好朋友
语句 printf(“ Hello”)将打印“ Hello”并返回5,语句 strlen(“ Hello”)将返回字符总数(5),因此条件为true。
10) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main()
{
char s1[]="IncludeHelp";
char s2[10];
strncpy(s2,s1,5);
printf("%s",s2);
return 0;
}
Answer
Correct Answer - 2
IncluGARBAGE_VALUE
strncpy is used to copy number of characters from one string to another...
strncpy(s2,s1,5) will move 5 characters from s1 to s2, but there is no NULL ('\0') character to terminate the string s2... IncluGARBAGE_VALUE will print.
IncluGARBAGE_VALUE
strncpy is used to copy number of characters from one string to another...
strncpy(s2,s1,5) will move 5 characters from s1 to s2, but there is no NULL ('\0') character to terminate the string s2... IncluGARBAGE_VALUE will print.
10)以下程序的输出是什么?
回答
正确答案-2
IncluGARBAGE_VALUE
strncpy用于将字符数从一个字符串复制到另一字符串...
strncpy(s2,s1,5)将5个字符从s1移至s2,但是没有NULL('\ 0')字符可终止字符串s2。 将显示IncluGARBAGE_VALUE。
IncluGARBAGE_VALUE
strncpy用于将字符数从一个字符串复制到另一字符串...
strncpy(s2,s1,5)将5个字符从s1移至s2,但是没有NULL('\ 0')字符可终止字符串s2。 将显示IncluGARBAGE_VALUE。
11) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main()
{
char str[50]="IncludeHelp";
printf("%d...%d",strlen(str),sizeof(str));
return 0;
}
Answer
Correct Answer - 2
11...50
strlen returns the number of characters, and sizeof(str) returns total occupied size by str.
11...50
strlen returns the number of characters, and sizeof(str) returns total occupied size by str.
11)以下程序的输出是什么?
回答
正确答案-2
11 ... 50
strlen返回字符数, sizeof(str)返回按str占用的总大小。
11 ... 50
strlen返回字符数, sizeof(str)返回按str占用的总大小。
12) What will be the output of following program ?
#include <stdio.h>
int main()
{
char *str="IncludeHelp";
while(*str)
printf("%s\n",str++);
return 0;
}
Answer
Correct Answer - 2
12)以下程序的输出是什么?
#include <stdio.h>
int main()
{
char *str="IncludeHelp";
while(*str)
printf("%s\n",str++);
return 0;
}
回答
正确答案-2
13) What will be the output of following program ?
#include <stdio.h>
#define string char*
int main()
{
string myName="IncludeHelp";
printf("My name is =%s",myName);
return 0;
}
Answer
Correct Answer - 1
IncludeHelp
string will be replaced by char *.
IncludeHelp
string will be replaced by char *.
14) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main()
{
printf("%d...%d",sizeof("Hello"),strlen("Hello"));
return 0;
}
Answer
Correct Answer - 4
6...5
sizeof operator returns the size of the string including NULL character, and strlen returns the number of characters stored in string.
6...5
sizeof operator returns the size of the string including NULL character, and strlen returns the number of characters stored in string.
14)以下程序的输出是什么?
回答
正确答案-4
6 ... 5
sizeof运算符返回字符串的大小(包括NULL字符),而 strlen返回存储在字符串中的字符数。
6 ... 5
sizeof运算符返回字符串的大小(包括NULL字符),而 strlen返回存储在字符串中的字符数。
15) What will be the output of following program ?
#include <stdio.h>
#include <string.h>
int main(){
char str[]="Ihelp";
while(str+strlen(str))
printf("*");
return 0;
}
Answer
Correct Answer - 3
***... infinite times
str+strlen(str) in this expression str is a pointer that return memory address and str+strlen(str) = str+5, also an address (integer value), so expression str+strlen(str) will return non zero value.
***... infinite times
str+strlen(str) in this expression str is a pointer that return memory address and str+strlen(str) = str+5, also an address (integer value), so expression str+strlen(str) will return non zero value.
15)以下程序的输出是什么?
回答
正确答案-3
*** ...无限次
STR + strlen的(STR)在这个表达式 str是一个指针返回存储器地址和 STR + strlen的(STR)= STR + 5,也是一个地址(整数值),所以表达 STR +的strlen(STR)将返回非零值。
*** ...无限次
STR + strlen的(STR)在这个表达式 str是一个指针返回存储器地址和 STR + strlen的(STR)= STR + 5,也是一个地址(整数值),所以表达 STR +的strlen(STR)将返回非零值。
翻译自: https://www.includehelp.com/c-programs/c-strings-aptitude-questions-and-answers.aspx
c-style字符字符串