0
点赞
收藏
分享

微信扫一扫

面试题~笔试易错题

1.当试图编译并运行下面程序时会出现什么结果()(单选)    
class A {
public int getNumber(int a) {
return a + 1;
}
}
class B extends A {
public int getNumber(int a, char c) {
return a + 2;
}
public static void main(String[] args) {
B b = new B();
System.out.println(b.getNumber(0));
}
}
a) 编译错误
b) 运行错误
c) 1
d) 2
说明:C,会执行父类的getNumber()方法

2.当编译并运行下面程序时会出现什么结果()(单选)
public class MyAr {
public static void main(String argv[]) {
int[] i = new int[5];
System.out.println(i[5]);
}
}
a) 编译错误
b) 运行错误
c) 输出0 d)输出“null”
说明:B,Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException:5

3.当编译并运行下面程序时会发生什么结果:(单选)
public class Hope {
public static void main(String argv[]) {
Hope h = new Hope();
}
protected Hope() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
a) 编译错误,构造方法不能被声明为protected
b) 运行错误,构造方法不能被声明为protected
c) 编译并运行输出0到10
d) 编译并运行输出0到9
说明:D

4.以下哪些表达式返回为真()(多选)
String a =
My field1”;
String b =
My field1”;
String c = new String(”My field1”);
String d = new String(”My field1”);
a) a == b
b) a== c
c) c==d
d) a.equals(b)e)a.equals(c)f)c.equals(d)
说明:ADEF

5.关于线程,以下说法正确的是:()(多选)
a) sleep方法执行时会释放对象锁。
b) wait方法执行时会释放对象锁。
c) sleep方法必须写在同步方法或同步块中
d) wait方法必须写在同步方法或同步块中
说明:BD

6.下面关于网络通信正确的描述是()(多选)
a) TCP/IP协议是一种不可靠的网络通信协议。
b) UDP协议是一种可靠的网络通信协议。
c) TCP/IP协议是一种可靠的网络通信协议。
d) UDP协议是一种不可靠的网络通信协议。
说明:CD

7.在Java中,下面关于抽象类的描述正确的是()(多选)
a) 抽象类可以被实例化
b) 如果一个类中有一个方法被声明为抽象的,那么这个类必须是抽象类
c) 抽象类的方法都必须是抽象的
d) 声明抽象类必须带有关键字abstract
说明:BD,抽象类不能直接实例化

8.给定java代码如下,编译运行结果是()(单选)
public class Test {
public int count() {
return 1 % 9;
}
public static void main(String[] args) {
System.out.println(count());
}
}
a) 编译错误
b) 运行错误
c) 正常运行,输出1
d)正常运行,输出0
说明:A,count()方法不是静态方法,不能再main()方法中直接调用


9.以下代码输出结果为() (单选)
public class Test {
public static String output = "";
public static void foo(int i) {
try {
if (i == 1) {
throw new Exception();
}
output += "1";
} catch (Exception e) {
output += "2";
return;
} finally {
output += "3";
}
output += "4";
}
public static void main(String args[]) {
foo(0);
foo(1);
System.out.println(output);
}
}
a) 1342 b) 123 c) 134234 d) 13423
说明:D

10.给定一个java程序的main方法的代码片段如下:假如d目录下不存在abc.txt文件,现运行该程序,下面的结果正确的是():(单选)
try {
PrintWriter out = new PrintWriter(new FileOutputStream("d:/abc.txt"));
String name = "chen";
out.print(name);
} catch(Exception e) {
System.out.println("文件没有发现!");
}
a) 将在控制台上打印:"文件没有发现!"
b) 正常运行,但没有生成文件abc.txt
c) 运行后生成abc.txt,但该文件中可能无内容
d) 运行后生成abc.txt,该文件内容为:chen
说明:C,已使用idea运行,添加out.close();代码之后,会选择D

11.下面哪些代码可放在Here处( ):(多选)
class Super {
protected float getNum() {
return 3.0f;
}
}
public class Sub extends Super {
//Here
}
a) float getNum() {
return 4.0f;
}

b) public void getNum() {
}

c) private void getNum(double d) {
}

d) public double Getnum() {
return 4.0d;
}

e) public float getNum() {
return 9;
}
说明:CDE,方法重写的时候,子类的权限修饰符必须大于等于父类

12.研究下面JAVA代码
public class TestException {
public static void main(String[] args) {
try {
System.out.println("hello,jr");
System.exit(0);
} finally {
System.out.println("88");
}
}
}
输出结果为()(单选)
a) hello,jr
b)88
c)hello,jr后是88
d)不能编译
说明:A,System.exit(0)是阻止finally中代码块执行的方法。

13.有以下代码:
package com;
public class Test {
public static void main(String[] args) {
Test test = new Test();//here
}
}
在here处加上以下什么语句,可以获得Class对象?(多选)
a) Class c = new Class();
b) Class c = test.class;
c) Class c = Test.class;
d) Class c = test.getClass();
e) Class c = Class.forName(“com.Test”);
f) Class c = Class.forName(“Test”);
说明:CDE

14.写出下面代码的输出
public class A {
public A() {
System.out.println("一个新的class A");
}
{
System.out.println("A");
} static {
System.out.println("static A");
}
}
public class B extends A {
public B() {
System.out.println("一个新的class B");
}
{
System.out.println("B");
}
static {
System.out.println("static B");
}
}
public class Test {
public static void main(String[] args) {
new B();
}
}
说明: static A static B A 一个新的class A B 一个新的class B
考察知识点:Java类的加载顺序:
1> 静态变量
2> 静态代码块
3> 成员属性
4> 成员代码块
5> 构造代码块

举报

相关推荐

0 条评论