目录
4. ConcurrentHashMap使用lock关键字保证线程安全;Arrays.asList返回值是List接口对象
5. 字符串对象比较相等,使用equals比较内容;使用== 比较的是地址
6. 编译命令: javac 源文件名称.java; 运行命令: java 主类名称 要传递的参数
7. int类型对象成员变量赋予默认值是在,对象产生时执行,在类加载之后,不属于类加载过程
12. 当所有前台线程(用户线程)执行完毕,java进程就认为程序全部执行完毕
13.Serial.parNew.CMS这三个收集器在JDK1的时候就有;G1收集器是在JDK7才出现的
14. instanceof运算符用于:判断该运算符前面引用类型变量指向的对象是否是后面类,或者其子类、接口实现类创建的对象。
题目链接:快到碗里来__牛客网 (nowcoder.com)
题目要求:

题目分析:
计算机周长:c = 2*3.14*r
这道题需要注意的是 输入的范围[1,2^128]
如果使用int和long的话,都不满足这个范围,
如果使用double的话,计算会受精度影响(不过这个题对精度问题要求不高也可以使用)
最好的还是使用BigDecimal
初始化BigDecimal对象,可以传两种参数
BigDecimal(String val) new BigDecimal("3.14");//注意带上双引号
BigDecimal(double val) 同样不能使用double作为构造参数
解决了用什么来接收输入的问题,下面就直接就是根据题意比较输出了
上代码
这个是用double来接收的,对于这道题double还是可以的
- import java.util.*;
- public class Main{
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- while(scan.hasNext()) {
- double n = scan.nextDouble();
- double r = scan.nextDouble();
- if(n > 2*3.14*r) {//放不进去
- System.out.println("No");
- }else {
- System.out.println("Yes");
- }
- }
- }
- }
使用BigDecimal
- import java.util.*;
- import java.math.*;
- public class Main{
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- while(scan.hasNext()) {
- BigDecimal n = scan.nextBigDecimal();
- BigDecimal r = scan.nextBigDecimal();
- BigDecimal len = new BigDecimal("6.28").multiply(r);
- System.out.println(n.compareTo(len) == 1 ? "No" : "Yes");
- }
- }
- }
题目链接:跳台阶_牛客题霸_牛客网 (nowcoder.com)
题目要求:

题目分析:

上代码
- import java.lang.*;
- public class Solution {
- public int jumpFloor(int target) {
- if(target <= 1) {
- return 1;
- }
- int a = 1,b = 1;
- for(int i = 2; i <= target; ++i) {
- int temp = a + b;
- a = b;
- b = temp;
- }
- return b;
- }
- }
题目要求:

要求时间复杂度和空间复杂度均为O(1)
题目分析:

上代码
- public int jumpFloorII (int number) {
- return (int)Math.pow(2,number-1);
- }
- public int jumpFloorII (int number) {
- return 1 << --number;
- }
在java7中,下列哪个说法是正确的(D)
A. ConcurrentHashMap使用Synchronized关键字保证线程安全
B. HashMap实现了Collection接口
C. Arrays.asList方法返回java.util.ArrayList对象
D. SimpleDateFormat对象是线程不安全的
ConcurrentHashMap使用lock关键字保证线程安全 A错
Collection接口是线性表的顶级接口,HashMap实现的是Map接口 B错
Arrays.asList返回值是List接口对象 C错
public static
List asList(T..a) {return newArrayList<(a)
}
关于以下程序段,正确的说法是:(C)
String s1 = "abc" + "def";//1
String s2 = new String(s1);//2
if(s1.equals(s2))//3
System.out.println(".equals succeeded");//4
if(s1 == s2)
System.out.println("==succeeded");//6
A. 行4,行6都不执行
B. 行6执行,行4不执行
C. 行4执行,行6不执行
D. 行4,行6都执行
字符串对象比较相等,使用equals比较内容
使用== 比较的是地址 选C
用命令方式运行以下代码的运行结果是(C)

命令: java f a b c
A. 程序编译错误 B. a b c C. 程序运行错误 D. f
编译命令: javac 源文件名称.java
*.java -> *.class
运行命令: java 主类名称 要传递的参数(传递给main的args中)
java f a b c ------> args{a,b,c}注意传入三个参数,下标是0-2
而代码中访问args[3] 很明显数组访问越界,所以是程序运行错误,选C
以下哪项不属于java类加载过程 (B)
A. 生成java.lang.Class对象
B. int类型对象成员变量赋予默认值
C. 执行static块代码
D. 类方法解析
B选项是,对象产生时执行,在类加载之后,不属于类加载过程
题目链接:不用加减乘除做加法_牛客题霸_牛客网 (nowcoder.com)
题目要求:

题目分析:

上代码
- public class Solution {
- public int Add(int num1,int num2) {
- while(num2 != 0) {
- int sum = num1^num2;
- int carray = (num1&num2) << 1;
- num1 = sum;
- num2 = carray;
- }
- return num1;
- }
- }
题目要求:

