Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 565|回复: 0

[默认分类] 较难一些的C/C++面试题目

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-7-5 18:08:27 | 显示全部楼层 |阅读模式

    动态内存分配(Dynamic memory allocation)


    14. 尽管不像非嵌入式计算机那么常见,嵌入式系统还是有从堆(heap)中动态分配内存的过程的。那么嵌入式系统中,动态分配内存可能发生的问题是什么?

    这里,我期望应试者能提到内存碎片,碎片收集的问题,变量的持行时间等等。这个主题已经在ESP杂志中被广泛地讨论过了(主要是 P.J. Plauger, 他的解释远远超过我这里能提到的任何解释),所有回过头看一下这些杂志吧!让应试者进入一种虚假的安全感觉后,我拿出这么一个小节目:下面的代码片段的输出是什么,为什么?

    char *ptr;
    if ((ptr = (char *)malloc(0)) == NULL)
    puts("Got a null pointer");
    else
    puts("Got a valid pointer");

    这是一个有趣的问题。最近在我的一个同事不经意把0值传给了函数malloc,得到了一个合法的指针之后,我才想到这个问题。这就是上面的代码,该代码的输出是“Got a valid pointer”。我用这个来开始讨论这样的一问题,看看被面试者是否想到库例程这样做是正确。得到正确的答案固然重要,但解决问题的方法和你做决定的基本原理更重要些。

    Typedef

    15. Typedef 在C语言中频繁用以声明一个已经存在的数据类型的同义字。也可以用预处理器做类似的事。例如,思考一下下面的例子:
    #define dPS struct s *
    typedef struct s * tPS;

    以上两种情况的意图都是要定义dPS 和 tPS 作为一个指向结构s指针。哪种方法更好呢?(如果有的话)为什么?
    这是一个非常微妙的问题,任何人答对这个问题(正当的原因)是应当被恭喜的。答案是:typedef更好。思考下面的例子:
    dPS p1,p2;
    tPS p3,p4;

    第一个扩展为
    struct s * p1, p2;

    上面的代码定义p1为一个指向结构的指,p2为一个实际的结构,这也许不是你想要的。第二个例子正确地定义了p3 和p4 两个指针。

    晦涩的语法

    16. C语言同意一些令人震惊的结构,下面的结构是合法的吗,如果是它做些什么?
    int a = 5, b = 7, c;
    c = a+++b;

    这个问题将做为这个测验的一个愉快的结尾。不管你相不相信,上面的例子是完全合乎语法的。问题是编译器如何处理它?水平不高的编译作者实际上会争论这个问题,根据最处理原则,编译器应当能处理尽可能所有合法的用法。因此,上面的代码被处理成:
    c = a++ + b;
    因此, 这段代码持行后a = 6, b = 7, c = 12。
    如果你知道答案,或猜出正确答案,做得好。如果你不知道答案,我也不把这个当作问题。我发现这个问题的最大好处是:这是一个关于代码编写风格,代码的可读性,代码的可修改性的好的话题

    What will print out?

    main()
    {
    char *p1=“name”;
    char *p2;
    p2=(char*)malloc(20);
    memset (p2, 0, 20);
    while(*p2++ = *p1++);
    printf(“%sn”,p2);

    }

    Answer:empty string.

    What will be printed as the result of the operation below:

    main()
    {
    int x=20,y=35;
    x=y++ + x++;
    y= ++y + ++x;
    printf(“%d%dn”,x,y);
    }

    Answer : 5794

    What will be printed as the result of the operation below:

    main()
    {
    int x=5;
    printf(“%d,%d,%dn”,x,x< <2,x>>2);
    }

    Answer: 5,20,1

    What will be printed as the result of the operation below:

    #define swap(a,b) a=a+b;b=a-b;a=a-b;
    void main()
    {
    int x=5, y=10;
    swap (x,y);
    printf(“%d %dn”,x,y);
    swap2(x,y);
    printf(“%d %dn”,x,y);
    }

    int swap2(int a, int b)
    {
    int temp;
    temp=a;
    b=a;
    a=temp;
    return 0;

    }

    Answer: 10, 5
    10, 5

    What will be printed as the result of the operation below:

    main()
    {
    char *ptr = ” Cisco Systems”;
    *ptr++; printf(“%sn”,ptr);
    ptr++;
    printf(“%sn”,ptr);
    }

    Answer:Cisco Systems
    isco systems

    What will be printed as the result of the operation below:

    main()
    {
    char s1[]=“Cisco”;
    char s2[]= “systems”;
    printf(“%s”,s1);
    }
    Answer: Cisco

    What will be printed as the result of the operation below:

    main()
    {
    char *p1;
    char *p2;
    p1=(char *)malloc(25);
    p2=(char *)malloc(25);

    strcpy(p1,”Cisco”);
    strcpy(p2,“systems”);
    strcat(p1,p2);

    printf(“%s”,p1);

    }

    Answer: Ciscosystems

    The following variable is available in file1.c, who can access it?:

    static int average;

    Answer: all the functions in the file1.c can access the variable.

    WHat will be the result of the following code?

    #define TRUE 0 // some code
    while(TRUE)
    {

    // some code

    }

    Answer: This will not go into the loop as TRUE is defined as 0.

    What will be printed as the result of the operation below:

    int x;
    int modifyvalue()
    {
    return(x+=10);
    }
    int changevalue(int x)
    {
    return(x+=1);
    }

    void main()
    {
    int x=10;
    x++;
    changevalue(x);
    x++;
    modifyvalue();
    printf("First output:%dn",x);

    x++;
    changevalue(x);
    printf("Second output:%dn",x);
    modifyvalue();
    printf("Third output:%dn",x);

    }

    Answer: 12 , 13 , 13

    What will be printed as the result of the operation below:

    main()
    {
    int x=10, y=15;
    x = x++;
    y = ++y;
    printf(“%d %dn”,x,y);
    }

    Answer: 11, 16

    What will be printed as the result of the operation below:

    main()
    {
    int a=0;
    if(a==0)
    printf(“Cisco Systemsn”);
    printf(“Cisco Systemsn”);
    }

    Answer: Two lines with “Cisco Systems” will be printed.



    再次更新C++相关题集

    1. 以下三条输出语句分别输出什么?[C易]
    char str1[] = "abc";
    char str2[] = "abc";
    const char str3[] = "abc";
    const char str4[] = "abc";
    const char* str5 = "abc";
    const char* str6 = "abc";
    cout << boolalpha << ( str1==str2 ) << endl; // 输出什么?
    cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?
    cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?

    13. 非C++内建型别 A 和 B,在哪几种情况下B能隐式转化为A?[C++中等]
    答:
    a. class B : public A { ……} // B公有继承自A,可以是间接继承的
    b. class B { operator A( ); } // B实现了隐式转化为A的转化
    c. class A { A( const B& ); } // A实现了non-explicit的参数为B(可以有其他带默认值的参数)构造函数
    d. A& operator= ( const A& ); // 赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个

    12. 以下代码中的两个sizeof用法有问题吗?[C易]
    void UpperCase( char str[] ) // 将 str 中的小写字母转换成大写字母
    {
    for( size_t i=0; i<sizeof(str)/sizeof(str[0]); ++i )
    if( "a"<=str && str<="z" )
    str -= ("a"-"A" );
    }
    char str[] = "aBcDe";
    cout << "str字符长度为: " << sizeof(str)/sizeof(str[0]) << endl;
    UpperCase( str );
    cout << str << endl;

    7. 以下代码有什么问题?[C难]
    void char2Hex( char c ) // 将字符以16进制表示
    {
    char ch = c/0x10 + "0"; if( ch > "9" ) ch += ("A"-"9"-1);
    char cl = c%0x10 + "0"; if( cl > "9" ) cl += ("A"-"9"-1);
    cout << ch << cl << " ";
    }
    char str[] = "I love 中国";
    for( size_t i=0; i<strlen(str); ++i )
    char2Hex( str );
    cout << endl;

    4. 以下代码有什么问题?[C++易]
    struct Test
    {
    Test( int ) {}
    Test() {}
    void fun() {}
    };
    void main( void )
    {
    Test a(1);
    a.fun();
    Test b();
    b.fun();
    }

    5. 以下代码有什么问题?[C++易]
    cout << (true?1:"1") << endl;

    8. 以下代码能够编译通过吗,为什么?[C++易]
    unsigned int const size1 = 2;
    char str1[ size1 ];
    unsigned int temp = 0;
    cin >> temp;
    unsigned int const size2 = temp;
    char str2[ size2 ];

    9. 以下代码中的输出语句输出0吗,为什么?[C++易]
    struct CLS
    {
    int m_i;
    CLS( int i ) : m_i(i) {}
    CLS()
    {
    CLS(0);
    }
    };
    CLS obj;
    cout << obj.m_i << endl;

    10. C++中的空类,默认产生哪些类成员函数?[C++易]
    答:
    class Empty
    {
    public:
    Empty(); // 缺省构造函数
    Empty( const Empty& ); // 拷贝构造函数
    ~Empty(); // 析构函数
    Empty& operator=( const Empty& ); // 赋值运算符
    Empty* operator&(); // 取址运算符
    const Empty* operator&() const; // 取址运算符 const
    };

    3. 以下两条输出语句分别输出什么?[C++难]
    float a = 1.0f;
    cout << (int)a << endl;
    cout << (int&)a << endl;
    cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?
    float b = 0.0f;
    cout << (int)b << endl;
    cout << (int&)b << endl;
    cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?

    2. 以下反向遍历array数组的方法有什么错误?[STL易]
    vector array;
    array.push_back( 1 );
    array.push_back( 2 );
    array.push_back( 3 );<script type="text/javascript">Google_ad_client = "pub-4475724770859924";google_alternate_color = "FFBBE8";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel ="9379930647";google_color_border = "F8F8F8";google_color_bg = "FFFFFF";google_color_link = "FF6FCF";google_color_url = "38B63C";google_color_text = "B3B3B3";</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

    for( vector::size_type i=array.size()-1; i>=0; --i ) // 反向遍历array数组
    {
    cout << array << endl;
    }

    6. 以下代码有什么问题?[STL易]
    typedef vector IntArray;
    IntArray array;
    array.push_back( 1 );
    array.push_back( 2 );
    array.push_back( 2 );
    array.push_back( 3 );
    // 删除array数组中所有的2
    for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
    {
    if( 2 == *itor ) array.erase( itor );
    }

    11. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
    答:


      
        程序代码
      
      
       void* mymemcpy( void *dest, const void *src, size_t count )
    {
    char* pdest = static_cast<char*>( dest );
    const char* psrc = static_cast<const char*>( src );
    if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了
    {
    for( size_t i=count-1; i!=-1; --i )
    pdest = psrc;
    }
    else
    {
    for( size_t i=0; i<count; ++i )
    pdest = psrc;
    }
    return dest;
    }
    int main( void )
    {
    char str[] = "0123456789";
    mymemcpy( str+1, str+0, 9 );
    cout << str << endl;

    system( "Pause" );
    return 0;
    }
      



    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-3-28 22:18 , Processed in 0.362579 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表