Java SE 8 Programmer (一)

Question No : 1

Given the code fragment:

public class Test {
	static int count = 0;
	int i = 0;
	public void changeCount() {
		while (i < 5) {
			i++;
			count++;
		}
	}
	public static void main(String[] args) {
		Test check1 = new Test();
		Test check2 = new Test();
		check1.changeCount();
		check2.changeCount();
		System.out.println(check1.count + " : " + check2.count);
	}
}

What is the result?

A. 10 : 10

B. 5 : 5

  1. 5 : 10
  2. Compilation fails

Answer: A

 Question No :2

Given:

public class Case {
	public static void main(String[] args) {
		String product = "Pen";
		product.toLowerCase();
		product.concat(" BOX".toLowerCase());
		System.out.print(product.substring(4,6));
	}
}

What is the result?

  1. box
  2. nbo
  3. bo
  4. nb

E. An exception is thrown at runtime 

Answer: E

若将“product.concat(" BOX".toLowerCase());”修改为“product= product.concat(" BOX".toLowerCase());”,则选C

Question No : 3

Which three are advantages of the Java exception mechanism?

  1. Improves the program structure because the error handling code is separated from the normal program function
  2. Provides a set of standard exceptions that covers all the possible errors
  3. Improves the program structure because the programmer can choose where to handle exceptions
  4. Improves the program structure because exceptions must be handled in the method in which they occurred
  5. Allows the creation of new exceptions that are tailored to the particular program being created

Answer: A,C,E

Question No : 4

Given the code fragment:

public class Person {
	String name;
	int age = 25;
	public Person(String name) {
		this();									//line n1
		setName(name);
	}
	public Person(String name, int age) {
		Person(name);							//line n2
		setAge(age);
	}
	//setter and getter methods go here
	public String show() {
		return name + " " + age + " " + number;
	}
	public static void main(String[] args) {
		Person p1 = new Person("Jesse");
		Person p2 = new Person("Walter",52);
		System.out.println(p1.show());
		System.out.println(p2.show());
	}
}

What is the result?

 缺少无参数的构造方法,缺少new 关键字,number未定义。

  1. Jesse 25 Walter 52
  2. Compilation fails only at line n1
  3. Compilation fails only at line n2
  4. Compilation fails at both line n1 and line n2

 Answer: D

Question No : 5

Given:

class Mid {
	public int findMid(int n1, int n2) {
		return (n1 + n2) / 2;
	}
}
public class Calc extends Mid {
	public static void main(String[] args) {
		int n1 = 22, n2 = 2;
		// insert code here
		System.out.print(n3);
	}
}

Which two code fragments, when inserted at // insert code here, enable the code to compile and print 12?

  1. Calc c = new Calc(); int n3 = c.findMid(n1,n2);
  2. int n3 = super.findMid(n1,n3);
  3. Calc c = new Mid(); int n3 = c.findMid(n1, n2);
  4. Mid m1 = new Calc(); int n3 = m1.findMid(n1, n2);
  5. int n3 = Calc.findMid(n1, n2);

Answer: A,D

Explanation:  

Incorrect:

Not B: circular definition of n3.

Not C: Compilation error. line Calc c = new Mid(); required: Calc found: Mid

Not E: Compilation error. line int n3 = Calc.findMid(n1, n2); non-static method findMid(int,int) cannot be referenced from a static context

Question No : 6

Given the code fragment:

import java.time.*;
import java.time.format.*;
class Test {
	public static void main(String[] args) {
		LocalDate date1= LocalDate.now();
		LocalDate date2= LocalDate.of(2014, 6, 20);
		LocalDate date3= LocalDate.parse("2014-06-20", DateTimeFormatter.ISO_DATE);
		System.out.println("date1 = " + date1);
		System.out.println("date2 = " + date2);
		System.out.println("date3 = " + date3);
	}
}

Assume that the system date is June 20, 2014. What is the result?

A.date1 = 2014-06-20

date2 = 2014-06-20

date3 = 2014-06-20

B.datel = 06/ 20/2014

date2 =2014-06-20l

date3 = Jun 20,2014T

C.Compilation fails.

D.A DateParseExcpetion is thrown at runtime.

Answer: A

Question No : 7

