java选择填空判断简答及编码题

1.单选
1.下面代码的输出结果是什么?(A)
class Test {
public static void main(String[] args) {
int a = 3;
int b = 4;
if ((a>b&(a++)>b&&(–b)<a)|(b++)<=(a–)) {
System.out.println(“判断结果为true, a=” + a + “,b=” + b);
} else {
System.out.println(“判断结果为false, a=” + a + “,b=” + b);
}
}
}
A、判断结果为true, a=3,b=5
B、判断结果为true, a=3,b=4
C、判断结果为false, a=3,b=5
D、判断结果为false, a=4,b=4

       a=3,b=4
      (a>b&(a++)>b&&(--b)<a)|(b++)<=(a--)
      false&false    不执行       true
        a=4,b=4                 b=5,a=3
       &&左边是false则右边不执行

2、下面赋值语句不正确的是(A )?
A、float f= 2.3
B、float f = 5.4f
C、double d = 3.14d
D、double d = 3.1415

float赋值时要加f/F

3、执行完代码"int[] x=new int[25];"后以下( A )说明 正确的()
A. x[24]为0
B. x[24]未定义
C. x[25]为0
D. x[0]为空

最大下标为24,int数组没赋值时默认为0,String数组没赋值时默认为null,char数组没赋值时默认为“ ”(空),double数组默认为0.0

