Java SE 8 Programmer (一)

燕寒
2023-12-01

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

 类似资料: