SCJP刷题学习笔记(Part twelve)

本文记录了SCJP认证考试的第十二部分学习笔记,涵盖了关键概念、类库使用和问题解决策略,旨在帮助备考者巩固知识,提升理解。
摘要由CSDN通过智能技术生成
QUESTION 111
Given:
public class Product {
    public double applyDiscount(double price)
    {
    	assert(price>0);//line n1
    	return price*0.50;
    }
	public static void main(String[ ] args)
{
		Product p =new Product();
    	double newPrice=
    			p.applyDiscount(Double.parseDouble(args[0]));
    	System.out.println("New Price: "+newPrice);
    	}
}

 

and the command:
java Product 0
What is the result?
A. An AssertionError is thrown.
B. A compilation error occurs at line n1 .
C. New Price: 0.0
D. A NumberFormatException is thrown at run time.
Correct Answer: C
Section: (none)
Explanation
Explanation/Reference:
 
 
 
QUESTION 112
Given the code fragment:
	LocalTime  now =LocalTime.now();
		long  timeToBreakfast=0;
		LocalTime office_start =LocalTime.of(7, 30);
		if(office_start.isAfter(now)) {
			timeToBreakfast=now.until(office_start, MINUTES);
		}else {
			timeToBreakfast=now.until(office_start, HOURS);
		}
		System.out.println(timeToBreakfast);

 

Assume that the value of now is 6:30 in the morning.
What is the result?
A. An exception is thrown at run time.
B. 0
C. 60
D. 1
Correct Answer: C
Section: (none)
Explanation Explanation/Reference:
 
 
 
QUESTION 113
Given the code fragments:
class R implements Runnable{
public void run() {System.out.println("Run...");}
}
class C implements Callable<String > {
	public String call() throws Exception{return "Call...";}
}

 

and
	ExecutorService es=Executors.newSingleThreadExecutor();
	es.execute(new R());					//line n1
	Future<String> f1=es.submit(new C());   //line n2
	System.out.println(f1.get());
	es.shutdown();
 
What is the result?
A. The program prints Run… and throws an exception.
B. A compilation error occurs at line n1.
C. Run…
Call…
D. A compilation error occurs at line n2.
Correct Answer: C
Section: (none)
Explanation
Explanation/Reference:
 
 
 
QUESTION 114
Which two are elements of a singleton class? (Choose two.)
A. a transient reference to point to the single instance
B. a public method to instantiate the single instance
C. a public static method to return a copy of the singleton reference
D. a private constructor to the class
E. a public reference to point to the single instance
Correct Answer: BD
Section: (none)
Explanation
Explanation/Reference:
 
 
 
QUESTION 115
Given the code fragment:
Deque<String> queue=new ArrayDeque<>();
		queue.add("Susan");
		queue.add("Allen");
		queue.add("David");
		System.out.println(queue.pop());
		System.out.println(queue.remove());
		System.out.println(queue);

 

What is the result?
A. David
David
[Susan, Allen]
B. Susan
Susan
[Susan, Allen]
C. Susan
Allen
[David]
D. David
Allen
[Susan]
E. Susan
Allen
[Susan, David]
Correct Answer: C
Section: (none)
Explanation
Explanation/Reference:
 
 
 
 
QUESTION 116
Given:
public class Vehicle {
int vId;
String vName;
public Vehicle(int vIdArg,String vNameArg) {
	this.vId=vIdArg;
	this.vName=vNameArg;
}
public int getVId() {return vId;}
public String getVName() {return vName;}
public String toString() {
	return vName;
}
}

 

and the code fragment:
    List<Vehicle> vehicle=Arrays.asList(
			new Vehicle(2,"Car"),
			new Vehicle(3,"Bike"),
			new Vehicle( 1,"Truck"));
	vehicle.stream()
	//line n1
	.forEach(System.out::print);

 

Which two code fragments, when inserted at line n1 independently, enable the code to print
TruckCarBike ?
A. .sorted ((v1, v2) -> v1.getVId() < v2.getVId())
B. .sorted (Comparable.comparing (Vehicle: :getVName)).reversed ()
C. .map (v -> v.getVid())
.sorted ()
D. .sorted((v1, v2) -> Integer.compare(v1.getVId(), v2.getVId()))
E. .sorted(Comparator.comparing ((Vehicle v) -> v.getVId()))
Correct Answer: DE
Section: (none)
Explanation
D:
E:
Explanation/Reference:
 
 
 
QUESTION 117
Given the code fragment:
List<String> valList =Arrays.asList("","George","","John","Jim");
	Long newVal= valList.stream()		//line n1
			.filter(x->!x.isEmpty())
			.count();					//line n2
	System.out.print(newVal);

 

What is the result?
A. A compilation error occurs at line n2 .
B. 3
C. 2
D. A compilation error occurs at line n1.
Correct Answer: B
Section: (none)
Explanation
Explanation/Reference:
 
 
 
QUESTION 118
Given the code fragment:
	//Login time:2015-01-12T21:58:18.817z
		Instant loginTime =Instant.now();
		Thread.sleep(1000);
    //Logout time:2015-01-12T21:58:19.880z
		Instant logoutTime=Instant.now();
		
	loginTime =loginTime.truncatedTo(ChronoUnit.MINUTES);	//line n1
	logoutTime=logoutTime.truncatedTo(ChronoUnit.MINUTES);
	
	if(logoutTime.isAfter(loginTime))
		System.out.println("Logged out at:"+logoutTime);
	else
		System.out.println("Can't logout");

 

What is the result?
A. A compilation error occurs at line n1.
B. Logged out at: 2015-01-12T21:58:19.880Z
C. Can’t logout
D. Logged out at: 2015-01-12T21:58:00Z
Correct Answer: D
Section: (none)
Explanation
Explanation/Reference:
 
 
 
QUESTION 119
Given the code fragment:
List<String> words =Arrays.asList("win","try","best","luck","do");
Predicate<String> test1=w->{
	System.out.println("Checling...");
	return w.equals("do");					//line n1
};
Predicate test2=(String w)->w.length()>3;	//line n2
words.stream().filter(test2).filter(test1).count();

 

What is the result?
A. A compilation error occurs at line n1.
B. Checking…
C. Checking… Checking…
D. A compilation error occurs at line n2 .
Correct Answer: D
Section: (none)
Explanation
error:
Lambda expression's parameter w is expected to be of type Object
Explanation/Reference:
 
 
 
QUESTION 120
Assume customers.txt is accessible and contains multiple lines.
Which code fragment prints the contents of the customers.txt file?
A. Stream<String> stream = Files.find (Paths.get (“customers.txt”));
stream.forEach((String c) -> System.out.println(c));
B. Stream<Path> stream = Files.find (Paths.get (“customers.txt”));
stream.forEach( c) -> System.out.println(c));
C. Stream<Path> stream = Files.list (Paths.get (“customers.txt”));
stream.forEach( c) -> System.out.println(c));
D. Stream<String> lines = Files.lines (Paths.get (“customers.txt”));
lines.forEach( c) -> System.out.println(c));
Correct Answer: D
Section: (none)
Explanation
Explanation/Reference:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值