您好,欢迎访问一九零五行业门户网

分享一篇Java入门题

描述前几天在知乎里看到一份这样的题,当时只是随便做了一下,对了一下答案。昨天又有了一份进阶的题,里面有些还是需要记录一下,于是就从这个入门的题开始。
题目和答案来自阿里云大学 - 知乎专栏
题目现在假设有如下程序
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的值是多少?
a. 4
b. 5
c. 3
d. 6
下面的数据声明及赋值哪一个是没有错误的?
a. float f = 1.3;
b. char c = "a"
c. byte b = 257
d. int i = 10
编译java源程序文件产生的字节码文件的扩展名为?
a. java
b. class
c. html
d. exe
现在假设有如下程序:
public class demo {public static void main(string args[]) {boolean flag = 10%2 == 1 && 10 / 3 == 0 && 1 / 0 == 0 ; system.out.println(flag ? "aliyunedu" : "yootk") ; } }
以上程序的最终执行结果是什么?
a. aliyunedu
b. yootk
c. true
d. 程序出错
现在假设有如下程序:
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
b. 0212.0
c. 302.0
d. 1020.210
现在假设有如下程序:
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
b. 10
c. 14
d. 25
现在假设有如下程序:
public class demo {public static void main(string args[]) { system.out.println(inc(10) + inc(8) + inc(-10)) ; }public static int inc(int temp) {if (temp > 0) {return temp * 2 ; }return -1 ; } }
以上程序的最终执行结果是什么?
a. 35
b. 8
c. 28
d. 12
现在假设有如下程序:
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) ; } }
以上程序的最终执行结果是什么?
a. 11
b. 13
c. 12
d. 10
现在假设有如下程序:
public class demo {public static void main(string args[]) {int sum = 0 ;for (int x = 1 ; x < 10 ; x ++) { sum += x ;if (x % 3 == 0) {continue ; } } system.out.println(sum) ; } }

以上程序的最终执行结果是什么?
a. 6
b. 0
c. 程序错误,死循环
d. 45
现在假设有如下程序:
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) ; } }

以上程序的最终执行结果是什么?
a. 6
b. 0
c. 程序错误,死循环
d. 45
答案bdbba aacdb
个人解析主要考验i++和++i的区别,只要记住“先++,先自增;后++,后自增”,这道题就只剩下考验细心了。
class happy {public static void main(string[] args) {int i = 1;int j = i++; // i = 2, j = 1if ((i == (++j)) && ((i++) == j)) { // 第一个判断:j先自增1变为2后与i比较// 第二个判断:i先与j比较后再自增1,// if内为true,i = 3, j = 2i += j; // i = 5, j = 2} system.out.println("i = " + i); } }
如果选项a最后没有那个;,那么这道题就没有争议了
int b = 257;
byte b = 57;
char c = 'a';
string c = "a";
float f = 1.3f;
double f = 1.3;
float f =(float) 1.3;
double f = 1.3f;
a. float f = 1.3;
1.3默认是double类型,java中基本数据类型由高级向低级转换需要强转。
b. char c = "a"
java中的字符常量应该用单引号括起来,双引号括起来的为字符串。(末尾少了个分号)
c. byte b = 257
byte的范围是 -128~127。(末尾少了个分号)
d. int i = 10
(末尾少了个分号)

public class demo {public static void main(string args[]) {boolean flag = 10 % 2 == 1 && 10 / 3 == 0 && 1 / 0 == 0 ;// 10对2取余为0,故flag为falsesystem.out.println(flag ? "aliyunedu" : "yootk") ; } }
&&(短路与)一旦前面的条件为false,就会跳过后面的条件。
x = 条件 ? a : b为三元表达式,与
if (条件) { x = a; } else { x = b; }
意思相同
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) ; } }

*的优先度高于+,故优先计算乘法,随后从左往右依次进行+。当有字符串参与+运算时,加法变为字符串拼接,结果为字符串。故最后为字符串"10"和202.0的拼接。
见上
public class demo {public static void main(string args[]) { system.out.println(inc(10) + inc(8) + inc(-10)) ; // 20 + 16 - 1}public static int inc(int temp) {if (temp > 0) {return temp * 2 ; }return -1 ; } }
如果为正数,返回参数的2倍值;如果不是正数,返回-1。结果为20 + 16 + (-1)
public class demo {public static void main(string args[]) {char c = 'a' ;int num = 10 ;switch(c) {case 'b' : num ++ ;case 'a' :// 匹配成功,开始执行num ++ ; // num = 11case 'y' : num ++ ; // num = 12break ;// 因break跳出switchdefault : num -- ; } system.out.println(num) ; } }
switch-case语句块中break的重要性
public class demo {public static void main(string args[]) {int sum = 0 ;for (int x = 1 ; x < 10 ; x ++) { sum += x ;if (x % 3 == 0) {continue ; } } system.out.println(sum) ; } }

感觉这道题sum += x的位置可能写错了,应该在if的后面,要么就只是单纯的和下一道题作对比。现在这段代码里if的用处几乎没有,结果和没有if时是一样的,都是1+2+…+9=45。
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) ; } }

和上一题类似,不过i的初始值变成了0,if里面的continue变成了break。由于0对3取余为0,所以直接跳出循环,输出sum的值0。
以上就是分享一篇java入门题的详细内容。
其它类似信息

推荐信息