Java SE 8 Programmer(二)

本文讨论了Java编程中的类封装原则,通过修改Circle类实现计算面积的方法。同时,展示了for循环的基本构造和使用,以及如何处理运行时可能抛出的异常。在代码示例中,解释了如何捕获并处理特定异常,以确保程序的正常运行。此外,还探讨了变量初始化的重要性以及方法重载的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?
A.Remove the area field.
B.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

Question No : 11
Given the code fragment:

class Test {
	public static void main(String[] args) {
		int row = 10;
		for ( ; row > 0  ; ) {
			int col = row;
			while (col >=0) {
				System.out.print(col + " ");
				col -= 2;
			}
			row = row / col;
		}
	}
}

What is the result?
A.10 8 6 4 2 0
B.10 8 6 4 2
C.AnArithmeticException is thrown at runtime
D.The program goes into an infinite loop outputting: 10 8 6 4 2 0. . .
E.Compilation fails
Answer: A

Question No : 12
Given:

class Patient {
	String name;
	public Patient(String name) {
		this.name = name;
	}
}

And the code fragment:

import java.util.*;
public class Test {
public static void main(String[] args) {
 	List ps = new ArrayList();
 	Patient p2 = new Patient("Mike");
 	ps.add(p2);

 	// insert code here

	if (f >=0 ) {
 		System.out.print("Mike Found");
 	}
  }
}

Which code fragment, when inserted at line 14, enables the code to print Mike Found?
A.int f = ps.indexOf {new patient (“Mike”)};
B.int f = ps.indexOf (patient(“Mike”));
C.patient p = new Patient (“Mike”); int f = ps.indexOf§;
D.int f = ps.indexOf(p2);
Answer: D

Question No : 13
Given:

class CD{
    int r;
    CD(int r){
        this.r;
    }
}

class DVD extends CD {
    int c;
    DVD(int r ,int c){
        // line 1
    }
}
And given the code frgment:
DVD dvd = new DVD(10,20);

Which code fragment should you use at line n1 to instantiate the dvd object successfully?
A.Option A
B.Option B
C.Option C
D.Option D
Answer: C

Question No : 14
Given the for loop construct:
for ( expr1 ; expr2 ; expr3 ) {
statement;
}
Which two statements are true?
A.This is not the only valid for loop construct; there exits another form of for loop constructor.
B.The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin.
C.When expr2 evaluates to false, the loop terminates. It is evaluated only after each iteration through the loop.
D.The expression expr3 must be present. It is evaluated after each iteration through the loop.
Answer: A,B
Explanation:
The for statement have this forms: for (init-stmt; condition; next-stmt) { body
}
There are three clauses in the for statement.
The init-stmt statement is done before the loop is started, usually to initialize an iteration variable.
The condition expression is tested before each time the loop is done. The loop isn’t executed if the boolean expression is false (the same as the while loop).
The next-stmt statement is done after the body is executed. It typically increments an iteration variable.

Question No : 15
Which three statements are true about the structure of a Java class?
A.A class can have only one private constructor.
B.A method can have the same name as a field.
C.A class can have overloaded static methods.
D.A public class must have a main method.
E.The methods are mandatory components of a class.
F.The fields need not be initialized before use.
Answer: A,B,C
Explanation: A: Private constructors prevent a class from being explicitly instantiated by its callers.
If the programmer does not provide a constructor for a class, then the system will always provide a default, public no-argument constructor. To disable this default constructor, simply add a private no-argument constructor to the class. This private constructor may be empty.
B: The following works fine:
int cake() { int cake=0; return (1);
}
C: We can overload static method in Java. In terms of method overloading static method are just like normal methods and in order to overload static method you need to provide another static method with same name but different method signature.
Incorrect:
Not D: Only a public class in an application need to have a main method.
Not E:
Example:
class A
{
public string something; public int a;
}
Q: What do you call classes without methods?
Most of the time: An anti pattern.
Why? Because it faciliates procedural programming with “Operator” classes and data structures. You separate data and behaviour which isn’t exactly good OOP.
Often times: A DTO (Data Transfer Object)
Read only datastructures meant to exchange data, derived from a business/domain object.
Sometimes: Just data structure.
Well sometimes, you just gotta have those structures to hold data that is just plain and simple and has no operations on it.
Not F: Fields need to be initialtized. If not the code will not compile.
Example:
Uncompilable source code - variable x might not have been initialized

