JavaSE面向对象小测试

来源:互联网 发布:北京sql培训班 编辑:程序博客网 时间:2024/05/21 00:51

java面向对象考试
一:看程序写结果(每题5分,5*13=65)
1.写出下面程序运行的结果
int x = 1,y = 1;
if(x++==2 & ++y==2)
{
x =7;
}
System.out.println(“x=”+x+”,y=”+y);

//x=2,y=2

2.写出下面程序运行的结果
boolean b = true;
if(b=false)
System.out.println(“a”);
else if(b)
System.out.println(“b”);
else if(!b)
System.out.println(“c”);
else
System.out.println(“d”);

//c
3.以下代码运行时得到什么打印结果?
int output=10;
boolean b1 = false;
if((b1==true) && ((output+=10)==20)){
System.out.println(“We are equal “+output);
}else{
System.out.println(“Not equal! “+output);
}

// Not equal!10
4.以下代码运行时得到什么打印结果?
int i = 4;
switch (i) {
default:
System.out.println(“default”);
case 0:
System.out.println(“zero”);
break;
case 1:
System.out.println(“one”);
case 2:
System.out.println(“two”);

}
//default
Zero
不管default之后的条件是否满足,都会继续向下执行,直到遇到break,才会退出循环,因为没有break它不会中断,会当成上一个条件中的内容进行输出。
5.以下代码运行时得到什么打印结果?
public class Hope{
public static void main(String argv[]){
Hope h = new Hope();
}
protected Hope(){
int i=1;
do{
System.out.println(i);
}while(++i<3);
}
}
//1
2
6.题目:输出9*9口诀。(用代码完成需求)
程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

public class Multi{

public static void main(String[] args){for(int i=1;i<9;i++)//i每次从1开始循环{   for(int j=i;j<=i;j++)//j每次从i开始循环   {       System.out.println(i+"*"+j+"="+i*j+" ");   }   System.out.println();//i每次循环一次后换行}

}

7.以下代码共创建了几个对象?
String s1=new String(“hello”);
String s2=new String(“hello”);
String s3=s1;
String s4=s2;
//2个
8.对于以下程序,运行“java Pass”,得到什么打印结果?
public class Pass{
static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);//10
System.out.println(j);//40
}

    public void amethod(int x){        x=x*2;//20        j=j*2;//40    }

}

//10
40

9.下列的代码哪个正确
a) class Fruit { }
public class Orange extends Fruit {
public static void main(String[] args){
Fruit f=new Fruit();
Orange o=f;
}
}
//从右向左看,水果不一定是橙子。
b) class Fruit {}
public class Orange extends Fruit {
public static void main(String[] args){
Orange o=new Orange();
Fruit f=o;
}
//从右向左看,橙子一定是水果。
c) interface Fruit {}
public class Apple implements Fruit {
public static void main(String[] args){
Fruit f=new Fruit();//不能实例化接口
Apple a=f;
}
}
d) interface Fruit {}
public class Apple implements Fruit {
public static void main(String[] args){
Apple a=new Apple();
Fruit f=a;
}
}

//b对,d对

10.分析下面的程序,写出运行结果。

class Aclass {
Public void go() {
System.out.println(“Aclass”);
}
}

public class Bclass extends Aclass {
Public void go() { System.out.println(“Bclass”); }
public static void main(String args[]) {

            Aclass a = new Aclass();            Aclass a1 = new Bclass();            a.go();            a1.go();

} }
// Aclass
Bclass

11.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果?
abstract class Base{
abstract public void myfunc();
public void another(){
System.out.println(“Another method”);
}
}
public class Abs extends Base{
public static void main(String argv[]){
Abs a = new Abs();
a.amethod();
}

public void myfunc(){    System.out.println("My func");}   public void amethod(){    myfunc();}

}
// My func
12.以下代码能否编译通过?
interface A{
int x = 0;
Public A(){
x= 5;
}

Public A(int s){        x = s;}

}
//不能,x为静态常量,不能更改
13.以下的运行结果是什么:
class Fu {
static {
System.out.println(“静态代码块Fu”);
}

{    System.out.println("构造代码块Fu");}public Fu() {    System.out.println("构造方法Fu");}

}

class Zi extends Fu {
static {
System.out.println(“静态代码块Zi”);
}

{    System.out.println("构造代码块Zi");}public Zi() {    System.out.println("构造方法Zi");}

}

class ExtendsTest2 {
public static void main(String[] args) {
Zi z = new Zi();
}
}

/*
静态代码块Zi
构造代码块Zi
静态代码块Fu
构造代码块Fu
构造方法Fu
构造方法Zi
*/

二.简答(每题5分,5*5= 25)
1.方法重写和方法重载的区别?方法重载能改变返回值类型吗? 
方法重写:方法返回值,方法名,参数,都和父类相同
方法重载:方法重载出现在同一个类中
方法名必须相同,方法参数必须不同
方法重载可以改变返回值类型

2.请简述类与类,类与接口以及接口与接口的关系?
类与类:
继承
类与接口:
类实现接口
接口与接口:
继承

3.给成员变量赋值有几种方式?分别是什么?
五种:
1,定义时赋值
2,构造方法中赋值
3,构造代码块中赋值
4,set方法
5,通过实例化类的对象进行赋值

4.多态是什么?多态的前提是什么?

多态:同一种事物表先出的不同状态
前提:
a:必须有继承关系
b:方法重写
c:必须有父类引用指向子类对象:向上转型

5.this关键字和super关键字分别代表什么?以及他们各自的使用场景和作用
this:对当前对象的引用
使用场景:子类,父类中都可以
作用:确保是对当前对象的引用

super:对父类对象的引用
使用场景:子类的构造方法中,以及需要在子类中需要调用父类的成员
作用: 在子类中需要调用父类的成员

三:编程(每道题10分,10*3 = 30)
1.请用封装的方式定义一个员工类(属性私有化,提供setXxx()/getXxx()方法,无参有参数构造,成员方法)
员工属性:姓名,年龄,编号
员工功能:吃饭,睡觉,打游戏

2.请利用继承,抽象类,接口的方式设计如下程序
程序员:
属性:1.姓名,2.年龄
功能:1.会写程序(程序员写的简单的代码,抽象类实现),2.睡觉

项目经理:
属性:1.姓名,2.年龄
功能,1.会写程序(项目经理写的是复杂的代码,抽象类实现),2.睡觉,3.通过努力给自己扩展了一种说日语的能力(接口实现)

3: 补齐代码(10分):在控制台输出helloworld
interface Inter {
void show();
}

class Outer {
//补齐代码:::::::::::::::

}

class OuterDemo {
public static void main(String[] args){
Outer.method.show();

}

}
补齐代码:
public static Inter method()
{
Return i=new Inter(){
Public void show(){
System.out.println(“helloword”);
}
};
}

原创粉丝点击