zoukankan      html  css  js  c++  java
  • 牛客网错题集

    牛客网错题集

    class Test{
        public static void hello() {
            System.out.println("hello");
        }
    }
    
    public class MyApplication {
        public static void main(String[] args) {
            Test test = null;
            test.hello();
        }
    }
    

    A、能编译通过,并正确运行.
    B、因为使用了未初始化的变量,所以不能编译通过.
    C、以错误的方式访问了静态方法
    D、能编译通过,但因变量为null,不能正常运行.

    点击查看结果

    ``` 参考答案: A 反编译代码为 import java.io.PrintStream;

    class Test
    {
    public static void hello()
    {
    System.out.println("hello");
    }
    }

    public class MyApplication
    {
    public static void main(String[] args)
    {
    Test test = null;
    Test.hello();
    }
    }

    </div>
    
    
    * [Test.main() 函数执行后的输出是()。](https://www.nowcoder.com/questionTerminal/af8cf04602e045958d13d16d20a1bf02)
    ```java
    public class Test {
        public static void main(String[] args) {
            System.out.print(new B().getValue()+" ");
        }
    
        static class A {
            protected int value;
    
            public A(int v) {
                setValue(v);
            }
    
            public void setValue(int value) {
                this.value = value;
            }
    
            public int getValue() {
                try {
                    value++;
                    return value;
                } catch (Exception e) {
                    System.out.println(e.toString());
                } finally {
                    this.setValue(value);
                    System.out.print(value+" ");
                }
                return value;
            }
        }
        
        static class B extends A {
            public B() {
                super(5);
                setValue(getValue() - 3);
            }
    
            public void setValue(int value) {
                super.setValue(2 * value);
            }
        }
    }
    

    A、11 17 34
    B、22 74 74
    C、6 7 7
    D、22 34 17
    点击查看结果

    ``` 参考答案: D ```
    public class TestInteger {
        public static void main(String[] args) {
            Integer s = new Integer(9);
            Integer t = new Integer(9);
            Long u = new Long(9);
    
            // System.out.println(s==u);    // Incompatible operand types Integer and Long
            System.out.println(s == t);
            System.out.println(s.equals(t));
            System.out.println(s.equals(9));
            System.out.println(s.equals(new Integer(9)));
        }
    }
    

    点击查看结果

    ``` false true true true ```
    public class Test {
        private static int i = 1;
    
        public int getNext() {
            return i++;
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            Test testObject = new Test();
            test.getNext();
            testObject.getNext();
            System.out.println(testObject.getNext());
        }
    }
    

    请问最后打印出来的是什么?()
    A、2
    B、3
    C、4
    D、5
    点击查看结果

    ``` 参考答案: B 该题主要考察的是static属性和i++操作。 因为i是static的,是类属性,所以不管有多少对象,都共用的一个变量。这里getNext()方法被调用了三次,所以进行了三次i++操作。 但是由于getNext()操作的返回是:return i++; i++是先返回,后++,所以在println是,已经返回了i(此时i为3),再进行自增的, 所以这里结果为3。 ```
    public class Test {
        static String x="1";
        static int y=1;
        public static void main(String args[]) {
            static int z=2;
            System.out.println(x+y+z);
        }
    }
    

    A、3
    B、112
    C、13
    D、程序有编译错误
    点击查看结果

    ``` 正确答案:D 被static修饰的变量称为静态变量,静态变量属于整个类,而局部变量属于方法,只在该方法内有效, 所以static不能修饰局部变量。

    public class Test {
    static String x="1";
    static int y=1;
    public static void main(String args[]) {
    int z=2;
    System.out.println(x+y+z);
    }
    }

    </div>
    **参考链接**
    * [https://www.nowcoder.com/profile/8667211/wrongset](https://www.nowcoder.com/profile/8667211/wrongset)
  • 相关阅读:
    gradle
    1-NIO使用
    处理非正常终止的错误
    一个取消多生产者单消费者的日志线程池服务
    executes()源码
    死锁
    CyclicBarrier使用
    Semaphore
    Spring学习(4)IOC容器配置bean:定义与实例化
    在Maven上Web项目添加Spring框架
  • 原文地址:https://www.cnblogs.com/hgnulb/p/10202300.html
Copyright © 2011-2022 走看看