java 面试题

1.class A {
​     protected int method1(int a, int b) { return 0; }
 }

public class B extends A{}

Which two are valid in class B that extends class A? (Choose two)
A. public int method1(int a, int b) { return 0; }
B. private int method1(int a, int b) { return 0; }
C. private int method1(int a, long b) { return 0; }
D. public short method1(int a, int b) { return 0; }
E. static protected int method1(int a, int b) { return 0; }

复制代码
答案: A,C
A:可以
B: 子类的可见性不能比父类低,错误
C: 方法签名变了,属于和子类继承的method1方法的重载,而非重写。
D: 子类和父类的返回类型要一致
E : 不可以。编译器提示:This static method cannot hide the instance method from Inherit

2.public class Outer{
  public void someOuterMethod(){
​    //Line1
   }
  public class Inner{}
  public static void main(String args[]){
  Outer o = new Outer();
  //Line2
  }
}

which instantiates an instance of Inner?
A.new Inner();//At line 1
B.new Inner();//At line 2
C.new o.Inner();//At line 2
D.new Outer.Inner();//At line 2//new Outer.new Inner()
答案:A

3.Which method is used by a servlet to place its session ID in a URL that is written to the servlet’s response output stream?
(译:哪个方法是servlet用于将其session ID入在一个URL中,该URL写入servlet的响应输出流)
A. The encodeURL method of the HttpServletRequest interface.
B. The encodeURL method of the HttpServletResponse interface.
C. The rewriteURL method of the HttpServletRequest interface.
D. The rewriteURL method of the HttpServletResponse interface.
答案:B

4.Which two are equivalent?
A.<%=YoshiBean.size%>
B.<%=YoshiBean.getSize()%>
C.<%=YoshiBean.getProperty("size")%>
D.<jsp:getProperty id="YoshiBean" param="size">
E.<jsp:getProperty name="YoshiBean" param="size">
F.<jsp:getProperty id="YoshiBean" property="size">
G.<jsp:getProperty name="YoshiBean" property="size">
答案:C=G

5.Which of the following is an advantege of an SMS table space versus a DMS table sapce?
A.The table space can use raw devices.
B.A table can be defined in a table space ant its associated index in a different table space.
C.Space for the objects int the table space is not allocated until required.

D.The size of the containers int the table space can be changed using the ALTER TABLESAPCE statement.

答案:B
6.Given the table T1 created by:
CREATE TABLE t1(
id INTEGER NT NULL GENERATED ALWAYS AS IDENTITY,
c1 CHAR(10)NOT NULL,
c2 CHAR(10)
)

A.INSERT INTO t1 VALUES('abc',NULL)
B.INSERT INTO t1 VALUES(1,'abc',NULL)
C.INSERT INTO t1(c1,c2) VALUES('abc',NULL)
D.INSERT INTO t1(c1,c2) VALUES(NULL,'def')
答案:C

7,有关类的实例化描述中,正确的是?
A.同一个类的对象,具体有不同的静态数据成员
B.不同类的对象,具有相同的静态数据成员
C.同一个类的对象具体有不同的对象自身引用(this)值
D.同一个类的对象具体有相同的对象自身引用(this)值
答案:C

8.what is the result?

public class Test {   

static boolean foo(char c) {
​        System.out.print(c);
​      return true;   
}
​    public static void main(String[] argv) {
​        int i = 0;
​     for (foo('A'); foo('B') && (i < 2); foo('C')) {
​        i++;       
​    foo('D');    
   }  
  }
}

A. ABDCBDCB
B. ABCDABCD
C. Compilation fails.
D. An exception is thrown at runtime.
输出结果是:ABDCBDCB

分析:

FOR循环里面讲究的条件要为真,与你的判断式是什么没有关系

就像这里,虽然是打印的字母,但是却不是false,所以可以执行
第一次进行循环:
foo('A')打印字母A,(注:这里不是false条件就默认为true条件)
foo('B')打印字母B,i=0,比较(i < 2),条件为true,进行循环体,foo('D')打印D
foo('C')打印字母C
第二次循环:
foo('B')打印B,i=1,比较(i < 2)为true,进行循环体,foo('D')打印D
foo('C')打印字母C
第三次循环:
foo('B')打印字母B,i=2,比较(i < 2)为false,退出循环,得结果

9.简单介绍死锁是如何形成的.
产生死锁的原因主要是:
(1) 因为系统资源不足。
(2) 进程运行推进的顺序不合适。
(3) 资源分配不当等。

10.描述数据库事务的4个隔离级别,哪几种隔离级别下读取数据的事务不会禁止其它写事务?

数据库事务的隔离级别有4个,由低到高依次为:

