JavaSE基础题

1、用最有效的方法算出2×8=?

2<<3

2、Math.round(11.5)和Math.round(-11.5)的值?

Math.round(11.5) = 12

Math.round(-11.5) = -11

3、==和equals()区别?

==:比较对象地址

equals():如果没有重写,效果和==相同;如果重写了就按重写的规则比较

4、switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?

switch支持的类型:byte、short、int、char,JDK1.5之后支持枚举,JDK1.7之后支持String类型

5、char型变量中是否可以存储一个汉字?

能,Java中使用Unicode编码,一个字符占两个字节,可以用char存储Unicode中包含的汉字

6、float f = 3.4是否正确?表达式15/2*2的值是多少?

不正确,float f = 3.4f

15/2*2 = 14

7、编写代码实现两个变量值交换,int m = 3, n = 5;

int temp = m;

m = n;

n = temp;

m = m + n;

n = m – n;

m = m – n;

m = m ^ n;

n = m ^ n;

m = m ^ n;

8、Java的基本数据类型有哪些?String是基本数据类型吗?

byte,short,int,long,float,double,char,boolean

String是引用数据类型,不是基本数据类型

9、数组有没有length()方法?String有没有length()方法?File有没有length()方法?ArrayList有没有length()方法?

数组没有length()方法,但是有length属性

String和File有length()方法

ArrayList没有length()方法,有size()方法获取有效元素个数

10、String str = new String(“hello”);创建了哪些对象?

字符串常量池中有一个对象,堆中有一个字符串对象

11、如何将String类型转化为Number类型?举例说明String str = “123”;

Integer num1 = new Integer(str);

int num2 = Integer.parseInt(str);

Integer num3 = Integer.valueOf(str);

12、以下代码的运行结果:

  1 public static void main(String[] args) {
  2 	char x = 'x';
  3 	int i = 10;
  4 	System.out.println(true? x : i);
  5 	System.out.println(true? 'x' : 10);
  6 }

120

x

如果其中有一个是变量,按照自动类型转换规则处理成一致的类型

如果都是常量,一个是char,另一个是[0-65535]之间的整数按char处理;如果一个是char,另一个是其他,按照自动类型转换规则处理成一致的类型

13、以下代码的执行结果

  1 public static void main(String[] args) {
  2 	int a = 8, b = 3;
  3 	System.out.println(a>>>b);
  4 	System.out.println(a>>>b | 2);
  5 }

1

3

14、下面程序片段的输出结果是?

  1 public static void main(String[] args) {
  2 	int a = 3;
  3 	int b = 1;
  4 	if(a = b){
  5 		System.out.println("Equal");
  6 	}else{
  7 		System.out.println("Not Equal");
  8 	}
  9 }

编译不通过,应该是a==b

15、执行如下代码后,c的值是多少?

  1 public static void main(String[] args) {
  2 	int a = 0;
  3 	int c = 0;
  4 	do {
  5 		--c;
  6 		a = a - 1;
  7 	} while (a >= 0);
  8 	System.out.println("c = " + c);
  9 }

c = -1

16、以下代码的运行结果?

  1 public static void main(String[] args) {
  2 	int i=10;
  3 	while(i>0){
  4 		i = i +1;
  5 		if(i==10){
  6 			break;
  7 		}
  8 	}
  9 	System.out.println("i=" + i);
 10 }

是一个负数,因为i一直累加会超过int的存储范围

死循环

17、修正如下代码

下面是一段程序,目的是输出10个=,但是不小心代码写错了,现在需要修改代码,使得程序完成功能,但是只能“增加”或“修改”其中“一个”字符(很明显,将i--改为i++,可以完成功能,但是需要修改“两个”字符,所以并不是一个正确的答案)

  1 public static void main(String[] args) {
  2 	int n=10;
  3 	for (int i = 0; i < n; i--) {
  4 		System.out.println("=");
  5 	}
  6 }//不改就是一个死循环

i<n修改为-i<n

18、以下代码的运行结果是什么?

  1 public class Test {
  2 	public static boolean foo(char c) {
  3 		System.out.print(c);
  4 		return true;
  5 	}
  6 
  7 	public static void main(String[] args) {
  8 		int i = 0;
  9 		for (foo('A'); foo('B') && (i < 2); foo('C')) {
 10 			i++;// 1 2
 11 			foo('D');
 12 		}
 13 	}
 14 }

ABDCBDCB

19、以下代码的执行结果是什么?

  1 public static void main(String[] args) {
  2 	int i = 0;
  3 	change(i);
  4 	i = i++;
  5 	System.out.println("i = " + i);
  6 }
  7 public static void change(int i){
  8 	i++;
  9 }

i=0

20、以下程序的运行结果?

  1 public static void main(String[] args) {
  2 	String str = new String("world");
  3 	char[] ch = new char[]{'h','e','l','l','o'};
  4 	change(str,ch);
  5 	System.out.println(str);
  6 	System.out.println(String.valueOf(ch));
  7 }
  8 public static void change(String str, char[] arr){
  9 	str = "change";
 10 	arr[0] = 'a';
 11 	arr[1] = 'b';
 12 	arr[2] = 'c';
 13 	arr[3] = 'd';
 14 	arr[4] = 'e';
 15 }

world

abcde

