一、选择题
1. A 派生出子类 B , B 派生出子类 C ,并且在 java 源代码有如下声明:
1. A a0=new A();
2. A a1=new B();
3. A a2=new C();
问以下哪个说法是正确的?(D)
A 只有第一行能通过编译
B 第1、2行能通过编译,但第3行编译出错
C 第1、2、3行能通过编译,但第2、3行运行时出错
D 第1行,第2行和第3行的声明都是正确的
class A{}
class B extends A{}
class C extends B{}
public class Test{}
public static void main(String[] args){
A a0 = new A();
A a1 = new B();
A a2 = new C();
}
}爷爷管爸爸也能管到孙子
2.下面代码将输出什么内容:(B)
public class SystemUtil{
public static boolean isAdmin(String userId){
return userId.toLowerCase()=="admin";
}
public static void main(String[] args){
System.out.println(isAdmin("Admin"));
}
}
A true
B false
C 1
D 编译错误
解析:这里admin指向堆内存字符串常量池里admin的地址,而toLowerCase方法在源码中重新new String()返回的,指向的是这个字符串对象在堆内存中的地址,而这个地址里存放了指向堆内存字符串常量池里admin的地址。所以二者引用不相等,==判断为false
3.阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有(A)
package NowCoder;
class Test {
public static void hello() {
System.out.println("hello");
}
}
public class MyApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=null;
test.hello();
}
}
A 能编译通过,并正确运行
B 因为使用了未初始化的变量,所以不能编译通过
C 以错误的方式访问了静态方法
D 能编译通过,但因变量为null,不能正常运行
4.在使用super和this关键字时,以下描述正确的是(A)
A 在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一行,否则编译不通过
B super()和this()不一定要放在构造方法内第一行
C this()和super()可以同时出现在一个构造函数中
D this()和super()可以在static环境中使用,包括static方法和static语句块
子类继承父类,子类的构造方法必须调用super()即父类的构造方法,而且必须放在构造方法的第一行。
5.如下代码的 结果是什么 ?(B)
A Base
B BaseBase
C 编译失败
D 代码运行但没有输出
E 运行时抛出异常
class Base {
Base() {
System.out.print("Base");
}
}
public class Alpha extends Base {
public static void main( String[] args ) {
new Alpha();
//调用父类无参的构造方法
new Base();
}
}
解析:在执行new Alpha()时,会先执行父类的构造方法,在执行相应的子类的构造方法,所以此处会打印Base,在执行new Base()时,会直接执行Base中无参构造器的代码,再输出Base
6.如下代码的输出结果是什么?(D)
public class Test {
public int aMethod(){
static int i = 0;
i++;
return i;
}
public static void main(String args[]){
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
A 0
B 1
C 2
D 编译失败
解析:Java中静态变量只能在类主体中定义,不能在方法中定义。 静态变量属于类所有而不属于方法。
7.下列哪一种叙述是正确的(D)
A abstract修饰符可修饰字段、方法和类
B 抽象方法的body部分必须用一对大括号{ }包住
C 声明抽象方法,大括号可有可无
D 声明抽象方法不可写出大括号
8.下列说法正确的有:(C)
A class中的constructor不可省略
B constructor必须与class同名,但方法不能与class同名
C constructor在一个对象被new 时执行
D 一个class只能定义一个constructor
9.选项中哪一行代码可以替换 //add code here 而不产生编译错误(A)
public abstract class MyClass { //抽象方法
public int constInt = 5;
//add code here
public void method() {
}
}
A public abstract void method(int a);
B consInt=constInt+5;
C public int method();
D public abstract void anotherMethod(){}
10.在使用 interface 声明一个外部接口时,只可以使用(D )修饰符修饰该接口。
A private
B protected
C private protected
D public
二、编程题
1.牛牛定义排序子序列为一个数组中一段连续的子序列,并且这段子序列是非递增或者非递减排序的。牛牛有一个长度为n的整数数组A,他现在有一个任务是把数组A分为若干段排序子序列,牛牛想知道他最少可以把这个数组分为几段排序子序列.
如样例所示,牛牛可以把数组A划分为[1,2,3]和[2,2,1]两个排序子序列,至少需要划分为2个排序子序列,所以输出2
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] array = new int[n+1];//防止越界 for(int i=0;i<n;i++){ array[i]=sc.nextInt(); } int i = 0; int count = 0; while(i<n){ if(array[i]<array[i+1]){//非递减 while(i<n && array[i]<=array[i+1]){ i++; } count++; i++; }else if(array[i] == array[i+1]){ i++; }else{ while(i<n && array[i]>=array[i+1]){ i++; } count++; i++; } } System.out.println(count); } }
2.【倒置字符串】将一句话的单词进行倒置,标点不倒置。比如 I like beijing. 经过函数后变为:beijing. like I
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] s = sc.nextLine().split(" "); StringBuilder res = new StringBuilder(); for(int i = s.length - 1; i >= 0; i--) { res.append(s[i]).append(" "); } String re = res.toString().trim(); System.out.println(re); } }