Read uncommitted、

Read committed、

Repeatable read、

Serializable,

这四个级别可以逐个解决脏读、不可重复读、幻读这几类问题。

当隔离级别设置为Repeatable read时,读取数据的事务将会禁止写事务,但允许读事务,写事务则禁止任何其他事务。

11.写一个单例(要求不能通过new 来实例化该类)。
import java.io.ObjectStreamException;
public class A {
​    private static final String a ="a";
​    // 类初始化时,不初始化这个对象(延迟加载,真正用的时候再创建)
​    private static A instance;

​    private A() {
​        // 防止反射获取多个对象的漏洞
​        if (null != instance) {
​            throw new RuntimeException();
​        }
​    }
​    // 方法同步,调用效率低
​    public static synchronized A getInstance() throws Exception{
​        if (null == instance){
​            Class clazz = Class.forName(A.class.getName());
​            instance = (A)clazz.newInstance();
​        }
​        return instance;
​    }
​    // 防止反序列化获取多个对象的漏洞。
​    // 无论是实现Serializable接口,或是Externalizable接口,当从I/O流中读取对象时,readResolve()方法都会被调用到。
​    // 实际上就是用readResolve()中返回的对象直接替换在反序列化过程中创建的对象。
​    private Object readResolve() throws ObjectStreamException {
​        return instance;
​    }
​    public static void main(String args[])throws Exception{
​        A obj = A.getInstance();
​        System.out.println(obj.a);
​    }
}
12.以下哪种操作适合最先进行排序处理?
A.找最大值,最小值
B.计算算术平均值
C.找中间值
D.找出现次数最多的值
答案:C

13.输出结果?

public class Test {

​    public static void changeStr(String str){
​        str="welcome";
  }
public static void main(String[] args) {
  String str="1234";
  changeStr(str);
  System.out.println(str); 
  }
}

输出结果:1234
这里虽然是一个静态方法,但是里面的变量是一个局部变量,
所以这里不因为是静态方法,就误认为里面的变量也是静态变量了

14.输出结果?
      public static void main(String[] args) {
        Map<byte[], String> m = new HashMap<byte[], String>();
        byte[] key = "abcd".getBytes();
        m.put(key, "abcd");
        System.out.println(m.containsKey(key));
        System.out.println(m.containsKey("abcd"));
        System.out.println(m.containsKey("1234".getBytes()));
    }
  
    答:返回true,false,false

15.输出结果?
    public static void main(String[] args) {
        String s="1\\2\\3\\4";
        //split the string with “\”
        String []result = s.split("____");
        for(String r:result){
            System.out.println(r);
        }
    }

答:返回1\2\3\4 

16.输出结果?
    public static void main(String[] args) {
        char[] c = new char[] { '1' };
        String s = new String(c);
        System.out.println("abcd" + c);
        System.out.println("abcd" + s);
    }

答:第一个out把abcd和对象打出来了,第二个out打出了abcd1 

17.输出结果?
    public static void main(String args[])throws Exception{
        int result = calc();
        System.out.print(result);
    }

    public static int calc() throws IOException {
        int ret = 0;
        try{
            ++ret;
            throw new IOException("try");
        } catch(IOException ioe){
            --ret; return ret;
        }finally{
            ++ret; return ret;
        }
    }

结果:1

18.输出结果?
public class Value {
    private int value;

    public int get() {
        return value;
    }

    public void set(int v) {
        value = v;
    }
}

import java.util.Iterator;

public class Values implements Iterable<Value> {
    public Values(int capacity) {
        this.capacity = capacity;
    }

    int count = 1;
    int capacity;
    Value v = new Value();

    public Iterator<Value> iterator() {
        return new Iterator<Value>() {
            public boolean hasNext() {
                return count <= capacity;
            }

            public Value next() {
                v.set(count++);
                return v;
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }
}
                                        
public class Main {
    public static void main(String[] args) {
        Values vs = new Values(10);
        Value result = null;
        for (Value v : vs) {
            if (result == null) {
                result = v;
            } else {
                result.set(result.get() + v.get());
            }
        }
        System.out.println(result.get());
    }
}       
                           
结果:20

19.输出结果?
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] b = new byte[]{(byte) 0x0, (byte) 0x1, (byte) 0x2
        };
        baos.write(b);
        baos.write(0x0102);
        byte[] result = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(result);
        System.out.println(bais.available());
    }
}

结果:4

20.输出结果?
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        try {
            int result = 3 / (2 - 2);
            System.out.print("result=" + result);
        } catch (Exception e) {
            System.out.print("exception,");
        } finally {
            System.out.print("Some process");
        }
    }
}

输出:exception,Some process

