一.不定项选择
1.for which of these values of a
Ⅰ 4
Ⅱ 6
Ⅲ 8
is the expression
(a!=3&&a!=4&&a!=5&&a!=6)
false? (D)
A Ⅰonly
B Ⅱ only
C Ⅲ only
D Ⅰ and Ⅱ only
E Ⅰ Ⅱ Ⅲ
2.Given the follling code :what is the result ? (B)
public class test{
private static int j=0;
private static boolean methodB(int k){ 3
j+=k;
returrn true;
}
public static void methodA(int){ 2
boolean b;
b=i<10|methodB(4); 单或两个都要执行
b=i<10|methodB(8); 双或执行一个就可以
}
public static void main (String[] args){ 先看主函数 1
methodA(0);
Systeem.out.ppritln(j);
}
}
A the program prints"0"
B the program prints"4"
C the program prints"12"
D the code does not complete
3.What is the result? ( A )
public class test{
static boolean foo(char c)
System.out.print(c);
return true;
}
public void mian (String[]args){
int i=0;
foo(foo('A');foo('B')&&(i<2);foo('C')){
i++;
foo('D');
}
}
}
A ABDCBDCB
B ABCDABCD
C Compilation fails
D An exception is thrown at runtime.
4.What will happen when you attempt to compile and run the following code? (D)
public class test{
static {
int x=5;
}
static int x,y;
public static void main (String[]args){
x--;
myMethod();
System.out.println(x+y+ ++x);
}
public static void myMethod(){
y=x++ + ++x;
}
}
A compiletime error
B prints:1
C prints:3
D prints:4
5.which lines of the folllowing willl produce an error? (A)
byte a1=2,a2=4,a3;
short s=16;
a2=s;
a3=a1*a2;
A line 3 and line 4
B line 1 only
C line 3 only
D line 4 only
6.Please write the output result (A)
public class test{
public static void changeStr(String str)){
str="welcome";
}
public static void main(String [] args){
String str="1234";
changeStr(str);
System.out.println(Str);
}
}
A 1234
B welcome
C null
D undefine
7.Given
public class test{
private static float[] f=new float[2];
public static void mian(String [] args){
System.out.println("f[0]="+f[0]);
}
}
What is the result?(B)
A f[0]=0
B f[0]=0.0
C Compilation fails 编译失败
D An exception is thrown at runtime 运行时异常抛出
8.下列说法正确的有(ACD)
A 环境变量可在编译source code 时指定
B 在编译过程中,所能指定的环境变量不包括class path
C Javac一次可同时编译数个Java源文件
D Javac.exe能指定编译结果要置于哪个目录(direcory)
9.下列运算符合法的是(A )
A && B< >
C if D :=
10. 已知int a []=new int[10],则下列对数组元素访问不正确的是(A)
A a[0] B a[1]
C a[9] D a[10]
11.下面这三条语句
System.out.println(“is”+100+5);
System.out.println(100+5+“is”);
System.out.println(“is” +(100+5));
的输出结果分别是(D)
A is 1005,1005 is , is 1005
B is 105 , 105 is ,is 105
C is 1005 ,1005 is ,is 105
D is 1005, 105 is , is 105
12.对于下列这些代码,以下说法正确的是(C )
public class test {
public int x;
public static void main (String[]args){
System..out.println("Value is "+x);
}
}
A 程序会打出“Value is 0”
B 程序会抛出 NullPointerException
C 非静态变量不能被静态方法引用
D 编译会抛出“”
13.已知声明并初始化二维数组 int a[ ][ ]={{1,2},{3,4},{5,6}},则a[1][1]的值为(C)
A 1 B 2 C 4 D 5
14.代码System.out.println(10%3*2);将打印出(B)
A 1 B 2 C 4 D 6
15.有如下代码:请写出程序的输出结果(D)
public class test{
public static void main (String[]args){
int x=0;
int y=0;
int k=0;
for(int z=0;z<5;z++){
if((++x>2)&&(++y>2)&&(k++>2)){
x++;
++y;
k++;
}
}
System.out.println(x+" " +y+""+k);
}
}
A 4 5 1 B 3 2 1
C 1 2 3 D 5 3 1
16.如下代码:
int i, sum=0;
for(i=0;i<10;++i,sum+=i)
i的最终结果是(A)
A 10 B 9
C 11 D 以上答案都不正确
17.下列代码执行的结果是(B)
public class Test3{
public static void main(String []args){
System.out.println(100%3);
System.out.println(100%3.0);
}
}
A 1和1 B 1和1.0
C 1.0和1 D 1.0和1.0
18.根据下面的代码,
String s =null;
会抛出NullpointerException的异常有(AC)
A if((s!=null)&(s.length()>0) )
B if( (s!=null)&&(s.length()>0) )
C if( (s==null)|(s.length()==0) )
D if( (s==null)||(s.length()==0) )
19.在类tester中定义的方法如下,
public double max( int x,int y){
//省略
}
则在该类中定义如下哪个方法头是对上述方法的重载(Overload)(D)
A public int max(int a ,int b){}
B public int max (doublea, double b){}
C public double max (int x,int y){}
D private double max(int a,int b){}
20.下面有关重载函数的说法正确的是(C)
A 重载函数必须具有不同的返回值类型
B 重载函数形参个数必须不同
C 重载函数必须有不同的形参列表
D 重载函数名可以不同
21.以下产生信息丢失的类型转换的是(B)
A float a=10
B int a=(int) 8846.0
C byte a=10; int b=-a
D double d=100
22.下面所示的Java代码,运行时,会产生(D)类型的异常
int Array_a[]=new int[10];
System.out.println(Array_a[10]);
A ArithmeticException
B nullpointerException
C IOException
D ArrayIndexOutOfBoundsException
23.设有定义 int a=3,b=4,c=5;则以下的表达式中,值为false的是(D)
A a<b&&b<c
B a<=b
C a<(b+c)
D !(a<b)
24.下面代码的运行结果是(C)
public static void main(String[] args){
String s;
System.out.println("s="+s);
}
A 代码编程成功,并输出“s=”
B 代码编译成功,并输出“s=null”
C 由于String s没有初始化,代码不能编译通过。
D 代码编译成功,但捕获到NullPointerException异常
25.代码片段
byte b1=1,b2=2,b3,b6;
final byte b4=4,b5=6;
b6=b4+b5;
b3=(b1+b2);
System.out.println(b3+b6);
关于上面代码正确的是(C)
A 输出的结果:13
B 语句:b6=b4+b5编译出错
C 语句:b3=b1+b2编译出错
D 运行期抛出异常
26.下列Java程序输出结果为(B)
int i=0;
Integer j=new Integer(0);
System.out.println(i==j);
System.out.println(j.equals(i));
A true,false
B true,true
C false,true
D false,false
27.有如下代:
int i=5;
int s=(i++)+(++i)+(i–)+(–i);
最终s的值为(E )
A 28 B 25 C 21
D 26 E 24 F 23
28.阅读代码,以下说法正确的是(C)
public class Test{
public int x ;
public static void main(String[]args){System.out.println("value is "+x);}
}
A 程序会打出“Value is 0”
B 程序会抛出NullpointerException
C 非静态变量不能被静态方法引用
D 编译器会抛出“possible reference before assignment”的错误
29.下列语句中,将函数int sum(int x, int y)正确的是(C)
A float sum (int x, int y);
B int sum(int x,int y);
C float sum (float x, float y);
D double sum(int y,int x);
30.You have the following code.which numbers will cause"Test2" to be printed(D )
swicth (x){
case 1:
System.out.println("test1");
case2:
case3:
System.out.pritnln("test2");
break;
default;
System.out.println("test3");
break;
]
A 0
B 1
C 2
D 3
E 4
二,编程题
1.(按奇偶顺序排序数组)给定一个非负整数数组A,返回一个由A的所有偶数元素组成的数组,后面跟A的所有奇数元素,即所有偶数在数组A的前面,所有奇数在数组A的后面。
示例:
输入:[3,1,2,4]
输出:[2,4,3,1]
输出:[4,2,3,1],[2,4,1,3]和[4,2,1,3]也会被接受。
class Solution1{
public int[] sortArrayByParity(int[] A){
}
}
2.(求众数)给定一个大小为n的数组,找到其中的众数。众数是指数组中出现次数大于n/2的元素。
示例1:
输入:[3,2,3]
输出:3
示例2:[2,2,1,1,1,2,2]
输出:2
class Solution2{
public int majorityElement(int []nums){
}
}
3.(打印图形)编写代码打印如下数列。
1
212
32123
4321234
32123
212
1
class Solution3{
public void print(int line){
}
}
4.(双素数)双素数是指一对差值为2 的素数。找出小于1000的所有双素数。
class Solution4{
public void find (int maxLimit)
}
}