我是靠谱客的博主 虚拟嚓茶,这篇文章主要介绍java面试题,现在分享给大家,希望可以做个参考。

1、字符定义:

复制代码
1
2
char c=65; System.out.println(c);

输出结果:

A

分析:java中定义字符方式:

  • 直接定义字符:char c1=‘A’;
  • 用字符对应的ASCII码:char c2=65;
  • byte定义范围是-128~127

2、程序输出结果

复制代码
1
2
int a = 5; System.out.println("vlaue is " + ((a < 5) ? 10.9 : 9));

输出结果:

vlaue is 9.0

条件表达式执行说明:

  • 先求解表达式1,若值为非0,表示条件为真,则求解表达式2,此时表达式2的值就作为整个条件表达式的值;若表达式1的值为0,表示条件为假,则求解表达式3,表达式3的值就是整个条件表达式的值。
  •  条件表达式的优先级别仅高于赋值运算符,而低于前面遇到过的所有运算符。
  • 条件运算符的结合方向为"自右至左"。
  • 只要一个运算中有不同的类型,涉及到类型转换,那么编译器会往下(基本类型)转型,再进行运算。
  • 三目运算中后两个表达式,有一个是常量表达式,另一个是T类型,而常量表达式可以被T类型表示,则输出结果为T类型。

分析:因为前面有一个10.9,所以9会自动提升为9.0,所以最终输出结果是9.0。

3、程序输出结果

复制代码
1
2
3
4
char x = 'x'; int i = 10; System.out.println(false ? i : x); System.out.println(false ? 10 : x);

输出结果:

120
x

分析:int i=10;中的i是个变量,所以第一个输出的x被提升为int型,‘x’对应的值是120,所以输出120;当三目运算中后两个表达式,有一个是常量表达式(本题中是10),另一个是T类型(本题中是char),而常量表达式可以被T类型表示,则输出结果为T类型。因为10为常量,且可以表示为char类型,所以输出结果为char。

4、下列输出结果:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class test1 { private int count; public static void main(String[] args) { // TODO Auto-generated method stub test1 t1 = new test1(99); System.out.println(t1.count); } test1(int ballcount) { count = ballcount; } }

输出结果:

99

分析:count定义为私有属性,并不会阻止构造方法对其进行初始化,当然也可在该类进行调用私有属性,但是如果不在同一个类,则不行。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class test1 { public static void main(String[] args) { test2 t2 = new test2(88); System.out.println(t2.count); } } class test2 { private int count2; test2(int ballcount) { count2 = ballcount; } }

上述代码会编译出错:The field test2.count2 is not visible

5、关于下面代码片段叙述正确的是: 

复制代码
1
2
3
4
5
byte b1 = 1, b2 = 2, b3, b6; final byte b4 = 4, b5 = 6; b6 = b4 + b5; b3 = (b1 + b2); System.out.println(b3 + b6);

A 输出结果:13

B 语句:b6=b4+b5编译出错

C 语句:b3=b1+b2编译出错

D 运行期抛出异常

解析:被final修饰的变量是常量,这里的b6=b4+b5可以看成是b6=10;在编译时就已经变为b6=10了而b1和b2是byte类型,java中进行计算时候将他们提升为int类型,再进行计算,b1+b2计算后已经是int类型,赋值给b3,b3是byte类型,类型不匹配,编译不会通过,需要进行强制转换。Java中的byte,short,char进行计算时都会提升为int类型。

6、下列程序输出结果:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ZeroTest { public static void main(String[] args) { try { int i = 100 / 0; System.out.print(i); } catch (Exception e) { System.out.print(1); throw new RuntimeException(); } finally { System.out.print(2); } System.out.print(3); } }

输出结果:12

分析:

1)inti = 100/ 0; 会出现异常,会抛出异常,System.out.print(i)不会执行,

2)catch捕捉异常,继续执行System.out.print(1);

3)当执行 thrownewRuntimeException();  又会抛出异常,这时,除了会执行finally中的代码,其他地方的代码都不会执行

7、 下列程序输出结果:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static int aMethod(int i) throws Exception { try { return i / 0; } catch (Exception ex) { throw new Exception("exception in a Method"); } finally { System.out.println("finally"); } } public static void main(String[] args) { try { aMethod(0); } catch (Exception ex) { System.out.println("exception in main"); } System.out.println("finished"); }

输出结果:

finally
exception in main
finished

 

转载于:https://www.cnblogs.com/SaraMoring/p/5748367.html

最后

以上就是虚拟嚓茶最近收集整理的关于java面试题的全部内容,更多相关java面试题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部