1、main方法是Java Application程序执行的入口点,关于main方法的方法头以下哪项是合法的( )?
A、public static void main( )
B、public static void main( String a[] )
C、public static int main(String [] arg )
D、public void main(String arg[] )
2、下列程序编译或运行的结果是。( )
public static void main(String args[]){
int a = 10; int b,c;
if(a>50){ b=9; }
c = b + a;
System.out.println(c);
}
A.10 B. 19 C.9 D.编译错误
3、下列语句片段中,four的值为 ( )
int three = 3; char one = ‘1’;
char four = (char)(three+one);
A. 3 B. 1 C. 31 D. 4
4、以下程序的输出结果为( )。
public static void main(String[]args) {
int x = 1, y = 1, z =1;
if(x--==1&&y--==1||z--==1){
System.out.println(“x=” +x+”,y=” +y+”,z=” +z);
}
}
A.x=0, y=1,z=1 B.x=0,y=2,z=2
C.x=0,y=2,z=1 D.x=0,y=0,z=1
5、程序的执行结果是 ( )
public class Test{
int x;
public static void main(String[] args){
Test t = new Test();
t.x = 5;
change(t);
System.out.println(t.x);
}
public static void change(Test t){
t.x = 3;
}
}
A. 5 B. 3 C. 0 D. 4
6、Java中单一文件里的关键字class import package 出现的顺序应为:( )
A.class package import B. class import package
C. package import class D. import package class
7、关于下列代码书法正确的是 ( )
public class A{
public void doit(){}
public String doit(){ return “a”; }
public double doit(int x){ return 1.0; }
}
A. 无编译错误
B. 代码public String doit()行,出现编译错误
C. 代码public double doit(int x)行,出现编译错误
D. 代码return “a”;行处 出现编译错误
8、编译Java Application 源程序文件将产生相应的字节码文件,这些字节码文件的扩展名为( )。
A. java B. class C. html D. exe
9、下面哪个表达式可用得到x和y的最大值( )?
A) x>y?y:x B) x<y?y:x C) x>y?(x+y):(x-y) D) x==y?y:x;
10、 给定Java代码如下所示,在横线处新增下列( )方法,是对cal方法的重载。
public class Test{
public void cal(int x, int y, int z) {}
横线
}
A. public int cal(int x, int y, float z){ }
B. public int cal(int x, int y, int z){ return 0; }
C. public void cal(int x, int z){ }
D. public void cal(int z, int y, int x){ }
二、看程序写结果(20分)
1、
int a[] = { 2, 3, 4, 5, 6 };
for (int i = a.length - 1; i >= 0; i--)
System.out.print(a[i] + "");
2、
public class Person {
String name; int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Person c = new Person("Peter", 17);
System.out.println(c.name + " is " + c.age + " years old!");
}
}
3、
int[] x={122,33,55,678,-987};
int k=x[0];
for(int i=1;i<x.length;i++){
if(x[i]>k)
k=x[i];
}
System.out.println(k);
4、
public class Test{
public static void main(String[] args){
Son s = new Son();
}
}
class Parent{
{
System.out.println(“A”);
}
static{
System.out.println(“B”);
}
public Parent(){
System.out.println(“C”);
}
}
class Son extends Parent{
{
System.out.println(“D”);
}
static{
System.out.println(“E”);
}
public Son(){
System.out.println(“F”);
}
}
5.
interface Cryable {
void cry();
}
class Dog implements Cryable {
publicvoid cry() {
System.out.print("汪汪 ");
}
}
class Cat implements Cryable {
public void cry() {
System.out.print("喵喵 ");
}
public static void main(String[] args) {
Cryable c = new Dog();
c.cry();
c = newCat();
c.cry();
}
}
三、简单题(20分)
1、写出类Student的无参构造方法
2、方法重载和方法重写的区别
3、简述final关键字的特点
4、用两种方法定义一个String类型的二维数组并赋值
5、抽象类和接口的区别
四、编程题
1、请写出两种单例模式的实现方式(10分)
2、定义一个方法,将一个数组逆序返回(10分)
3、 按如下要求编写Java程序:(20分)
(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
(2)定义接口B,里面包含抽象方法void setColor(String c)。
(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。
(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
圆柱体的高height、颜色color。
(5)创建主类来测试类Cylinder。
附录答案
一(每个两分)
1-5:BDDDB
5-10:CBBBC
二(每个4分)
1.6,5,4,3,2
2.Peter is 17 years old!
3.678
4.B E A C D F
5.汪汪 喵喵
三(每个4分)
1. public Student(){}
2. 方法的重载(overload):方法名一样,参数列表不同,和返回值类型没有关系
方法的重写(override):子类中定义了和父类中一模一样的方法,但是方法体不同
重载和重写的区别;
(1)重写必须继承,重载不用。
(2)重写的方法名,参数数目相同,参数类型兼容,重载的方法名相同,参数列表不同。
(3)重写的方法修饰符的优先级别大于等于父类的方法,重载和修饰符无关。
(4)重写不可以抛出父类没有抛出的一般异常,可以抛出运行时异常
3. final:最终的
修饰类:类就不能被继承
修饰方法:方法不能重写
修饰变量:变成了常量
4. int[][] arr = new int[2][3];
arr[1][2]=5;
int[][] arr = new int[2][];
a[0]=new int[3]
a[0][2] = 5;
int[][] c = new int[][]{
{1,2,4},
{4,5},
{6,7,8,9}
};
5. 抽象类:在实际开发中,父类中有些操作是无法给出具体实现,但是又要申明子类必须实现的方法,就需要声明为抽象方法,
在java中如果一个类中有了抽象方法,那么这个类必须定义为抽象类
接口:把一些额外的功能定义到一个接口中,不给具体的实现,某一个类想要具有这些额外的功能,实现这个接口即可
接口和抽象类的区别
(1)接口是公开的,里面不能有私有的方法或变量,是让别人使用的,而抽象类是可以有私有方法或私有变量的,
(2)实现接口的一定要实现接口里定义的所有方法,而实现抽象类可以有选择地重写需要用到的方法,
(3)接口可以实现多重继承,而一个类只能继承一个父类,但可以通过继承多个接口实现多重继承.
四(10+10+20)
1. 饿汉式: public class Singleton1{
private static Singleton1 s = new Singleton1();
private Singleton1(){
}
public static Singleton1 getInstance(){
return s;
}
}
懒汉式:
public class Singleton2{
private static Singleton2 s = null;
private Singleton2(){
}
public static Singleton2 getInstance(){
if(s==null){
s = new Singleton2();
}
return s;
}
}
2. //定义一个方法,将一个数组逆序返回
public class Test1 {
public static void main(String[] args) {
int [] a= new int[5];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
Test1.nixu(a);
}
private static void nixu(int[] a) {
for(int j=a.length-1;j>=0;j--){
System.out.println(a[j]);
}
}
}
3.
public class Test2{
public static void main(String[] args) {
Test2.Cylinder v = new Test2().new Cylinder();
System.out.println("area = "+v.area());
System.out.println("volume = "+v.volume());
System.out.println("color = "+v.color);
}
interface A{
double PI = 3.14;
abstract double area();
}
interface B{
abstract void setColor(String c);
}
interface C extends A,B{
abstract double volume();
}
public class Cylinder implements C{
public double radius = 1.0;
public double height = 1.0;
public String color = "blue";
public double area(){
return ((this.radius)*(this.radius)*(this.PI));
}
public void setColor(String c){
System.out.println("blue");
}
public double volume(){
return ((this.radius)*(this.radius)*(this.PI)*(this.height));
}
}
}