1Z0-808
Question 1
Given:
public class TestTry {
public static void main (String[] args) {
StringBuilder message = new StringBuilder(“hello java!”);
int pos =0;
try {
for ( pos = 0;pos < 12; pos++) {
switch (message.charAt(pos)) {
case ‘a’:
case ‘e’:
case ‘o’:
String uc=Character.toString(message.charAt(pos)).toUpperCase();
message.replace(pos,pos+1,uc);
}
}
} catch (Exception e) {
System.out.println(“Out of limits”);
}
System.out.println(message);
}
}
What is the result?
A. hEllOjAvA!
B. Hello java!
C. Out of limits hEllOjAvA!
D. Out of limits
Question 2(1)
Given the code fragment:
1.public class Test{
2. public static void main(String[] args){
3. / * insert code here */
4. array [0]=10;
5. array 1=20;
6. System.out. print(array [0]+":"+array1);
7. }
8. }
Which code fragment, when inserted at line 3, enables the code to print 10:20?当插入第3行时,哪一个代码片段使代码能够打印出“10:20”?
A. int[] array= new int2;
B. int[] array; array = int2;
C. int array = new int2;
D. int array 2 ;
Question 3(2)
Given:
class x{
static int i;
int j;
public static void main(String[] args){
X x1 = new X();
X x2 = new X();
x1.i = 3;
x1.j = 4;
x2.i = 5;
x2.j = 6;
System.out.println(
x1.i + " " +
x1.j + " " +
x2.i + " " +
x2.j);
}
}
What is the result?
A. 3 4 5 6
B. 3 4 3 6
C. 5 4 5 6
D. 3 6 4 6
Question 4(2)
Given:
public class CharToStr {
public static void main(String[] args) {
String str1 = “Java”;
char str2[] = { ‘J’, ‘a’,‘v’,‘a’ };
String str3 = null;
for (char c : str2) {
str3 = str3 + c;
}
if (str1.equals(str3))
System.out.print("Successful");
else
System.out.print("Unsuccessful");
}
}
What is result?
A. Successful
B. Unsuccessful
C. Compilation fails
D. An exception is thrown at
Question 5(2)
Given:
class Sports {
int num_players;
String name, ground_condition;
Sports(int np, String sname, String sground){
num_players = np;
name = sname;
ground_condition = sground;
}
}
class Cricket extends Sports {
int num_umpires;
int num_substitutes;
//
}
Which code fragment can be inserted at line //insert code here to enable the code to compile?
哪一个代码片段可以插入//行,在这里插入代码以使代码能够编译?
A. Cricket() {
super(11, “Cricket”, “Condidtion OK”);
num_umpires =3;
num_substitutes=2;
}
B. Cricket() {
super.ground_condition = “Condition OK”; super.name=“Cricket”; super.num_players = 11;
num_umpires =3; num_substitutes=2;
}
C. Cricket() { this(3,2);
super(11, “Cricket”, “Condidtion OK”);
}
Cricket(int nu, ns) { this.num_umpires =nu; this.num_substitutes=ns;
}
D. Cricket() { this.num_umpires =3; this.num_substitutes=2;
super(11, “Cricket”, “Condidtion OK”);
}
Question 6(2)
Given:
public class Calculator{
public static void main(String[] args){
int num = 5;
int sum;
do {
sum + =num;
}while((num–)>1);
System.out.println(“The sum is” + sum + “.”);
}
}
What is the result?
A. The sum is 2
B. The sum is 14
C. The sum is 15
D. The loop executes infinite//循环无限执行
E. Compilation fails//编译失败
Question 7
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.print(check1.count + “:” + check2.count);
}
}
What is the result?
A. 10 : 10
B. 5 : 5
C. 5 : 10
D. Compilation fails
Question 8
Which two items can legally be contained within a java class declaration?
//java类声明中可以合法包含哪两项?
A. An import statement
B. A field declaration
C. A package declaration
D. A method declaration
Question 9
Given the class definitions:
给定类定义
class Alpha {
public String doStuff(String msg){
return msg;
}
}
class Beta extends Alpha {
public String doStuff(String msg){
return msg.replace(‘a’,‘e’);
}
}
class Camma extends Beta {
public String doStuff(String msg){
return msg.substring(2);
}
}
And the code fragment of the main() method,
main()方法的代码片段,
12. List strs=new ArrayList();
13. strs.add(new Alpha());
14. strs.add(new Beta());
15. strs.add(new Gamma());
16. for(Alpha t:strs){
17. System.out.println(t.doStuff(“Java”));
18. }
What is the result?
A. Java Java Java
B. Java Jeve va
C. Java Jeve ve
D. Compilation fails
Question 10
int [] array = {1,2,3,4,5}; for (int i: array) {
if ( i < 2) { keyword1 ;}
System.out.println(i);
if ( i == 3) { keyword2 ;}
}
What should keyword1 and keyword2 be respectively, in oreder to produce output 2345?
为了产生输出2345,关键字1和关键字2应该分别是什么?
A. continue, break
B. break, break
C. break, continue
D. continue, continue
Question 11(2)
Given:
int x=10;
if(x>10){
System.out.println(">");
}else if(x<10){
System.out.println("<");
}esle{
System.out.println("=");
}
Which of the following is equivalent to the above code fragment?
下列哪一项等同于上述代码片段?
A. System.out.printLn(x>10?">,’: “<”:,’=");
B. System.out.println(x>10? “>”?"<":"=");
C. System.out.println(x>10?">":x<10?"<":"=");
D. System.out.printLn(x>10?">"?,’<"?"=");
E. None of the above
Question 12(1)
Given the code fragment:
int num[] [] = new int1;
for (int i = 0; i < num.length; i++){
for(int j = 0; j< num[i].length; j++){
num[i][j] = 10;
}
}
Which option represents the state of the num array after successful completion of the outer loop?哪个选项表示外环成功完成后num数组的状态?
A)num[0][0]=10
num0=10
num0=10
B)num[0][0]=10
num[1][0]=10
num[2][0]=10
C)num[0][0]=10
num0=0
num0=0
D)num[0][0]=10
num0=10
num0=10
num0=10
num[1][0]=0
num1=0
num1=0
num1=0
Question 13(1)
Given:
public class Test3 {
public static void main(String[] args) {
String names[] = new String3;
names[0] = “Mary Brown”; //Mar+rown
names1 = “Nancy Red”; //Nan + string out of limits
names2 = “Jseey Orange”; //Jse + Oran
try {
for (String n:names) {
try {
String pwd=n.substring(0,3)+n.substring(6,10);
System.out.println(pwd);
} catch (StringIndexOutOfBoundsException sie) {
System.out.println(“string out of limits”);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“array out of limits”);
}
}
}
What is the result?
A. Marrown String out of limits JesOran
B. Marrown String out of limits Array out of limits
C. Marrown String out of limits
D. Marrown NanRed JesOran
Question 14(1)
Given the code fragment:
public static void main(String[] args) {
int iArray[] = {65, 68, 69};
iArray2 = iArray[0];
iArray[0] = iArray1;
iArray1 = iArray2;
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//编译失败
Question 15(1)
Given:
public class Natural {
private int i;
void disp() {
while (i <= 5) {
for (int i=1; i <=5;) {
System.out.print(i + " ");
i++;
}
i++;
}
}
public static void main(String[] args) {
new Natural().disp();
}
}
What is the result?
A. Prints 1 2 3 4 5 once
B. Prints 1 3 5 once
C. Prints 1 2 3 4 5 five times
D. Prints 1 2 3 4 5 six times
E. Compilation fails
Question 16(1)
Given:
public class Vowel {
private char var;
public static void main(String[] args) {
char var1=‘a’;
char var2=var1;
var2=‘e’;
Vowel obj1=new Vowel();
Vowel obj2=new Vowel();
obj1.var='i';
obj2.var='o';
System.out.println(var1+","+var2);
System.out.println(obj1.var+","+obj2.var);
}
}
A. a, e i, o
B. a, e o, o
C. e, e I, o
D. e, e o, o
Question 17(2)
Given:
public abstract class Shape {
private int x;
private int y;
public abstract void draw();
public void setAnchor(int x,int y){
this.x=x;
this.y=y;
}
}
Which two classes use the shape class correctly?
哪两个类正确使用了形状类?
A)public class Circle implements Shape{
private int radius;
}
B)public abstract class Circle extends Shape{
private int radius;
}
C)public class Circle extends Shape{
private int radius;
public void draw();
}
D)public abstract class Circle implements Shape{
private int radius;
public void draw();
}
E)public class Circle extends Shape{
private int radius;
public void draw();{/code here/}
}
F)public abstract class Circle implements Shape{
private int radius;
public void draw();{/code here/}
}
Explanation: When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class (E). However, if it does not, then the subclass must also be declared abstract (B).
Note: An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
Question 18(2)
Given the code fragment:
public class Employee {
String name;
boolean contract;
double salary;
Employee() {
// line n1
}
public String toString(){
return name + “:” + contract + “:” + salary;
}
public static void main(String[] args) {
Employee e = new Employee();
// line n2
System.out.print(e);
}
}
Which two modifications, when made independently, enable the code to print Joe:true: 100.0?
A)Replace line n2 with;
e.name = “Joe”;
e.contract = true;
e.salary = 100;
B)Replace line n2 with;
this.name = “Joe”;
this.contract = true;
this.salary = 100;
C)Replace line n1 with;
this.name = new String(“Joe”);
this.contract = new Boolean(true);
this.salary = new Double(100);
D)Replace line n1 with;
name = “Joe”;
contract = TRUE;
salary = 100.0f;
E)Replace line n1 with;
this(“Joe”, true, 100);
Question 19(1)
Given the following code:
int[] intArr = {15, 30, 45, 60, 75};
intArr2 = intArr4;
intArr4 = 90;
What are the values of each element in intArr after this code has executed?
这段代码执行后,intArr中每个元素的值是多少?
A. 15, 60, 45, 90, 75
B. 15, 90, 45, 90, 75
C. 15, 30, 75, 60, 90
D. 15, 30, 90, 60, 90
E. 15, 4, 45, 60, 90
Question 20
Given:
class Vehicle {
int x;
Vehicle(){
this(10); //line n1
}
Vehicle(int x){
this.x = x;
}
}
class Car extends Vehicle {
int y;
Car() {
super();
this(20); //line n2
}
Car(int y) {
this.y = y;
}
public String toString() {
return super.x + “:” + this.y;
}
}
And given the code fragment:
And given the code fragment:
Vehicle y = new Car();
System.out.println(y);
What is the result?
A. 10:20
B. 0:20
C. Compilation fails at line n1
D. Compilation fails at line n2
Question 21(2)
Given the code fragment:
public static void main(String[] args) {
ArrayList myList=new ArrayList();
String[] myArray;
try{
while (true){
myList.add(“My String”);
}
}
catch (RuntimeException re) {
System.out.println(“Caught a RuntimeException”);
}
catch (Exception e) {
System.out.println(“Caught an Exception”);
}
System.out.println(“Ready to use”);
}
What is the result?
A.Execution terminates in the first catch statement, and caught a RuntimeException is printed to the console.
执行在第一个catch语句中终止,并且捕获到一个运行时异常被打印到控制台
B.Execution terminates In the second catch statement, and caught an Exception is printed to the console.
执行在第二个catch语句中终止,并且catch异常被打印到控制台。
C.A runtime error is thrown in the thread “main”.
线程“main”中引发运行时错误。
D.Execution completes normally, and Ready to us© is printed to the console.
执行正常完成,准备就绪打印到控制台。
E.The code fails to compile because a throws keyword is required.
代码无法编译,因为需要一个throws关键字
Question 22(1)
Given:
class Product{
double price:
}
public class Test{
punlic void updatePrice(Product product,double price){
price=price*2;
product.price=product.price+price;
}
public static void main(String[] args){
Product prt=new Product();
prt.price=200;
double newPrice=100;
Test t=new Test();
t.updatePrice(prt, newPrice);
System.out.println(prt.price +" : "+newPrice);
}
}
What is the result?
A. 200.0 : 100.0
B. 400.0 : 200.0
C. 400.0 : 100.0
D. Compilation fails.
Question 23(2)
Given:
abstract class X{
public abstract void methodx();
}
interface Y{
public void methodY();
}
Which two code fragments are valid?
哪两个代码片段有效?
A)class Z extends X implements Y{
pulic void methodZ(){}
}
B)abstract class Z extends X implements Y{
pulic void methodZ(){}
}
C)class Z extends X implements Y{
pulic void methodX(){}
}
D)abstract class Z extends X implements Y{
}
E)class Z extends X implements Y{
pulic void methodY(){}
Question 24(1)
Given:
class Test {
public static void main(String[] args){
int numbers[];
numbers = new int2;
numbers[0] = 10;
numbers1 = 20;
numbers = new int[4];
numbers[2] = 30;
numbers[3] = 40;
for (int x:numbers){
System.out.print(" "+x);
}
}
}
what is the result?
A)10 20 30 40
B)Complication fails’
C)An exception is thrown at runtime
D)0 0 30 40
Question 25(1)
Given:
System.out.println(“5+2= ”+3+4);
System.out.println(“5+2=”+(3+4));
What is the result?
A) 5+2 =34 5+2 =34
B) 5+2+3+4 5+2=7
C) 7=7 7+7
D) 5+2=34 5+2=7
Question 26(1)
Given the definitions of the MyString class and the Test class:
MyString.java:
Package p1;
Class Mystring{
String msg;
Mystring (String msg){
This.msg = msg;
}
}
Test.java:
Package p1;
Public class Test{
Public static void main (String[] args){
System.out.println(“Hello”+new StringBuilder (“Java SE 8 ”));
System.out.println(“Hello”+ new Mystring(“Java SE 8”));
}
}
What is the result?
A) Hello Java SE 8 Hello Java SE 8
B) Hello java.lang.StringBuilder@<> Hello p1.Mystring@<>
C) Hello Java SE 8 Hello p1.MyString@<>
D) Compilation fails at the Test class
A. Option A
B. Option B
C. Option C
D. Option D
Question 27(2)
Class A { }
Class B { }
Interface X { } Interface Y { }
Which two definitions of class C are valid?
A. Class C extends A implements X { }
B. Class C implements Y extends B { }
C. Class C extends A, B { }
D. Class C implements X, Y extends B { }
E. Class C extends B implements X, Y { }
Question 28(1)
Given:
}
Which is true?
A. Sum for 0 to 0 = 55
B. Sum for 0 to 10 = 55
C. Compilation fails due to error on line 6.
D. Compilation fails due to error on line 7.
E. An Exception is thrown at the runtime.
Question 29(1)
You are developing a banking module. You have developed a class named CCMask that has a maskcc method.
您正在开发一个银行模块。您已经开发了一个名为CCMask的类,它有一个maskcc方法。
Given the code fragment:
Class CCMask{
public static String maskCC(String ceditCard){
String x=”XXXX-XXXX-XXXX-”;
//line n1
}
public static void main (String [ ] args){
System.out.println(maskCC(“1234-5678-9101-1121”));
}
}
You must ensure that the maskcc method returns a string that hides all digits of the credit card number except the four last digits (and the hyphens that separate each group of four digits).
您必须确保maskcc方法返回一个字符串,该字符串隐藏信用卡号的所有数字,除了最后四个数字(以及分隔每组四个数字的连字符)。
Which two code fragments should you use at line n1, independently, to achieve this requirement?
为了达到这一要求,您应该在n1行单独使用哪两个代码片段?
A)StringBuilder sb = new StringBuilder(creditCard);
sb.substring(15,19);
return x + sb;
B) Return x + creditCard.substring(15,19);
C) StringBuilder sb = new StringBuilder(x);
sb.append(creditCard,15,19);
return sb.toString();
D) StringBuilder sb =new StringBuilder(creditCard);
StringBuilder s = sb.insert(0,x);
return s.toString();
Question 30(1)
Given the fragment:
String[][] arra = new String3;
arra[0] = new String[]{“rose”, “lily”};
arra1 = new String[]{“apple”, “berry”,“cherry”,“grapes”};
arra[0] = new String[]{“beans”, “carrot”,“potato”};
// insert code fragment here
Which code fragment when inserted at line ‘// insert code fragment here’, enables the code to successfully change arra elements to uppercase?
在第’//行插入代码片段时,哪个代码片段使代码能够成功地将arra元素更改为大写?
A. for (int i = 0; i < arra.length; i++) {
for (int j=0; j < arra[i].length; j++) {
arra[i][j] = arra[i][j].toUpperCase();
}
}
B. for (int i = 0; i < 3; i++) {
for (int j=0; j < 4; j++) {
arra[i][j] = arra[i][j].toUpperCase();
}
}
C. for (String a[]:arra[][]) { for (String x:a[]) {
toUpperCase();
}
}
D. for (int i:arra.length) {
for (String x:arra) { arra[i].toUpperCase();}
}
Question 31(2)
Which of the following can fill in the blank in this code to make it compile?
下列哪一项可以填补这段代码中的空白以使其编译?
interface CanFly{
String type = “A”;
void fly();
__ String getType(){
return type;
}
}
A. abstract
B. public
C. default
D. It will not compile with any as interfaces cannot have non abstract methods.它不会用任何方法编译,因为接口不能有非抽象方法。
E. It will compile without filling the blank.它将编译而不填充空白。
Question 32(2)
Given:
int sum = 0;
public void doCheck(int number){
if (number %2 == 0){
break;
}else{
for (int i = 0; i < number;i++){
sum + = i;
}
}
}
public static void main (String[] args){
Test obj= new Test();
System.out.println(“Red”+obj.sum);
obj.doCheck(2);}
}
What is the result?
A. Red 0 Orange 0 Green 3
B. Red 0
Orange 0
Green 6
C. Red 0 Orange 1
D. Green 4
E. Compilation fails
Question 33(1)
Given:
public class MyClass {
public static void main(String[] args) {
while (int ii = 0; ii < 2) {
ii++;
System.out.println("ii = " + ii);
}
}
}
What is the result?
A. ii = 1 ii = 2
B. Compilation fails
C. The program prints nothing
D. The program goes into an infinite loop with no output
E. The program goes to an infinite loop outputting:
ii = 1
ii = 1
Question 34(1)
Public static void main(String[] args){
String[] arr ={“A”,“B”,“C”,“D”};
for (int i =0;i< arr.length; i++){
System.out.print(arr[i]+ " ");
if (arr[i].equals(“C”)){
continue;
}
System.out.println(“Work done”);
break;
}
}
What is the result?
A. A B C Work done
B. A B C D Work done
C. A Work done
D. Compilation fails
Question 35(2)
Given the classes:
给定类别
Which option lists only those classes that belong to the unchecked exception category?
哪个选项仅列出那些属于未检查异常类别的类?
A. AssertionError, ArrayIndexOutOfBoundsException, ArithmeticException
B. AssertionError, IOError, IOException
C. ArithmeticException, FileNotFoundException, NumberFormatException
D. FileNotFoundException, IOException, SQLException
E. ArrayIndexOutOfBoundException, IllegalArgumentException, FileNotFoundException
Question 36(1)
Whizlabs wb = new Whizlabs();
System.out.print("name="+wb.name);
System.out.print(",pass ="+wb.pass);
}
10.}
What would be the output, if it is executed as a program?
如果以程序的形式执行,输出会是什么?
A. name =, pass =
B. name = null, pass = null
C. name = null, pass = false
D. name = null pass = true
E. Compile error.
Question 37(2)
Which of the following data types will allow the following code snippet to compile?
Float i =4;
Float j =2;
____z=i+j;
A. Long
B. Double
C. Int
D. Float
E. Byte
Question 38(1)
Which three statements describe the object-oriented features of the Java language?
哪三个语句描述了Java语言的面向对象特性?
A. Objects cannot be reused.对象不能重复使用。
B. A subclass can inherit from a superclass.子类可以从超类继承。
C. Objects can share behaviors with other objects.对象可以与其他对象共享行为…
D. A package must contain more than one class.一个包必须包含多个类
E. Object is the root class of all other objects.对象是所有其他对象的根类。
F. A main method must be declared in every class.每个类中都必须声明一个主方法。
Question 39(2)
Given the code fragment:
String[ ] strs = new String2;
int idx =0;
for (String s : strs) {
strs[idx].concat(“element” + idx);
idx++;
}
for (idx = 0; idx < strs.length; idx++){
System.out.println(strs[idx]);
}
What is the result?
A.Element 0
Element 1
B.Null element 0 Null element 1
C.Null Null
D.A NullPointerException is thrown at runtime
Question 40(2)
Given the code fragment:
/*code goes here*/
throw new IOException();
}
X xobj = new X();
Which two modifications should you make so that the code compiles successfully?为了使代码编译成功,您应该进行哪两项修改?
A)Replace line8 with
public static void main (String[] args )throws Exception{
B) Replace line 10 with;
try {
xobj.printFileContent();
}catch(Exception e){ }
C) Replace line2 with public void printFileContent() throws IOException{
D) Replace line4 with throw IOException(“Exception raised”);
E) At line 11,insert throw new IOException();
Question 41
You are asked to develop a program for a shopping application, and you are given the following information:
要求您为购物应用程序开发一个程序,并向您提供以下信息:
The application must contain the classes Toy, EduToy, and consToy. The Toy class is the superclass of the other two classes.
应用程序必须包含玩具、教育玩具和健身类。玩具类是另外两个类的超类。
The int caicuiatePrice (Toy t) method calculates the price of a toy.
国际玩具价格(Toy t)方法计算玩具的价格。
The void printToy (Toy t) method prints the details of a toy.
空白打印玩具(Toy t)方法打印玩具的细节。
Which definition of the Toy class adds a valid layer of abstraction to the class hierarchy?
玩具类的哪个定义给类层次结构增加了一个有效的抽象层?
A) public abstract class Toy{
public abstract int calculatePrice(Toy t);
public void printToy(Toy t){ /* code goes here*/ }
B) public abstract class Toy {
public int calculatePrice(Toy t) ;
public void printToy(Toy t) ;
c) public abstract class Toy{
public int calculatePrice(Toy t);
public final void printToy(Toy t){ /* code goes here * /}
D) public abstract class Toy{
public abstract int calculatePrice(Toy t) { /* code goes here /}
public abstract void printToy(Toy t){ / code goes here*/ }
Question 42(2)
Given:
Base.java:
class Base{
public void test(){
System.out.println(“Base”);
}
}
class DeriveredA extends Base{
public void test(){
System.out.println(“DerivedA”);
}
}
class DeriveredB extends DeriveredA{
public void test(){
System.out.println(" DeriveredB");
}
public static void main(String[] args) {
Base b1=new DeriveredB();
Base b2=new DeriveredA();
Base b3=new DeriveredB();
b1=(Base)b3;
Base b4= (DeriveredA) b3;
b1.test();
b4.test();
}
}
What is the result?
A. Base DerivedA
B. Base DerivedB
C. DerivedB DerivedB
D. DerivedB DerivedA
E. A classcast Except ion is thrown at runtime.
Question 43(1)
Given:
public class SumTest{
public static void doSum (Integer x, Integer y){
System.out.println(“Integer sum is” + (x+y));
}
public static void doSum(double x,double y){
System.out.println(“double sum is”+(x+y));
}
public static void doSum (float x,float y){
System.out.println(“float sum is”+(x+y));
}
public static void doSum (int x, int y){
System.out.println(“int sum is”+(x+y));
}
public static void main (String[] args){
doSum (10,20);
doSum(10.0,20.0);
}
}
What is the result?
A) int sum is 30
float sum is 30.0
B) int sum is 30
double sum is 30.0
C) Integer sum is 30
Double sum is 30.0
D) Integer sum is 30
Float sum is 30.0
Question 44(1)
Given:
public class Test2{
public static void doChange (int[] arr){
for(int pos=0; pos < arr.length: pos++){
arr[ pos]=arr[ pos]+1;
}
}
public static void main(String[] args)(
int[] arr =(10,20,30);
doChange(arr);
for(int x: arr){
System. out. print(x+", “);
}
doChange(new int []{arr[0],arr1,arr2});
System. out. print (arr[0]+”,"+arr[1l+","+arr2);
}
}
What is the result?
A. 11, 21, 31, 11, 21, 31
B. 11, 21, 31, 12, 22, 32
C. 12, 22, 32, 12, 22, 32
D. 10, 20, 30, 10, 20, 30
Question 45(1)
Given:
import java. utiLArrayList;
import java. utiLList;
public class Whizlabs{
public static void main(String[] args){
List list=new ArrayList<>();
list.add(21);
list.add(13);
List.add(30);
list.add(11);
list.removeIf( e -> e%2 != 0);
System. out. println(list);
}
}
What is the output?
A. [21, 13, 11]
B. [30]
C. []
D. Compilation fails due to error at line 7
E. Compilation tails due to error at line 10
Question 46(1)
Given:
class CD {
int r;
CD(int r) {
this.r=r;
}
}
class DVD extends CD {
int c;
DVD(int r, int c) {
// line n1
}
}
And given the code fragment:
DVD dvd = new DVD(10,20);
Which code fragment should you use at line n1 to instantiate the dvd object successfully?
您应该在n1行使用哪个代码片段来成功实例化dvd对象?
A)super.r=r;
this.c=c;
B)super®;
this©;
C)super®;
this.c=c;
D)this.c=r;
super©;
Question 47(1)
Given the following classes:
给定以下类别:
public class Employee {
public int salary ;
}
public class Manager extends Employee {
public int budget ;
}
public class Director extends Manager {
public int stockOptions ;
}
And given the following main method:
public static void main (String[] args) {
Employee employee = new Employee () ;
Manager manager = new Manager () ;
Director director = new Director () ; //line n1
}
Which two options fail to compile when placed at line n1 of the main method?
当放在主方法的n1行时,哪两个选项无法编译?
A. employee.salary = 50_000;
B. director.salary = 80_000;
C. employee.budget = 200_000;
D. manager.budget = 1_000_000;
E. manager.stockOption = 500;
F. director.stockOptions = 1_000;
Question 48(1)
Given the code fragment:
public static void main (string[] args){
short s1 = 200;
Integer s2 =4O0;
Long s3=(long) s1 + s2; //line n1
String s4 =(string) (s3*s2);// line n2
System. out. println ("Sum is " +s4);
}
What is the result?
A. Sum is 600
B. Compilation fails at line n1.
C. Compilation fails at line n2.
D. A ClassCastException is thrown at line n1.
E. A ClassCastException is thrown at line n2.
Question 49(1)
Which two are valid array declaration?
哪两个是有效的数组声明?
A. Object array[];
B. Boolean array3;
C. int[] array;
D. Float2 array;
Question 50(1)
Given the code fragment from three files:
给定来自三个文件的代码片段:
package sales;
public class SalesMan{ }
Product.java;
package sales.products;
public class Product { }
Market.java:
Which code fragment, when inserted at line 2, enables the code to compile?
当插入第2行时,哪一个代码片段使代码能够编译?
A)import sales.;
B)import java.sales.products.;
C)import sales;
import sales.products;
D)import sales.;
import products.;
E)import sales.;
import sales.products.;
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Question 51(1)
Given:
class MarksOutOfBoundsException extends IndexOutOfBoundsException { } public class GradingProcess {
void verify(int marks) throws IndexOutOfBoundsException {
if (marks > 100) {
throw new MarksOutOfBoundsException();
}
if (marks > 50) {
System.out.print(“Pass”);
} else { System.out.print(“Fail”);
}
}
public static void main(String[] args) {
int marks = Integer.parseInt(args2);
try {
new GradingProcess().verify(marks));
} catch(Exception e) {
System.out.print(e.getClass());
}
}
}
And the command line invocation: Java grading process 89 50 104 What is the result?命令行调用:Java分级过程89 50 104结果是什么?
A. Pass
B. Fail
C. Class MarketOutOfBoundsException
D. Class IndexOutOfBoundsException
E. Class Exception
Question 52(1)
Given:
class Star{
public void dostuff() {
System.out.println(“Twinkling star”);
}
}
interface Universe{
public void dostuff();
}
class Sun extends Star implements Universe{
public void dostuff() {
System.out.println(“Shining sun”);
}
}
public class Bob {
public static void main(String[] args) {
Sun obj2 = new Sun();
Star obj3 = obj2;
((Sun) obj3).dostuff();
((Star) obj2).dostuff();
((Universe) obj2).dostuff();
}
}
What is the result?
A. Shining Sun Shining Sun Shining Sun
B. Shining Sun Twinkling Star Shining Sun
C. Compilation fails
D. A ClassCastException is thrown at runtime
Question 53(2)
Given the following two classes:
public class Customer{
ElectricAccount acct = new ElectricAccount () ;
public void useElectricity (double kWh){
acct. addKWh (kIWh) ;}
}
public class ElectricAccount {
private double kWh;
private double rate = O. 07 ;
private double bill ;
/ /line n1
}
How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always equal to the value of the member variable kwh multiplied by the member variable rate?
您应该如何在n1行的ElectricAccount类中编写方法,以便成员变量清单始终等于成员变量kwh乘以成员变量比率的值?
Any amount of electricity used by a customer (represented by an instance of the customer class) must contribute to the customer’s bill (represented by the member variable bill) through the method useElectricity method. An instance of the customer class should never be able to tamper with or decrease the value of the member variable bill.
客户使用的任何电量(由客户类别的实例表示)都必须通过“使用电量”方法贡献给客户账单(由成员变量账单表示)。客户类的实例永远不能篡改或降低成员变量清单的值
A)public void addKWh(double kWh){
this.kWh +=kWh;
this.bill = this.kWhthis.rate;
}
B)public void addKWh(double kWh){
if(kWh>0){
this.kWh +=kWh;
this.bill = this.kWhthis.rate;
}
}
C)private void addKWh(double kWh){
if(kWh>0){
this.kWh +=kWh;
this.bill = this.kWhthis.rate;
}
}
D)public void addKWh(double kWh){
if(kWh>0){
this.kWh +=kWh;
setBill(this.KWh);
}
}
public void setBill(double kWh){
bill = kWhrate;
}
A. Option A
B. Option B
C. Option C
D. Option D
Question 54(1)
Given:
public class Test1 {
static void doubling (Integer ref, int pv) {
ref =20;
pv = 20;
}
public static void main(String[] args) {
Integer iObj = new Integer(10);
int iVar = 10;
doubling(iObj++, iVar++);
System.out.println(iObj+ ", "+iVar);
What is the result?
A. 11, 11
B. 10, 10
C. 21, 11
D. 20, 20
E. 11, 12
Question 55(1)
Given:
class Patient{
String name;
public Patient(String name){
this.name = name;
}
}
public class Test15 {
public static void main(String[] args) {
List ps = new ArrayList();
Patient p2 = new Patient(“Mike”);
ps.add(p2);
if(f>=0){
System.out.println(“Mike Found”);
}
}
}
Which code fragment, when inserted at line 14, enables the code to print Mike Found?当插入第14行时,哪一个代码片段使代码能够打印“找到迈克”?
A. int f = ps.indexOf {new patient (“Mike”)};
B. int f = ps.indexOf (patient(“Mike”));
C. patient p = new Patient (“Mike”); int f = pas.indexOf§
D. int f = ps.indexOf(p2);
Question 56(1)
Given:
public class Test {
public static void main(String[] args) { try {
String[] arr =new String4;
arr1 = “Unix”;
arr2 = “Linux”;
arr3 = “Solarios”;
for (String var : arr) {
System.out.print(var + " ");
}
} catch(Exception e) { System.out.print (e.getClass());
}
}
}
What is the result?
A. Unix Linux Solaris
B. Null Unix Linux Solaris
C. Class java.lang.Exception
D. Class java.lang.NullPointerException
Question 57(1)
Given:
class SpecialException extends Exception{
public SpecialException(String message){
super(message);
System.out.println(message);
}
}
public class ExceotionTest {
public static void main(String[] args) {
try {
doSomething();
}
catch (SpecialExceptions e){
System.out.println(e);
}
}
static void doSomething() throws SpecialException{
int[] ages = new int4;
ages4 = 17;
doSomethingElse();
}
static void doSomethingElse() throws SpecialException {
throw new SpecialException("Thrown at the end of doSomething method");
}
}
What will be the output?
A)SpecialException: Thrown at end of doSometing() method
B)Error in thread “main” java.lang.ArrayIndexOutOfBoundsError;
C)Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:4
at ExceptionTest.doSomething(ExceptionTest.java:13)
at ExceptionTest.main(ExcetionTest.java:4)
D)SpecialException: Thrown at end of doSomething() method
at ExceptionTest.doSomethingElse(ExceptionTest.java:16)
at ExceptionTest.doSomething(ExceptionTest.java:13)
at ExceptionTest.main(ExceptionTest.java:4)
A. Option A
B. Option B
C. Option C
D. Option D
Question 58
Given:
public class TestOperator {
public static void main(String[] args) {
int result = 30 -12 / (2*5)+1;
System.out.print("Result = " + result);
}
}
What is the result?
A. Result = 2
B. Result = 3
C. Result = 28
D. Result = 29
E. Result = 30
Question 59
Given the code fragment:
int b = 3;
if ( !(b > 3)) {
System.out.println("square ");
}
{
System.out.println("circle ");
}
System.out.println("…");
What is the result?
A. square …
B. circle …
C. square circle …
D. Compilation fails.
Question 60(1)
Given the code fragment:
public class ForTest {
public static void main(String[] args) {
int[] array = {1, 2, 3};
for ( foo ) {
}
}
Which three code fragments, when replaced individually for foo, enables the program to compile?
哪三个代码片段,当被foo单独替换时,使程序能够编译?
A. int i : array
B. int i = 0; i < 1;
C. ; ;
D. ; i < 1; i++
E. i = 0; i<1;
Question 61
Which two statements are true for a two-dimensional array of primitive data type?对于原始数据类型的二维数组,哪两种说法是正确的?
A. It cannot contain elements of different types.它不能包含不同类型的元素
B. The length of each dimension must be the same.每个维度的长度必须相同。
C. At the declaration time, the number of elements of the array in each dimension must be specified.在声明时,必须指定每个维度中数组的元素数量。
D. All methods of the class object may be invoked on the two-dimensional array类对象的所有方法都可以在二维数组上调用
Question 62
Given:
import java.util.*;
public class Ref {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder(“Hello Java!”);
String s2 = s1.toString();
List lst = new ArrayList();
lst.add(s2);
System.out.println(s1.getClass());
System.out.println(s2.getClass());
System.out.println(lst.getClass());
}
}
What is the result?
A. class java.lang.String class java.lang.String class java.util.ArrayList
B. class java.lang.Object class java.lang. Object class java.util.Collection
C. class java.lang.StringBuilder class java.lang.String
class java.util.ArrayList
D. class java.lang.StringBuilder class java.lang.String
class java.util.List
Question 63
Given the code from the Greeting.Java file:
给定Greeting.Java文件中的代码:
Public class Greeting{
Public static void main (String [ ] args){
System.out.println(“Hello”+args[0]);
}
}
Which set of commands prints Hello Duke in the console?
哪组命令在控制台中打印Hello Duke?
A) javac Greeting
java Greeting Duke
B) javac Greeting.java Duke
java Greeting
C) javac Greeting.java
java Greeting Duke
D) javac Greeting.java
java Greeting.class Duke
A. Option A
B. Option B
C. Option C
D. Option D
Question 64
Given:
public class MainMethod {
void main()
{ System.out.println(“one”);
}
static void main(String args)
{ System.out.println(“two”);
}
public static void main(String[] args)
{ System.out.println(“three”);
}
void mina(Object[] args) { System.out.println(“four”);
}
}
What is printed out when the program is excuted?
当程序被执行时,打印出什么?
A. one
B. two
C. three
D. four
Question 65
Given:
public class Test {
public static void main(String[] args) {
int arr[] = new int4;
arr[0] = 1;
arr1 = 2;
arr2 = 4;
arr3 = 5;
int sum = 0;
try {
for (int pos = 0; pos <= 4; pos++) { sum = sum + arr[pos];
}
} catch (Exception e) { System.out.println(“Invalid index”);
}
System.out.println(sum);
}
}
What is the result?
A. 12
B. Invalid Index 12
C. Invalid Index
D. Compilation fails
Question 66
Given the code fragment:
class Student{
String name;
Int age;
}
And,
Which statement is true?哪个陈述是正确的?
A.After line 8, three objects are eligible for garbage collection
在第8行之后,三个对象有资格进行垃圾收集
B. After line 8, two objects are eligible for garbage collection在第8行之后,两个对象有资格进行垃圾收集
C. After line 8, one object is eligible for garbage collection在第8行之后,一个对象有资格进行垃圾收集
D. After line 8, none of the objects are eligible for garbage collection
在第8行之后,没有一个对象符合垃圾收集的条件
Question 67
Given:
public class Test { static boolean bVar;
public static void main(String[] args) {
boolean bVar1 = true; int count =8;
do {
System.out.println("Hello Java! " +count); if (count >= 7) {
bVar1 = false;
}
} while (bVar != bVar1 && count > 4); count -= 2;
}
}
What is the result?
A. Hello Java! 8 Hello Java! 6 Hello Java! 4
B. Hello Java! 8 Hello Java! 6
C. Hello Java! 8
D. Compilation fails
Question 68
Given:
public class Test2{
public static void main(String[] args){
int ar1[] = {2,4,6,8};
int ar2[] = {1,3,5,7,9};
ar2 = ar1;
for(int e2 : ar2){
System.out.print(" " + e2);
}
}
}
What is the result?
A. 2 4 6 8
B. 2 4 6 8 9
C. 1 3 5 7
D. 1 3 5 7 9
Question 69
Given the code fragment:
public static void main(String[] args) {
boolean opt = true;
switch (opt) {
case true;
System.out.print(“True”);
break;
default:
System.out.print("***");
}
System.out.println(“Done”);
}
Which modification enables the code fragment to print TrueDone?
哪种修改使代码片段能够打印TrueDone?
A. Replace line 5 With String result = “true”; Replace line 7 with case “true”:
B. Replace line 5 with boolean opt = l;
Replace line 7 with case 1=
C. At line 9, remove the break statement.
D. Remove the default section.
Question 70
Given the code fragment:
List colors = new ArrayList();
colors.add(“green”);
colors.add(“red”);
colors.add(“blue”);
colors.add(“yellow”);
colors.remove(2);
colors.add(3,“cyan”);
System.out.print(colors);
What is the result?
A. [green, red, yellow, cyan]
B. [green, blue, yellow, cyan]
C. [green, red, cyan, yellow]
D. Am IndexOutOfBoundsException is thrown at runtime
Question 71
Given the following four Java file definitions:给定以下四个Java文件定义
// Foo.java package facades;
public interface Foo { }
// Boo.java package facades;
public interface Boo extends Foo { }
// Woofy.java package org.domain
// line n1
public class Woofy implements Boo, Foo { }
// Test.java package.org;
// line n2
public class Test {
public static void main(String[] args) { Foo obj=new Woofy();
Which set modifications enable the code to compile and run?哪些集合修改使代码能够编译和运行?
A.At line n1, Insert: import facades;
At line n2, insert: import facades;
import org.domain;
B.At line n1, Insert: import facades.;
At line n2, insert: import facades;
import org.;
C.At line n1, Insert: import facades.;
At line n2, insert: import facades.Boo;
import org.;
D.At line n1, Insert: import facades.Foo, Boo;
At line n2, insert: import org.domain.Woofy;
E.At line n1, Insert: import facades.;
At line n2, insert: import facades.;
import org.domain.Woofy;
Question 72(1)
Given the code fragment:
StringBuilder sb = new StringBuilder ( ) ;
Sb.append (“world”);
Which code fragment prints Hello World?
A sb.insert(0,"Hello "); System.out.println(sb);
B sb.append(0,"Hello "); System.out.println(sb);
C sb.add(0,"Hello "); System.out.println(sb);
D sb.set(0,"Hello "); System.out.println(sb);
Question 73(2)
Given the fragments:
public class TestA extends Root {
public static void main(String[] args) {
Root r = new TestA();
System.out.println(r.method1()); // line n1
System.out.println(r.method2()); // line n2
}
}
class Root {
private static final int MAX = 20000;
private int method1() {
int a = 100 + MAX;
return a;
}
protected int method2() {
int a = 200 + MAX;
return a;
}
}
Which line causes a compilation error?
A. Line n1
B. Line n2
C. Line n3
D. Line n4
Question 74
Given:
public static void main (String[] args) {
String[] [] chs = new String2;
chs[0] = new String2;
chs1 = new String[5];
int i = 97;
for (int a=0;a
i++;
}
}
for(String[] ca : chs) {
for(String c : ca) {
System.out.println(c + " ");
}
System.out.println();
}
}
}
What is the result?
A. 97 98
99 100 null null null
B. 91 98
99 100 101 102 103
C. Compilation rails.
D. A NullPointerException is thrown at runtime.
E. An ArraylndexOutOfBoundsException is thrown at runtime.
Question 75(2)
Given the code fragment:
public class Test{
void readCard (int cardNo)throws Exception{
System.out.println(“Reading Card”);
}
void checkCard(int cardNo) throws RuntimeException{
//line n1
System.out.println(“Checking Card”);
}
public static void main(String[] args) {
Test ex=new Test();
int cardNo=12344;
ex.checkCard(cardNo); //line n2
ex.readCard(cardNo); //line n3
}
}
What is the result?
A. Reading Card Checking Card
B. Compilation fails only at line n1.
C. Compilation fails only at line n2.
D. Compilation fails only at line n3.
E. Compilation fails at both line n2 and line n3.
Question 76
Given the code fragment
class Test2{
int fvat;
static int cvar;
public static void main(String[] args) {
Test2 t=new Test2();
//insert code here to write field variables
}
}
Which code fragments, inserted independently, enable the code compile?
哪些独立插入的代码片段支持代码编译?
A. t.fvar = 200;
B. cvar = 400;
C. fvar = 200;
cvar = 400;
D.this.fvar = 200;
this.cvar = 400;
E. t.fvar = 200;
Test2.cvar = 400;
F.this.fvar = 200;
Test2.cvar = 400;
Question 77(2)
Given:
public class Test{
static void dispResult(int [] num) {
try {
System.out.println(num1/(num1-num2));
}catch (ArithmeticException e) {
System.out.println("first exception");
}
System.out.println("Done");
}
public static void main(String[] args) {
try {
int[] arr= {100,100};
dispResult(arr);
}catch (IllegalArgumentException e) {
System.err.println(“second exception”);
}catch (Exception e) {
System.err.println(“third exception”);
}
}
}
What is the result?
A. 0 Done
B. First Exception Done
C. Second Exception
D. Done
Third Exception
E. Third Exception
Question 78
Given:
abstract class A1 {
public abstract void m1();
public void m2() { System.out.println(“Green”); }
}
abstract class A2 extends A1 { public abstract void m3();
public void m1() { System.out.println(“Cyan”); }
public void m2() { System.out.println(“Blue”); }
}
public class A3 extends A2 {
public void m1() { System.out.println(“Yellow”); }
public void m2() { System.out.println(“Pink”); }
public void m3() { System.out.println(“Red”); }
public static void main(String[] args) {
A2 tp = new A3();
tp.m1();
tp.m2();
tp.m3();
}
}
What is the result?
A. Yellow Pink Red
B. Cyan Blue Red
C. Cyan Green Red
D. Compilation Fails
Question 79(2)
Given:
class A {
public A(){
System.out.print("A ");
}
}
class B extends A{
public B(){ //line n1
System.out.print("B ");
}
}
class C extends B{
public C(){ //line n2
System.out.print("C ");
}
public static void main(String[] args){
C c = new C();
}
}
What is the result?
A. C B A
B. C
C. A B C
D. Compilation fails at line n1 and line n2
Question 80(1)
Given the code fragment:
String shirts[][] = new String2;
shirts[0][0] = “red”;
shirts0 = “blue”;
shirts[1][0] = “small”;
shirts1 = “medium”;
Which code fragment prints red: blue: small: medium?
哪个代码片段打印红色:蓝色:小型:中型?
A) for (int index = 1; index < 2; index++) {
for (int idx = 1; idx < 2; idx++) {
System.out.print(shirts[index][idx] + “:”);
}
}
B)for (int index = 0; index < 2; index++) {
for (int idx = 0; idx < index; ++idx) {
System.out.print(shirts[index][idx] + “:”);
}
}
C)for (String c : color) {
for (String s : sizes) {
System.out.print(s + “:”);
}
}
D)for (int index = 0; index < 2;) {
for (int idx = 0; idx < 2;) {
System.out.print(shirts[index][idx] + “:”);
idx++;
}
index++;
}
A. Option A
B. Option B
C. Option C
D. Option D
Question 81(1)
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:给定代码片段:
4.Product p1 = new Product(101, “pen”);
5.Product p2 = new Product(101, “pen”);
6.Product p3 = p1;
7.boolean ans1 = p1 == p2;
8.boolean ans2 = p1.name.equals(p2.name);
9.System.out.print(ans1 + “:” + ans2);
What is the result?
A. true:true
B. true:false
C. false:true
D. false:false
Question 82
Given:
Public class MyFor1{
Public static void main(String[ ] args){
int [ ] x ={6,7,8};
for(int i : x ){
System.out.print(i+” ”);
i++;
}
}
}
What is the result?
A. 6 7 8
B. 7 8 9
C. 0 1 2
D. 6 8 10
E. Compilation fails
Question 83
Given:
public class Test {
public static void main(String[] args) {
int ax = 10, az = 30;
int aw = 1, ay = 1;
try {
aw = ax % 2; ay = az / aw;
} catch (ArithmeticException e1) {
System.out.println(“Invalid Divisor”);
} catch (Exception e2) {
aw = 1;
System.out.println(“Divisor Changed”);
}
ay = az /aw; // Line 14
System.out.println("Succesful Division " + ay);
}
}
What is the result?
A. Invalid Divisor Divisor Changed Successful Division 30
B. Invalid Divisor Successful Division 30
C. Invalid Divisor
Exception in thread “main” java.lang.ArithmeticException: / by zero at test.Teagle.main(Teagle.java:14)
D. Invalid Divisor
Exception in thread “main” java.lang.ArithmeticException: / by zero at test.Teagle.main(Teagle.java:14)
Successful Division 1
Question 84(1)
Given the code fragment:
7.StringBuilder sb1 = new StringBuilder(“Duke”);
8.string str1 =sb1.toString();
Which code fragment, when inserted at line 9, enables the code to print true?当插入第9行时,哪一个代码片段使代码能够打印为真?
String str2 = str1;
String str2 = new String (str1);
String str2 = sb1. toString ();
String str2 = “Duke”;
Question 85
Given:
public class App {
String myStr = “7007”;
public void doStuff(String str){
int myNum = 0;
try {
String myStr =str;
myNum = Integer.parseInt(myStr);
}catch(NumberFormatException ne ){
System.err.println("Error");
}
System.out.println(
"myStr: " + myStr + ", myNum: " + myNum);
}
public static void mian(String[] args){
App obj = new App();
obj.doStuff("9009");
}
}
What is the result?
A. myStr:9009, myNum:9009
B. myStr:7007, myNum:7007
C. myStr:7007, myNum:9009
D. Compilation fails
Question 86(2)
Given:
1 public class Whizlabs{
2 public static void main(String[] args){
3 StringBuilder sb = new StringBuilder(“1Z0”);
4 sb.concat("-808");
5 System.out.println(sb);
6 }
7 }
What is the output?
A.1Z0
B.1Z0-808
C.An exception will be thrown.
D. Compilation fails to error at line 3
E. Compilation fails to error at line 4
Question 87(2)
Given the code fragment:
9. int a =-10;
10.int b = 17;
11.int c = expression1;
12.int d = expression2;
13.c++;
14.d–;
15.System.out.println(c + “,” + d);
What could expression1 and expression2 be,respectively,in order to produce output -8,16?
为了产生输出-8,16,表达式1和表达式2分别是什么?
A.++a,–b
B.++a,b–
C.A++,–b
D.A++,b–
Question 88(1)
Given:
class X{
public void mX(){
System.out.println(“Xm1”);
}
}
class Y extends X{
public void mX(){
System.out.println(“Xm2”);
}
public coid mY(){
System.out.println(“Ym”);
}
}
public class Test{
public static void main(String[] args){
X xref = new Y();
Y yref = (Y) xref;
yref.mY();
xref.mX();
}
}
A.Ym
Xm2
B.Ym
Xm1
C.Compilation fails
D.A ClassCastException is thrown at runtime
Question 89
Given:
class Alpha{
int ns;
static int s;
Alpha(int ns){
if(s
this.ns=ns;
}}
void doPrint(){
System.out.println(“ns=”+ns+“s=”+s);
}}
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 this 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=125
ns=125s=125
ns=100 s=100
D ns=50 s=50
ns=125 s=125
ns=0 s=125
Question 90
Which two are java Exception classes?那两个为java异常类?
A SercurityException安全异常
B DuplicatePathException 复制异常
C IllegalArgumentException 非法异常
D TooManyArgumentsException 参数过多异常
Question 91
public static void main(String[]args) {
String [] [] arr= {{“A”,“B”,“C”},{“D”,“E”}};
for(int i=0;i
if(arr[i][j]).equals(“B”)){
break;
}
}
continue;
}
}
A. A B C
B. A B C D E
C. A B D E
D. Complilaton fails.
Question 92
Given:
Public class FieldInit{
char c;
boolean b;
float f;
void printAll(){
System.out.println(“c=”+c);
System.out.println(“b=”+b);
System.out.println(“f=”+f);
}
public static void main(String[] args){
FieldInit f=new FieldInit();
f.printAll();
}
}
A c=null
b=false
f=0.0F
B c=0
b=false
f=0.0f
C c=null
b=true
f=0.0
D
c=
b=false
f=0.0
Question 93(1)
Given the code fragment:
String color = “teal”;
switch (color) {
case “Red”:
System.out.println(“Found Red”);
case “Blue”:
System.out.println(“Found Blue”);
case “Teal”:
System.out.println(“Found Teal”);
break;
default:
System.out.println(“Found Default”):
}
What is the result?
A. Found Red
Found Default
B. Found Teal
C. Found Red
Found Blue
Found Teal
D. Found Red
Found Blue
Found Teal
Found Default
E. Found Default
Question 94
Given the code in a file Traveler.java:
给定文件中的代码Traveler.java
class Tour{
public static void main (String[] args){
System.out.println(“Happy Journey!” + args1);
}
}
public class Traveler{
public static void main (String[] args){
Tours.main(args);
}
}
And the commands:
Javac Traveler.java
Java Traveler Java Duke
What is the result?
A. Happy Journey! Duke
B. Happy Journey! Java
C. An exception is thrown at runtime
D. The program fails to execute due to a runtime error
Question 95
Given:
1.import java.util.ArrayList;
2.import java.util.List;
3.
4.public class Whizlabs{
5.
6.public static void main(String[]args){
7.Listlist= new ArrayList<>();
8.list.add(21);list.add(13);
9.list.add(30);list.add(11);
10.list.add(2);
11//insert here
12.System.out.ptintln(list);
13.}
14.}
Which inserted at line 11, will provide the following output?
插入第11行,将提供以下输出。
[21, 13, 11]
A. list.removelf(e > e%2 != 0);
B. list.removelf(e -> e%2 != 0);
C. list.removelf(e -> e%2 == 0);
D. list.remove(e -> e%2 == 0);
E. None of the above.
Question 96(1)
Given the code fragment:
public static void main(String[] args) {
ArrayList List = new ArrayList<>();
List.add("SE");
List.add("EE");
List.add("ME");
List.add("SE");
List.add("EE");
List.remove("SE");
System.out.println("Values are :" + List);
}
What is the result?
A. Values are : [EE, ME]
B. Values are : [EE, EE, ME]
C. Values are : [EE, ME, EE]
D. Values are : [SE, EE, ME, EE]
E. Values are : [EE, ME, SE, EE]
Question 97
Given:
class Vehicle{
String type = “4w”;
int maxSpeed = 100;
Vehicle(String type ,int maxSpeed){
this.type = type ;
this.maxSpeed = maxSpeed;
}
}
class Car extends Vehicle{
String trans ;
Car (String trans){ // line n1
this.trans = trans ;
}
Car(String type,int maxSpeed,String trans){
super(type,maxSpeed);
this(trans); //line n2
}
}
}
And given the code fragment:
Car c1 = new Car (“Auto”);
Car c2 = new Car(“4w”,150,“Manual”);
System.out.println(c1.type+" “+ c1.maxSpeed + " " + c1.trans);
System.out.println(c2.type+” “+ c2.maxSpeed +” " + c2.trans);
What is the result?
A. 4W 100 Auto
4W 150 Manual
B. Null 0 Auto
4W 150 Manual
C. Compilation fails only at line n1
D. Compilation fails only at line n2
E. Compilation fails at both line n1 and line n2
Question 98(1)
Given the code fragment:
public class Test {
public static void main(String[] args) {
// line n1
switch(x){
case 1:
System.out.println(“One”);
break;
case 2:
System.out.println(“Two”);
break;
}
}
}
Which three code fragments can be independently inserted at line nl to enable the code toprint one?哪三个代码片段可以独立地插入nl行,以使代码能够 打印一张?
A. Byte x = 1;
B. short x = 1;
C. String x = “1”;
D. Long x = 1;
E. Double x = 1;
F. Integer x = new Integer (“1”);
Question 99
Given:
public static void main(String[] args) {
String ta = "A ";
ta = ta.concat("B ");
String tb = "C ";
ta =ta.concat(tb);
ta.replace(‘C’,‘D’);
ta =ta.concat(tb);
System.out.println(ta);
}
What is the result?
A. A B C D
B. A C D
C. A B C
D. A B D
E. A B D C
F. A B C C
Question 100
Given:
public class TestField {
int x;
int y;
public void doStuff(int x, int y) {
this.x = x;
y =this.y;
}
public void display() {
System.out.print(x + " " + y + " : ");
}
public static void main(String[] args) {
TestField m1 = new TestField();
m1.x = 100;
m1.y = 200;
TestField m2 = new TestField();
m2.doStuff(m1.x, m1.y);
m1.display();
m2.display();
}
}
What is the result?
A. 100 200 : 100 200
B. 100 0 : 100 0 :
C. 100 200 : 100 0 :
D. 100 0 : 100 200 :
Question 101(2)
public class App {
// Insert code here
System.out.print(“Welcome to the world of Java”);
}
}
Which two code fragments, when inserted independently at line // Insert code here, enable the program to execute and print the welcome message on the screen?哪两个代码片段在第//行独立插入时启用 在屏幕上执行和打印欢迎信息的程序?
A. static public void main (String [] args) {
B. static void main (String [] args) {
C. public static void Main (String [] args) {
D. public static void main (String [] args) {
E. public void main (String [] args) {
Question 102(2)
Given the code fragment:
public class Test {
public static void main(String[] args) {
boolean isChecked = false;
int arry[] = {1,3,5,7,8,9};
int index = arry.length;
while ( ) {
if (arry[index-1] % 2 ==0) {
isChecked = true;
}
}
System.out.print(arry[index]+", "+isChecked);
}
}
Which set of changes enable the code to print 1, true?哪组更改使代码能够打印1,true
A. Replacing with index > 0 and replacing with index–;
B. Replacing with index > 0 and replacing with --index;
C. Replacing with index > 5 and replacing with --index ;
D. Replacing with index and replacing with --index ;
Question 103
Which of the following can fill in the blank in this code to make it compile?
下列哪一项可以填补这段代码中的空白以使其编译?
public class Exam{
void method(){}
}
public class OCAJP extends Exam{
____void method(){}}
A. abstract
B. final
C. private
D. default
E.int
Question 104
Given the code fragment:
int x = 100;
int a = x++; a=100 X=101
int b = ++x; b=102 x=102
int c = x++; c=102 x=103
int d = (a < b) ? (a < c) ? a: (b < c) ? b: c;
System.out.println(d) ;
What is the result?
A.100
B.101
C.102
D.103
E. Compilation fails
Question 105
Given:
package ocp808;
public class Question105 {
public static void main(String[] args){
int x = 100;
int a = x++;
int b = ++x;
int c = x++;
int d = (a < b) ? (a < c) ? a: (b < c) ? b: c;
System.out.println(d) ;
}
}
Question 106
Given:
class X{
int x1, x2, x3;
}
class Y extends X {
int y1;
Y() {
x1 = 1;
x2 = 2;
y1 = 10;
}
}
class Z extends Y {
int z1;
Z() {
x1 = 3;
y1 = 20;
z1 = 100;
}
}
And,
public class Test3{
public static void main(String[] args){
z obj = new Z();
System.out.println(obj.x3 + “,” + obj.y1 + “,” + obj.z1);
}
}
Which constructor initializes the variable x3?哪个构造函数初始化变量x3?
A.Only the default constructor of class X
B.Only the no-argument constructor of class Y
C.Only the no-argument constructor of class Z
D.Only the default constructor of object class
Question 107
Given:
public class Msg {
public static String doMsg (char x) {
return “Good Day!”;
}
public static String doMsg (int y) {
return "Good Luck! ";
}
public static void main (String[] args){
char x= 8;
int z = ‘8’;
System.out.println(doMsg (x));
System.out.print (doMsg (z)) ;
}
}
What is the result?
A.Good Day!
Good Luck!
B.Good Day!
Good Day!
C.Good Luck!
Good Day!
D.Good Luck!
Good Luck!
E.Compilation fails
Question 108
Given the code fragment?
public class Test {
public static void main(String[] args)
{ Test t = new Test();
int[] arr = new int[10];
arr = t.subArray(arr,0,2);
}
// insert code here
}
Which method can be inserted at line // insert code here to enable the code to compile?哪种方法可以在第//行插入代码以使代码能够编译?
A. public int[] subArray(int[] src, int start, int end) { return src;
}
B. public int subArray(int src, int start, int end) { return src;
}
C. public int[] subArray(int src, int start, int end) { return src;
}
D. public int subArray(int[] src, int start, int end) { return src;
}
Question 109
Given:
public class MarkList{
int num;
public static void graceMarks(MarkList obj4) {
obj4.num+=10;
}
public static void main(String[] args) {
MarkList obj1=new MarkList();
MarkList obj2=obj1;
MarkList obj3=null;
obj2.num=60;
graceMarks(obj2);
}
}
How many MarkList instances are created in memory at runtime?
运行时在内存中创建了多少个标记列表实例?
A . 1
B . 2
C . 3
D . 4
Question 110
Which usage reprensents a valid way of compling java source file with the name" Main"?哪种用法代表了一种用名称“Main”编译java源文件的有效方法?
A. javac Main.java
B. java Main.class
C.java Main.java
D.javac Main
E.java Main
Question 111
Given:
interface Readable{
public void readBook();
public void setBookMark();
}
abstract class Book implements Readable{ //line n1
public void readBook() {}
//line n2
}
class EBook extends Book{ //line n3
public void readBook() {}
//line n4
}
which option enables the code to compile?哪个选项使代码能够编译?
A) Replace the code fragment at line n1 with :
class Book implements Readable{
B) At line n2 insert
public abstrace void setBookMark();
C) Replace the code fragment at line n3 with:
abstract class EBook extends Book{
D) At line n4 insert :
public void setBookMark(){}
A. option A
B. option B
C. option C
D. option D
Question 112
Given the code fragment:
int x=5;
while(isAvailable(x)) {
System.out.println(x);
}
}
public static boolean isAvailable(int x) {
return x>0 ? true : false;
}
which modification enables the code to print 54321?哪种修改使代码能够打印54321?
A. Replace line 6 with System.out.print(–x);
B. At line 7 insert x–;
C. Replace line 6 with --x; and at line 7 insert sysytem.out.print(x);
D. Replace line 12 with return (x>0) ? false : true;
Question 113
Given:
class Caller {
private void init() {
System.out.println(“Inintialized”);
}
public void start() {
init();
System.out.println(“Started”);
}
}
public class Testcall {
public static void main(String[] args) {
Caller c = new Caller();
c.start();
c.init();
}
}
What is the result?
A. Initialized
Started
B. Initialized
Started
Initialized
C. Compilation fails
D. An exception is thrown at runtime
Question 114
Given:
public class APP {
public static void main(String[] args) {
int i = 10;
int j = 20;
int k = j += i / 5;
System.out.println(i + " : " + j + " : " + k);
}
}
What is the result?
A. 10 : 22 : 20
B. 10 : 22 : 22
C. 10 : 22 : 6
D. 10 : 30 : 6
Question 115
Given the fragment:
What is the result?
A. 13480.0
B. 13480.02
C. Compilation fails
D. An exception is thrown at runtime
Question 116
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将day的类型更改为字符串
E. Arranging the case labels in ascending order按升序排列案例标签
Question 117
Which three statements are true about the structure of a Java class?
关于一个Java类的结构,哪三个陈述是正确的?
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. 这些字段在使用前不需要初始化。
Question 118
Given:
class Base {
public static void main(String[] args) {
System.out.println("Base " + args2);
}
}
public class Sub extends Base{
public static void main(String[] args) {
System.out.println("Overriden " + args1);
}
}
And the commands:
javac Sub.java
java Sub 10 20 30
What is the result?
A. Base 30
B. Overridden 20
C. Overridden 20
Base 30
D. Base 30
Overridden 20
Question 119
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.println(sum);
}
}
What is the result?
//结果是什么
A. 20
B. 25
C. 29
D. Compilation fails
E. AnArrayIndexOutOfBoundsException is thrown at runtime
Question 120
import java.time.LocalDate;
import java.time.Period;
public class Whizlabs {
public static void main(String[] args) {
LocalDate date=LocalDate.of(2015,3,26);
Period p=Period.ofDays(1);
System.out.println(date.plus§);
}
}
What is the output?
//输出结果是什么
A. 2015-03-27
B. 2015-04-27
C. 2015-02-27
D. Compilation fails due to error at line 6.
E. Compilation fails due to error at line 8.
Question 121
public class X {
public static void main(String[] args) {
String theString =“HelloWorld”;
System.out.println(theString.charAt(11));
}
}
What is the result?
A. The program prints nothing
B.d
C A StringIndexOutOfBoundsException is thrown at runtime.
D An ArrayIndexOutOfBoundsException is thrown at runtime.
E A nullpointException is thrown at runtime
Question 122
int row =10;
for( ; row>0;){
int col=row;
while(col>=0){
System.out.println(col+" ");
col-=2;
}
row=row/col;
}
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
Question 123(题目不明确)
A.Allows a class implementation to change without changing t he clients
//允许在不更改客户机的情况下更改实现类
B.Protects confidential data from leaking out of the objects
//保护机密数据不泄漏出对象
C.Prevents code from causing exceptions
//防止代码引起异常
D.Enables the class implementation to protect its invariants
//使类实现能够保护其不变量
E.Permits classes to be combined into the same package
//允许将类合并到同一个包中
F.Enables multiple instances of the same class to be created safely
//允许安全创建同一个类的多个实例
(未验证)答案:ABD
Question 124
public static void main(String[] args) {
float [] myarray={10.20f,20.30f,30.40f,50.60f};
int index =0;
boolean isFound=false;
float key=30.40f;
System.out.println(isFound);
}
Which code fragment,when inserted at line 7 ,enables the code print ture?
//在输出前加上下列哪组代码时,能打印true
A. while(key==myarray[index++]){
isFound=true;
}
B. while (index <=4){
if(key ==myarray[index]){
index++;
isfound=ture;
break;
}
}
C. while (index ++<5){
if(key ==myarray[index]){
isfound=ture;
break;
}
}
D. while (index <5){
if(key ==myarray[index]){
isfound=ture;
break;
}
index++;
}
Question 125
Given:
public class TestScope{
public static void main(String[] args) {
int var1 = 200;
System.out.println(doCalc(var1));
System.out.println(" "+var1);
}
static int doCalc(int var1){
var1 = var1*2;
return var1;
}
}
What is the result?
A.400 200
B.200 200
C.400 400
D.Compilation fails
Question 126
public class StringReplace{
public static void main(String[] args) {
String message = “Hi everyone!”;
System.out.println("message = "+message.replace(“e”,“X”));
}
}
What is the result?
A.message = Hi everyone!
B.message = Hi XvXryonX!
C.A compile time error is produced
D.A runtime error is produced
E.message =
F.message = Hi Xveryone!
Question 127
Which two are benefits of polymorphism? 多态的好处
A. Faster code at runtime(代码运行时更快)
B. More efficient code at runtime(运行时更高效)
C. More dynamic code at runtime(动态运行)
D. More flexible and reusable code(更灵活和更简洁的代码)
E. Code that is protected from extension by other classes(保护其他类扩展的代码)
Question 128
Given:
public class Series{
private boolean flag;
public void displaySeries(){
int num =2;
while(flag){
if(num % 7 == 0)
flag = false;
System.out.println(num);
num +=2;
}
}
public static void main(String[] args) {
new Series().displaySeries();
}
}
What is the result?
A. 2 4 6 8 10 12
B. 2 4 6 8 10 12 14
C. Compilation fails//编译失败
D. The program prints multiple of 2 infinite times//该程序打印2个无限次的倍数
E. The program prints nothing//程序什么也没打印
Question 129
Given:
Test.java
public class Test{
public static void main(String[] args){
Integer num = Integer.parseInt(args1);
System.out.println("Number is : " + num);
}
}
And the commands:
Javac Test.java
Java Test 12345
What is the result?
A:Number us:12345
B:A NullPointerException is thrown at runtime//在运行时抛出空指针异常
C:A NumberFormatException is thrown at runtime//运行时抛出数字超出范围异常
D:An ArrayIndexOutOfBoundException is thrown at runtime.//运行时抛出数组下标越界异常.
Question 130
Given the code format:
//给定代码格式
1 class DBConfiguration {
2 String user;
3 String password; }
4 public class DBHandler{
5 DBConfiguration configureDB(String uname,String password){
6 // insert code here
7 }
8 public static void main(String[] args) {
9 DBHandler r = new DBHandler();
10 DBConfiguration dbConf = r.configureDB(“manger”, “manger”);
11 }
12 }
Which code fragment must be inserted at line 6 to enable the code to compile?
//必须在第6行插入哪个代码片段才能编译代码
A. DBConfiguration f;
return f;
B. return DBConfiguration;
C. return new DBConfiguration;
D. retutn 0;
E. DBConfiguration f=new DBConfiguration(); return f;
F. return new DBConfiguration();
Question 131
Given:
interface Pet { }
class Dog implements Pet { }
public class Beagle extends Dog{ }
Which three are valid?
//下面哪三个是有效的?
A. Pet a = new Dog();
B. Pet b = new Pet();
C. Dog f = new Pet();
D. Dog d = new Beagle();
E. Pet e = new Beagle();
F. Beagle c = new Dog(); //incompatible type. Required Beagle, found Dog.
Question 132
Consider following method//考虑以下方法
default void print(){
}
Which statement is true?//哪个陈述是正确的?
A. This method is invalid.//这个方法无效
B. This method can be used only in an interface.//此方法只能在接口中使用
C. This method can return anything.//这个方法可以返回任何东西。
D. This method can be used only in an interface or an abstract class.
//此方法只能在接口或抽象类中使用。
E. None of above.//上面都不对。
Question 133
Given:
public class SuperTest {
public static void main(String[] args) {
statement1
statement2
statement3
}
}
class Shape {
public Shape() {
System.out.println(“Shape: constructor”);
}
public void foo() {
System.out.println(“Shape:foo”);
}
}
class Square extends Shape {
public Square() {
super();
}
public Square(String lable) {
System.out.println(“Square: constructor”);
}
public void foo() {
super.foo();
}
public void foo(String label) {
System.out.println(“Square:foo”);
}
}
}
}
What should statement1,statement2,and statement3,be respectively,in order to produce the result?
Shape: constructor
Square: foo
Shape:foo
//为了生成结果,statement1、statement2和statement3应该分别是什么?
Shape:constructor
Square:foo
Shape:foo
A.Square square = new Square (“bar”);
square.foo(“bar”);
square.foo();
B.Square square = new Square (“bar”);
square.foo (“bar”);
square.foo (“bar”);
C.Square square = new Square ();
square.foo();
square.foo(bar);
D.Square square = new Square ();
square.foo ();
square.foo (“bar”);
E.Square square = new Square ();
square.foo();
square.foo();
F.Square square = new Square();
square.foo(“bar”);
square.foo();
Question 134
Given the code fragment://给定代码段:
if (aVar++ < 10) {
System.out.println(aVar + “Hello World!”);
} else {
System.out.println (aVar + “Hello Universe!”);
}
What is the result if the integer aVar is 9?
//如果整数aVar是9,结果是什么?
A.10 Hello World!
B.Hello Universe!
C.Hello World!
D.Compilation fails.
Question 135
Given:
public class Test {
public static void main(String[] args) {
Test ts = new Test ();
System.out.print (isAvailable + " ");
isAvailable= ts.doStuff ();
System.out.println(isAvailable);
}
public static boolean doStuff () {
return !isAvailable;
}
}
static boolean isAvailable = false;
What is the result?
A.true true
B.true false
C.false true
D.false false
E.Compilation fails
Question 136
Given the code fragment:
String[] colors= {“red”,“blue”,“green”,“yellow”,“marron”,“cyan”};
Which code fragment prints blue,cyan,?
//下列哪个代码片段打印蓝色、青色、?
A) for(String c:colors){
if(c.length()!=4){
continue;
}
System.out.print(c+", “);
}
B) for(String c:colors[]){
if(c.length()<=4){
continue;
}
System.out.print(c+”, “);
}
C) for(String c:String[] colors){
if(c.length()>=3){
continue;
}
System.out.print(c+”, “);
}
D) for(String c:colors){
if(c.length()!=4){
System.out.print(c+”, ");
continue;
}
}
A. Option A
B. Option B
C. Option C
D. Option D
Question 137
Given:
public class Triangle{
static double area;
int b=2,h=3;
public static void main(String[] args) {
double p, b, h; //line n1
if (area == 0) {
b = 3;
h = 4;
p = 0.5;
}
area = p * b * h; //line n2
System.out.println("Area is " + area);
}
}
What is the result?
A. Area is 6.0
B. Area is 3.0
C. Compilation fails at line n1
//第n1行编译失败
D.Compilation fails at line n2
//第n2行编译失败
Question 138
Given:
public class ColorTest{
public static void main(String[] args) {
String[] colors = { “red”, “blue”, “green”, “yellow”, “maroon”, “cyan” };
int count = 0;
for (String c : colors) {
if (count >= 4) {
break;
} else {
continue;
}
if (c.length() >= 4) {
colors[count] = c.substring(0, 3);
}
count++;
}
System.out.println(colors[count]);
}
}
What is the result?
A. Yellow
B. Maroon
C. Compilation fails//编译失败
D. A StringIndexOutOfBoundsException is thrown at runtime
//在运行时抛出StringIndexOutOfBoundsException
Question 139
Given:
public class String1 {
public static void main(String[] args) {
String s = “123”;
if (s.length() >2)
s.concat(“456”);
for(int x = 0; x ❤️; x++)
s += “x”;
System.out.println(s);
}
}
What is the result?
A. 123
B. 123xxx
C. 123456
D. 123456xxx
E. Compilation fails//编译失败
Question 140
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?
//在这里插入// insert代码时,哪两个代码片段使代码能够编译并打印12?
A. Calc c = new Calc();
int n3 = c.findMid(n1,n2);
B. int n3 = super.findMid(n1,n3);
C. Calc c = new Mid();
int n3 = c.findMid(n1, n2);
D. Mid m1 = new Calc();
int n3 = m1.findMid(n1, n2);
E. int n3 = Calc.findMid(n1, n2);
Question 141
Given the code fragment:
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?
//假设系统日期为2014年6月20日。结果如何?
A. Option A
B. Option B
C. Option C
D. Option D
Question 142
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?
//并给出了要求:
→如果qty变量的值大于或等于90,则discount = 0.5
→如果qty变量的值在80到90之间,则discount = 0.2
哪两个代码片段可以单独放置在n1行以满足需求?
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;
A.Option A
B.Option B
C.Option C
D.Option D
E.Option E
Question 143
Give:
public class Alpha {
public String[] main = new String2;
Alpha(String[] main) {
for (int ii = 0; ii < main.length; ii++) {
this.main[ii] = main[ii] + 5;
}
}
public void main(){
System.out.println(main[0] + main1);
}
}
public class Test {
public static void main(String[] args) {
Alpha main = new Alpha(args);
main.main();
}
}
And the commands:
javac Test.java
java Test 1 2
What is the result?
A. 1525
B. 13
C. Compilation fails//编译失败
D. An exception is thrown at runtime//在运行时抛出异常
E. The program fails to execute due to runtime error
//由于运行时错误,程序无法执行
Question 144
Given the following code for a Planet object:
public class Planet {
public String name;
public int moons;
public Planet (String name,int moons){
this.name = name;
this.moons = moons;
}
public static void main(String[] args) {
Planet[] planets = {
new Planet("Mercury",0),
new Planet("Venus",0),
new Planet("Earth",1),
new Planet("Mars",2)
};
System.out.println(planets);
System.out.println(planets[2]);
System.out.println(planets[2].moons);
}
}
What is the output?
A)planets
Earth
1
B)[LPlanets.Plant;@15db9742
Earth
1
C)[LPlanets.Plant;@15db9742
Planets.Plant;@6d06d69c
1
D)[LPlanets.Plant;@15db9742
Planets.Plant;@6d06d69c
[LPlanets.Moon;@7852e922
E)[LPlanets.Plant;@15db9742
Vens
0
A.Option A
B.Option B
C.Option C
D.Option D
E.Option E
Question 145
A method is declared to take three arguments. A program calls this method and passes only two arguments. What is the results?
//方法声明为接受三个参数。程序调用此方法并只传递两个参数。结果如何?
A. Compilation fails.//编译失败。
B. The third argument is given the value null.
//第三个参数的值为null。
C.The third argument is given the value void.
//第三个参数的值为void。
D.The third argument is given the value zero.
//第三个参数的值为零。
E.The third argument is given the appropriate falsy value for its declared type.
//第三个参数为其声明的类型提供了适当的falsy值。
F. An exception occurs when the method attempts to access the third argument.
//当方法试图访问第三个参数时发生异常。
Question 146
Given the code fragment:
public static void main(String[] args) {
String date = LocalDate
.parse("2014-05-04")
.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(date);
}
What is the result?
A. May 04, 2014T00:00:00.000
B. 2014-05-04T00:00: 00. 000
C. 5/4/14T00:00:00.000
D. An exception is thrown at runtime.//在运行时抛出异常
Question 147
Which three are advantages of the Java exception mechanism?
//Java异常机制的三个优点是什么?
A.Improves the program structure because the error handling code is separated from the normal program function.
//改进程序结构,因为错误处理代码与普通程序函数是分离的。
B. Provides a set of standard exceptions that covers all the possible errors.
//提供一组涵盖所有可能错误的标准异常
C. Improves the program structure because the programmer can choose where to handle exceptions.
//改进程序结构,因为程序员可以选择在何处处理异常。
D. Improves the program structure because exceptions must be handled in the method in which they occurred.
//改进程序结构,因为异常必须在其发生的方法中处理。
E. Allows the creation of new exceptions that are tailored to the particular program being created.
//允许创建针对正在创建的特定程序的新异常
Answer: A,C,E
Question 148
Class StaticField {
static int i = 7;
public static void main(String[] args) {
StaticFied obj = new StaticField();
obj.i++;
StaticField.i++;
obj.i++;
System.out.println(StaticField.i + " "+ obj.i);
}
}
What is the result?
A. 10 10
B. 8 9
C. 9 8
D. 7 10
Question 149
public class Test{
public static void main(String[] args) {
String str1 = “Java”;
String str2 = new String(“java”);
//line n1
{
System.out.println(“Equal”);
} else {
System.out.println(“Not Equal”);
}
}
}
Which code fragment, when inserted at line n1,enables the App class to print Equal?//当插入第n1行时,哪个代码片段使App类能够打印Equal?
A) String str3 = str2;
if(str1 == str3)
B) if(str1.equalsIgnorecase(str2))
C)String str3 = str2;
if(str1.equals(str3))
D)if(str1.toLowerCase() == str2.toLowerCase())
Question 150
package p1;
public interface DoInterface {
void m1(int n); //line n1
public void m2(int n);
}
package p3;
import p1.DoInterface;
public class DoClass implements DoInterface {
int x1,x2;
DoClass(){
this.x1 = 0;
this.x2 = 10;
}
public void m1(int p1) { x1 += p1;System.out.println(x1); } //line n2
public void m2(int p1) { x2 += p1;System.out.println(x2); }
}
package p2;
import p1.;
import p3.;
class Test{
public static void main(String[] args) {
DoInterface doi = new DoClass(); //line n3
doi.m1(100);
doi.m2(200);
}
}
What is the result?
A.100
210
B.Compilation fails due to an error in line n1//编译失败,原因是第n1行出现错误
C.Compilation fails due to an error at line n2
D.Compilation fails due to an error at line n3
Question 151
package p1;
public class Test {
static double dvalue;
static Test ref;
public static void main(String[] args) {
System.out.println(ref);
System.out.println(dvalue);
}
}
What is the result?
A.p1.Test.class
0.0
B.//引用 引用的摘要地址
0.000000
C.Null
0.0
D.Compilation fails//编译失败
E.A NullPointerException is thrown at runtime
//在运行时抛出NullPointerException
Question 152
class Jump{
static String args[] = {“lazy”, “lion”, “is”, “always”};
public static void main(String[] args) {
System.out.println(
args1 + " " + args2 + " " + args3 + " jumping");
}
}
And the commands:
Javac Jump.java
Java Jump crazy elephant is always
What is the result?
A.Lazy lion is jumping
B.Lion is always jumping
C.Crazy elephant is jumping
D.Elephant is always jumping
E.Compilation fails
Question 153
Given:
public class SampleClass{
public static void main(String[] args) {
AnotherSampleClass asc = new AnotherSampleClass();
SampleClass sc = new SampleClass();
sc = asc;
System.out.println("sc: " + sc.getClass());
System.out.println("asc: " + asc.getClass());
}
}
class AnotherSampleClass extends SampleClass{
}
What is the result?
A. sc: class Object
asc: class AnotherSampleClass
B. sc: class SampleClass
asc: class AnotherSampleClass
C. sc: class AnotherSampleClass
asc: class SampleClass
D. sc: class AnotherSampleClass
asc: class AnotherSampleClass
Question 154
public class MyException extends RuntimeException{}
public class Test {
public static void main(String[] args) {
try {
method1();
}
catch(MyException ne){
System.out.println(“A”);
}
}
public static void method1(){ //line1
try{
Throw Math.random()>0.5?new MyException():new RuntimeException();
}catch(RuntimeException re){
System.out.println(“B”);
}
}
}
A. A
B. B
C. Either A or B
D. A B
E A complie time error occurs at line n1
Question 155
Consider following interface.
//考虑下面的界面。
interface Runnable{
public void run();
}
Which of the following will create instance of Runnable type?
//下面哪个选项将创建Runnable类型的实例?
A. Runnable run = () -> {System.out.println(“Run”);}
B. Runnable run = () -> System.outprintlnfRun");
C. Runnable run = () > System.outprintlnfRun");
D. Runnable run = > System.ouLprintlnfRun"};
E. None of the above.
Question 156
public class Test{
public static void main(String[] args) {
System.out.println(2 + 4 * 9 - 3); //Line 21
System.out.println((2 + 4) * 9 - 3); //Line 22
System.out.println(2 + (4 * 9) - 3); //Line 23
System.out.println(2 + 4 * (9 - 3)); //Line 24
System.out.println((2 + 4 * 9) - 3); //Line 25
}
}
Which line of codes prints the highest number?
//哪一行代码打印的数字最高?
A.Line 21
B.Line 22
C.Line 23
D.Line 24
E.Line 25
Question 157
boolean log3 = (5.0!=6.0)&&(4!=5);
boolean log4 = (4!=4)||(4==4);
System.out.println(“log3:”+log3+“log4:”+log4);
A)log3:false
log4:true
B)log3:true
log4:true
C)log3:true
log4:false
D)log3:false
log4:false
Question 158
Given:
public class TestLoop1{
public static void main(String[] args){
int a=0,z=10;
while(a
–z;
}
System.out.print(a+":"+z);
}
}
what is the result?
A)5:5
B)6:4
C)6:5
D)5:4
Question 159
Given:
package p1;
public class Acc{
int p;
private int q;
protected int r;
public int s;
}
Test.java:
package p2;
import p1.Acc;
public class Test extends Acc{
public static void main(String[] args){
Acc obj = new Test();
}
}
which statement is true?//哪个陈述是正确的?
A)both p and s are accessible by obj
//p和s都可以被obj访问
B)only s is accessible by obj
//只有s可以被obj访问
C)both r and s are accessible by obj
//r和s都可以被obj访问
D)p,r,and s are accessible by obj
//p r s可以被obj得到
Question 160
which of the following exception will be thrown due to the statement given here? //下面哪个异常会因为这里给出的语句而引发?
int array[] = new int[-2];
A:nullPointerException
B.NegativeArraySizeException
C.ArrayIndexOutOfBoundsExcepyion
D.IndexOutOfBountsException
E.This statement dose not cause any exception
Question 161
Which statement will empty the contents of a StringBuilder variable named sb?哪个语句将清空名为sb的StringBuilder变量的内容?
A. sb.deleteAll();
B. sb.delete(0, sb.size());
C. sb.delete(0, sb.length());
D. sb.removeAll();
Question 162
Consider
Integer number = Integer.valueOf(“808.1”);
Which is true about the above statement?
//关于上面的陈述,哪一个是正确的?
A.The value of the variable number will be 808.1
//变量number的值为808.1
B.The value of the variable number will be 808
//变量number的值将是808
C.The value of the variable number will be 0.
//变量number的值为0。
D.A NumberFormatException will be throw.
//将抛出数据格式转换异常
E. It will not compile.
//它不会编译。
Question 163
Which code fragment cause a compilation error?
//哪个代码片段导致编译错误?
A. float flt = 100F;
B. float flt = (float) 1_11.00;
C. float flt = 100;
D. double y1 = 203.22;
float flt = y1
E. int y2 = 100;
float flt = (float) y2;
Question 164
Given the following code:
public static void main(String[] args){
String[] planets = {“Mercury”,“Venus”,“Earth”,“Mars”};
System.out.println(planets.length);
System.out.println(planets[1].length());
}
What is the output?
A. 4
4
B. 3
5
C. 4
7
D. 5
4
E. 4
5
F. 4
21
Question 165
Given the following main method://给出以下主要方法:
public static void main(String[] args) {
int num = 5;
do {
System.out.print(num-- +" ");
} while (num == 0);
}
What is the result?
A. 5 4 3 2 1 0
B. 5 4 3 2 1
C. 4 2 1
D. 5
E. Nothing is printed
Question 166
Which statement is/are true?//哪个陈述是正确的?
I.Default constructor only contains “super();” call.
//默认构造函数只包含“super();”调用。
II.We can’t use any access modifier with a constructor.
//我们不能在构造函数中使用任何访问修饰符。
III.A constructor should not have a return type.
//构造函数不应该具有返回类型。
A. Only I.
B. Only II.
C. Only I and II.
D. Only I and III.
E. AIL
Question 167
Given the code fragment
int var1=-5;
int var2=var1–;
int var3=0;
if (var2<0) {
var3=var2++;
}else{
var3=–var2;
}
System.out.println(var3);
What is the result?
A.-6
B.-4
C.-5
D.5
E.4
F.Compliation fails
Question 168
public static void main(String[] args) {
List names=new ArrayList<>();
names.add(“Robb”);
names.add(“Bran”);
names.add(“Rick”);
names.add(“Bran”);
if(names.remove(“Bran”)){
names.remove(“Jon”);
}
System.out.println(names);
}
What is the result?
A.[Robb, Rick, Bran]
B.[Robb, Rick]
C.[Robb, Bran, Rick, Bran]
D.An exception is thrown at runtime
//在运行时抛出异常
Question 169
Given:
public class Case {
public static void main(String[] args) {
String product=“Pen”;
product.toLowerCase();
product.concat(" BOX".toLowerCase());
System.out.println(product.substring(4,6));
}
}
What is the result?
A.box
B.nbo
C.bo
D.nb
E.An exception is thrown at runtime
//在运行时抛出异常
Question 170
View the exhibit.
class MissingInfoException extends Exception()
class AgeOutofRangeException extends Excepiton()
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:
4.public class Test {
5. public static void main(String[] args) {
6. Candidate c=new Candidate(“James”,20);
7. Candidate c1=new Candidate(“Williams”,32);
8. System.out.println©;
9. System.out.println(c1);
10. }
11.}
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:
//将第6行和第7行包含在一个try块中,并添加:
catch(Exception e1){//code goes here}
catch(MissingInfoException e2){//code goes here}
catch(AgeOutofRangeException e2){//code goes here}
D.Enclosing line 6 and line 7 within a try block and adding:
catch(MissingInfoException e2){//code goes here}
catch(AgeOutofRangeException e2){//code goes here}
Question 171
Given the code fragment:
int nums1[]=new int3;
int nums2[]={1,2,3,4,5};
nums1=nums2;
for(int x:nums1){
System.out.println(x+";
}
What is the result?
A.1:2:3:4:5:
B.1:2:3
C.Compiltationg fails.
D.An ArrayoutofBoundsException is thrown at runtime.
Question 172
Given the code fragment:
interface Contract{ }
class Super implements Contract{ }
class Sub extends Super { }
public class Ref {
public static void main (String[] args) {
List objs = new ArrayList();
Contract c1 = new Super();
Contract c2 = new Sub();
Super s1 = new Sub(); //Line n1
objs.add(c1);
objs.add(c2);
objs.add(s1); //Line n2
for(Object itm: objs){
System.out.println(itm.getClass().getName());
}
}
}
A. Super
Sub
Sub
B. Contract
Contract
Super
C. Compilation fails at line n1
D. Compilation fails at line n2
Question 173
public class ForTest {
public static void main(String[] args) {
int[] array = {1,2,3};
for ( foo ) {
}
}
}
Which three are valid replacements for foo so that the program will compiled and run?
哪三个是有效的foo替换,以便程序编译和运行
A. int i: array
B. int i = 0; i < 1; i++
C. ;;
D. ; i < 1; i++
E. ; i < 1;
Question 174
Which of the following can fill in the blank in this code to make it compile? (Select 2 options.)//下面哪个选项可以填充这段代码中的空白使其编译?
1 public void method()________Exception{
2 ___________Exception();
3 }
A. On line 1, fill in throws
B. On line 1, fill in throws new
C. On line 2, fill in throw new
D. On line 2, fill in throws
E. On line 2, fill in throws new
Question 175
Given the following array:
int [] intArr = {8,16,32,64,128};
Which two code fragments, independently, print each element in this array?
哪个代码片段独立地打印这个数组中的每个元素
A. for(int i : intArr) {
System.out.print(intArr[i] +"");
}
B.for(int i : intArr) {
System.out.print(i +"");
}
C.for(int i=0 : intArr) {
System.out.print(intArr[i] +"");
i++;
}
D.for(int i=0;i
Question 176(2)
Given the code fragement:
public static void main (String[] args){
StringBuilder sb = new StringBuilder(5);//赋予的是初始容量
String s ="";
if (sb.equals(s)) {
System.out.println(“Match 1”);
} else if ( sb.toString().equals(S.toString())) {
System.out.println(“Match 2”);
} else {
System .out .println(“No Match”);
}
}
What is the result?
A Match 1
B Match 2
C No match
D A NullPointerException is thrown at runtime.
Question 177(1)
Given:
public class TestLoop{
public static void main (String[] args){
int array[]={0,1,2,3,4};
int key = 3;
for (int pos =0 ; pos
break;
}
}
System.out.print(“Found”+key+“at”+pos);
}
}
What is the result?
A Found 3at 2
B Found 3at 3
C Compilation fails
D An exception is thrown at runtime
Question 178(题目难以理解)
Which two statements correctly describe checked exception(编译时异常)?
//哪两条语句正确地描述了已检查的异常?
A these are exceptional conditions that a well- written application should anticipate and recover from.
//这些都是编写良好的应用程序应该预料到并从中恢复的异常情况。
B these are exception conditions that are external to the application, and that the application usually cannot anticipate or recover from
//这些是应用程序外部的exception条件,应用程序通常无法预测或从中恢复
C these are exceptional conditions that are internal to the application, and that the application
usually cannot anticipate or recover from
//这些都是应用程序内部的异常条件,而应用程序通常无法预测或恢复
D Every class that is a subclass of RuntimeException and Error is categorized as checked exception
//作为RuntimeException和Error子类的每个类都被归类为已检查异常
E every class that is a subclass of Exception, excluding RuntimeExcepion and its subclasses,
is categorized as checked excepion
//每个异常的子类,不包括RuntimeExcepion及其子类,
是否属于已检查的例外
Answer:BD
Question 179
Given:
package p1;
public interface DoInterface {
void method1(int n1); // line n1
}
package p3;
import p1.DoInterface;
public class DoClass implements DoInterface {
public DoClass(int p1) { }
public void method1(int p1) { } // line n2
private void method2(int p1) { } // line n3
}
public class Test {
public static void main(String[] args) {
DoInterface doi= new DoClass(100); // line n4
doi.method1(100);
doi.method2(100);
}
}
Which change will enable the code to compile?
//哪些更改将使代码能够编译?
A.Adding the public modifier to the declaration of method1 at line n1
//在第n1行将公共修饰符添加到method1的声明中
B.Removing the public modifier from the definition of method1 at line n2
//从第n2行method1的定义中删除公共修饰符
C. Changing the private modifier on the declaration of method 2 public at line n3//在第n3行更改方法2上的私有修饰符为公共修饰符
D. Changing the line n4 DoClass doi = new DoClass ( );
Question 180
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
//setter和getter方法到这里
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(pl. show()) ;
system.out.println(p2. show() );
}
}
What is the result?
A. Jesse 25
Walter 52
B. Compilation fails only at line n1
C. Compilation fails only at line n2
D. Compilation fails at both line n1 and line n2
Question 181
Given
public class App{
public static void main (String [] args) {
Boolean [] bool = new Boolean [2];
bool[0] = new Boolean(Boolean.parseBoolean("true"));
bool[1] = new Boolean(null);
System .out. println(bool[0] + " " +bool[1]);
}
}
What is the result?
A. True false
B. True null
C. Compilation fails
D. A NullPointerException is thrown at runtime
Question 182
Given
class Dog{
Dog() {
try {
throw new Exception();
} catch (Exception e) { }
}
}
class Test {
public static void main (String [] args ) {
Dog d1 = new Dog () ;
Dog d2 = new Dog () ;
Dog d3 = d2 ;
// do complex stuff
}
}
How many objects have been created when the line // do complex stuff is reached?
//当到达// do complex stuff时,创建了多少个对象?
A. Two
B. Three
C. Four
D. Six
Question 183
public class Painting {
private String type;
public String getType(){
return type;
}
public void setType(String type){
this.type=type;
}
public static void main(String[] args){
Painting obj1 = new Painting();
Painting obj2 = new Painting();
obj1.setType(null);
obj2.setType("Fresco");
System.out.print(obj1.getType()+ ":" + obj2.getType());
}
}
What is the result?
A):Fresco
B)null:Fresco
C)Fresco :Fresco
D)A NullPointerException is thrown at runtime
Question 184(1)
public class Question189 {
public static void main(String[] args){
String str = " “;
str.trim();
System.out.println(str.equals(”") + " " + str.isEmpty());
}
}
What is the result?
A) true true
B) true false
C) false false
D) false true
Question 185(2)
public class Question190 {
abstract class Planet {
protected void revolve(){ //line n1
}
abstract void rotate(); //line n2
}
class Earth extends Planet {
void revolve(){ //line n3
}
protected void rotate(){ //line n4
}
}
}
Which two modifications,made independtly,enable the code to compile?
//哪两个独立的修改使代码能够编译?
A) Make the method at line n1 public.
B) Make the method at line n2 public.
C) Make the method at line n3 public.
D) Make the method at line n3 protected.
E) Make the method at line n4 public.
Question 186(1)
public class Student {
public String name ="";
public int age = 0;
public String major = “Undeclared”;
public boolean fulltime = true;
public void display(){
System.out.println(“Name:” + name + “Major:” + major);}
public boolean isFulltime(){
return fulltime;
}
}
Which line of code initializes a student instance?
//哪一行代码初始化学生实例?
A) Student student1;
B) Student student1 = Student.new();
C) Student student1 = new Student();
D) Student student1 = Student();
Question 187(1)
Given the code fragment:
String[] cartoons = {“tom”,“jerry”,“micky”,“tom”};
int counter =0;
if (“tom”.equals(cartoons[0])) {
counter++;
} else if (“tom”.equals(cartoons1)) {
counter++;
} else if (“tom”.equals(cartoons2)) {
counter++;
} else if (“tom”.equals(cartoons3)) {
counter++;
}
System.out.print(counter);
What is the result?
A. 1
B. 2
C. 4
D. 0
Question 188(2)
Which statement best describes encapsulation?
什么是最好的封装?
(哪个语句最能描述封装?)
A. Encapsulation ensures that classes can be designed so that only certain fields and
methods of an object are accessible from other objects.
封装确保类可以被设计为只有特定的字段和对象的方法可以从其他对象访问。
B. Encapsulation ensures that classes can be designed so that their methods are
inheritable.
封装确保类可以被设计成它们的方法是可继承的。
C. Encapsulation ensures that classes can be designed with some fields and methods
declared as abstract.
封装确保可以使用某些字段和方法设计类声明为抽象的.
D. Encapsulation ensures that classes can be designed so that if a method has an
argument MyType x, any subclass of MyType can be passed to that method.
封装确保类可以被设计成如果一个方法有参数MyType x, MyType的任何子类都可以传递给那个方法
Question 189(1)
Given:
MainTest.java:
public class MainTest {
public static void main (int[] args) {
System.out.println("int main " +args[0]);
}
public static void main (Object[] args) {
System.out.println("Object main " +args[0]);
}
public static void main (String[] args) {
System.out.println("String main " +args[0]);
}
}
and commands :
javac MainTest.java
java Maintest 1 2 3
what is the result?
A. int main 1
B. Object main 1
C. String main 1
D. Compilation fails
E. An exception is thrown at runtime
Question 190(2)
Given the code fragment:
for (int ii = 0; ii < 3;ii++) {
int count = 0;
for (int jj = 3; jj > 0; jj–) {
if (ii == jj) {
++count;
break;
}
}
System.out.print(count);
continue;
}
What is the result?
A. 011
B. 012
C. 123
D. 000
Question 191(1)
What is the proper way to defined a method that take two int values and returns their sum as an int value?
//定义一个方法的正确方法是什么?该方法接受两个int值并将它们的和作为int值返回?
A.int sum(int first, int second) { first + second; }
B.int sum(int first, second) { return first + second; }
C.sum(int first, int second) { return first + second; }
D.int sum(int first, int second) { return first + second; }
E.void sum (int first, int second) { return first + second; }
Question 192(2)
Given the content of three files://给定三个文件的内容:
A.java:
public class A {
public void a() {}
int a;
}
B.java:
public class B {
private int doStuff() {
private int x = 100;
return x++;
}
}
C.java:
import java.io.*;
package p1;
class C{
public void main (String fileName) throws IOException { }
}
Which statement is true?
A.Only the A.Java file compiles successfully.//只有A.Java文件能够成功编译。
B.Only the B.java file compiles successfully.
C.Only the C.java file compiles successfully.
D.The A.Java and B.java files compile successfully.
//A.java和B.java文件编译成功。
E.The B.java and C.java files compile successfully.
F.The A.Java and C.java files compile successfully.
Question 193(1)
Given the code fragment:
int iVar = 100;
float fVar = 100.100f;
double dVar = 123;
iVar = fVar; *
fVar = iVar;
dVar = fVar;
fVar = dVar; *
dVar = iVar;
iVar = dVar; *
Which three lines fail to compile?
//哪三行不能编译?
A.Line 7
B.Line 8
C.Line 9
D.Line 10
E.Line 11
F.Line 12
Question 194(1)
Given:
public class Access{
private int x = 0;
private int y = 0;
public static void main(String[] args) {
Access accApp = new Access();
accApp.printThis(1,2);
accApp.printThat(3,4);
}
public void printThis(int x,int y) {
x = x;
y = y;
System.out.println("x:" + this.x +"y:" + this.y);
}
public void printThat(int x,int y) {
this.x = x;
this.y = y;
System.out.println("x:" + this.x + "y:" + this.y);
}
}
What is the result?
A.x:1 y:2 x:3 y:4
B❌0 y:0 x:3 y:4
C❌1 y:2 x:0 y:0
D❌0 y:0 x:0 y:0
Question 195(2)
Given:
public class Palindrome{
public static int main(String[] args) {
System.out.println(args1);
return 0;
}
}
And the commands:
javac Palindrome.java
java Palindrome Wow Mom
What is the result?
A.Compilation fails
B.The code compiles,but does not execute.//代码编译,但不执行。
C.Paildrome
D.Wow
E.Mom
Question 196
What is the name of the Java concept that uses access modifiers to protect variables and hide them within a class?
//使用访问修饰符来保护变量并将其隐藏在类中的Java概念的名称是什么?
A.Encapsulation
B.Inheritance
C.Abstraction
D.Instantiation
E.Polymorphism
Question 197
Given the code fragment:
public class Test{
public static List data = new ArrayList();
//insert code here
{
for (String x : strs) {
data.add(x);
}
return data;
}
public static void main (String[] args){
String[] d = {“a” , “b” , “c”};
update(d);
for(String s : d){
System.out.print(s + " ");
}
}
}
Which code fragment , when inserted at //insert code here , enables the code to compile
and print a b c ?
//在这里插入//insert代码时,哪个代码片段使代码能够编译打印abc ?
A.List update (String[] strs)
B.static ArrayList update(String[] strs)
C.static List update(String[] strs)
D.static void update(String[] strs)
E.ArrayList static update(String[] strs)
Question 198(1)
Given the code fragment:
System.out.println(28 + 5 <= 4 + 29);
System.out.println((28 + 5) <= (4 + 29));
A. 28false29
true
B.285 < 429
true
C.true
true
D.complication fails
Question 199(1)
Which statement is true about Java byte code?
A.It can run on any platform.
B.It can run on any platform only if it was compiled for that platform.
C.It can run on any platform that has the Java Runtime Environment.
D.It can run on any platform that has a Java compiler.
E.It can run on any platform only if that platform has both the Java Runtime Environment and a Java compiler.
Question 200
Which statement is true about the default constructor of a top-level class?
关于顶级类的默认构造函数,下列哪项陈述是正确的?
A.It can take arguments.
B.It has private access modifier in its declaration.
C.It can be overloaded.
D.The default constructor of a subclass always invokes the no-argument constructor of its superclass.
Question 201
public class Whizlabs{
public static void main(String[] args){
try{
Double number = Double.valueOf("120D");
}catch(NumberFormatException ex){
}
System.out.println(number);
}
}
what is the result?
A.120
B.120D
C.A NumberFormatException will be thrown.
D.Compilation fails due to error at line 5.
E.Compilation fails due to error at line 8.
Question 202
Given the code fragment:
int[] array={1,2,3,4,5};
And given the requirements:鉴于这些要求:
Process all the elements of the array in the order of entry.
按输入顺序处理数组的所有元素。
Process all the elements of the array in the reverse order entry.
按相反顺序处理数组的所有元素。
Process alternating elements of the array in the order of entry.
按输入顺序处理数组的交替元素。
Which two statements are true?
A.Requirements 1,2 and 3 can be implemented by using the enhanced for loop.
需求1、2和3可以通过使用增强for循环来实现。
B.Requirements 1,2 and 3 can be implemented by using the standard for loop.需求1、2和3可以通过使用循环标准来实现
C.Requirements 2 and 3 CANNOT be implemented by using the standard for loop.要求2和3不能通过使用循环标准来实现。
D.Requirement 1 can be implemented by using the enhanced for loop.需求1可以通过使用增强for循环来实现。
E.Requirement 3 CANNOT be implemented by using either the enhanced for loop or the standard for loop.需求3不能通过使用增强for循环或标准for循环来实现。
Question 203(1)
Given:
public class ComputeSun{
public int x;
public int y;
public int sum;
public ComputeSum(int nx,int ny){
x=nx; y=ny;
updateSum();
}
public void setX(int nx){x=nx;updateSum();}
public void setY(int ny){x=ny;updateSum();}
void updateSum(){sum=x+y;}
}
This class needs to protect an invariant on the sum field.
这个类需要保护和字段上的不变量。
Which three members must have the private access modifier to ensure that this invariant is maintained?哪三个成员必须拥有私有访问修饰符才能确保这种不变性得以保持?
A.The x field
B.The y field
C.The sum field
D.The ComputerSum()constructor
E.The setX()method
F.The setY()method
Question 204(1)
Given:
public class X implements Z {
public String toString(){
return “X”;
}
public static void main(String[] args){
X myY = new Y();
X myX = myY;
Z myZ = myX;
System.out.print(myX);
System.out.print((Y)myX);
System.out.print(myZ);
}
}
class Y extends X{
public String toString(){
return “Y”;
}
}
A.X XX
B.X Y X
C.Y Y X
D.Y YY
Question 205(1)
Given the code fragment:
int a[] = {1,2,3,4,5};
for(XXX){
System.out.print(a[e]);
}
Which option can replace xxx to enable the code to print 135?
哪个选项可以代替xxx来打印代码135?
A.int e =0;e<=4;e++
B.int e =0;e<5;e+=2
C.int e =1;e<=5;e+=1
D.int e =1;e<5;e+=2
Question 206
GIven the for loop construct:
for(expr1; expr2;expr3){
statment
}
Which two statement are true?
A) This is not the only valid for loop construct;there exits another from of for loop constructor;这不是循环构造的唯一有效方法;对于for循环构造函数,存在另一个来自的;
B) The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin.
表达式expr1是可选的。它初始化循环,并在循环开始时计算一次。
C) When expr2 evaluates to false, the loop terminates. It is evaluated only after each iteration through the loop.当expr2计算为false时,循环终止。只有在循环中的每次迭代之后,才评估它。
D)The expression expr3 must be present. It is evaluated after each iteration through the loop.表达式expr3必须存在。它在循环的每次迭代后被评估。
Question 207(2)
Given the code fragment
//insert the code fragment
arr[0] = new int3;
arr[0][0] = 1;
arr0 = 2;
arr0 = 3;
arr1 = new int4;
arr[1][0] = 10;
arr1 = 20;
arr1 = 30;
arr1 = 40;
Which two statements, when inserted independently at line // insert code here, enable the code to compile?哪两个语句在第//行独立插入代码时,会启用 要编译的代码?
A. int[][] arr = null;
B. int[][] arr = new int 2;
C. int[][] arr = new int 2 [ ];
D. int[][] arr = new int [] 4;
E. int[][] arr = new int 2 [0];
F. int[][] arr = new int [0] 4;
Question 208(1)
Given the following class declarations:给定以下类声明:
public abstract class Animal
public interface Hunter
public class Cat extends Animal implements Hunter
public class Tiger extends Cat
Which answer fails to compile? 哪个答案不可信?
A)ArrayList myList = new ArrayList<>();
myList.add(new Tiger());
B)ArrayList myList = new ArrayList<>();
myList.add(new Cat());
C)ArrayList myList = new ArrayList<>();
myList.add(new Tiger());
D)ArrayList myList = new ArrayList<>();
myList.add(new Cat());
E)ArrayList myList = new ArrayLIst<>();
myList.add(new Cat());
Question 209(2)
Given:
public class Series{
int arr[] = {1,2,3};
for (int var:arr){
int i = 1;
while(i <= var);
System.out.println(i++);
}
}
What is the result?
A.1
1
1
B.1
2
3
C.2
3
4
D. Compilation fails
E. The loop executes infinite times循环执行无限次?
Question 210(2)
public class Cirle {
double radius;
public double area;
public Cirle(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) {
Cirle c1 = new Cirle(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
类的封装很差。您需要更改circle类来计算并返回面积
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 Match.PI * radius * radius; }
C: Add the following method: public double getArea ( ) {area = Match.PI * radius * radius; }
D: Change the cacess modifier of the setRadius ( ) method to be protected.
更改要保护的setRadius()方法的cacess修饰符
Question 211(2)
public class Student {
int rollnumber;
String name;
List cources = new ArrayList<>();
//insert code here
public String toString() {
return rollnumber+":"+name+":"+cources;
}
}
AND:
public class Test {
public static void main(String[] args) {
List cs = new ArrayList<>();
cs.add(“Java”);
cs.add(“C”);
Student s = new Student(123,“Fred”,cs);
}
}
Which code fragment, when inserted at line // insert code here, enables class Test to print 123 : Fred : [Java, C]?
A. private Student(int i, String name, List cs) { /* initialization code goes here / }
B. public void Student(int i, String name, List cs) { / initialization code goes here / }
C. Student(int i, String name, List cs) { / initialization code goes here / }
D. Student(int i, String name, ArrayList cs) { / initialization code goes here */ }
Question 212(2)
The protected modifier on a Field declaration within a public class means that the field .
公共类中的字段声明上的受保护修饰符表示字段。
A. Cannot be modified
A.不能修改
B. Can be read but not written from outside the class
B.可以类外访问,但不能在类外修改
C. Can be read and written from this class and its subclasses only within the same package
只能从这个类及其子类在同一个package中读写
D. Can be read and written from this class and its subclasses defined in any package
可以从这个类及其在任何包中定义的子类中读写
Question 213(1)
public class MyClass {
public static void main(String[] args) {
String s = “Java Duke”;
int len = s.trim().length();
System.out.println(len);
}
}
What is the result?
A.8
B.9
C.11
D.10
E. Compilation fails
Question 214(1)
Given:
public class Equal {
public static void main(String[] args) {
String str1 = “Java”;
String[] str2 = {“J”,“a”,“v”,“a”};
String str3 = “”;
for (String str : str2) {
str3 = str3+str;
}
boolean b1 = (str1 == str3);
boolean b2 = (str1.equals(str3));
System.out.print(b1+", "+b2);
}
What is the result?
A. true, false
B. false, true
C. true, true
D. false, false
Question 215(1)
Given the code fragment:
float x = 22.00f % 3.00f;
int y = 22 % 3;
System.out.print(x + ", "+ y);
What is the result?
A. 1.0, 1
B. 1.0f, 1
C. 7.33, 7
D. Compilation fails
E. An exception is thrown at runtime
Question 216(1)
Which two actions will improve the encapsulation of a class?
哪两个操作将改进类的封装?
A. Changing the access modifier of a field from public to private将字段的访问修饰符从公共更改为私有
B. Removing the public modifier from a class declaration从类声明中移除公共修饰符
C. Changing the return type of a method to void将方法的返回类型更改为void
D. Returning a copy of the contents of an array or ArrayList instead of a direct reference返回数组或数组列表内容的副本,而不是直接引用
Question 217(1)
int i, j=0;
i = (3* 2 +4 +5 ) ;
j = (3 * ((2+4) + 5));
System.out.println(“i:”+ i + “\nj”:+j);
What is the result?
A.i:16 j:33
B.i:15 j:33
C.i:33 j:23
D.i:15 j:23
Question 218(1)
Given:
class Cake {
int model;
String flavor;
Cake() {
model = 0;
flavor = “Unknown”;
}
}
public class Test {
public static void main(String[] args) {
Cake c = new Cake();
bake1©;
System.out.println(c.model + " " + c.flavor);
bake2©;
System.out.println(c.model + " " + c.flavor);
}
public static Cake bake1(Cake c) {
c.flavor = “Strawberry”;
c.model = 1200;
return c;
}
public static void bake2(Cake c) {
c.flavor = “Chocolate”;
c.model = 1230;
return;
}
}
What is the result?
A. 0 unknown
0 unknown
B. 1200 Strawberry
1200 Strawberry
C. 1200 Strawberry
1230 Chocolate
D. Compilation fails
Question 219(1)
Given:
public class Test{
public static void main(String[] args) {
if (args[O] . equals (“Hello”) ? false : true)
system. out. println (“success”) ;
}else {
System. out. println(“Failure”) ;
}
}
}
And given the commands:
javac Test.Java
Java Test Hello
What is the result?
A.Success
B.Faliure
C.Compilation fails.
D.An exception is thrown at runtime.
Question 220(1)
Which of the following will print current time?
A. System.out.print(new LocalTime()-now0);
B. System.out.print(new LocalTime());
C. System.ouLprint(LocalTime.now());
D. System.ouLprint(LocalTime.today());
E. None of the above.
Question 221(2)
Given:
import java.io.Error ;
public class TestApp{
public static void main (string[] args){
TestApp t=new TestApp ;
try{
t.doPrint();
t.doList();
}catch (Exception e2){
system.out.println(“Caught” + e2);
}
}
public void doList() throws Exception{
throw new Error (“Error”);
}
public void doPrint() throws Exception{
throw new RuntimeException(“Exception”);
}
}
What is the result?
A)Caught java.lang.RuntimeException:Exception
Exception in thread “main” java.lang.Error:Error
at TestApp.doList(TestApp.java:14)
at TestApp.main(TestApp.java:6)
B)Exception in thread “main” java.lang.Error:Error
at TestApp.doList(TestApp.java:14)
at TestApp.main(TestApp.java:6)
C)Caught java.lang.RuntimeException:Exception
Caught java.lang.Error:error
D)Caught java.lang.RuntimeException:Exception
A.Option A
B.Option B
C.Option C
D.Option D
Question 222(1)
public class MyFor3 {
public static void main(String[]args) {
int[] xx=null;
for(int ii:xx) {
System.out.println(ii);
}
}
}
What is the result?
A. Null
B. Compilation fails
C. An exception is thrown at runtime
D. 0
Question 223(1)
public class test {
public static void main(String[]args) {
int ii=0;
int jj=7;
for(ii=0;ii
}
}
}
What is the result?
A. 2 4
B. 0 2 4 6
C. 0 2 4
D. Compilation fails
Question 224(1)
public class Test {
static String[][] arr =new String3;
private static void doPrint() {
//insert code here
}
public static void main(String[] args) {
String[] class1 = {“A”,“B”,“C”};
String[] class2 = {“L”,“M”,“N”,“O”};
String[] class3 = {“I”,“J”};
arr[0] = class1;
arr1 = class2;
arr2 = class3;
Test.doPrint();
}
}
Which code fragment, when inserted at line //insert code here, enables the code to print COJ?
在第//行插入代码时,哪一个代码片段使代码能够打印COJ?
A. int i = 0;
for (String[] sub: arr) { int j = sub.length -1; for (String str: sub) {
System.out.println(str[j]); i++;
}
}
B. private static void doPrint() {
for (int i = 0;i < arr.length;i++) { int j = arr[i].length-1; System.out.print(arr[i][j]);
}
}
C. int i = 0;
for (String[] sub: arr[][]) { int j = sub.length; System.out.print(arr[i][j]); i++;
}
D. for (int i = 0;i < arr.length-1;i++) { int j = arr[i].length-1; System.out.print(arr[i][j]);
i++;
}
Question 225(1)
Given the following class
public class CheckingAccount {
public int amount;
public CheckingAccount(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void changeAmount(int x) {
amount +=x;
}
}
And given the following main method, located in another class:
并给出了位于另一类中的以下主要方法:
public static void main(String[]args) {
CheckingAccount acct = new CheckingAccount((int)(Math.random()*1000));
//line n1
}
Which three lines, when inserted independently at line n1, cause the program to print a 0 balance?
在n1行独立插入哪三行会导致程序打印O平衡?
A. this.amount = 0;
B. amount = 0;
C. acct (0) ;
D. acct.amount = 0;
E. acct. getAmount () = 0;
F. acct.changeAmount(0);
G. acct.changeAmount(-acct.amount);
H. acct.changeAmount(-acct.getAmount());
Question 226(2)
Given:
1.public class Whizlabs{
2.
3. public static void main (String[] args){
4. String s = “A”;
5.
6. switch (s){
7. case “a”:
8. System.out.print("simaple A ");
9. default:
10. System.out.print("default ");
11. case “A”:
12 System.out.print("Capttal A ");
13. }
14. }
15.}
What is the result?
A. simaple A
B. Capital A
C. simaple A default Capital A
D. simaple A default
E. Compilation fails.
Question 227(1)
Given:
class Overloading {
int x(double d) {
System.out.println(“one”);
return 0;
}
String x(double d) {
System.out.println(“two”);
return null;
}
double x(double d) {
System.out.println(“three”);
return 0.0;
}
public static void main(String[] args) {
new Overloading().x(4.0);
}
}
What is the result?
A. One
B. Two
C. Three
D. Compilation fails.
Question 228(2)
Given:
class Base {
// insert code here
}
public class Derived extends Base{
public static void main(String[] args) {
Derived obj = new Derived();
obj.setNum(3);
System.out.println("Square = " + obj.getNum() * obj.getNum());
}
}
Which two options, when inserted independently inside class Base, ensure that the class is being properly encapsulated and allow the program to execute and print the square of the number?
哪两个选项在独立插入基类时,可以确保类被正确封装,并允许程序执行和打印数字的平方?
A.private int num; public int getNum() { return num; }public void setNum(int num) {
this.num = num;}
B. public int num; protected public int getNum() { return num; }protected public void
setNum(int num) { this.num = num;}
C. private int num;public int getNum() {return num;} private void setNum(int num) {
this.num = num;}
D. protected int num; public int getNum() { return num; } public void setNum(int num) {
this.num = num;}
E. protected int num; private int getNum() { return num; } public void setNum(int num) {
this.num = num;}
Question 229(1)
4.Given:
public class Test {
public static int stVar = 100;
public int var = 200;
public String toString(){
return var + “:” + stVar;
}
}
And given the code fragment:
给定代码片段
Test t1 = new Test();
t1.var = 300;
System.out.println(t1);
Test t2 = new Test();
t2.stVar = 300;
System.out.println(t2);
What is the result?
这个结果是什么?
A.300:300
200:300
B.300:100
200:300
C.300:0
0:300
D.200:300
200:300
Question 230 (2)
Given the code fragment:
给定代码块
int wd = 0;
String days[] = {“sun”,“mon”,“wed”,“sat”};
for (String s:days) {
switch (s) {
case “sat”:
case “sun”:
wd -= 1;
break;
case “mon”:
wd++;
case “wed”:
wd += 2;
}
}
System.out.println(wd);
What is the result?
A -1
B Compilation fails. //编译失败
C 4
D 3
Question 231(1)
Given the code fragment:
public static void main(String[] args){
String ta = “A”;
ta = ta.concat(“B”);
String tb = “C”;
ta = ta.concat(tb);
ta.replace(‘C’, ‘D’);
ta = ta.concat(tb);
System.out.println(ta);
}
What is the result?
A.A B D C
B.A B C C
C.A B C D
D.A C D
E.A B C
Question 232(1)
11.Given:
class Animal{
String type = “Canine”;
int maxSpeed = 60;
Animal(){}
Animal(String type,int maxSpeed){
this.type=type;
this.maxSpeed=maxSpeed;
}
}
class WildAnimal extends Animal{
String bounds;
WildAnimal(String bounds){
//line n1
}
WildAnimal(String type, int maxSpeed, String bounds){
//line n2
}
}
And Given the code fragment:
Which two modifications enable the code to print the following output?
哪两个修改使代码能够打印以下输出
Canine 60 Long
Feline 80 short
//Replace 替换
A.Replace line n2 with:
super(type,maxSpeed);
this.bounds = bounds;
B.Replace line n1 with:
this.bounds = bounds;
super();
C.Replace line n1 with:
this(“Canine”,60);
this.bounds = bounds;
D.Replace line n2 with:
super(type,maxSpeed);
this(bounds);
E.Replace line n1 with:
super();
this.bounds = bounds;
Question 233(2)
12.Given:
public class Test{
public static void main(String[] args){
boolean a=new Boolean(Boolean.valueOf(args[0]));
boolean b=new Boolean(args1);
System.out.println(a+" "+b);
}
}
And given the commands: //给出以下命令
javac Test.java
java Test TRUE null
What is the result?
A) TRUE null
B) A ClassCastException is thrown at runtime //运行时抛出类转换异常
C) true true
D) false false
E) true false
Question 234(2)
Given the code fragment: //代码块
public static void main (String[] args) {
int[] stack = {10,20,30};
int size = 3;
int idx =0;
/* line n1 */
System.out.print(“The Top element:” + stack[idx]);
}
Which code fragment, inserted at line n1,prints The Top element: 30?
在代码块中 n1行 插入一段代码 打印 The Top element: 30
A)do{
idx++;
}while (idx >= size);
B)do{
idx++;
}while (idx <= size);
C)while (idx <= size - 1){
idx++;
}
D)while(idx < size);{
idx++;
}
E)do{
idx++;
}while(idx < size - 1);
Question 235(1)
Which three statements are true about exception handling?
关于异常处理,哪三个陈述是正确的?
A) Only unchecked exceptions(运行时异常) can be rethrown.(只有运行时异常才能被重新抛出)
B) All subclasses of the Exception class except the RuntimeException class are checked exceptions.(除了RuntimeException类之外,异常类的所有子类都是检测异常)
C) All subclasses of the Error class are checked exceptions and are recoverable.
(错误类的所有子类都是检查过的异常,并且是可恢复的)
D) The parameter in a catch block is of Throwable type.(捕获块中的参数是Throwable类型)
E) All subclasses of the RuntimeException class must be caught or declared to be thrown.(必须捕获或声明要抛出RuntimeException类的所有子类)
F) All subclasses of the RuntimeException class are recoverable.
(RuntimeException类的所有子类都是可恢复的。)
答案应该为:BDF
Question 236(1)
public static void main (String[] args){
LocalDate date=LocalDate.of(2012,01,32);
date.plusDays(10);
System.out.println(date);
}
What is the result?
A)2012-02-10
B)Compilation fails
C)2012-02-11
D)A DateTimeException is thrown at runtime//运行时抛出DateTimeException
Question 237(2)
28.Given the code fragments:
Person.java:
public class Person {
String name;
int age;
public Person(String n,int a) {
name = n;
age = a;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Test.java:
public static void checkAge(List list,Predicate predicate) {
for(Person p : list) {
if(predicate.test§) {
System.out.println(p.name + " ");
}
}
}
public static void main(String[] args) {
List iList = Arrays.asList(new Person(“Hank”,45),
new Person(“Charlie”,40),
new Person(“Smith”,38));
//line n1
}
Which code fragment,when insert at line n1,enables the code to print Hank?
在第n1行插入时,哪个代码片段使代码能够打印Hank
A.checkAge(iList , (Person p)->{p.getAge() > 40;} );
B.checkAge(iList , Person p -> p.getAge() > 40 );
C.checkAge(iList , () -> p.getAge() > 40 );
D.checkAge(iList , p -> p.getAge() > 40 );
Question 238(1)
32.Given the following array:
int[] intArr={8,16,32,64,128};
which two code fragment,independently,print each element in this array?
哪个代码片段独立地打印这个数组中的每个元素
A for(int:intArr){
System.out.print(intArr[i]+"");
}
B for{int:intArr}{
System.out.print(i+"");
}
C for (int i=0;i
}
D for(int i=0;intArr){
System.out.print(intArr[i]+"");
}
E for(int i=0;i
}
F for(int i;i
}
Question 239(2)
Given:
class A{
public void test() {
System.out.println(“A”);
}
}
class B extends A{
public void test() {
System.out.println(“B”);
}
}
public class C extends A{
public void test() {
System.out.println(“C”);
}
public static void main(String[] args){
A b1=new A();
A b2=new C();
b1=(A) b2; //line n1
A b3=(B) b2; //line n2
b1.test();
b3.test();
}
}
What is the result?
A) A ClassCastException is thrown only at line n2.
B) A
C
C) C
C
D) A ClassCastException is thrown only at line n1.
E) A
B
Question 240(2)
Given
class C2 {
Public void displayC2() {
System.out.print(“C2”);
}
}
interface I {
public void displayI ();
}
class C1 extends C2 implements I {
public void displayI() {
System.out.print(“C1”);
}
}
And given the code fragment:
C2 obj1 = new C1();
I obj2 = new C1();
C2 s = obj2;
I t = obj1;
t.displayI();
s.displayC2();
What is the result?
A) Compilation fails.
B) C1C1
C) C2C2
D) C1C2
Question 241(1)
Given:
public class Test {
public static final int MIN = 1;
public static void main(String[] args) {
int x = args.length;
if(checkLimit (x)){
System.out.println(“Java SE”);
}
else{
System.out.println(“Java EE”);
}
}
public static boolean checkLimit(int x) {
return (x >= MIN) ? true : false;
}
}
And given the commands: // commands 命令
javac Test.java
java Test
What is the result?
A) A NullPointerException is thrown at runtime.
B) Compilation
C) Java EE
D) Java SE
Question 242(1)
Given the code fragment:
public static void main(String[] args) {
ArrayList points = new ArrayList<>();
points.add(1);
points.add(2);
points.add(3);
points.add(4);
points.add(null);
points.remove(2);
points.remove(null);
System.out.println(points);
}
What is the result?
A)[1,3,4,null]
B) A NullPointerException is thrown at runtime. //运行时抛出空指针异常
C)[1,2,4]
D)[1,3,4]
E) Compilation fails.
F)[1,2,4,null]
Question 243(1)
Which statement is true about the switch statement?
关于switch,哪个陈述是正确的?
A.the break statement,at the end of each came block, is mandatory.
B.It must contain the default section.
C.Its case label Iiterals can be changed at runtime.
D.Its expression must evaluate to a single value.
A.在每个输入块的末尾的break语句是强制性的。
B.它必须包含默认部分.
C.它的case标签Iiterals可以在运行时更改.
D.它的表达式必须求值为单个值
Question 244(1)
45.Given:
public class MyClass{
public static void main(string []args){
String s=" java Duke ";
int len=s.trim().length();
System.out.println(len);
}
}
What is the result?
A: Complication fails
B: 11
C: 8
D: 9
E: 10
Question 245(1)
Given the code fragment:
public static void main(String[] args) {
int data[] = {2010,2013,2014,2015,2014};
int key = 2014;
int count = 0;
for ( int e:data) {
if(e!=key) {
continue;
count++;
}
}
System.out.print(count + " Found");
}
A 1 Found
B Compilation fails //编译出错
C 0 Found
D 3 Found
Question 246(1)
Given the following segment of code: 给出下面这段代码
ArrayList myList = new ArrayList<>();
myList.add(new MotorCycle());
Which two statements, if either were true,would make the code compile?
哪两条语句(如果其中一条为真)将使代码编译?
A MotoCycle is an interface that implements the Vehicle class.
MotoCycle是实现Vehicle类的接口。
B Vehicle and MotoCycle both extend the Transportation superclass.
Vehicle和MotoCycle都扩展了运输超类。
C Vehicle is an interface that is implemented by the Motorcycle class.
Vehicle是由Motorcycle类实现的接口。
D Vehicle is a superclass of MotorCycle
Vehicle是Motorcycle的一个超类
E MotorCycle is a superclass of Vehicle.
Motorcycle是一种超级Vehicle。
F Vehicle and MotoCycle both implement the Transportation interface.
Vehicle和MotoCycle都实现了运输接口。
Question 247(1)
You are asked to create a method that accepts an array of integers and returns the highest value from that array.
要求您创建一个方法,该方法接受一个整数数组并从该数组返回最大值。
Given the code fragment:
public class Test {
public static void main(String[] args) {
int numbers[] = {12,13,42,31,15,156,23,51,12};
int max = findMax(numbers);
}
/* line n1 /{
int max = 0;
/ coede goes here */
return max;
}
}
Which method signature do you use at line n1?
在第n1行使用哪个方法签名?
A)final int findMax(int[])
B)static int[] findMax(int max)
C)public int findMax(int[] numbers)
D)static int findMax(int[] numbers)
Question 248(1)
55.class Employee{
private String name;
private int age;
private int salary;
public Employee(String name,int age){
setName(name);
setAge(age);
setSalary(2000);
}
public Employee(String name, int age, int salary){
setSalary(salary);
this(name, age);
}
//getter and setter methods for attributes go here // attributes属性
public void printDetails(){
System.out.println(name + " : " + age + " : " + salary);
}
}
Test.java:
Class Test {
public static void main(String[] args){
Employee e1 = new Employee();
Employee e2 = new Employee(“Jack”, 50);
Employee e3 = new Employee(“Chole”, 40, 5000);
e1.printDetails();
e2.printDetails();
e3.printDetails();
}
}
What is the result?
(A)Compilation fails in the Employee class
(B)Both the Employee class and the Test class fail to compile // compile 编译
©Compilation fails in the Test class
(D)null : 0 : 0
Jack : 50 : 2000
Chole : 40 : 5000
(E)null : 0 : 0
Jack : 50 : 0
Chole : 40 : 5000
Question 249(Lambda表达式)
Given that these files exist and are accessible:
假设这些文件存在并且可以访问
/sports/info.txt
/sports/cricket/players.txt
/sports/cricket/data/ODI.txt
and given the code fragment:
int maxDepth = 2; //目录深度
Stream paths = Files.find(
Paths.get("/sports"),
maxDepth,
(p,a) -> p.getFileName().toString().endsWith(“txt”),
FileVisitOption.FOLLOW_LINKS);
Long fCount = paths.count();
System.out.println(fCount);
Assuming that there are NO soft-link/symbolic links to any of the files in the directory structure,what is the result?
假设目录结构中没有指向任何文件的软链接/符号链接,那么结果是什么?
A) 2
B) 1
C) An exception is thrown at runtime
D) 3
Question 250(Lambda表达式)
The data.doc,data.txt,and data.xml files are accessible and contain text
data.doc,数据。可以访问txt和data.xml文件并包含文本
Given the code fragment:
Stream paths = Stream.of(Paths.get(“data.doc”),
Paths.get(“data.txt”),
Paths.get(“data.xml”));
Paths.filter(s -> s.toString().endsWith(“txt”)).forEach(
s -> {
try {
Files.readAllLines(s)
.stream()
.forEach(System.out::println); // line n1
} catch (IOException e) {
System.out.println(“Exception”);
}
}
};
What is the result?
A) The program prints:
Exception
<
Question 251(2)
Given the code fragment:
LocalDateTime dt = LocalDateTime.of(2014,7,31,1,1);
dt.plusDays(30);
dt.plusMonths(1);
System.out.println(dt.format(DateTimeFormatter.ISO_DATE));
What is the result?
A)2014-09-30
B)07-31-2014
C)2014-07-31
D)An exception is thrown at runtime 运行时抛出异常
Question 252(2)
Given:
public class Engine {
double fuelLevel;
Engine(int fuelLevel) { this.fuelLevel = fuelLevel; }
public void start(){
//line n1
System.out.println(“started”);
}
public void stop(){
System.out.println(“Stopped”);
}
}
Your design requires that fuelLevel of Engine must be greater than zero when the start()method is invoked.
您的设计要求在调用start()方法时,引擎的fuelLevel必须大于零。
The code must terminate if fuelLevel of Engine is less than or equal to zero. 如果发动机的燃油水平小于或等于零,则代码必须终止
Which code fragment should be added at line n1 to exspress this invariant condition? 应该在第n1行添加哪个代码片段来处理这个不变条件?
A) assert (fuelLevel) : “Terminating…”; Terminating终止
B) assert (fuelLevel > 0 ) : System.out.println(“Impossible fuel”); 燃料不可能
C) assert fuelLevel > 0 : "Impossbile fuel " ;
D) assert fuelLevel < 0 : System.exit(0);
Question 253(2)
Given:
final class Folder { // line n1
//line n2
public void open(){
System.out.println("Open ");
}
}
public class Test {
public static void main(String[] args) throws Exception {
try (Folder f = new Folder()) {
f.open();
}
}
}
Which two modifications enable the code to print Open class?
哪两个修改使代码能够打印Open类?
A) At line n2 , insert :
public void close() throws IOException{
System.out.println(“Close”);
}
B) Replace line n1 with:
class Folder implements AutoCloseable{
C)At line n2 ,insert:
final void close(){
System.out.println(“Close”);
}
D) Replace line n1 with:
class Folder extends Exception{
E) Replace line n1 with:
class Folder extends Closeable {
Question 254(2)
Given the defintion of the Runner interface, and Vehicle ,Car, and Jeep classes 给出了Runner接口的定义,以及Vehicle、Car和Jeep类
interface Runner{}
abstract class Vehicle{ abstract void start(); }
class Car extends Vehicle implements Runner{
public void start(){
System.out.println(getClass().getName() + " Started");
}
}
Given :
class TestRunner{
public static void check(Runner r){
if (r instanceof Vehicle){
Vehicle v = (Vehicle)r;
v.start();
}
}
public static void main(String[] args) {
Runner v = new Car();
check(v);
}
}
What is the result?
A) A ClassCastException is thorwn at runtime. 运行时抛出类转换异常
B) Runner Started
C) Car Started
D) vehicle started
Question 255(1)
Given the code fragments:
public class Book Implements Comparator{
String name;
double price;
public Book (){}
public Book(string name, double price){
this.name =name ;
this.price =price;
}
public int compare(Book b1, Book b2){
return b1.name.compareTo(b2.name);
}
public String toString (){
return name + “:” +price;
}
}
and
list books =Arrays.aslist(
new Book(“Beginning with Java”,2),
new Book(“A Guide to Java Tour”,3)
);
collections.sort( books, new Book());
System. out. print(books);
What is the result?
A)An Exception is thrown at runtime 运行时有异常
B)A compilation error occurs because the Book class does not override the abstract method compareTo()发生编译错误是因为Book类没有覆盖compareTo()抽象方法
C)[A Guide to Java Tour: 3.0, Beginning with Java: 2.0]
D)[Beginning with Java: 2.0, A Guide to Java Tour: 3.0]
Question 256(1)
Given the code fragment:
Path source= Paths. get("/data/december/log.txt");
Path destination = Paths.get("/data");
Files.copy(source, destination);
and assuming that the file /data/december/log.txt is accessible and contains: 假设文件 /data/december/log.txt 是可访问和包含的
10-Dec-2014-Executed successfully 成功执行 时间2014.12.10
What is the result?
A)A FileAlreadyExistsexception is thrown at runtime. 在运行时抛出FileAlreadyExistsexception
B) The program executes successfully and does NOT change the file system
该程序成功执行,并且不更改文件系统
C)A Filenotfoundexception is thrown at run time在运行时抛出Filenotfoundexception
D)A file with the name log. txt is created in the /data directory and the content of the/data/december/log.txt file is copied to it.
名称为log.txt的文件是在/data目录和/data/december/log.txt的内容中创建的。将log.txt文件复制到它
Question 257(1)
Given the code fragment:
A.java:
package p1;
public class A{ }
B.java:
package pl.p2;
//line nl
public class B{
public void dostuff(){
A b=new A();
}
}
C.java:
package p3;
//line n2
public class c{
public static void main(string [] args) {
A ol= new A();
B o2= new B();
}
}
Which modification enables the code to compile? 哪些修改使代码能够编译?
A)Replace line nl with:
import pl.A;
Replace line n2 with:
import Pl.A;
import pl.p2.B;
B)Replace line n1 with:
import pl.A;
Replace line n2 with:
import pl.*;
C)Replace line n1 with:
import pl;
Replace line n2 with:
import pl;
import pl.p2;
D) Replace line n1 with:
import pl.;
Replace line n2 with:
import pl.p2.:
Question 258(2)
public class Person {
String name;
int age=25;
public Person(String name){
this(); //line nl
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;
}
public static void main(String [] args) {
Person pl=new Person(“Jesse”);
Person p2=new Person(“Walter”,52);
System.out.printIn(p1.show());
System.out.println(p2.show());
}
}
What is the result?
o A) Compilation fails only at line n1.
o B) Compilation fails at both line nl and line n2.
o C) Compilation fails only at line n2.
O D) Jesse 25
Walter 52
Question 259(1)
Given:
String stuff = “TV”;
String res = null;
if(stuff.equals(“TV”)){
res = “Walter”;
} else if (stuff.equals(“Movie”)){
res = “White”;
}else{
res = “No Result”;
}
which code fragment can replace the if block?
哪个代码片段可以替换if块?
A)res = stuff.equals(“TV”) ?
“Walter” else stuff.equals(“Movie”) ? “White”:“No Result”;
B)res = stuff.equals(“TV”) ? stuff.equals(“Movie”) ? “Walter”: “White”:“No Result”;
C)res = stuff.equals(“TV”) ? “Walter”:stuff.equals(“Movie”) ? “White”:“No Result”;
D)stuff.equals(“TV”) ? res =
“Walter” : stuff.equals(“Movie”) ? res = “White”: res = “No Result”;
Question 260(2)
Given:
class Vehicle{
int vno;
String name;
public Vehicle (int vno,String name){
this.vno=vno;
this.name=name;
}
public String toString()[
return vno + “:” +name;
}
}
and this code fragment:
Set vehicles =new TreeSet<>();
vehicles.add(new Vehicles(10123,“Ford”));
vehicles.add(new Vehicles(10124,“BMW”));
System.out.println(vehicles);
What is the result?
A.10123:Ford
10124:BMW
B.A classCastException is thrown at run time.
C.A compilation error occurs.
D.10124:BMW
10123:Ford
Question 261(1)
Given the code fragment:
public static void main (String[] args){
String names[] = {“Thomas”,“Peter”,“Joseph”};
String pwd[] =new String 3;
int idx = 0;
try {
for (String n : names) {
pwd[idx] =n.substring(2,6);//
idx++;
}
} catch (Exception e){
System.out.println(“Invalid Name”);
}
for (String p:pwd){
System.out.println§;
}
}
What is the result?
A.Invalid Name
B.omas
ter
seph
C.Invalid Name
omas
null
null
D.Invalid Name
omas
Question 262(1)
which code fragment a compilation error? 哪个代码块编译错误
A)float flt = (float) 1_11.00;
B)float flt = 100;
C)int y2 =100;
float flt = (float) y2;
D)float flt =100F;
E)double y1 = 203.22;
float flt = y1;
Question 263(2)
Which two statemnts are true?
A)Error class is extendable extendable可拓展
B)Error is a throwable throwable异常
C)Error is an Exception Exception例外
D)Error is a RuntimeException
E)Error class is unextendable.
Question 264(2)
Given:
public class Job {
string name;
Integer cost;
Job(string name, Integer cost){
this.name=name;
this.cost =cost;
}
String getName(){ return name;}
int getcost() { return cost;}
public static void main(string[] args){
Job jl= new Job(“IT”,null);
DoubleSupplier js1 = j1::getCost;
system.out.println(j1. getName()+":"+js1.getAsDouble());
}
What is the result?
A)IT: null
B)A NullPointerException is thrown at run time.
C)IT:0.0
D) A compilation error occurs.编译错误出现
Question 265(1)
Given:
class Student {
String name;
public Student(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
Student[] students = new Student[3];
Student[1] = new Student("Richard");
Student[2] = new Student("Donald");
for (Student s : students) {
System.out.println("" + s.name);
}
}
}
What is the result?
A: Compilation fails. 编译失败
B: null
Richard
Donald
C: A NullPointerException is thrown at runtime.
D: An ArrayindexOutOfBoxindsException is thrown at runtime.
E: Richard
Donald
Question 266(1)
Given the following array:
int[] intArr = {8, 16, 32, 64, 128};
Which two fragment,independenty,print each element in this array?
哪二个片段(独立的)打印这个数组中的每个元素
A: for (int i=0; i < intArr.length;i++) {
System.out.println(intArr[i] +" ");
}
B: for (int i : intArr) {
System.out.println(intArr[i] +" ");
}
C: for (int i=0; i < intArr.length;i++) {
System.out.println(intArr[i] +" ");
i++;
}
D: for (int i : intArr) {
System.out.println(i +" ");
}
E: for (int i; i < intArr.length;i++) {
System.out.println(intArr[i] +" ");
}
Question 267(1)
Given the code fragment
int x=100;
int a=x++; //100
int b=++x; //102
int c=x++; //102
int d=(a System.out.println(d);
what is the result?
A.102
B.103
C.101
D.100
E.Compilation fails.
Question 268(1)
Given the code fragment:
public static void main(String[] args){
System.out.println(“Result A”+0+1);
System.out.println(“Result B”+(1)+(2));
}
what is the result?
A. Result A 1
Result B 3
B. Result A 01
Result B 3
C. Result A 1
Result B 12
D. Result A 01
Result B 12
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。
我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:
撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G
直接输入1次#,并按下space后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用TOC
语法后生成一个完美的目录。
强调文本 强调文本
加粗文本 加粗文本
标记文本
删除文本
引用文本
H2O is是液体。
210 运算结果是 1024.
链接: link.
图片:
带尺寸的图片:
居中的图片:
居中并且带尺寸的图片:
当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
var foo = 'bar';
一个简单的表格是这么创建的:
项目 | Value |
---|---|
电脑 | $1600 |
手机 | $12 |
导管 | $1 |
使用:---------:
居中
使用:----------
居左
使用----------:
居右
第一列 | 第二列 | 第三列 |
---|---|---|
第一列文本居中 | 第二列文本居右 | 第三列文本居左 |
SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:
TYPE | ASCII | HTML |
---|---|---|
Single backticks | 'Isn't this fun?' |
‘Isn’t this fun?’ |
Quotes | "Isn't this fun?" |
“Isn’t this fun?” |
Dashes | -- is en-dash, --- is em-dash |
– is en-dash, — is em-dash |
一个具有注脚的文本。2
Markdown将文本转换为 HTML。
您可以使用渲染LaTeX数学表达式 KaTeX:
Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n−1)!∀n∈N 是通过欧拉积分
Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=∫0∞tz−1e−tdt.
你可以找到更多关于的信息 LaTeX 数学表达式here.
可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图:
这将产生一个流程图。:
我们依旧会支持flowchart的流程图:
如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。
如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。
mermaid语法说明 ↩︎
注脚的解释 ↩︎