题目(一):C++中我们可以用static修饰一个类的成员函数,也可以用const修饰类的成员函数(写在函数的最后表示不能修改成员变量,不是指写在前面表示返回值为常量)。请问:能不能同时用static和const修饰类的成员函数?
回答:
题目(二):运行下面C++代码,输出是什么?
class A
{
};
class B
{
public:
B() {}
~B() {}
};
class C
{
public:
C() {}
virtual ~C() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d, %d, %d/n", sizeof(A), sizeof(B), sizeof(C));
return 0;
}
回答:
题目(三):运行下面的C++代码,得到的结果是什么?
class A
{
private:
int m_value;
public:
A(int value)
{
m_value = value;
}
void Print1()
{
printf("hello world");
}
void Print2()
{
printf("%d", m_value);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A* pA = NULL;
pA->Print1();
pA->Print2();
return 0;
}
回答:
题目(四):运行下面的C++代码,得到的结果是什么?
class A
{
private:
int m_value;
public:
A(int value)
{
m_value = value;
}
void Print1()
{
printf("hello world");
}
virtual void Print2()
{
printf("hello world");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A* pA = NULL;
pA->Print1();
pA->Print2();
return 0;
}
回答:
题目(五):C++中静态成员函数能不能同时也是虚函数?
回答:
题目(六):运行下列C++代码,输出什么?
struct Point3D
{
int x;
int y;
int z;
};
int _tmain(int argc, _TCHAR* argv[])
{
Point3D* pPoint = NULL;
int offset = (int)(&(pPoint)->z);
printf("%d", offset);
return 0;
}
回答:
题目(七):运行下列C++代码,输出什么?
class A
{
public:
A()
{
Print();
}
virtual void Print()
{
printf("A is constructed./n");
}
};
class B: public A
{
public:
B()
{
Print();
}
virtual void Print()
{
printf("B is constructed./n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A* pA = new B();
delete pA;
return 0;
}
回答:
题目(八):运行下列C#代码,输出是什么?
namespace ChangesOnString
{
class Program
{
static void Main(string[] args)
{
String str = "hello";
str.ToUpper();
str.Insert(0, " WORLD");
Console.WriteLine(str);
}
}
}
回答:
题目(十):运行下图中的C#代码,输出是什么?
namespace StaticConstructor
{
class A
{
public A(string text)
{
Console.WriteLine(text);
}
}
class B
{
static A a1 = new A("a1");
A a2 = new A("a2");
static B()
{
a1 = new A("a3");
}
public B()
{
a2 = new A("a4");
}
}
class Program
{
static void Main(string[] args)
{
B b = new B();
}
}
}
回答:
题目(11):运行下图中的C#代码,输出是什么?
namespace StringValueOrReference
{
class Program
{
internal static void ValueOrReference(Type type)
{
String result = "The type " + type.Name;
if (type.IsValueType)
Console.WriteLine(result + " is a value type.");
else
Console.WriteLine(result + " is a reference type.");
}
internal static void ModifyString(String text)
{
text = "world";
}
static void Main(string[] args)
{
String text = "hello";
ValueOrReference(text.GetType());
ModifyString(text);
Console.WriteLine(text);
}
}
}
回答:
题目(12):运行下图中的C++代码,输出是什么?
#include <iostream>
class A
{
private:
int n1;
int n2;
public:
A(): n2(0), n1(n2 + 2)
{
}
void Print()
{
std::cout << "n1: " << n1 << ", n2: " << n2 << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
a.Print();
return 0;
}
回答:
题目(13):编译运行下图中的C++代码,结果是什么?(A)编译错误;(B)编译成功,运行时程序崩溃;(C)编译运行正常,输出10。请选择正确答案并分析原因。
#include <iostream>
class A
{
private:
int value;
public:
A(int n)
{
value = n;
}
A(A other)
{
value = other.value;
}
void Print()
{
std::cout << value << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a = 10;
A b = a;
b.Print();
return 0;
}
回答:
题目(14):运行下图中的C++代码,输出是什么?
int SizeOf(char pString[])
{
return sizeof(pString);
}
int _tmain(int argc, _TCHAR* argv[])
{
char* pString1 = "google";
int size1 = sizeof(pString1);
int size2 = sizeof(*pString1);
char pString2[100] = "google";
int size3 = sizeof(pString2);
int size4 = SizeOf(pString2);
printf("%d, %d, %d, %d", size1, size2, size3, size4);
return 0;
}
回答:
题目(15):运行下图中代码,输出的结果是什么?这段代码有什么问题?
#include <iostream>
class A
{
public:
A()
{ std::cout << "A is created." << std::endl; }
~A()
{ std::cout << "A is deleted." << std::endl; }
};
class B : public A
{
public:
B()
{ std::cout << "B is created." << std::endl; }
~B()
{ std::cout << "B is deleted." << std::endl; }
};
int _tmain(int argc, _TCHAR* argv[])
{
A* pA = new B();
delete pA;
return 0;
}
回答: