牛客网错题集

牛客网错题集

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();
  }
}
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);
    }
}

参考链接

转载于:https://www.cnblogs.com/hglibin/p/10202300.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值