描述
前几天在知乎里看到一份这样的题,当时只是随便做了一下,对了一下答案。昨天又有了一份进阶的题,里面有些还是需要记录一下,于是就从这个入门的题开始。
题目和答案来自阿里云大学 - 知乎专栏
题目
-
现在假设有如下程序
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入门题的详细内容。更多信息请关注PHP中文网其他相关文章!

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

Java的强类型系统通过类型安全、统一的类型转换和多态性确保了平台独立性。1)类型安全在编译时进行类型检查,避免运行时错误;2)统一的类型转换规则在所有平台上一致;3)多态性和接口机制使代码在不同平台上行为一致。

JNI会破坏Java的平台独立性。1)JNI需要特定平台的本地库,2)本地代码需在目标平台编译和链接,3)不同版本的操作系统或JVM可能需要不同的本地库版本,4)本地代码可能引入安全漏洞或导致程序崩溃。

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器