JAVA面试题2(2)

2019-05-17 17:27

} } }

17.阅读下列程序,分析程序的输出结果: My func 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(); } }

18.分析以下代码,编译时会出现的错误信息是: Test.java:6:secret 可以在A中访问private (return a.secret++; 出错) class A{

private int secret; }

public class Test{ public int method(A a){ return a.secret++; }

public static void main(String args[]){ Test test=new Test(); A a=new A();

System.out.println(test.method(a)); } }

19、分析以下程序,写出运行结果: 1234 public class Test19 {

public static void changeStr(String str){ str=”welcome”; }

public static void main(String[] args) { String str=”1234″; changeStr(str); System.out.println(str); } }

20、分析以下程序,写出运行结果: ABDCBDCB public class Test20 { static boolean foo(char c) { System.out.print(c); return true; }

public static void main(String[] args) { int i = 2;

for (foo(?A?); foo(?B?) && (i < 4); foo(?C?)) { i++; foo(?D?); } } }

21.编写程序,要求创建一个Dog类,添加name,eyeColor属性,为该属性自动添加相应的set和get方法,并给出至少两个构造方法。 public class Dog {

private String name, eyeColor; // 无形参的构造方法 public Dog (){ this. name =”逗逗“;

this. eyeColor = ”黑“; }

// 有形参的构造方法

public Dog(String name, String eyeColor ){ this. name = name; this. eyeColor = eyeColor; }

public String getEyeColor() { return eyeColor; }

public void setEyeColor(String eyeColor) { this.eyeColor = eyeColor; }

public String getName() { return name; }

public void setName(String name) { this.name = name; } }

22.统计一个字符串中出现某个字母的次数(注意区分大小写)。 String类中的相关方法(具体用法请查看JDK帮助文档): length( ):计算字符串长度,得到一个int型数值; indexOf( ):在字符串中定位某个子串,并返回位置编号

substring( ):截取字符串中的一部分,并作为一个新字符串返回; equals( ):比较两个String内容是否完全相同。

String str = ”abckajbfhbbkhfgabkbjkdfasjkbdanjkasfbai”; String chr = ”b”; int count = 0;

for (int i = 0; i < str.length(); i++) { if (chr.equals(str.charAt(i))) count++; }

System.out.println(“The count is ” + count);

23. 创建一个桌子(Table)类,该类中有桌子名称、重量、桌面宽度、长度和桌子高度属性,

以及以下几个方法:

(1) 构造方法:初始化所有成员变量。 (2) area( ):计算桌面的面积。

(3) display( ):在屏幕上输出所有成员变量的值。 (4) changeWeight(int w):改变桌子重量。

在测试类的main()方法中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子属性的值。

package com.test; public class Table { String name;//名称 double weight;//重量 double width;//宽 double longth;//长 double height;//高 //构造方法

public Table(String name, double weight, double width, double longth, double height) { super();

this.name = name; this.weight = weight; this.width = width; this.longth = longth; this.height = height; }

//计算桌面的面积 public void area(){

System.out.println(“桌子面积是“+longth*width); }

// 在屏幕上输出所有数据成员的值 public void display(){

System.out.println(“桌子名称:”+name+”\\n”+”重量:”+weight+”\\n”+”宽:”+width+”\\n”+”长:”+longth+”\\n高:”+height); }

//改变桌子重量的方法

public void changeWeight(int i){ this.weight=i;

System.out.println(“面积改为“+this.weight); }

public static void main(String[] args) {

Table table=new Table(“红木桌“,100.5,3.2,2.3,1.5); System.out.println(“创建一个桌子对象,属性如下“); table.display(); table.area();

table.changeWeight(100);

System.out.println(“更改重量后,属性如下“); table.display(); } }

24.编写一个程序,在主类中创建和调用方法sumf(),方法sumf()的功能是进行两个浮点数的加法运算。试将12.7和23.4两个数相加并显示运算结果。 import java.util.Scanner; public class test {

static float sumf(float x,float y) { return x+y; }

public static void main(String[]args){ Scanner sc = new Scanner(System.in);

System.out.println(“输入2个浮点数求和表达式,如:23.4+67.01″); String []str = sc.next().split(“\\\\+”); float m = Float.parseFloat(str[0]); float n = Float.parseFloat(str[1]);

System.out.println(m+”+”+n+”=”+ sumf(m,n)); } } 习题5

1.接口与抽象类有哪些异同点?

在面向对象的概念中,我们知道所有的对象都是通过类来描绘的,但是反过来却不是这样。并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。抽象类往往用来表征我们在对问题领域进行分析、设计中得出的抽象概念,是对一系列看上去不同,但是本质上相同的具体概念的抽象。正是因为抽象的概念在问题领域没有对应的具体概念,所以用以表征抽象概念的抽象类是不能够实例化的。


JAVA面试题2(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《企业和公司法》模拟题1-4及答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: