今天的Java笔试题

今天面试做了一套Java笔试题,下面是我根据记忆,对题目进行的测试,我是把几个题目综合在一起进行测试的:
         TestB.java代码:

import java.util.*;
class Parant
{
       Parant()
      {
    System.out.println("parant");
}
static
{
    System.out.println("parant static");
}
{
     System.out.println("parant not static");
}
public void test(ArrayList list)
{
    System.out.println("parant list");
}
}
class Child extends Parant
{
Child()
{
    System.out.println("child");
}
static
    {
     System.out.println("child static");
}

       {
      System.out.println("child not static");
}
public void test(List list)
    {
     System.out.println("child list");
}
}
class TestB
{
      public static void main(String[] args)
      {
    //先调用父类的构造方法,然后调用子类的构造方法,不管static还是没有static的代码段,都会被执行
    Child ac=new Child();
    ArrayList al=new ArrayList();
    ac.test(al);              //输出的是parant list,子类没有重写父类的方法
    String a="aaa";
    String b="aaa";
    String c=new String("aaa");
    //如果要比较两个基本类型的值是否相等,请用= =;

         //如果要比较两个对象的地址是否相等,请用= =;

         //如果要比较两个对象(引用)是否一致,请用equals
    System.out.println(a==b);
    System.out.println(a.equals(c));
    int temp=99;
    test(temp);            //输出的是99
    System.out.println(temp);     //99
    System.out.println(test(temp));      //199
      }
      public static int test(int bb)
      {
    bb=bb+100;
    return bb;
}
}
interface A
{
void test2();     //接口类不能带有方法主体,接口中可以没有方法,编辑能通过
}
abstract class B
{
abstract void test3(); //如果没有abstract,必须有方法主体
void test4()
{
}
}

TestC.java:

class TestC
{
      public static void main(String[] args)
      {
    int a=test();
    System.out.println(a);          //输出的结果是3
}
public static int test()
{
    try
    {
     String s=null;
     s.substring(0,1);
     return 1;
    }
    catch(Exception e)
    {
     return 2;
    }
          finally         //如果没有finally,则输出2
      {
       return 3;
    }
}
}

TestD.java

import java.util.*;
class TestD
{
public static void main(String[] args)
{
     ArrayList a=new ArrayList();
     test1(a);
     System.out.println(a.size());      //输出是0
     test2(a);
     System.out.println(a.size());      //输出是1
}

    static void test1(ArrayList al)
     {
       al=null;
}
static void test2(ArrayList al)
{
   al.add("all");
}
}

TestC的运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值