【17-06-16】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 = 257D.
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 = 1 if ((i == (++j)) && ((i++) == j)) {
// 第一个判断:j先自增1变为2后与i比较
// 第二个判断:i先与j比较后再自增1,
// if内为true,i = 3, j = 2
i += j; // i = 5, j = 2
} System.out.println("i = " + i);
}
}
如果选项A最后没有那个;,那么这道题就没有争议了
A.
float f = 1.3;
1.3默认是double类型,java中基本数据类型由高级向低级转换需要强转。float f = 1.3f;double f = 1.3;float f =(float) 1.3;double f = 1.3f;
B.
char c = "a"
java中的字符常量应该用单引号括起来,双引号括起来的为字符串。(末尾少了个分号)char c = 'a';String c = "a";
C.
byte b = 257
byte的范围是 -128~127。(末尾少了个分号)int b = 257;byte b = 57;
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为false
System.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 = 11
case 'Y' :
num ++ ; // num = 12
break ;
// 因break跳出switch
default :
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。
【17-06-16】Java入门测试题,测测你基础知识掌握程度(附答案及个人解析)的更多相关文章
- Java 入门课程视频实战-0基础 上线了,猜拳游戏,ATM实战,欢迎围观
Java 入门课程视频实战-0基础 已经上传完了.欢迎小伙伴们过来围观 直接进入: http://edu.csdn.net/course/detail/196 课程文件夹例如以下: 1 初识Java ...
- Java做acm所需要的基础知识之排序问题
Java做acm所需要的基础知识. 以前做acm的题都是用C/C++来写代码的,在学习完Java之后突然感觉Java中的方法比C/C++丰富很多,所以就整理一下平时做题需要用到的Java基础知识. 1 ...
- 0.Python 爬虫之Scrapy入门实践指南(Scrapy基础知识)
目录 0.0.Scrapy基础 0.1.Scrapy 框架图 0.2.Scrapy主要包括了以下组件: 0.3.Scrapy简单示例如下: 0.4.Scrapy运行流程如下: 0.5.还有什么? 0. ...
- JAVA 入门第一章(语法基础)
本人初学java 博客分享记录一下自己的学习历程 java我的初步学习分为六章,有c和c++的基础学起来也简便了很多. 第一章 语法基础 第二章 面向对象 第三章 常用工具类 第四章 文件操纵 第五章 ...
- (转)JAVA AJAX教程第二章-JAVASCRIPT基础知识
开篇:JAVASCRIPT是AJAX技术中不可或缺的一部分,所以想学好AJAX以及现在流行的AJAX框架,学好JAVASCRIPT是最重要的.这章我给大家整理了一些JAVASCRIPT的基础知识.常用 ...
- 学 Java 网络爬虫,需要哪些基础知识?
说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少.有不少人都不知道 Java 可以做网络爬虫,其实 Java 也能做网络爬 ...
- Python入门方法推荐,哪些基础知识必学?
很多想入门的小伙伴还不知道Python应该怎么学,哪些知识必学,今天我们就来盘点一下. 01.入门方法推荐 总体来讲,找一本靠谱的书,由浅入深,边看边练. 网上的学习教程有很多,多到不知道如何选择.所 ...
- java第九节 网络编程的基础知识
/** * * 网络编程的基础知识 * 网络协议与TCP/IP * IP地址和Port(端口号) * 本地回路的IP地址:127.0.0.1 * 端口号的范围为0-65535之间,0-1023之间的端 ...
- 想入门Web安全,这些基础知识都学会了吗?
毕业季已经正式告一段落,这届毕业生都找到心仪的工作了吗? 正在实习期或者试用期的职场新人,是否在岗位上做的风生水起? 工作了一两年,从未升职加薪的菜鸟,还愿意继续原地踏步吗? 在校学生.IT从业者.毕 ...
随机推荐
- Keras学习环境配置-GPU加速版(Ubuntu 16.04 + CUDA8.0 + cuDNN6.0 + Tensorflow)
本文是个人对Keras深度学习框架配置的总结,不周之处请指出,谢谢! 1. 首先,我们需要安装Ubuntu操作系统(Windows下也行),这里使用Ubuntu16.04版本: 2. 安装好Ubunt ...
- svn 提交 working copy is not up-to-date
svn在提交时报错信息如下: working copy is not up-to-date svn:commit failed(details follow): svn:file "xxxx ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Linux下MySQL5.7.19
第一次在自己虚机上安装mysql 中间碰到很多问题 在这里记下来,分享一下. linux centOS 6 mysql版本 mysql-5.7.19-linux-glibc2.12-x86_64.ta ...
- 微信小程序之页面跳转路径问题
错误如下: 业务需求:在movie页面点击进入detail页面. 在遍历跳转路径的时候,写绝对路径了 只需改一下就好了 教程参考地址:http://blog.csdn.net/reylen/artic ...
- javascript高性能写法
看到一篇不错的博文,如果想写出比较高性能的代码,可参看这个链接http://developer.51cto.com/art/200906/131335.htm
- C++STL之双端队列容器
C++STL之双端队列容器 deque双端队列容器与vector很类似,采用线性表顺序存储结构.但与vector区别,deque采用分块的线性存储结构来存储数据,每块的大小一般为512B,将之称为de ...
- Treats for the Cows
Treats for the Cows Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- HDU 4325 Flowers(树状数组)
Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Scala从入门到精通之四-映射和元组
在Scala中映射之键值对的集合,元组是n个对象的聚集,但是对象的类型不一定相同 本节内容要点 Scala中映射的创建,遍历和查询 如何从可变和不可变映射中做出选择 Scala映射和Java映射见的互 ...