Question No : 16
View the exhibit.

class MissingInfoException extends Exception {
}
class AgeOutofRangeException extends Exception {
}
class Candidate {
	String name;
	int age;
		Candidate(String name, int age ) throws Exception {
			if (name == null) {
			throw new MissingInfoException();
		}	else if (age <= 10 || age >= 150) {
			throw new AgeOutofRangeException();
		}	else {
			this.name = name;
			this.age = age;
		}
	}
	public String toString() {
		return name + " age: " + age;
	}
}

Given the code fragment:

 public class Test{
	 public static void main(String[] args)	{
		 Candidate c = new Candidate("James", 20);
		 Candidate c1 = new Candidate("Williams", 32);	
		 System.out.println(c);
		 System.out.println(c1);
	 }
 }

Which change enables the code to print the following?
James age: 20
Williams age: 32
A.Replacing line 5 with public static void main (String [] args) throws MissingInfoException,AgeOutofRangeException {
B.Replacing line 5 with public static void main (String [] args) throws Exception {
C.Enclosing line 6 and line 7 within a try block and adding: catch(Exception e1) { //code goes here} catch (missingInfoException e2) { //code goes here} catch (AgeOutofRangeException e3) {//code goes here}
D.Enclosing line 6 and line 7 within a try block and adding:
catch (missingInfoException e2) { //code goes here} catch (AgeOutofRangeException e3) {//code goes here}
Answer: B

Question No : 17
Given the code fragment:

public class Test{
	public static void main(String[] args) {
		int iArray[] = {65, 68, 69};
		iArray[2] = iArray[0];
		iArray[0] = iArray[1];
		iArray[1] = iArray[2];
		for (int element : iArray) {
			System.out.print(element + " ");
		}
	}
}

A.68, 65, 69
B.68, 65, 65
C.65, 68, 65
D.65, 68, 69
E.Compilation fails
Answer: B

Question No : 18
Given:
public class Test {
public static void main(String[] args) {
int day = 1;
switch (day) {
case “7”: System.out.print(“Uranus”);
case “6”: System.out.print(“Saturn”);
case “1”: System.out.print(“Mercury”);
case “2”: System.out.print(“Venus”);
case “3”: System.out.print(“Earth”);
case “4”: System.out.print(“Mars”);
case “5”: System.out.print(“Jupiter”);
}
}
}
Which two modifications, made independently, enable the code to compile and run?
A.Adding a break statement after each print statement
B.Adding a default section within the switch code-block
C.Changing the string literals in each case label to integer
D.Changing the type of the variable day to String
E.Arranging the case labels in ascending order
Answer: C,D

Question No : 19
Given:

public class Product {
	int id;
	String name;
	public Product(int id, String name) {
		this.id = id;
		this.name = name;
	}
}
And given the code fragment:
public class Test {
	public static void main(String[] args) {
		Product p1 = new Product(101, "Pen");
		Product p2 = new Product(101, "Pen");
		Product p3 = p1;
		boolean ans1 = p1 == p2;
		boolean ans2 = p1.name.equals(p2.name);
		System.out.print(ans1 + ":" + ans2);
	}
}

What is the result?
A.true:true
B.true:false
C.false:true
D.false:false
Answer: C

Question No : 20
Given:

  class Alpha {
        int ns;
        static int s;

        Alpha(int ns) {
            if (s < ns) {
                s = ns;
                this.ns = ns;
            }
        }

        void doPrint() {
            System.out.println("ns = " + ns + " s = " + s);
        }
    }

And

    public class TestA{
        public static void main(String[] args) {
            Alpha ref1 = new Alpha(50);
            Alpha ref2 = new Alpha(125);
            Alpha ref3 = new Alpha(100);
            ref1.doPrint();
            ref2.doPrint();
            ref3.doPrint();
        }
    }

What is the result?

A. ns = 50 s = 125
ns = 125 s = 125
ns = 100 s = 125
B. ns = 50 s = 125
ns = 125 s = 125
ns = 0 s = 125
C. ns = 50 s = 50
ns = 125 s = 125
ns = 100 s = 100
D. ns = 50 s = 50
ns = 125 s = 125
ns = 0 s = 125
Answer: B

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&#39;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&#39;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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值