您的位置:首页 > 职场人生

Java基础练习题 考试题 笔试题 面试题 (2)

2009-12-07 12:47 1036 查看
四、循环程序设计
1)执行以下程序后,输出结果为
public class ex2{
public static void main(String args[]) {
int f=1;
int k;
for (k=2;k<5;k++)
f*=k;;
System.out.println(k);
}
}
A. 0 B. 1 C. 5 D. 4 E. 24
2) 设有如下类
class Loop{
public static void main(String[] agrs) {
int x=0;int y=0;
outer:
for(x=0;x<100;x++){
middle:
for(y=0;y<100;y++){
System.out.println("x="+x+"; y="+y);
if(y==10){ <<<insert code>>> }
}
}
}
}
在<<<insert code>>>处插入什么代码可以结束外循环?
A.continue middle;
B.break outer;
C.break middle;
D.continue outer;
E.none of these
3)以下代码的运行结果为:
public class Calc {
public static void main (String args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
System.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
System.out.println("Total " + total);
}
}
A. 产生运行错误
B. 产生编译错误
C. 输出 "Total 0"
D. 产生如下输出:
i = 0 : j = 10
i = 1 : j = 9
i = 2 : j = 8
Total 30
4)以下程序的运行结果为:
public class test {
public static void main(String args[]) {
int i=0, j=2;
do {
i=++i;
j--;
} while(j>0);
System.out.println(i);
}
}
A. 0 B. 1 C. 2  D.3
5)以下程序的运行结果为?
class xyz {
public static void main(String args[]) {
int i,j,k;
for (i = 0; i < 3; i++) {
for(j=1; j < 4; j++) {
for(k=2; k<5; k++) {
if((i == j) && (j==k))
System.out.println(i);
}
}
}
}
}
A. 0 B. 1 C. 2 D. 3 E. 4
6) 以下程序的运行结果为?
class test {
public static void main(String args[]) {
int i,j=0;
for(i=10;i<0;i--) { j++; }
switch(j) {
case (0) : j=j+1;
case (1) : j=j+2; break;
case (2) : j=j+3; break;
case (10) : j=j+10; break;
default : break;
}
System.out.println(j);
}
}
A. 0 B. 1 C. 2 D. 3 E. 10
7) 观察以下程序段:
int i=1,j=10;
do{
if(i++>--j) continue;
} while(i<5);
执行完后,i、j的值分别为:
A. i=6 j=5   B. i=5 j=5
C. i=6 j=4  D. i=5 j=6
8)以下程序的输出结果为:
public class example {
public static void main(String args[]) {
int s=0;
for (int i=0;i<5;i++) {
for (int j=10;j>3*i;j--)
s += i*j;
}
System.out.println(s);
}
}
A. 127 B.136 C. 147 D.153
9) 以下程序的输出结果为:
public class example {
public static void main(String args[]) {
int i=0;
for (i=0;i<4;i++) {
if (i==3)
break;
System.out.print(i);
}
System.out.println(i);
}
}
A.0123 B.0122 C.123 D.234
10) 以下程序的运行结果为
class Prob10 {
static boolean b1;
public static void main(String [] args) {
int i1 = 11;
double f1=1.3;
do {
b1 = (f1 > 4) && (i1-- < 10);
f1 += 1.0;
} while (!b1);
System.out.println(b1 + "," + i1 + "," + f1);
}
}
A. false,9,4.3 B. true,11,1.3
C. false,8,1.3 D. true,8,7.3
五、方法设计
1)以下代码的输出结果?
public class Test{
static int x=5;
public static void main(String argv[]){
change(x);
x++;
System.out.println(x);
}
static void change(int m){
m+=2;
}
}
A. 7 B. 6 C. 5 D. 8
2) 以下代码的输出结果?
public class Test{
int x=5;
public static void main(String argv[]){
Test t=new Test();
t.x++;
change(t);
System.out.println(t.x);
}
static void change(Test m){
m.x+=2;
}
}
A. 7 B. 6 C. 5 D. 8
3) 以下代码的输出结果?
public class Test{
public static void main(String argv[]){
String x="hello";
change(x);
System.out.println(x);
}
static void change(String m){
m=m+2;
}
}
A. hello B. hello2
C. 编译报错 D. 运行报错,不能将串与整数相加
4)设有如下类:
class MyPoint {
void myMethod() {
int x, y;
x = 5; y = 3;
System.out.print( " ( " + x + ", " + y + " ) " );
switchCoords( x, y );
System.out.print( " ( " + x + ", " + y + " ) " );
}
void switchCoords( int x, int y ) {
int temp;
temp = x;
x = y;
y = temp;
System.out.print( " ( " + x + ", " + y + " ) " );
}
}
如果执行myMethod()方法,则输出结果为?
A. (5, 3) (5, 3) (5, 3)
B. (5, 3) (3, 5) (3, 5)
C. (5, 3) (3, 5) (5, 3)
5)以下程序的输出结果为:
public class test {
public static void main(String args[]) {
int s=0;
for (int k=0;k<=10;k++)
s+=method(2,k)-1;
System.out.println(s);
}
public static int method(int n,int m) {
if (m==0)
return 1;
else
return n*method(n,m-1);
}
}
A. 2048 B. 1024 C. 2036 D.2000
6) 以下程序的输出结果为:
public class test {
public static void main(String args[]) {
int m=0;
for ( int k=0;k<2;k++)
method(m++);
System.out.println(m);
}
public static void method(int m) {
System.out.print(m);
}
}
A. 000 B. 012 C.123 D.111

