之前发的章节开始慢慢更新运行截图以及知识点。
QUESTION 31
Given the code fragment:
public static void main (String [] args) throws IOException {Stream<Path> files = Files.walk(Paths.get(System.getProperty("user.home")));files.forEach (fName -> { //line n1try {Path aPath = fName.toAbsolutePath(); //line n2System.out.println(fName + ":"+ Files.readAttributes(aPath,BasicFileAttributes.class).creationTime());} catch (IOException ex) {ex.printStackTrace();}});}

What is the result?
A. All files and directories under the home directory are listed along with their attributes.
B. A compilation error occurs at line n1 .
C. The files in the home directory are listed along with their attributes.
D. A compilation error occurs at line n2 .
Correct Answer: A
Section: (none)
Explanation
Explanation/Reference:
Part two涉及到的 Path和Files用法: https://www.cnblogs.com/devilwind/p/8623098.html
QUESTION 32
Given:
class Vehicle {
int vno;
String name;
public Vehicle (int vno, String name) {
this.vno = vno;
this.name = name;
}
public String toString () {
return vno + ":" + name;
}
}

and this code fragment:
Set<Vehicle> vehicles = new TreeSet <> ();vehicles.add(new Vehicle (10123, "Ford"));vehicles.add(new Vehicle (10124, "BMW"));System.out.println(vehicles);

What is the result?

A. 10123 Ford
10124 BMW
B. 10124 BMW
10123 Ford
C. A compilation error occurs.
D. A ClassCastException is thrown at run time.
Correct Answer: D
Section: (none)
Explanation
Explanation/Reference:
Set 集合: https://blog.csdn.net/Strangerpedestrain/article/details/77108802
QUESTION 33
Given that course.txt is accessible and contains:
Course : : Java
and given the code fragment:
public static void main (String[ ] args) {
int i;
char c;
try (FileInputStream fis = new FileInputStream (“course.txt”);
InputStreamReader isr = new InputStreamReader(fis);) {
while (isr.ready()) { //line n1
isr.skip(2);
i = isr.read ();
c = (char) i;
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}

What is the result?
A. ur :: va
B. ueJa
C. The program prints nothing.
D. A compilation error occurs at line n1 .
Correct Answer: B
Section: (none)
Explanation
每次读取字符串,跳两位,故最终打印ueJa
Explanation/Reference:
Part three 涉及到的 InputStreamReader类: https://blog.csdn.net/ai_bao_zi/article/details/81133476

Part two 涉及到的FileInputStream类的使用:https://blog.csdn.net/chentiefeng521/article/details/51776218

QUESTION 34
Given:
public class Test<T> {
private T t;
public T get () {
return t;
}
public void set (T t) { this.t = t;
}
public static void main (String args [ ] ) {
Test<String> type = new Test<>();
Test type1 = new Test (); //line n1
type.set("Java");
type1.set(100); //line n2
System.out.print(type.get() + " " + type1.get());
}
}

What is the result?
A. Java 100
B. java.lang.string@< hashcode >java.lang.Integer@<hashcode>
C. A compilation error occurs. To rectify it, replace line n1 with:
Test<Integer> type1 = new Test<>();
D. A compilation error occurs. To rectify it, replace line n2 with:
type1.set (Integer(100));
Correct Answer: A
Section: (none)
Explanation
Explanation/Reference:
Java 泛型方法: https://blog.csdn.net/weixin_43819113/article/details/91042598
QUESTION 35
Given the definition of the Vehicle class:
class Vehicle {
String name;
void setName (String name) {
this.name = name;
}
String getName() {
return name;
}
}

Which action encapsulates the Vehicle class?
A. Make the Vehicle class public .
B. Make the name variable public .
C. Make the setName method public .
D. Make the name variable private .
E. Make the setName method private .
F. Make the getName method private .
Correct Answer: D
Section: (none)
Explanation
Explanation/Reference:
QUESTION 36
Given:
public class Product {
int id; int price;
public  Product(int id,int price) {
this.id = id;
this.price = price;
}
public String toString() { return id + ":" + price; }
}

and the code fragment:
List<Product> products = Arrays.asList(new Product(1, 10),
new Product (2, 30),
new Product (2, 30));
Product p = products.stream().reduce(new Product (4, 0), (p1, p2) -> {
p1.price+=p2.price;
return new Product (p1.id, p1.price);});
products.add(p);
products.stream().parallel()
.reduce((p1, p2) - > p1.price > p2.price ? p1 : p2)
.ifPresent(System.out: :println);

What is the result?
A. 2 : 30
B. 4 : 0
C. 4 : 60
D. 4 : 60
2 : 30
3 : 20
1 : 10
E. The program prints nothing.
Correct Answer: C
Section: (none)
Explanation
(找bug ing待完善...)
Explanation/Reference:
QUESTION 37
Given the code fragments:
public class Book implements Comparator<Book> {
String name;
double price;
public Book () {}
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public int compare(Book b1, Book b2) {
return b1.name.compareTo(b2.name);
}
public String toString() {
return name + “:” + price;
}
}

and
List<Book>books = Arrays.asList (new Book (“Beginning with Java”, 2), new book
(“A
Guide to Java Tour”, 3));
Collections.sort(books, new Book());
System.out.print(books);

What is the result?
A. [A Guide to Java Tour:3.0, Beginning with Java:2.0]
B. [Beginning with Java:2, A Guide to Java Tour:3]
C. A compilation error occurs because the Book class does not override the abstract method compareTo
().
D. An Exception is thrown at run time.
Correct Answer: A
Section: (none)
Explanation

Explanation/Reference:
Part three涉及到的Comparator 排序: https://blog.csdn.net/leexurui/article/details/48848083
QUESTION 38
Given the code fragment:
List<String> listVal = Arrays.asList(“Joe”, “Paul”, “Alice”, “Tom”);
System.out.println (
// line n1
);

Which code fragment, when inserted at line n1, enables the code to print the count of string elements
whose length is greater than three?
A. listVal.stream().filter(x -> x.length()>3).count()    //2
B. listVal.stream().map(x -> x.length()>3).count()    //4
C. listVal.stream().peek(x -> x.length()>3).count().get() //error
D. listVal.stream().filter(x -> x.length()>3).mapToInt(x -> x).count()//error
Correct Answer: B
Section: (none)
Explanation:
A
B
CThe method peek(Consumer<? super String>) in the type Stream<String> is not applicable for the arguments ((<no type> x) -> {})
Void methods cannot return a value
DType mismatch: cannot convert from String to int
Explanation/Reference:
Part two 涉及到的stream语法: https://blog.csdn.net/SeanTandol/article/details/86630437
QUESTION 39
Given the code fragments:
class Caller implements Callable<String> {
String str;
public Caller (String s) {this.str=s;}
public String call()throws Exception { return str.concat (“Caller”);}
}
class Runner implements Runnable {
String str;
public Runner (String s) {this.str=s;}
public void run () { System.out.println (str.concat (“Runner”));}
}

and
public static void main (String[] args) throws InterruptedException,ExecutionException {ExecutorService es = Executors.newFixedThreadPool(2);Future f1 = es.submit (new Caller ("Call"));Future f2 = es.submit (new Runner ("Run"));String str1 = (String) f1.get();String str2 = (String) f2.get(); //line n1System.out.println(str1+ ":" + str2);}

What is the result?
A. The program prints:
Run Runner
Call Caller : null
And the program does not terminate.
B. The program terminates after printing:
Run Runner
Call Caller : Run
C. A compilation error occurs at line n1 .
D. An Execution is thrown at run time.
Correct Answer: A
Section: (none)
Explanation

Explanation/Reference:

InterruptedException 异常:https://blog.csdn.net/derekjiang/article/details/4845757

Callable和Future:https://blog.csdn.net/ghsau/article/details/7451464

ExecutorService详解:https://blog.csdn.net/fwt336/article/details/81530581

QUESTION 40
Given:
public class Canvas implements Drawable {
public void draw () { }
}
public abstract class Board extends Canvas { }
public class Paper extends Canvas {
protected void draw (int color) { }
}
public class Frame extends Canvas implements Drawable {
public void resize () { }
}
public interface Drawable {
public abstract void draw ();
}

Which statement is true?
A. Board does not compile.
B. Paper does not compile.
C. Frame does not compile.
D. Drawable does not compile.
E. All classes compile successfully.
Correct Answer: E
Section: (none)
Explanation
Explanation/Reference:

SCJP刷题学习笔记(Part four)相关推荐

  1. SCJP刷题学习笔记(Part one)

    测试Java版本"1.8.0_131" 工具为eclipse QUESTION 1 Given the definition of the Vehicle class: class ...

  2. 手机号正则_一起刷题学习正则表达式

    在我最开始学习正则表达式的时候看到一堆符号简直头晕,所以很长一段时间我都是百度一下某某正则怎么写,比如:匹配所有手机号码的正则,但是有时候工作中碰到的一些问题网上搜不到,这就尴尬了,后面还是逼着自己花 ...

  3. 力扣刷题学习SQL篇——1-8 查询(按日期分组销售产品——利用聚合函数GROUP_CONCAT)

    力扣刷题学习SQL篇--1-8 查询(按日期分组销售产品--利用聚合函数GROUP_CONCAT) 1.题目 2.解法 3.group_concat() 1.题目 题目链接:https://leetc ...

  4. 各大编程语言、软件,电子电路刷题学习网站链接及微信公众号

    20210813 增加一些公众号 一些对程序员有用的网站 https://mp.weixin.qq.com/s/GiEbcBSReaKrVezjGA9_fA 20210715:公众号:拓跋啊秀 资源: ...

  5. 神了,无意中发现一位1500道的2021LeetCode算法刷题pdf笔记

    昨晚逛GitHub,无意中看到一位大佬的算法刷题笔记,感觉发现了宝藏!有些小伙伴可能已经发现了,但咱这里还是忍不住安利一波,怕有些小伙伴没有看到. 关于算法刷题的困惑和疑问也经常听朋友们提及.这份笔记 ...

  6. 无意中发现一位大佬的算法刷题pdf笔记

    昨晚逛GitHub,无意中看到一位大佬(https://github.com/halfrost)的算法刷题笔记,感觉发现了宝藏!有些小伙伴可能已经发现了,但咱这里还是忍不住安利一波,怕有些小伙伴没有看 ...

  7. LeetCode刷题复盘笔记—1373. 二叉搜索子树的最大键值和

    今日主要总结一下,1373. 二叉搜索子树的最大键值和 题目:1373. 二叉搜索子树的最大键值和 Leetcode题目地址 题目描述: 给你一棵以 root 为根的 二叉树 ,请你返回 任意 二叉搜 ...

  8. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MB Submit: 9894  Solved: 4561 [Su ...

  9. 刷题学习—算法思想(动态规划下)

    24.买卖股票的最佳时机系列 24.1买卖股票的最佳时机 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票.设计一个算法来计算你所能获取的最大利润. 只能一笔交易 cla ...

最新文章

  1. 110道 Redis面试题及答案(最新整理)
  2. matlab ufunc,ufunc函数
  3. shell实例第18讲:利用gzexe加密shell脚本
  4. Redux从入门到进阶,看这一篇就够了!
  5. CSS之window的视图属性
  6. 玩转Nodejs日志管理log4js
  7. 推荐一个自动写paper的软件,让IEEE吐血泪奔
  8. Liferay中页面的权限控制
  9. html认识数字游戏大全,认识HTML列表元素
  10. javaweb项目通过F5负载,获取客户端真实ip
  11. HP5100常见错误代码
  12. java中后台的跳转_java后台跳转
  13. wc,鹅厂码农最常用的三大编程语言,Java竟然没上榜!
  14. 交比不变性 matlab,交比 | 迪沙格定理
  15. 深入理解读写锁ReentrantReadWriteLock
  16. 详解STM32 PMW计算中的“死区”
  17. 利用Qt制作QQ的登录及主界面
  18. 高中计算机教学工作计划,教学工作计划
  19. 数字电路实验怎么接线视频讲解_电工知识:三相电表怎么接线?2种接线方法一一讲解,实物对照...
  20. 【web前端开发】CSS文字和文本样式

热门文章

  1. 【区块链技术开发】 Solidity使用Truffle Box工具实现预构建模板、自动化部署、创建智能合约示例代码
  2. PS Suite Studio 初探
  3. 10 模拟SPI驱动PS2无线手柄
  4. 《Feature Pyramid Networks for Object Detection》论文阅读笔记
  5. office tab enterprise是什么:Office Tab Enterprise是超级微软office多标签插件---高效办公必备神器
  6. 呱呱视频技术难点分享:遇到和填上的那些坑
  7. 红外线发射器与接收器模块使用教学
  8. ROS入门——胡春旭老师《机器人开发实践》在ROS-Melodic下的编译
  9. 洛谷P1024 [NOIP2001 提高组] 一元三次方程求解 C++ 思路加代码
  10. TestNG开源插件Arrow介绍