java题库——认证考试题1

这是一份关于Java编程的认证考试题库,包含单选和多选题。题目涉及了类文件导入、变量作用域、程序输出、类的正确性等多个方面,涵盖了Java的基础语法和编程概念。例如,讨论了哪些导入可以被删除而不影响代码编译,类变量的默认值,以及有效Java标识符等。此外,还涉及到变量类型、垃圾收集、方法参数使用var关键字的限制等深入知识点。
摘要由CSDN通过智能技术生成

目录

一. 单选题

1. (单选题)Which of the following are correct? (Choose all that apply.)

2. (单选题)Given the following two class files, what is the maximum number of imports that can beremoved and have the code still compile?// Water.javapackage aquarium;public class Water { }// Tank.javapackage aquarium;import java.lang.*;import java.lang.System;import aquarium.Water;import aquarium.*;public class Tank {public void print(Water water) {System.out.println(water); } }

3. (单选题)What is the output of executing the following class?

4. (单选题)Which statements about the following class are correct? (Choose all that apply.)

5. (单选题)Assuming the following class compiles, how many variables defined in the class or methodare in scope on the line marked on line 14?

二. 多选题

6. (多选题)Which statements about the following class are true? (Choose all that apply.)

7. (多选题)Which of the following are true? (Choose all that apply.)public class Bunny {public static void main(String[] x) { Bunny bun = new Bunny();} }

8. (多选题)Which are true about the following code? (Choose all that apply.)var num1 = Long.parseLong("100");var num2 = Long.valueOf("100");System.out.println(Long.max(num1, num2));

9. (多选题)What lines are printed by the following program? (Choose all that apply.)

10. (多选题)Which of the following are legal entry point methods that can be run from the commandline? (Choose all that apply.)

11. (多选题)Which of the following expressions, when inserted independently into the blank line, allowthe code to compile? (Choose all that apply.)public void printMagicData() {var magic = ;System.out.println(magic);}

12. (多选题)Which of the following statements about garbage collection are correct? (Choose allthat apply.)

13. (多选题)Which of the following code snippets about var compile without issue when used in amethod? (Choose all that apply.)

14. (多选题)Which are true about this code? (Choose all that apply.)

15. (多选题)Which answer options represent the order in which the following statements can be assembled into a program that will compile successfully? (Choose all that apply.)X: class Rabbit {}Y: import java.util.*;Z: package animals;

16. (多选题)Which statements about the following class are correct? (Choose all that apply.)

17. (多选题)Which of the following statements about var are true? (Choose all that apply.)

18. (多选题)Given the following class, which of the following lines of code can independently replaceINSERT CODE HERE to make the code compile? (Choose all that apply.)public class Price {public void admission() {INSERT CODE HERESystem.out.print(amount);} }

19. (多选题)Which of the following are valid Java identifiers? (Choose all that apply.)

20. (多选题)Given the following classes, which of the following snippets can independently be inserted inplace of INSERT IMPORTS HERE and have the code compile? (Choose all that apply.)

21. (多选题)Which statements about the following program are correct? (Choose all that apply.)

22. (多选题)Which are true about this code? (Choose all that apply.)var blocky = """squirrel \spigeon \termite""";System.out.print(blocky);

23. (多选题)Which of the following statements about the code snippet are true? (Choose all that apply.)

一. 单选题

1. (单选题)Which of the following are correct? (Choose all that apply.)

  • A. An instance variable of type float defaults to 0.
  • B. An instance variable of type char defaults to null.
  • C. A local variable of type double defaults to 0.0.
  • D. A local variable of type int defaults to null.
  • E. A class variable of type String defaults to null.
  • F. A class variable of type String defaults to the empty string "".
  • G. None of the above.

我的答案: E

答案解析:E. Options C and D are incorrect because local variables don’t have default values. Option A
is incorrect because float should have a decimal point. Option B is incorrect because primitives do not default to null. Option E is correct and option F incorrect because reference
types in class variables default to null.

2. (单选题)Given the following two class files, what is the maximum number of imports that can be
removed and have the code still compile?
// Water.java
package aquarium;
public class Water { }
// Tank.java
package aquarium;
import java.lang.*;
import java.lang.System;
import aquarium.Water;
import aquarium.*;
public class Tank {
public void print(Water water) {
System.out.println(water); } }

  • A. 0
  • B. 1
  • C. 2
  • D. 3
  • E. 4
  • F. Does not compile

