1.假设有如下程序:
public class Demo {
public static void main(String args[]) {
int num = 2147483647 ;
num += 2 ;
System.out.println(num) ;
}
}
最终的执行结果是什么?
C. -2147483647
解释:
由于计算机中采用二进制存储数据,2147483647是int存储的最大正数,其+2,
相当于二进制右侧第二位+1,等于0,向左进1,导致后续的二进制统一+1=0进1,
最终最高位0+1=1变为负数,因为右侧第一位未动为1,则最终结果是最高位和最低位为1,其余都为0。
2.假设有如下程序:
public class Demo {
public static void main(String args[]) {
long num = 100 ;
int x = num + 2 ;
System.out.println(x) ;
}
}
最终程序的执行结果是什么?
D. 程序错误
解释:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from long to int
不能把Long强转为Int
3.编译Java源程序文件产生的字节码文件的扩展名为?
A. java
B. class
C. html
D. exe
解释:
4.java源文件的后缀名是.java。源文件通过jvm虚拟机编译后会生成二进制字节码文件,后缀是.class。
单选 4.假设有如下程序:
public class Demo {
public static void main(String args[]) {
int num = 2147483647 ;
long temp = num + 2L ;
System.out.println(num) ;
}
}
最终的执行结果是什么?
C. 2147483647
解释:num的值不变所以C
5.假设有如下程序:
public class Demo {
public static void main(String args[]) {
int x = 10 ;
double y = 20.2 ;
long z = 10L;
String str = "" + x + y * z ;
System.out.println(str) ;
}
}
最终执行结果是什么?
A. 10202.0
解释:
*的优先级比+高
z = 10 y = 20.2 x = 10,首先是y*z = 202,然后“” + 10.0 = “10.0”,“10.0” + 202 = “10202.0”
9.假设有如下程序:
public class Demo {
public static void main(String args[]) {
char c = 'A' ;
int num = 10 ;
switch(c) {
case 'B' :
num ++ ;
case 'A' :
num ++ ;
case 'Y' :
num ++ ;
break ;
default :
num -- ;
}
System.out.println(num) ;
}
}
最终执行结果是什么?
C. 12
解释:
如果一旦case相应的值成功,但内部没有break语句,那么将会无条件(不再进行case匹配)的继续向下执行其它case中的语句,直到遇到break;语句或者到达switch语句结束
10.假设有如下程序:
public class Demo {
public static void main(String args[]) {
int num = 50 ;
num = num ++ * 2 ;
System.out.println(num) ;
}
}
最终的执行结果是什么?
C. 100
解释:
这个就相当于num=50x2=100,然后,num在自权增+1=51
14.假设有如下程序:
public class Demo {
public static void main(String args[]) {
int sum = 0 ;
for (int x = 0 ; x < 10 ; x ++) {
sum += x ;
if (x % 3 == 0) {
break ;
}
}
System.out.println(sum) ;
}
}
最终执行结果是什么?
B. 0
解释:
第一次x=0时就成立x%3,所以此时sum仍然为0
15.现在有一个方法:public static int info(int x,double y),下面那个方法是对本方法的正确重载?
A. public static int infos(int x,int y);
B. public static void info(int x,double y);
C. public static int info(int x,int y);
D. public static void infos(int x,int y);
解释:
被重载的方法必须具有不同的参数列表。不能基于不同修饰符或返回值类型来重载方法.
16.假设有如下程序:
public class Demo {
public static void main(String args[]) {
String str = "" ;
for (int x = 0 ; x < 5 ; x ++) {
str += x ;
}
System.out.println(str) ;
}
}
最终的执行结果是什么?
A. 01234
解释:
数字自动转化为字符串
class Happy {
public static void main(String args[]) {
int i = 1 ;
int j = i++ ;
if((i==(++j))&&((i++)==j)) {
i += j ;
}
System.out.println("i = "+i);
}
}
运行完上面代码之后输出i的值是多少?
B. 5
解释:
i++是在本段语句执行结束时候++,++j是在本段语句执行前进行++
18.下面哪些语句在编译时不会出现错误或警告 ( )
A.float f=1.3;
B.byte b=257:
C.boolean b=null;
D.int i=10;
解释:
在Java语言中,对于实型数值,默认的类犁是 double型的,若要把它赋给float变量,必须对它进行类型转换,赋初值的话,必须在实型数后面加上f或F字母。给浮点型变量定义并且赋初值:选项B是错误的,因为byte是8个字符的,它所表示的范围是-128~127,而右边的 257已经超出其所能表示的范围了;选项C是错误的,对于布尔型变量,它的值只有true与 false两种,系统默认的是false。没有null这种取值;选项D是正确的,对整型变量定义并且赋初值。故本题答案是D
19.假设有如下程序:
public class Demo {
public static void main(String args[]) {
int num = 2147483647 ;
num += 2L ;
System.out.println(num) ;
}
}
最终的执行结果是什么?
C. -2147483647
解释:
仍然为整型,仍然溢出。。。
L代表long类型。。。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)