21.写一个方法将数字转为大写形式并打印。
  public static void Convert(int d) {
        String[] str = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String ss[] = new String[]{"个", "十", "百", "千", "万", "十", "百", "千", "亿"};
        String s = String.valueOf(d);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            String index = String.valueOf(s.charAt(i));
            sb = sb.append(str[Integer.parseInt(index)]);
        }
        String sss = String.valueOf(sb);
        int i = 0;
        for (int j = sss.length(); j > 0; j--) {
            sb = sb.insert(j, ss[i++]);
        }
        System.out.println(sb);
    }

22.运行结果?
class A {
    {
        System.out.print("a");
    }
    static {
        System.out.print("X");
    }

    public A() {
        System.out.print("1");
    }
}

class B extends A {
    {
        System.out.print("b");
    }
    static {
        System.out.print("Y");
    }

    public B() {
        System.out.print("2");
    }
}

public static class Extends {
    public static void main(String[] ars) {
        A ab = new B(); // 执行到此处,结果: XYa1b2
        B b = new B(); // 执行到此处,结果: a1b2
    }
}

结果:XYa1b2a1b2

23.运行结果?
public class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static void setPoint(final Point p) {
        p.x = 3;
        p.y = 5;
    }

    public static void main(String args[]) {
        Point p = new Point(2, 3);
        System.out.println("p.x=" + p.x + ",p.y=" + p.y);
        setPoint(p);
        System.out.println("p.x=" + p.x + ",p.y=" + p.y);
    }
}

结果:
p.x=2,p.y=3
p.x=3,p.y=5

24.设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
 public class ThreadTest {
    private int j;

    public static void main(String args[]) {
        ThreadTest tt = new ThreadTest();
        Inc inc = tt.new Inc();
        Dec dec = tt.new Dec();
        for (int i = 0; i < 2; i++) {
            Thread t = new Thread(inc);
            t.start();
            t = new Thread(dec);
            t.start();
        }
    }

    private synchronized void inc() {
        j++;
        System.out.println(Thread.currentThread().getName() + "-inc:" + j);
    }

    private synchronized void dec() {
        j--;
        System.out.println(Thread.currentThread().getName() + "-dec:" + j);
    }

    class Inc implements Runnable {
        public void run() {
            while (true) {
                inc();
            }
        }
    }

    class Dec implements Runnable {
        public void run() {
            while (true) {
                dec();
            }
        }
    }
}
25.位移?
左移动一位相当乘以2 右移动一位相当除以2 
16<<2 
16 左位移 2 位 等同于 16 * 2 * 2 也就等于64 

256>>4
256 右位移 4 位  等同于 256 / 2 / 2 / 2 / 2 也就等于16

26.分析运行结果?
public class Test {
    public static int k = 0;
    public static Test t1 = new Test("t1");
    public static Test t2 = new Test("t2");
    public static int i = print("i");
    public static int n = 99;
    private int a = 0;
    public int j = print("j");

    {
        print("构造块");
    }

    static {
        print("静态块");
    }

    public Test(String str) {
        System.out.println((++k) + ":" + str + "   i=" + i + "    n=" + n);
        ++i;
        ++n;
    }

    public static int print(String str) {
        System.out.println((++k) + ":" + str + "   i=" + i + "    n=" + n);
        ++n;
        return ++i;
    }

    public static void main(String args[]) {
        Test t = new Test("init");
    }
}
输出:
1:j   i=0    n=0
2:构造块   i=1    n=1
3:t1   i=2    n=2
4:j   i=3    n=3
5:构造块   i=4    n=4
6:t2   i=5    n=5
7:i   i=6    n=6
8:静态块   i=7    n=99
9:j   i=8    n=100
10:构造块   i=9    n=101
11:init   i=10    n=102

27.分析输出结果? 
public class XXX {
    public static int k = 0;
    public static XXX t1 = new XXX("t1");
    public static XXX t2 = new XXX("t2");
    public static int i = print("i");
    public static int n = 99;
    private int a = 0;
    public int j = print("j");

    {
        print("构造块");
    }

    static {
        print("静态块");
    }

    public XXX(String str) {
        System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
        ++i;
        ++n;
    }

    public static int print(String str) {
        System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
        ++n;
        return ++i;
    }

    public static void main(String args[]) {
        XXX t = new XXX("init");
    }
}

输出:
1:j   i=0    n=0
2:构造块   i=1    n=1
3:t1   i=2    n=2
4:j   i=3    n=3
5:构造块   i=4    n=4
6:t2   i=5    n=5
7:i   i=6    n=6
8:静态块   i=7    n=99
9:j   i=8    n=100
10:构造块   i=9    n=101
11:init   i=10    n=102

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值