//选择
- 下列程序的输出结果是()。
int main(void)
{
int a[10] = {0,1,2,3,4,5,6,7,8,9}, *p = a+3;
printf(“%d”, *++p);
return 0;
}
A.3
B.4
C.a[4]的地址
D.非法
int a[10] = {0,1,2,3,4,5,6,7,8,9},
*p=a[0]=0
*p = a+3,指针移动3位;*p=a[3]=3
printf(“%d”, *++p) =>> ++p , P先自加1, 投入指针运算,相当于*(p+1)=a[4]=4
- 以下程序的运行结果是()
#include<iostream>
using namespace std;
class Sample{
public:
Sample(int i,int j){
x=i;
y=j;
}
void disp(){
cout<<"disp1"<<endl;
}
void disp()const{
cout<<"disp2"<<endl;
}
private:
int x,y;
};
int main(){
const Sample a(1,2);
a.disp();
return 0;
}
A.disp1
B.disp2
C.disp1 disp2
D.程序编译出错
如果一个对象说明为常对象,则通过该对象只能调用它的常成员函数。 题中,对象a被定义成类Sample的常对象,通过对象a只能调用其常成员函数disp,所以程序最后输出disp2。
3. When is the text “Hi there”displayed?
public class StaticTest
{
static
{
System.out.println(“Hi there”);
}
public void print()
{
System.out.println(“Hello”);
}
public static void main(String args[])
{
StaticTest st1 = new StaticTest();
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}
}
A.Never.
B.Each time a new object of type StaticTest is created.
C.Once when the class is loaded into the Java virtual machine.
D.Only when the main() method is executed.
类中静态语句块仅在类加载时被执行一次
被static修饰的语句或者变量有如下特点:
1.随着类的加载而加载 2.优先于对象存在
3.被所有对象所共享 4.可以直接被类名所调用
使用注意:
1.静态方法只能访问静态成员
2.静态方法中不可以写this,super关键字
3.主函数是静态的
4.执行下面程序,正确的输出是()。
intx=5,y=7;
void swap ()
{ int z;
z=x;
x=y;
y=z;
}
int main (void)
{
int x=3,y=8;
swap();
printf ("%d,%d\n",x,y) ;
}
A.3,8
B.8,3
C.5,7
D.7,5
swap函数用的是全局的x和y,但是不会发生交换 注意:printf函数在进行输出的时候是就近原则的,所以输出的x和y是main函数里面的,而不是全局的x和y
5. 下列关于const关键字的说法错误的是:
A.用const常量代替宏定义可以让编译器进行安全性检查
B.类的const成员函数不能修改类的成员变量,而且一个const类对象只能