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
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?
E. An exception is thrown at runtime
若将“product.concat(" BOX".toLowerCase());”修改为“product= product.concat(" BOX".toLowerCase());”,则选C
Which three are advantages of the Java exception mechanism?
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未定义。
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?
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
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.
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?
&&是与与。
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?
Explanation:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
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
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?
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