4、下列程序的执行,说法正确的是(D)
Class ExMulti{
Static void procedure(){
try{
Int c[]={1};
c[42]=99;
}catch(ArrayIndexOutOfBoundsException e{
System.out.println(“数组超越界限异常:”+e);
}
}
Public static void main(String args[]){
try{
procedure();
Int a=args.length;
Int b=42/a;
System.out.println(“b=”+b);
}catch(ArithmeticException e){
System.out.println(“除0异常:”+e);
}
}
}
A、程序只输出第12行的异常信息
B、程序只输出第26行的异常信息
C、程序将不输出异常信息
D、程序将输出第12行和第26行的异常信息

5.下列程序的执行,说法正确的是(A)
public class MultiCatch
{
Public static void main(String args[])
{
try{
Int a=args.length;
Int b=42/a;
Int c[]={1};
c[42]=99;
System.out.println(“b=”+b);
}catch(ArithmeticException e){
System.out.println(“除0异常:”+e);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(“数组超越边界异常:”+e);
}
}
}
A、程序将输出第15行的异常信息
B、程序第10行编译出错
C、程序将输出“b=42”
D、程序将输出第15和19行的异常信息

2.多选

1、关于构造方法的理解,正确的是(ABC)
A、构造方法可以根据方法参数进行方法重载
B、构造方法用来初始化该类的一个新的对象
C、构造方法没有返回值类型
D、构造方法必须通过类名调用 (用new调用)

2、下面关于数组声明和初始化的语句那些有语法错误( AC )
A、int a1[]=new int[3]{3,4,5};
B、String a2[]={“string1”,“string1”,“string1”};
C、String a3[]=new String(3); {3}
D、int []a4[] =new int[3][3];

3、下列案例适用于封装的是:(BCD)
A、学生是一个抽象的概念,具体存在的学生如:张 三,则是学生中个一个对象
B、找朋友帮自己砍价,朋友不必将砍价的细节告知
C、将房屋看做一个类,封装就是保护了自己的私有 物品,同时又提供了门以便于他人访问
D、到饭店吃饭,无需知道蔬菜和肉是从哪里进货的 以及烹饪的具体过程,只需要下单付款即可想用 美食.

4、 能指定字符编码的I/O流类型是:(AD )
A、 InputStreamReader
B、 ObjectInputStream
C、 ObjectOutputStream
D、OutputStreamWriter

Reader,Writer字符流
InputStream,OutputStream字节流

3.判断

1、Map接口是自Collection接口继承而来(×)
2、 break关键字是switch…case…default结构中不可 缺少的语句( × )
3、在运行字节码文件中,使用java命令,一定要给出字节 码文件的扩展名.class(×)
4、一个方法中至多能存在一个return(×)
if(){
return;
}else{
return;
}

4.填空
1、执行下列代码后X的结果是什么_10__。
int x, a=2, b=3, c=4;
x=++a+b+++c++;
x=(++a)+(b++)+(c++)
++a=3 b++=3 c++=4

2、表达式“b”+10的值为__”b10”_,‘b’+10的值为108___。
’b’=98 “b”连接

3、类体中的方法分为:__实例__方法和类方法(静态方法)。

4、Java中处理异常的方式有___捕获异常_______抛出异常___

5、创建集合对象时,指定的泛型表示的是什么_集合中元素的数据类型___。

5.简答
1、四种访问修饰符的访问权限
Public 公开的 整个工程中都可以使用
Protected 本包其它类,其他包的子类
缺省:本包
Private 私有的,本类

2、 抽象类(abstract class)和接口(interface)有什么异同。

  1. 语法上 abstract class 接口 Interface
  2. 能包含的属性方法类型不同
    抽象类可以包含抽象方法,构造方法
    接口中只能包含抽象方法和静态常量,jdk1.8之后可以包含静态方法和默认方法
  3. 类只能单继承,extends接口可以多实现,implements多继承
  4. 使用场景:同类事物,接口:可以在任意类型上使用
  5. 抽象类和接口都不能被实例化

3、final关键字的使用方式以及特点
最终的
修饰成员变量,必须赋值且只能赋值一次
局部变量,可以不赋值且只能赋值一次
方法,只能被继承,不能被重写
类,不能被继承

4、列出一些你常见的运行时异常
NullPointerException,ClassCastException,
FileNotFoundException,ArrayIndexOutOfBoundsException,IOException,SQLException

5、阐述ArrayList、Vector、LinkedList的存储性能和特性。
ArrayList底层是数组,遍历修改速度快,插入删除效率低
LinkedList底层是链表,遍历修改速度慢,插入删除效率高
Vector底层数组,数据安全,效率较低

6、List、Map、Set三个接口存取元素时,各有什么特点?
List有序允许重复
Set无序不允许重复
Map键值对key不允许重复,value允许重复

7、Java中有几种类型的流?
文件流,缓冲流,对象流,打印流,转换流

6.编程题
1、请写出单例模式中饿汉式和懒汉式代码

Class HuangryInstance{
    
    
	//1,私有化构造方法
	private HuangryInstance(){
    
    
	          }
	//2. 创建一个HuangryInstance对象
	private static final HuangryInstance  intstance = new HuangryInstance  ();
	public static  HuangryInstance getInstance(){
    
    
	       return  instance;
	    }
	}
	Class LazyInstance
	{
    
    
	  private  LazyInstance(){
    
    }
	private static LazyInstance instance=null;
	public static LazyInstance  getInstatnce(){
    
    
	if(instance==null){
    
    
	intance = new LazyInstance ();
	}
	     return instance  ;
	    }
	}

2、 请写出冒泡排序代码

int[] arr= {
    
    5,3,7,1,9};
for(int i = 0; i<arr.length-1;i++){
    
    
 for(int j = 0; j<arr.length-i-1;j++){
    
    
     if(arr[j]>arr[j+1]){
    
    
           int temp = arr[j+1]
        arr[j+1] = arr[j];
         arr[j] = temp;
       }
  }
}

3、通过递归实现遍历指定文件夹下所有文件

static List<File> filelist = new ArrayList<>();

//遍历查找所有的文件装到集合里面去
public static List<File> getFileList(String strPath) {
    
    
 File dir = new File(strPath);
 File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
 if (files != null) {
    
    
     for (int i = 0; i < files.length; i++) {
    
    
         //是文件夹的话就是要递归再深入查找文件
         if (files[i].isDirectory()) {
    
     // 判断是文件还是文件夹
             getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径
         } else {
    
    
             //如果是文件,直接添加到集合
             filelist.add(files[i]);
         }
     }
 }
 return filelist;
}
public static void main(String[] args) {
    
    
    List<File> list = getFileList("e:/test");
    for (File file : list) {
    
    
        System.out.println(file.getName());
    }
}

猜你喜欢

转载自blog.csdn.net/Echoxxxxx/article/details/112802067