21、以下代码的运行结果?

  1 public static void main(String[] args) {
  2 	Integer i1 = 128;
  3 	Integer i2 = 128;
  4 	int i3 = 128;
  5 	int i4 = 128;
  6 	System.out.println(i1 == i2);
  7 	System.out.println(i3 == i4);
  8 	System.out.println(i1 == i3);
  9 }

false

true

true

Integer的i1和i2是对象,他们==比较的是地址,在-128~127范围内使用缓存的常量对象,如果超过这个范围,是新new对象,不是常量对象

22、以下代码的运行结果?

  1 public static void main(String[] args) {
  2 	double a = 2.0;
  3 	double b = 2.0;
  4 	Double c = 2.0;
  5 	Double d = 2.0;
  6 	System.out.println(a == b);
  7 	System.out.println(c == d);
  8 	System.out.println(a == d);
  9 }

true

false

true

23、以下代码的运行结果?

  1 public class Test {
  2 	int a;
  3 	int b;
  4 	public void f(){
  5 		a = 0;
  6 		b = 0;
  7 		int[] c = {0};
  8 		g(b,c);
  9 		System.out.println(a + " " + b + " " + c[0]);
 10 	}
 11 	public void g(int b, int[] c){
 12 		a = 1;
 13 		b = 1;
 14 		c[0] = 1;
 15 	}
 16 	public static void main(String[] args) {
 17 		Test t = new Test();
 18 		t.f();
 19 	}
 20 }

1 0 1

24、以下代码的运行结果?

  1 public class Test {
  2 	static int x, y, z;
  3 
  4 	static {
  5 		int x = 5;
  6 		x--;
  7 	}
  8 
  9 	static {
 10 		x--;
 11 	}
 12 
 13 	public static void main(String[] args) {
 14 		System.out.println("x=" + x);
 15 		z--;
 16 		method();
 17 		System.out.println("result:" + (z + y + ++z));
 18 	}
 19 
 20 	public static void method() {
 21 		y = z++ + ++z;
 22 	}
 23 }

x=-1

result:3

25、以下程序的运行结果?

  1 public class Test {
  2 
  3 	public static void main(String[] args) {
  4 		new A(new B());
  5 	}
  6 }
  7 class A{
  8 	public A(){
  9 		System.out.println("A");
 10 	}
 11 	public A(B b){
 12 		this();
 13 		System.out.println("AB");
 14 	}
 15 }
 16 class B{
 17 	public B(){
 18 		System.out.println("B");
 19 	}
 20 }

B

A

AB

26、如下代码是否可以编译通过,如果可以,运行结果是什么?

  1 interface A{
  2 	int x = 0;
  3 }
  4 class B{
  5 	int x = 1;
  6 }
  7 class C extends B implements A{
  8 	public void printX(){
  9 		System.out.println(x);
 10 	}
 11 	public static void main(String[] args) {
 12 		new C().printX();
 13 	}
 14 }

编译错误

System.out.println(x);报错,x有歧义

27、以下代码的运行结果?

  1 public class Test {
  2 
  3 	public static void main(String[] args) {
  4 		Base b1 = new Base();
  5 		Base b2 = new Sub();
  6 	}
  7 }
  8 class Base{
  9 	Base(){
 10 		method(100);
 11 	}
 12 	public void method(int i){
 13 		System.out.println("base : " + i);
 14 	}
 15 }
 16 class Sub extends Base{
 17 	Sub(){
 18 		super.method(70);
 19 	}
 20 	public void method(int j){
 21 		System.out.println("sub : " + j);
 22 	}
 23 }

base :100

sub :100

base :70

28、以下代码的执行过程?

  1 public static void main(String[] args) {
  2 	int test = test(3,5);
  3 	System.out.println(test);
  4 }
  5 
  6 public static int test(int x, int y){
  7 	int result = x;
  8 	try{
  9 		if(x<0 || y<0){
 10 			return 0;
 11 		}
 12 		result = x + y;
 13 		return result;
 14 	}finally{
 15 		result = x - y;
 16 	}
 17 }

8

29、以下代码的运行结果?

  1 public static void main(String[] args) {
  2 	Integer[] datas = {1,2,3,4,5};
  3 	List<Integer> list = Arrays.asList(datas);
  4 	list.add(5);
  5 	System.out.println(list.size());
  6 }

运行异常,不允许添加元素

30、在{1}添加什么代码,可以保证如下代码输出100?

提示:t.wait()或t.jion()或t.yield()或t.interrupt()

  1 public class Test {
  2 	public static void main(String[] args) {
  3 		MyThread m = new MyThread();
  4 		Thread t = new Thread(m);
  5 		t.start();
  6 
  7 		        {1}
  8 
  9 		int j = m.i;
 10 		System.out.println(j);
 11 	}
 12 }
 13 class MyThread implements Runnable{
 14 	int i;
 15 	public void run(){
 16 		try {
 17 			Thread.sleep(1000);
 18 		} catch (InterruptedException e) {
 19 			e.printStackTrace();
 20 		}
 21 		i=100;
 22 	}
 23 }

t.join()

31、以下代码如何优化?

  1 if(username.equals(“admin”){
  2   ....
  3 }

if(“admin”.equals(username)){

}

猜你喜欢

转载自www.cnblogs.com/daidai66/p/12018435.html