我的答案: E

答案解析:E. The first two imports can be removed because java.lang is automatically imported. The
following two imports can be removed because Tank and Water are in the same package,
making the correct option E. If Tank and Water were in different packages, exactly one of
these two imports could be removed. In that case, the answer would be option D.

3. (单选题)What is the output of executing the following class?

  • A. 7-0-2-1-
  • B. 7-0-1-
  • C. 0-7-2-1-
  • D. 7-0-2-4-
  • E. 0-7-1-
  • F. The class does not compile because of line 3.
  • G. The class does not compile because of line 4.
  • H. None of the above.

我的答案: D

答案解析:D. We start with the main() method, which prints 7- on line 10. Next, a new Salmon
instance is created on line 11. This calls the two instance initializers on lines 3 and 4 to be
executed in order. The default value of an instance variable of type int is 0, so 0- is printed
next and count is assigned a value of 1. Next, the constructor is called. This assigns a value
of 4 to count and prints 2-. Finally, line 12 prints 4-, since that is the value of count.
Putting it all together, we have 7-0-2-4-, making option D the correct answer.

4. (单选题)Which statements about the following class are correct? (Choose all that apply.)

  • A. It prints Q1=blue.
  • B. It prints Q2=1200.
  • C. It prints P1=null.
  • D. It prints P2=1400.
  • E. Line 4 does not compile.
  • F. Line 12 does not compile.
  • G. Line 13 does not compile.
  • H. None of the above.

我的答案: C

答案解析:C. The key thing to notice is that line 4 does not define a constructor, but instead a method
named PoliceBox(), since it has a return type of void. This method is never executed
during the program run, and color and age are assigned the default values null and 0L,
respectively. Lines 11 and 12 change the values for an object associated with p, but then, on
line 13, the p variable is changed to point to the object associated with q, which still has the
default values. For this reason, the program prints Q1=null, Q2=0, P1=null, and P2=0,
making option C the only correct answer.

5. (单选题)Assuming the following class compiles, how many variables defined in the class or method
are in scope on the line marked on line 14?

  • A. 2
  • B. 3
  • C. 4
  • D. 5
  • E. 6
  • F. 7
  • G. None of the above

我的答案: F

答案解析:F. To solve this problem, you need to trace the braces {} and see when variables go in and out of scope. The variables on lines 2 and 7 are only in scope for a single line block. The variable on line 12 is only in scope for the for loop. None of these are in scope on line 14. By
contrast, the three instance variables on lines 3 and 4 are available in all instance methods.
Additionally, the variables on lines 6, 9, and 10 are available since the method and while
loop are still in scope. This is a total of 7 variables, which is option F.

二. 多选题

6. (多选题)Which statements about the following class are true? (Choose all that apply.)

  • A. Line 3 generates a compiler error.
  • B. Line 6 generates a compiler error.
  • C. Line 7 generates a compiler error.
  • D. Line 10 generates a compiler error.
  • E. The program prints 3 on line 10.
  • F. The program prints 4 on line 10.
  • G. The program prints 50.0 on line 11.
  • H. The program prints 49.0 on line 11.

我的答案: AD

答案解析:A, D. The first compiler error is on line 3. The variable temp is declared as a float, but the
assigned value is 50.0, which is a double without the F/f postfix. Since a double doesn’t
fit inside a float, line 3 does not compile. Next, depth is declared inside the for loop and
only has scope inside this loop. Therefore, reading the value on line 10 triggers a compiler
error. For these reasons, options A and D are the correct answers.

7. (多选题)Which of the following are true? (Choose all that apply.)
public class Bunny {
public static void main(String[] x) { Bunny bun = new Bunny();
} }

  • A. Bunny is a class.
  • B. bun is a class.
  • C. main is a class.
  • D. Bunny is a reference to an object.
  • E. bun is a reference to an object.
  • F. main is a reference to an object.
  • G. The main() method doesn’t run because the parameter name is incorrect.

我的答案: AE

答案解析:A, E. Bunny is a class, which can be seen from the declaration: public class Bunny.
The variable bun is a reference to an object. The method main() is the standard entry
p

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sweep-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值