面试题Four

1、ArrayList list=new ArrayList(20);中的list扩充几次()

A. 0
B. 1
C. 2
D. 3
答案:A
分析:已经指定了长度, 所以不扩容

2.下列叙述中正确的是()

A. 循环队列有队头和队尾两个指针,因此,循环队列是非线性结构
B. 在循环队列中,只需要队头指针就能反映队列中元素的动态变化情况
C. 在循环队列中,只需要队尾指针就能反映队列中元素的动态变化情况
D. 在循环队列中元素的个数是由队头指针和队尾指针共同决定的
答案:D
分析:循环队列中元素的个数是由队首指针和队尾指针共同决定的,元素的动态变化也是通过队首指针和队尾指针来反映的,当队首等于队尾时,队列为空。

3.要从文件”file.dat”文件中读出第10个字节到变量c中,下列哪个正确

A.
FileInputStream in=new FileInputStream(“file.dat”);
in.skip(9);
int c=in.read();
B.
FileInputStream in=new FileInputStream(“file.dat”);
in.skip(10);
int c=in.read();
C.
FileInputStream in=new FileInputStream(“file.dat”);
int c=in.read();
D.
RandomAccessFile in=new RandomAccessFile(“file.dat”);
in.skip(7);
int c=in.readByte();
答案:A
分析: skip(long n)该方法中的n指的是要跳过的字节数

3.新建一个流对象,下面那个选项的代码是错误的?()

A. new BufferedWriter(new FileWriter(“a.txt”));
B. new BufferedReader (new FileInputStream(“a.dat”));
C. new GZIPOutputStream(new FileOutputStream(“a.zip”));
D. new ObjectInputStream(new FileInputStream(“a.dat”));
答案:B
分析:BufferedReader类的参数只能是Reader类型的,不能是InputStream类型。

4.输入年月日,计算该日期是这一年的第几天。


public class DayCounting {
public static void main(String[] args) {
int[][] data = {
{31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
Scanner sc = new Scanner(System.in);
System.out.print("请输入年月日(1980 11 28): ");
int year = sc.nextInt();
int month = sc.nextInt();
int date = sc.nextInt();
int[] daysOfMonth = data[(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0];
int sum = 0;
for(int i = 0; i < month -1; i++) {
sum += daysOfMonth[i];
}
sum += date;
System.out.println(sum);
sc.close();
}
}

第二种写法

public class test3 {
    public static void main(String[] args) throws ParseException {
        //如果明确要你计算这一天是今年的第几天
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = df.parse("2020-07-23");
//        Calendar c = Calendar.getInstance();
//        c.set(Calendar.MONTH, 2020);
//        c.setTime(date);

        //直接计算今天是今年的第几天
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, 2020);
        c.setTime(new Date());
        System.out.println(c.get(Calendar.DAY_OF_YEAR));
    }

}

5 转换大小写

public class test1 {
    public static void main(String[] args) {

            //转换大小写
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入Hello World");
            
            String character = sc.nextLine();
            char[] arr = character.toCharArray();
            
            for (int i = 0; i < arr.length; i++) {
//                if ((int) arr[i] >= 65 && (int) arr[i] <= 90) {
//                    //转换成小写
//                    arr[i] += 32;
//                }
                if ((int) arr[i] >= 97 && (int) arr[i] <= 122) {
                    //转换成大写
                    arr[i] -= 32;
                }
            }

            for (int i = 0; i < arr.length; i++) {
                //转换为小写之后输出
                System.out.print(arr[i]);
            }
        }


}


6 替换为特定字母(在你知道as码的情况下)

//将字母o替换成 *
public class test2 {
    public static void main(String[] args) {

        //字母小写o是111,大写的O是111-32
        //*是 42
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入Hello World");
    
        String character = sc.nextLine();
        char[] arr = character.toCharArray();
        
        for (int i = 0; i < arr.length; i++) {
            if ((int) arr[i]== 111) {
                //将字母o变成*
                arr[i] = 42;
            }
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }


}


7 画图(仔细观察要画的图)

在这里插入图片描述



public class test4 {
    public static void main(String[] args) {


            int a =4;
            //上半部分
            for (int i = 1; i <= a; i++) { //控制行数
                //画空格
                for (int j = 1; j <= a - i; j++) {//规律  3 2 1
                    System.out.print(" ");
                }
                //画星星
                if(i<3){ // 前两行规律 1 3
                for (int j = 1; j <= 2 * i - 1; j++) {
                    System.out.print("*");
                }
                }
                //画星星
                if(i==3){//因为第三行要求画6个星星   下标0-5 画6个
                    for (int j = 0; j <= 2 * i - 1; j++) {
                        System.out.print("*");
                    }
                }
                //画加号
                if(i==4){ //画8个 + 号
                    for (int j = 0; j <= 2 * i - 1; j++) {
                        System.out.print("+");
                    }
                }

                //画空格
                for (int j = 1; j <= a - i; j++) {
                    System.out.print(" ");
                }
                //换行
                System.out.println();
            }



            //下半部分
            for (int i = a - 1; i >= 0; i--) { //控制行数
                //画空格
                for (int j = 1; j <= a - i; j++) {
                    System.out.print(" ");
                }
                //画星星
                if(i<3){
                    for (int j = 1; j <= 2 * i - 1; j++) {
                        System.out.print("*");
                    }
                }
                //画星星
                if(i==3){
                    for (int j = 0; j <= 2 * i - 1; j++) {
                        System.out.print("*");
                    }
                }

                //画空格
                for (int j = 1; j <= a - i; j++) {
                    System.out.print(" ");
                }
                //换行
                System.out.println();
            }

        }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值