单选题
C++
题号 #2254962
下列程序的输出结果是( )。#include<iostream.h>
voidmain()
(char*str="12123434";
intx1=0,x2=0,x3=0,x4=0,i;
for(i=0;str[i]!='\0';i++)
switch(str[i])
{case'1':x4++;
case'2':x3++;
case'3':x2++;
case'4':x1++;
}
cout<<x1<<","<<x2<:<","<<x3<
<","<<x4;
}
参考答案
D
答案解析
D。【解析】本题考查循环的嵌套、for和switch语句的应用、C++中各种条件判断,循环语句的用法。只要注意在C++语言中,系统在字符指针的最后位置会增加一个字符串结束符'\0',执行8次for循环,再根据switch语句中的条件易知答案。
你可能感兴趣的试题
A.ofstreammyfile;myfile.open("d:ofile.txt");
B.ofstream*myfile=newofstream;myfile->open("d:ofile.txt”);
C.ofstreammyfile("d:ofile.txt");
D.ofstream*myfile=new("d:ofile.txt");
A.1
B.2
C.3
D.4
A.funlTestClasslfun2TestClass
B.funlTestClass1fun2TestC1ass1
C.funlTestClassfun2TestClass
D.funlTestClassfun2TestClassl