文章目录
1、类Account中方法声明正确的是哪几项? c
A.class Account {
deposit();
}
B.interface Account{
void deposit();
}
C.class Account{**
void deposit(){ }
}**
D. class Account{
void deposit{ }
}
2、如果想要一个类不能被任何类继承的话,需要使用哪个关键字来修饰该类? d
A. abstract
B. new
C. static
D. final
3、现有:
public class Pet{ }
public class Cat extends Pet{)
执行代码
Cat c= new Cat();
Pet p= (Pet)c;
后下列哪项是正确的?
A. Pet p=(Pet)c运行错误
B. Pet p=(Pet)c编译错误
C. Pet p= (Pet)c正常执行
D.以上都不对
4、程序:
public class Pet{
public void speak(){
System.out.print(“pet”);
}
}
public class Cat extends Pet{
pulolic void speak(){
System.out.print(“Cat”);
}
}
public class Dog extends Pet(
public void speak(){
System.out.print(“Dog”);
}
}
执行代码
Pet[ ] p={new Cat (),new Dog(),new Pet()};
for (int i=0;i<p.length;i++)
p[i] .speak();
后输出的内容是哪项?
petCatpetDog
pet
5、现有:
1.class Dog{ }
\2. class Harrier extends Dog { }
3.
\4. class DogTest{
5.public static void main (String [] args){
6. Dog dl=new Dog();
\7. Harrier hl=new Harrier();
8. Dog d2=hl;
\9. Harrier h2= (Harrier) d2;
10. Harrier h3=d2;
\11. }
\12. }
下面哪一项是正确的?
A.2个Dog对象被创建
B.2个Harrier对象被创建
C.3个Harrier对象被创建
D.编译失败
6、 现有:
class Bird {
void talk() { System.out.print("chirp "); }
}
class Parrot2 extends Bird {
protected void talk() { System.out.print("hello "); }
public static void main(String [] args) {
Bird [] birds = {ne Bwird(), new Parrot2 () };
for( Bird b : birds){
b.talk () ;
}
}
}
结果是什么 ?
A. chirp chirp
B. hello hello
C. chirp hello
D:编译错误
7、下列有关接口的叙述错误的是哪项?
A. 接口中只能包含抽象方法和常量
B.一个类可以实现多个接口
C.类实现接口时必须实现其中的方法
D.接口不能被继承
8、 现有:
1.interface Animal {
\2. void eat();
3.}
4.
\5. // insert code here
6.
\7. public class HouseCat implements Feline {
8. public void eat() { }
9.}
和以下三个接口声明:
interface Feline extends Animal{ }
interface Feline extends Animal { void eat(); }
interface Feline extends Animal { void eat() { } }
分别插入到第5行,有多少行可以编译?
A. 0
B. 1
C. 2
D. 3
9、现有:
class ClassA{ }
class ClassB extends ClassA { }
class ClassC extends ClassA { }
以及:
ClassA p0=new ClassA();
ClassB p1=new ClassB();
ClassC p2=new ClassC();
ClassA p3=new ClassB();
ClassA p4=new ClassC();
下列哪些是正确的?(选三项)
A.p0=p1;
B.p1 =p2;
C.p2=p4;
D.p2 = (ClassC)p1;
E.p1 = (ClassB)p3;
F.p2 = (Classc)p4;
10、现有:
class Pencil {
public void write (String content){
System.out.println (“Write”+content);
}
}
class RubberPencil extends Pencil{
public void erase (String content){
System.out.println (“Erase”+content);
}
}
执行下列代码的结果是哪项?
Pencil pen=new RubberPencil();
pen.write (“Hello”);
pen.erase (“Hello”);
A. Write Hello
Erase Hello**
B. Erase Hello
Write Hello
C.编译错误
D.运行时抛出异常
// pen.erase("Hello"); 需要向下转型
RubberPencil rubberPencil = (RubberPencil)pen;
rubberPencil.erase("Hello");
11、根据所给图完成类的设计
-
实线:继承 子类指向父类
-
虚线:接口 接口实现类指向接口
package com.ffyc.javaarray; public interface Task { public abstract void working(); void rest(); default void show() { } }
package com.ffyc.javaarray; public abstract class Person { String name; char sex; int age; public Person(String name, char sex, int age) { this.name = name; this.sex = sex; this.age = age; } public Person() { } public abstract void saying(); }
package com.ffyc.javaarray; public class Teacher extends Person implements Task{ private String department ; public Teacher(String department) { this.department = department; } public void teaching(){ System.out.println("老师正在教书"); } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public void saying() { } @Override public void working() { } @Override public void rest() { } }
package com.ffyc.javaarray; public class Student extends Person implements Task{ private String id; public Student(String id) { super(); this.id = id; } public void reading(){ System.out.println("学生正在读书"); } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public void saying() { } @Override public void working() { } @Override public void rest() { } }
12、数组练习题:
1、有一个数组:
int [] arr = {8,4,2,1,23,344,12}
猜数游戏:从键盘中任意输入一个数据,判断数组中是否包含此数
遍历
package com.ffyc.javaarray;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
//猜数游戏:从键盘中任意输入一个数据,判断数组中是否包含此数
int [] arr = {8,4,2,1,23,344,12};
System.out.println("请输入一个数字:");
Scanner scanner = new Scanner(System.in);
int temp = scanner.nextInt();
for (int i = 0; i < arr.length; i++) {
if(temp == arr[i]){
System.out.println("数组中包含:"+ temp);
}
}
}
}
2、把一个数组的元素逆序交换,
int[] a = { 5, 11, 15, 24, 36, 47, 59, 66 };
交换元素后
int[] b = { 66 ,59 ,47 ,36 ,24,15 ,11 ,5 };
package com.ffyc.javaarray;
import java.util.Arrays;
public class Demo5 {
/*
把一个数组的元素逆序交换
int[] a = { 5, 11, 15, 24, 36, 47, 59, 66 };
交换后: 0 1 2 3 4 5 6 7
int[] b = { 66 ,59 ,47 ,36 ,24,15 ,11 ,5 };
*/
public static void main(String[] args) {
int [] a = { 5, 11, 15, 24, 36, 47, 59, 66 };
int [] res = new int[a.length];
for (int i = 0; i < a.length; i++) {
res[i] = a[a.length-1-i];
}
System.out.println("倒序后前:");
for(int t : a){
System.out.print(t+"\t");
}
System.out.println(Arrays.toString(a));
System.out.println("倒序后:");
for(int t : res){
System.out.print(t+"\t");
}
System.out.println(Arrays.toString(res));
}
}
3.现在有如下一个数组:
int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
要求将以上数组中的0项去掉,将不为0的值存入一个新的数组.
package com.ffyc.javaarray;
import java.util.Arrays;
public class Demo6 {
/*
int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
要求将以上数组中的0项去掉,将不为0的值存入一个新的数组.
*/
public static void main(String[] args) {
int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int count = 0;
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] != 0){
count ++ ;
}
}
System.out.println(Arrays.toString(oldArr));
System.out.println(count);
int index = 0;
int [] arr = new int[count];
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] != 0){
arr[index] = oldArr[i];
index++;
}
}
System.out.println(Arrays.toString(arr));
}
}
4. 定义一个长度为10的int数组,自定义一组数据,统计数组中的总和、平均值、最大值、最小值、以及奇数和偶数的个数.
package com.ffyc.javaarray;
public class Demo7 {
/*
定义一个长度为10的int数组,
自定义一组数据,统计数组中的总和、平均值、最大值、最小值、以及奇数和偶数的个数.
*/
public static void main(String[] args) {
// int [] arr = new int[10];
int [] arr = new int[]{1, 4, 6, 4, 3, 8, 6, 5, 4, 8};
Demo7 demo7 = new Demo7();
demo7.arraySum(arr);
demo7.arrayAverage(arr);
demo7.max(arr);
demo7.min(arr);
demo7.arrayEven(arr);
demo7.arrayOdd(arr);
}
//sum
public void arraySum(int [] arr){
int sum = 0 ;
for (int i = 0; i < arr.length; i++) {
sum = arr[i] + sum;
}
System.out.println("sum:"+sum);
}
//average
public void arrayAverage(int [] arr){
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = arr[i] + sum;
}
double average = sum/arr.length;
System.out.println("average:"+average);
}
//max
public void max(int [] arr){
double maxIndex = arr[0];
for (int i = 0; i < arr.length; i++) {
if ( arr[i] > maxIndex){
maxIndex = arr[i];
}
}
System.out.println("max:"+maxIndex);
}
//min
public void min(int [] arr ){
double minIndex = arr[0];
for (int i = 0; i < arr.length; i++) {
if ( arr[i] < minIndex){
minIndex = arr[i];
}
}
System.out.println("min:"+minIndex);
}
//odd 奇数
public void arrayOdd(int [] arr){
System.out.println("\n奇数:");
for (int i = 0; i < arr.length; i++) {
if(arr[i]%2 != 0){
System.out.print(arr[i]+"\t");
}
}
}
//even 偶数
public void arrayEven(int [] arr){
System.out.println("\n偶数:");
for (int i = 0; i < arr.length; i++) {
if(arr[i]%2 == 0){
System.out.print(arr[i]+"\t");
}
}
}
}
5. 任意一个人输入年龄,就可以判断出他是哪个年龄段的人?
0-9 儿童
10-19 少年
20-29 青少年
30-39 青年
40-49 壮年
50-59 中年
60-69 中老年
70-79 老年
80-89 老老年
90-99 老老老年
package com.ffyc.javaarray;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
public class Demo8 {
/*
任意一个人输入年龄,就可以判断出他是哪个年龄段的人?
0-9 儿童
10-19 少年
20-29 青少年
30-39 青年
40-49 壮年
50-59 中年
60-69 中老年
70-79 老年
80-89 老老年
90-99 老老老年
//1.switch
int age = 34;
switch(age/10){
case 0: ....0-9....
case 1: ....10-19....
case 2: .....
}
//2.if...else.....
if(age>=0 && age<10){
.....
}else if(age>=10 && age< 20){
....
}else......
//3.数据结构的思想
int age = 22;
String[] arr = {"儿童","少年","青少年","青年","壮年","中年","","","",""};
int idx = age/10;
System.out.println(arr[idx]);
*/
public static void main(String[] args) {
int age = 40;
String [] arr = {"儿童","少年","青少年","青年","壮年","中年","中老年","老年","老老年","老老老年"};
int index = age/10;
System.out.println(arr[index]);
}
}
6.写出结果
package com.ffyc.javaarray;
public class Demo9 {
public static void main(String[] args){
String foo="blue";
boolean[] bar=new boolean[2];
if(bar[0]){
foo="green";
}
System.out.println(foo);
}
}
blue