C++测试题

  题目(一):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;
}
回答:

 

 

 

答案:http://www.51testing.com/html/31/n-230031.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值