1 程序阅读改错
题⽬要求:
- 请阅读并分析以下四个源⽂件中的内容,找出其中会导致程序⽆法通过编译或运⾏的错误
- 修改代码使它们能够编译和运⾏
Test1.java
package com.briup.md01;
public class Test1 {
public static void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
public class TestAnother1 {
public static void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
Answer:
public class Test1 {
public static void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
class TestAnother1 {
public static void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
Test2.java
public class Test2 {
public static main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
Answer:
public class Test2 {
public static void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
Test3.java
public class Test3 {
public static void main(String args) {
System.out.println("What's wrong with this program?");
}
}
Answer:
public class Test3 {
public static void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
Test4
public class Test4 {
public void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
Answer:
public class Test4 {
public static void main(String[] args) {
System.out.println("What's wrong with this program?");
}
}
2 变量声明改错
题⽬要求:
请观察下列代码中的变量声明语句,指出哪⼏句会产⽣编译报错或精度丢失,并解释原因。
byte a = 56;
byte b = 156; //超出表示范围
int c = 2000000000 + 2000000000;
long d = 999999999;
long e = 9999999999; //超出int??
long f = 9999999999L;
3进制格式考查
题⽬要求:
定义3个int类型变量,分别使⽤⼆进制、⼋进制、⼗六进制三种字⾯常量语法为其赋值,使其值均等于⼗进制值85。
public static void main(String[] args) {
int a = 0b101_0101;
int b = 0125; //? 八进制的表示?
int c = 0x55;
System.out.println("" + a + " " + b + " " + c);
}
???
八进制的字面常量如何表示?
二进制 0b或0B开头,由0和1组成
八进制 0开头,由0、1…6、7组成
十进制 常见整数,由0、1…8、9组成
十六进制 0x或0X开头,由0、1…8、9、a、b、c、d、e、f组成,大小写不区分
4 找零问题
题⽬要求:
⼩明有2元,买东⻄花了1.1元,问找零多少,请⽤Java代码描述上述过程!
注意:观察程序运算输出是否能得到0.9,如果不能,请解决该精度问题。
???
2 - 1.1 = 0.8999999999…
- 使用单精度 2.0f - 1.1f , why??
- BigDecimal 类可以精确表示小数点后任意位数的数值, 但是它相比浮点数慢得多,因此适用于计算精度要求高的场景。
- 对浮点数四舍五入
System.out.println(Math.round((2 - 1.1) * 100) / 100.0);
java 项目浮点数精度丢失,可以怎么解决?
5 字面值常量考查
题⽬要求:
请定义2个变量,分别⽤每天对应的毫秒数以及微秒数(表达式描述)给其赋值。输出变量值,观察是否正确。如果输出有问题,请解决
???
什么意思???
int 范围 21亿左右
6 数据类型问答
基础类型有哪些,取值范围分别是什么?
整型 byte short int long
浮点型 float double
字符型 char
布尔型 boolean
7 数据类型问答
题⽬要求:
请回答引⽤数据类型有哪些,分别是什么?
???
类 数组 ???:接口
8 数据类型问答
题⽬要求:
请简述基本类型和引⽤类型变量的区别。
???
基本类型在栈存储的是值, 引用类型存储的是是对象的地址,对象存储在堆中
基本类型变量互相赋值时,传的是实际的值,引用类型传的是地址
详细的看这篇文章
9 编程题
题⽬要求:
请编码输出下图内容:
输出内容包含3部分,表头、表格、表尾,具体要求如下:
表头部分:可以采⽤多条System.out.println语句输出,其是固定数据, 直接输出即可
表格中间:请定义变量描述商品型号、尺⼨、价格、库存,然后按格式进⾏输出(注意变量数据类型 )
表格尾巴:先对商品数据进⾏数学计算,得出结果后输出内容
public static void main(String[] args) {
String name1 = "MacBookAir";
double size1 = 13.3;
double price1 = 5699.0;
int store1 = 5;
String name2 = "ThinkPadT490";
double size2 = 14.0;
double price2 = 8499.0;
int store2 = 10;
String name3 = "MateBook 14";
double size3 = 14.0;
double price3 = 7199.0;
int store3 = 18;
System.out.println("---------------商场库存清单---------------");
System.out.println("商品型号\t\t\t尺寸\t\t价格\t\t库存数");
System.out.printf("%s\t\t%.1f\t%.1f\t%d\n",name1,size1,price1,store1);
System.out.printf("%s\t%.1f\t%.1f\t%d\n",name2,size2,price2,store2);
System.out.printf("%s\t\t%.1f\t%.1f\t%d\n",name3,size3,price3,store3);
System.out.println("----------------------------------------");
System.out.printf("总库存数:%d\n", store1 + store2 + store3);
System.out.printf("商品库存总金额:%.1f\n", store1 * price1 + store2 * price2 + store3 * price3);
}
10 程序
题⽬要求:
分析以下代码的运⾏结果。
public static void main(String[] args) {
short s = 1;
s = s + 2; //编译报错
System.out.println(s);
short x = 1;
x += 1; //编译通过
System.out.println(x);
}
???
Java你也搞双标??💩
注意:+=、-=、*=、/= 等扩展的赋值运算符,隐含了强制类型转换!