当前位置:   article > 正文

48天笔试训练错题——day08

48天笔试训练错题——day08

目录

选择题

1.

2.

3. Test.main() 函数执行后的输出是()

编程题

1. 两种排序方法


选择题

1.

数组不是原生类,数组使用基本类型来定义的一个顺序表。

数组的大小不能随意改变,不能自动进行扩容。

数组是对象,对象存储在堆上。

2.

按顺序执行静态代码块。

3. Test.main() 函数执行后的输出是()

  1. // Test.main() 函数执行后的输出是()
  2. public class Test {
  3. public static void main(String[] args) {
  4. System.out.println(new B().getValue());
  5. }
  6. static class A {
  7. protected int value;
  8. public A(int v) {
  9. setValue(v);
  10. }
  11. public void setValue(int value) {
  12. this.value = value;
  13. }
  14. public int getValue() {
  15. try {
  16. value++;
  17. return value;
  18. } catch (Exception e) {
  19. System.out.println(e.toString());
  20. } finally {
  21. this.setValue(value);
  22. System.out.println(value);
  23. }
  24. return value;
  25. }
  26. }
  27. static class B extends A {
  28. public B() {
  29. super(5);
  30. setValue(getValue() - 3);
  31. }
  32. public void setValue(int value) {
  33. super.setValue(2 * value);
  34. }
  35. }
  36. }

子类重写父类方法,默认优先调用子类重写后的方法。

编程题

1. 两种排序方法

按照题目的要求写即可,就是遍历数组,判断是否按长度排序,以及字典序,判断字典序可以使用 Object 类自带的 compareTo 方法来比较。

代码实现:

  1. import java.util.Scanner;
  2. // 注意类名必须为 Main, 不要有任何 package xxx 信息
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner in = new Scanner(System.in);
  6. // 注意 hasNext 和 hasNextLine 的区别
  7. while (in.hasNextInt()) { // 注意 while 处理多个 case
  8. int n = in.nextInt();
  9. String[] arr = new String[n];
  10. for (int i = 0; i < n; i++) {
  11. arr[i] = in.next();
  12. }
  13. print(arr, n);
  14. }
  15. }
  16. public static void print(String[] arr, int n) {
  17. // 长度
  18. boolean flg1 = true;
  19. // 字典序
  20. boolean flg2 = true;
  21. for (int i = 0; i < n - 1; i++) {
  22. if (arr[i].length() >= arr[i + 1].length()) {
  23. flg1 = false;
  24. }
  25. if (arr[i].compareTo(arr[i + 1]) >= 0) {
  26. flg2 = false;
  27. }
  28. }
  29. if (flg1 && flg2) {
  30. System.out.println("both");
  31. } else if (flg1) {
  32. System.out.println("lengths");
  33. } else if (flg2) {
  34. System.out.println("lexicographically");
  35. } else {
  36. System.out.println("none");
  37. }
  38. }
  39. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/864403
推荐阅读
相关标签
  

闽ICP备14008679号