Given the code fragment:

	int[] lst = {1, 2, 3, 4, 5, 4, 3, 2, 1};
	int sum = 0;
	for (int frnt = 0, rear = lst.length - 1;
		 frnt < 5 && rear >= 5;
		 frnt++, rear--) {
		sum = sum + lst[frnt] + lst[rear];
	}
	System.out.print(sum);

What is the result?

  1. 20
  2. 25
  3. 29
  4. Compilation fails
  5. AnArrayIndexOutOfBoundsException is thrown at runtime

Answer: A

 &&是与与。

Question No : 8

Given the code fragment:

import java.time.*;
import java.time.format.*;
class Test {
	public static void main(String[] args) {
		String date = LocalDate
						.parse("2014-05-04")
						.format(DateTimeFormatter.ISO_DATE_TIME);
		System.out.print(date);
	}
}

What is the result?

  1. May 04, 2014T00:00:00.000
  2. 2014-05-04T00:00: 00. 000
  3. 5/4/14T00:00:00.000
  4. An exception is thrown at runtime.

Answer: D

Explanation:  

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay

Question No : 9

Given the code fragment:

  public static void main(String[] args) {
        double discount = 0;
        int qty = Integer.parseInt(args[0]);
        // line n1
    }

And given the requirements:

 If the value of the qty variable is greater than or equal to 90, discount = 0.5

 If the value of the qty variable is between 80 and 90, discount = 0.2

Which two code fragments can be independently placed at line n1 to meet the requirements?

A) if (qty >=90){ discount = 0.5; }
if (qty >80&& qty < 90) {discount = 0.2; }
B) discount = (qty >= 90) ?0.5 :0;
   discount = (qty >80) ?0.2 : 0;
C) discount = (qty >= 90)? 0.5 : (qty >80)? 0.2 : 0;
D)if (qty > 80 && qty <90){
    discount = 0.2;
} else {
    discount = 0;
    if (qty >= 90){
        discount =0.5;
    }else {
        discount =0;
    }
 E) discount =(qty > 80)?0.2 : (qty >= 90)? 0.5 : 0;

Answer: A,C

Question No : 10

Given:

public class Circle {
	double radius;
	public double area;
	public Circle(double r) { radius = r;}
	public double getRadius() { return radius; }
	public void setRadius(double r) { radius = r; }
	public double getArea ( ) { return /* ??? */; } 
}
class App {
	public static void main(String[] args) {
		Circle c1 = new Circle(17.4);
		c1.area = Math.PI * c1.getRadius() * c1.getRadius();
	}
}

The class is poorly encapsulated. You need to change the circle class to compute and return the area instead.

Which two modifications are necessary to ensure that the class is being properly encapsulated?

  1. Remove the area field.
  2. Change the getArea( ) method as follows:  

public double getArea ( ) { return Math.PI * radius * radius; }

C. Add the following method:  

public double getArea ( ) { area = Math.PI * radius * radius; }

D. Change the cases modifier of the SetRadius ( ) method to be protected.

Answer: B,D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Title: OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808 Author: Jeanne Boyarsky, Scott Selikoff Length: 432 pages Edition: 1 Language: English Publisher: Sybex Publication Date: 2014-12-31 ISBN-10: 1118957407 ISBN-13: 9781118957400 Full coverage of functional programming and all OCA Java Programmer exam objectives OCA, Oracle Certified Associate Java SE 8 Programmer I Study Guide, Exam 1Z1-808 is a comprehensive study guide for those taking the Oracle Certified Associate Java SE 8 Programmer I exam (1Z1-808). With complete coverage of 100% of the exam objectives, this book provides everything you need to know to confidently take the exam. The release of Java 8 brought the language's biggest changes to date, and for the first time, candidates are required to learn functional programming to pass the exam. This study guide has you covered, with thorough functional programming explanation and information on all key topic areas Java programmers need to know. You'll cover Java inside and out, and learn how to apply it efficiently and effectively to create solutions applicable to real-world scenarios. * Work confidently with operators, conditionals, and loops * Understand object-oriented design principles and patterns * Master functional programming fundamentals Table of Contents Chapter 1 Java Building Blocks Chapter 2 Operators and Statements Chapter 3 Core Java APIs Chapter 4 Methods and Encapsulation Chapter 5 Class Design Chapter 6 Exceptions Appendix A Answers to Review Questions Appendix B Study Tips

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值