趋势科技几道笔试题
1. What is the output of the following program?
#include<iostream>
using namespace std;
int findChar(char *str,char ch)
{
for(unsigned int i=strlen(str)-1;i>=0;--i)
{
if(str[i]==ch)
return i;
}
return -1;
}
int main(int argc,char *argv[])
{
printf("%s\n",findChar(argv[0],'q'));
return 0;
}
A) Compile error B) -1 C)Infinite loop, line 2 needs change. D) The program will crash
循环是个死循环,但是这不是导致崩溃的根本原因。main函数里面%s的格式才是导致崩溃的根本原因
2.What is the output of following code:
#include<iostream>
using namespace std;
class opOverload
{
public:
bool operator==(const opOverload & temp);
};
bool opOverload::operator==(const opOverload & temp)
{
if(*this==temp)
{
cout<<"the both are same object"<<endl;
return 1;
}
else
{
cout<<"the both are different"<<endl;
return 0;
}
}
int main()
{
opOverload a1,a2;
a1==a2;
}
A) compile error B) The both are same objects C) The both are different D) Runtime error
3.Regarding the scope of the vaiables ,identify the incorrect statement:
A ) automatic variables are automatically initialized to 0
B ) static variables are automatically initialized to 0
C) the address of a register variable is not accessible
D static varialbes cannot be initalized with any expression
4.What is the result of the following program?
void main()
{
char p[]="String";
int x=0;
if(p=="String") //注意这里p和一个常量"String“比较,p是数组的首地址,这两者肯定是不等的。 但是如果把前面绿色部分换成 char *p="String",则此处相等。
{
cout<<"Pass 1,";
if(p[sizeof(p)-2]=='g')
cout<<"Pass 2"<<endl;
else
cout<<"Fail 2"<<endl;
}
else
{
cout<<"Fail 1,"
if(p[sizeof(p)-2]=='g')
cout<<"Pass 2"<<endl;
else
cout<<"Fail 2"<<endl;
}
}
Fail 1 , Pass 2
5.What will be the result of the following program?
#include<iostream>
using namespace std;
void myalloc(char *x,int n)
{
x=(char*)malloc(n*sizeof(char));
memset(x,0,n*sizeof(char));
}
int main()
{
char *g="String";
myalloc(g,20);
strcpy(g,"Oldstring");
printf("The string is %s",g);
}
a) The string is :String
b)Run time error/Core dump
c)The string is :Oldstring
d)Sysntax error during complication
- None of these