题目分析:
判断三条边是否可以组成一个三角形
要么使用"任意两边之和,大于第三边" ; 要么使用"任意两边之差,小于第三边"来进行判断
但这道题还有个问题时 三个边的长度范围是[1,10^100]
所以用int 和long来接收这三条边肯定是不可以的
使用double的话,虽然范围满足了,但又会存在精度丢失的问题(这道题不影响)
所以要学会使用BigDecimal
初始化BigDecimal对象,它的构造方法传参有两种
BigDecimal(String val) BigDecimal(double val)
如果需要精度高的BigDecimal,还是不能使用double来作为构造参数
要传入一个字符串,才可以
对于BigDecimal做加法: add(BigDecimal)
减法: subtract(BigDecimal)
乘法: multiply(BigDecimal)
除法: divide(BigDecimal)
上代码(使用double来接收三条边)
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- while(scan.hasNext()) {
- double a = scan.nextDouble();
- double b = scan.nextDouble();
- double c = scan.nextDouble();
- if((a+b) > c && (a+c) > b && (b+c) > a) {
- System.out.println("Yes");
- }else {
- System.out.println("No");
- }
- }
- }
- }
使用BigDecimal来接收三条边
- import java.util.*;
- import java.math.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- while(scan.hasNext()) {
- BigDecimal a = scan.nextBigDecimal();
- BigDecimal b = scan.nextBigDecimal();
- BigDecimal c = scan.nextBigDecimal();
- if(a.add(b).compareTo(c)>0 && a.add(c).compareTo(b)>0 && b.add(c).compareTo(a)>0) {
- System.out.println("Yes");
- }else {
- System.out.println("No");
- }
- }
- }
- }
题目要求:


题目分析:

上代码
- import java.util.*;
- public class Main{
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- while(scan.hasNext()) {
- long n = scan.nextLong();
- if(n == 0) {
- break;
- }
- long a = (long)Math.pow(5,n);
- long b = (long)Math.pow(4,n);
- System.out.println((a-4) + " " + (n + b - 4));
- }
- }
- }
题目链接:反转部分单向链表__牛客网 (nowcoder.com)
题目要求:

题目分析:

上代码
-
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
-
-
-
- public class Main{
- static class Node {
- int val;
- Node next;
- public Node(int val) {
- this.val = val;
- }
- }
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(br.readLine());
- String[] nodes = br.readLine().trim().split(" ");
- Node dummyHead = new Node(-1);
- Node tail = dummyHead;
- for(int i = 0; i < n; i++ ) {
- //在链表中进行尾插
- Node node = new Node(Integer.parseInt(nodes[i]));
- tail.next = node;
- tail = node;
- }
- String[] part = br.readLine().split(" ");
- int left = Integer.parseInt(part[0]);
- int right = Integer.parseInt(part[1]);
-
- // Scanner scan = new Scanner(System.in);
- // int n = scan.nextInt();
- // //去除掉nextInt后面的 \n
- // scan.nextLine();
- // //"1 2 3 4"
- // String nodeValue = scan.nextLine();
- // String[] nodes = nodeValue.split(" ");
- // //创建虚拟头结点
- // Node dummyHead = new Node(-1);
- // Node tail = dummyHead;
- // for(int i = 0; i < n; i++ ) {
- // //在链表中进行尾插
- // Node node = new Node(Integer.parseInt(nodes[i]));
- // tail.next = node;
- // tail = node;
- // }
- // //输入左右反转区间
- // String part = scan.nextLine();
- // int left = Integer.parseInt(part.split(" ")[0]);
- // int right = Integer.parseInt(part.split(" ")[1]);
-
- Node newHead = reversepartList(dummyHead.next,left,right);
- //进行打印
- while(newHead != null) {
- System.out.print(newHead.val + " ");
- newHead = newHead.next;
- }
- }
-
- private static Node reversepartList(Node head, int left, int right) {
- Node dummyHead = new Node(-1);
- dummyHead.next = head;
- Node prev = dummyHead;
- //走到left的前一个位置
- for(int i = 0; i < left-1; i++) {
- prev = prev.next;
- }
- //保存prev的下一个位置,进行头插
- Node cur = prev.next;
- for(int i = left; i < right; i++) {
- //保存cur的下一个节点
- Node node = cur.next;
- //断开cur和node的连接
- cur.next = node.next;
- //再把node结点插入到prev之后
- node.next = prev.next;
- prev.next = node;
- }
- return dummyHead.next;
- }
- }
-
jre判断程序是否执行结束的标准是 ( A )
A. 所有的前台线程执行完毕
B. 所有的后台线程执行完毕
C. 所有的线程执行完毕
D. 和以上都无关
JRE: java运行时环境
JDK: java开发工具包,包含了JRE
当所有前台线程(用户线程)执行完毕,java进程就认为程序全部执行完毕
setDameon(true) 一般我们创建的线程都是前台线程
后台线程: JVM垃圾回收机制 所以答案选A
下列哪项不属于jdk1.6垃圾收集器 (D)
A. Serial收集器 B. parNew收集器 C. CMS收集器 D. G1收集器
Serial.parNew.CMS这三个收集器在JDK1的时候就有了
而G1收集器是在JDK7才出现的,所以选D
instanceof运算符能够用来判断一个对象是否为: (C)
A. 一个类的实例 B. 一个实现指定接口的类的实例
C. 全部正确 D. 一个子类的实例
instanceof 运算符用于:判断该运算符前面引用类型变量指向的对象是否是后面类,或者其子类、接口实现类创建的对象。如果是则返回true,否则返回false,
使用方法
引用类型变量 instanceof (类、抽象类或接口) 所以答案选C