1.代码块的面试题(看程序结果)
class Student {
static {
System.out.println("Student 静态代码块");
}
{
System.out.println("Student 构造代码块");
}
public Student() {
System.out.println("Student 构造方法");
}
}
class StudentDemo {
static {
System.out.println("StudentDemo的静态代码块");
}
public static void m**加粗样式**ain(String[] args) {
System.out.println("我是main方法");
Student s1 = new Student();
Student s2 = new Student();
}
}
StudentDemo的静态代码块
我是main方法
Student 静态代码块
Student 构造代码块
Student 构造方法
Student 构造代码块
Student 构造方法
2.继承中的面试题(看程序写结果)
class Fu{
public int num = 10;
public Fu(){
System.out.println("fu");
}
}
class Zi extends Fu{
public int num = 20;
public Zi(){
System.out.println("zi");
}
public void show(){
int num = 30;
System.out.println(num);
System.out.println(this.num);
System.out.println(super.num);
}
}
class Test {
public static void main(String[] args) {
Zi z = new Zi();
z.show();
}
}
fu
zi
30
20
10
3.抽象类中的面试题
A:面试题1
一个类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?
答案: 可以 . 不能创建对象.
B:面试题2
abstract不能和哪些关键字共存?
private 冲突
final 冲突
static 不能共存 无意义
4.成员内部类的面试题
A:面试题
要求:使用已知的变量,在控制台输出30,20,10。
class Outer {
public int num = 10;
class Inner {
public int num = 20;
public void show() {
int num = 30;
System.out.println(?);num
System.out.println(??);this.num
System.out.println(???);new Outer().num
}
}
}
class InnerClassTest {
public static void main(String[] args) {
Outer.Inner oi = new Outer().new Inner();
oi.show();
}
}
5.匿名内部类面试题
A
interface Inter {
public static final int a = 23 ;
}
public class Test {
public static void main(String[] args) {
new Inter() {
public void show() {
// 这个this表示的是匿名内部类的这个对象
System.out.println(this.a);23
}
}.show();
}
}
6.请编写程序,统计键盘录入的字符串中包含大写字母、小写字母、数字的个数,并测试。
public class Demo9 {
public static void main(String[] args) {
System.out.println("请输入");
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
byte[] b = a.getBytes();
int c = 0, d = 0, e = 0;
for (int i = 0; i < b.length; i++) {
if (b[i] >= 65 && b[i] <= 90) {
c++;
} else if (b[i] >= 97 && b[i] <= 122) {
d++;
} else if (b[i] >= 48 && b[i] <= 57) {
e++;
} else {
continue;
}
}
System.out.println("该字符串长为"+b.length);
System.out.println("大写字母有" + c + "个");
System.out.println("小写字母有" + d + "个");
System.out.println("数字有" + e + "个");
}
}