(1)A 派生出子类 B , B 派生出子类 C ,并且在 java 源代码有如下声明:
1. A a0=new A();
2. A a1=new B();
3. A a2=new C();
问以下哪个说法是正确的()
A 只有第一行能通过编译
B 第1、2行能通过编译,但第3行编译出错
C 第1、2、3行能通过编译,但第2、3行运行时出错
D 第1行,第2行和第3行的声明都是正确的
解析:虽然A不是C的直接父类,但也是存在继承关系的
(2)下面代码将输出什么内容:()
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 编译错误
解析:本题考查==的含义,对于两个引用变量,只有他们指向同一个引用时,==才会返回true。题目中"admin"指向堆内存字符串常量池里admin的地址,而String类的方法都是通过创建新的对象也就是new String()的方式返回的,因此userId.toLowerCase()指向的是这个字符串对象在堆内存中的地址。如果题目中isAdmin方法返回值更改为`return userId.toLowerCase().equals("admin");`,输出结果将变为true。
(3)阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有()
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,不能正常运行
解析:Test test=null;代表这个引用不指向任何一个对象,但是hello是一个静态方法,静态方法的调用不依赖任何对象。
(4)在使用super和this关键字时,以下描述正确的是()
A 在子类构造方法中使用 super() 显示调用父类的构造方法,super() 必须写在子类构造方法的第一行,否则编译不通过
B super() 和 this() 不一定要放在构造方法内第一行
C this() 和 super() 可以同时出现在一个构造函数中
D this() 和 super() 可以在static环境中使用,包括static方法和static语句块
解析:
B:super() 和 this()都需要在第一行
C:this() 和 super()不可以同时出现在一个构造函数中(都要在第一行,怎么办)
D:this() 和 super()不可以在static环境中使用,包括static方法和static语句块
(5)如下代码的 结果是什么 ?
class Base { Base() { System.out.print("Base"); } } public class Alpha extends Base { public static void main( String[] args ) { new Alpha(); //调用父类无参的构造方法 new Base(); } }A Base
B BaseBase
C 编译失败
D 代码运行但没有输出
E 运行时抛出异常
解析:
Alpha 是 Base 的子类,在new Alpha 的时候会默认自动帮 Alpha 生成父类,调用父类的构造方法,
new Alpha();打印一次Base,
new Base();打印一次Base。
(6)如下代码的输出结果是什么?
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 编译失败
解析:static在定义的时候不能定义成局部变量,static定义的变量一定是静态成员变量,不能定义在普通方法的内部,静态方法内部也不可以,static定义的变量是类变量,属于类,放在方法里是属于方法的。
(7)下列哪一种叙述是正确的()
A abstract修饰符可修饰字段、方法和类
B 抽象方法的body部分必须用一对大括号{ }包住
C 声明抽象方法,大括号可有可无
D 声明抽象方法不可写出大括号
解析:
A:abstract修饰符不可以修饰字段
B:抽象方法的body部分不需要用一对大括号{ }包住
C:声明抽象方法,大括号必须没有
(8)下列说法正确的有:()
A class中的constructor不可省略
B constructor必须与class同名,但方法不能与class同名
C constructor在一个对象被new 时执行
D 一个class只能定义一个constructor
解析:
A class中的constructor可以省略,编译器自动生成
B constructor必须与class同名,但方法也可以与class同名(构造方法)
D 一个class可以定义多个constructor(重载)
(9)选项中哪一行代码可以替换 //add code here 而不产生编译错误
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(){}
解析:
B:成员变量赋值可以在方法外,但是运算一定要在方法里面
C:没有花括号{},还不是抽象方法
D:抽象方法不能有花括号{}
(10)在使用 interface 声明一个外部接口时,只可以使用( )修饰符修饰该接口。
A private
B protected
C private protected
D public
解析:
A:接口是要被实现的,不能是私有的
B:protected是包保护的
链接:排序子序列_牛客笔试题_牛客网 (nowcoder.com)
- 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
- array[i] = sc.nextInt();
- }
- int i = 0;
- int count = 0;
- while(i
- if(i
1]){//非递减 - while(array[i]
1]){ - i++;
- }
- count++;
- i++;
- }else if(array[i]==array[i+1]){
- i++;
- }else{
- while(i
array[i+1]){ - i++;
- }
- count++;
- i++;
- }
- }
- System.out.println(count);
- }
- }
解题思路
用循环把临近递增递减的数字分组,一共三种情况:递增递减相等。当递增转为递减,递减转为递增时,count++,小组数加1,
注意:循环比较会对数组越界访问,通过对数组额外加一个元素0来解决
倒置字符串
- import java.util.*;
- public class Main{
- public static void reverse(char[] array,int start,int end){
- while(start
- char ret = array[start];
- array[start] = array[end];
- array[end] = ret;
- end--;
- start++;
- }
- }
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- String str =sc.nextLine();
- char[] ch = str.toCharArray();
- int len = str.length();
- //整体倒置
- reverse(ch,0,len-1);
- //部分倒置
- int i=0;
- while(i
- int j=i;
- while(j < len && ch[j] != ' '){
- j++;
- }
- reverse(ch,i,j-1);
- i=j+1;
- }
- String str2 = new String(ch);
- System.out.print(str2);
- }
- }
整体思路:
将字符整体倒置,再部分倒置。
为什么不用String Buffer自带的倒置字符串函数?
首先将从键盘输入的字符串转为字符型数组,因为String Buffer自带的倒置字符串函数reverse不能传递参数,所以需要自己写一个reverse函数。
详细思路:
reverse函数:传递三个参数分别为数组ch,起始引用i,结尾引用j,首先定义两个引用,一个在头(i)一个在尾(j),在i和j没有相遇(即i
然后先将字符串全部倒置 即 reverse(ch,0,len-1);
再倒置每个小部分:利用两个while循环,最外层是为了保证越界,要让i始终小于数组长度len;在第二层之前首先要让j在i位置,然后利用第二层while循环在j
因为以上是将字符串转换为char型数组实现的,最后输出根据题目要求 要输出字符串
所以new一个字符串类型变量,将数组转成字符串,最后输出该字符串常量
-
相关阅读:
中文大语言模型汇总
QQ五毛项目记
java招投标系统 招投标系统简介 招投标系统源码
JavaScript的原型和继承
蓝牙学习五(广播包分析wireshark)
PEFT学习:使用LORA进行LLM微调
4-UI自动化-selenium三大等待操作
pytorch深度学习实战24
SQL优化
MONAI_Label 安装试用
-
原文地址:https://blog.csdn.net/m0_65601072/article/details/128031035