二级计算机C++试题
1 . 在 c++ 里 const 和 define 关键 词 分别 用作 什么 ? 针对 # define P 13 . 1415926 和 controlconstoublePI 3 . 1415926 , 各自 的 优 缺点 。 ( 10 point ) 2 . 请 使用 2 种 方法 来 交换 2 个 变量 a 和 b 的 值 , 要求 不 能 借助 第 3 个 变量 。 ( 6 point ) 3 . 请 定义 一个 宏 , 比较 两 个数 a 、 b 的 大小 , 不 能 使用 大于 、 小于 、 if 语句 _ 。 ( 3 point ) 4 . 请 写 出 下列 代码 的 输出 内容 _ 。 ( 4 point ) # includevoid main ( ) { int a , b , c , d ; a = 10 ; b = a + + ; c = + + a ; d = 10 ^ { * } a + + ; printf ( " a : % d , b : % d , c : % d , d : % d " , a , b , c , d ) ; return 0 ; } 5 . 写 出 下列 程序 的 输出 结果 ( WIN 32 ) : ( 3 point ) int length ( charstr [ ] ) { cout < < sizeof ( str ) < < endl ; } void main ( ) { charstr [ ] = " abcdef " ; cout < < sizeof ( str ) < < endl ; cout < < sizeof ( * str ) < < endl ; length ( str ) ; } 6 . C++ 中 的 空 类 , 默认 产生 哪些 类 成员 函数 ? ( 12 point ) 7 . 写 出 下面 树 的 先 序 遍 历 、 中 序 遍 历 、 后 序 遍 历 结果 ( 6 point ) ABCDHtGH 8 . 请 简述 以下 两 个 for 循环 的 优 缺点 ( 8 point ) a . for ( inti = 0 ; i < N ; i + + ) \ { if ( condition ) DoSomething ( ) ; else DoOtherthing ( ) ; } b . if ( condition ) { for ( int i = 0 ; i < N ; i + + ) DoSomething ( ) ; } else { for ( int i = 0 ; i < N ; i + + ) DoOtherthing ( ) ; } 9 . 分析 以下 程序 的 执行 结果 ( 8 point ) # include < iostream . h > class A { intn ; public : A ( int a ) { cout < < " constructing A class " < < endl ; n = a ; cout < < " n = " < < n < < endl ; } ~ A ( ) { cout < < " destructing A class " < < endl ; } } ; class subs : public A { A bobbj ; intm ; public : subs ( int a , int b , int c ) : A ( a ) , bobj ( c ) { cout < < " constructing sub cass " < < endl ; m = b ; cout < < " m = " < < m < < endl ; } ~ subs ( ) { cout < < " destructing sub class " < < endl ; } } ; void main ( ) { subss ( 1 , 2 , 3 ) ; } 10 . 分析 下面 程序 实现 了 什么 功能 ( 5 point ) boolf ( int n ) { return ! ( n & ( n - 1 ) ) ; } 11 . 不 调用 库 函数 , 写 出 计算 字符 串 长度 函数 的 实现 ( 15 point ) int strlen ( const char * str ) 12 . 写 一个 函数 , 计算 两 个 3 X3 矩阵 相乘 ( 外 积 ) 的 函数 . ( 20 point ) double multi ply 3 X3 Matrices ( double a [ 3 ] [ 3 ] , double b [ 3 ] [ 3 ] )