六、数组的使用
1)输入如下命令运行Java应用程序。
java MyTest "1 2 3"
则命令行参数数组args中得到的值哪个正确?
A. args[0] = "MyTest 1 2 3"
B. args[0] = "1 2 3"
C. args[0] = "1"
D. args[1]= "1 2 3"
2) 在注释//Start For loop 处要插入哪段代码可实现根据变量i的值定位数组ia[]的元素?
public class Lin{
public void amethod(){
int ia[] = new int[4];
//Start For loop
{
ia[i]=i;
System.out.println(ia[i]);
}
}
}
A. for (int i=0; i < ia.length() -1; i++)
B. for (int i=0; i< ia.length(); i++)
C. for (int i=1; i < 4; i++)
D. for (int i=0; i< ia.length;i++)
3)以下代码的调试结果?
public class Q {
public static void main(String argv[]) {
int anar[]= new int[5];
System.out.println(anar[0]);
}
}
A. 编译错误:anar 在引用前未初始化。
B. null
C. 0
D. 5
4) 下列创建二维整型数组正确语句是:
A. int a[][] = new int [10,10];
B. int a[10][10] = new int [][];
C. int a[][] = new int [10][10];
D. int []a[] = new int [10][10];
5) 给出下面代码:
public class Person{
  static int arr[] = new int[10];
  public static void main(String a[]) {
   System.out.println(arr[1]);
  }
}
以下那个说法正确?
A. 编译时将产生错误;
B. 编译时正确,运行时将产生错误;
C. 输出0;
D. 输出null。
6)设有如下说明:
char[] c = new char[100];
则,c[50]的值为?
A. 50
B. '/u0000'
C. " "
D. 不定
E. 为null,直到被赋值。
7) 设有如下程序,其调试结果为:
class Q2 {
public static void main(String[] args) {
int[] seeds = {1,2,3,4,6,8};
int n= seeds.length;
for (int i = 0; i < 3; i++)
for (int k = 0; k< n-1; k++)
seeds[k]= seeds[k+1];
for (int i = 0; i <n-1; i++)
System.out.print("/t"+seeds[i]);
}
}
A.输出: 1 2 3 4 6
B.输出: 4 6 8 8 8
C.输出: 2 3 4 6 8
D.输出: 2 3 4 6
七、类与对象编程
1) 以下程序的运行结果为:
public class My{
int value;
public static void main(String args[]) {
My x=new My();
if (x==null)
System.out.println("No Object");
else
System.out.println(x.value);
}
}
A. 0 B. 1 C. No Object D. 编译错误 E. null
(2)以下程序的运行结果为:
public class A {
static int k=3;
public static void main(String[] args) {
int k=4;
A x1=new A();
x1.k++;
A x2=new A();
x2.k++;
k++;
System.out.println(x1.k);
}
}
A. 3 B. 4 C.5 D.6 E.7
3) 编译和运行以下程序结果为:
public class A {
static int k=3;
static int m;
public static void main(String[] args) {
k++;
if (m==0)
System.out.println(k);
else
System.out.println(B.k);
k++;
}
}
class B {
static int k=6;
}
A. 3 B. 4 C.5 D.编译错误 E.6
4)编译和运行以下程序结果为:
1: public class Q21 {
2: int maxElements;
3: void Q21() {
4: maxElements = 100;
5: System.out.println(maxElements);
6: }
7: Q21(int i) {
8: maxElements = i;
9: System.out.println(maxElements);
10: }
11: public static void main(String[] args) {
12: Q21 a = new Q21();
13: Q21 b = new Q21(999);
14: }
15: }
A. 输出100 和 999.
B. 输出999 和 100.
C. 第2行出现编译错误,变量 maxElements未初始化.
D. 12行出现编译错误.
5)以下的程序的调试结果为
public class Scope{
int i;
public static void main(String argv[]){
Scope s = new Scope();
s.amethod();
}
public static void amethod(){
System.out.println(i);
}
}
A. 输出结果为:0
B. 无输出
C. 编译错误
D. 输出null
6)给出下面代码:
public class Person{
  static int arr[] = new int[10];
  public static void main(String a[]) {
   System.out.println(arr[1]);
  }
}
以下那个说法正确?
A. 编译时将产生错误;
B. 编译时正确,运行时将产生错误;
C. 输出0;
D. 输出null。
7)以下的程序的调试结果为?
public class As{
int i = 10;
int j;
char z= 1;
boolean b;
public static void main(String argv[]){
As a = new As();
a.amethod();
}
public void amethod(){
System.out.println(j);
System.out.println(b);
}
}
A.输出0 和false
B. 输出0 和true
C. 编译错误,b 未初始化
D. 编译错误, z 必须赋字符值
8)以下的程序的调试结果为?
public class MyAr{
public static void main(String argv[]) {
MyAr m = new MyAr();
m.amethod();
}
public void amethod(){
static int i;
System.out.println(i);
}
}
A. 输出结果为 0
B. 运行出错
C. 输出结果为 null
D. 编译错误
9) 以下程序的运行结果为?
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.print( v.i );
}
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}
}
A.10030 B. 20030 C. 209930 D. 10020
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: