黑马程序员 基础测试题

这是一个包含多个Java编程基础测试题的集合,涵盖了时间计算、排序算法(选择排序)、TCP与UDP协议适用场景、继承原理、构造方法、字符串组合与文件操作等知识点。题目涉及从键盘接收数字并转化为时间表达、实现选择排序算法、分析继承机制的工作原理、讨论TCP和UDP的特性以及如何在不同场景下选择,还包含了如何在类中创建成员变量并使用set和get方法、在嵌套类中访问变量、列出字符串全字符组合以及编写文件拷贝和扩展名转换的程序。这些题目旨在考察和巩固Java编程基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Test1 {


/**
* 第1题:求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55

* @param args
*/
public static void main(String args[]) {
int n, fun;// n为第n项,fn为第n项的值
java.util.Scanner in;
while (true) {//循环输出数字
in = new Scanner(System.in);
n = in.nextInt();
if(n==0){//输入0跳出循环结束输入
break;
}
while (n >= 30 || n <= 0) {//输入范围0-30之间
System.out.println("请重新输入n的范围为  0<n<30");
in = new Scanner(System.in);
n = in.nextInt();
}
fun = function(n);
System.out.println("斐波那契数列第" + n + "项为:" + fun);
}
}


public static int function(int n) {
if (n == 1 || n == 2)
return 1;
return function(n - 1) + function(n - 2);
}


}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Test2 {


/**

* 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如:

* 键盘输入6,打印6秒;//
* 键盘输入60,打印1分; //
* 键盘输入66,打印1分6秒; //
* 键盘输入666,打印11分6秒;//
* 键盘输入3601,打印1小时1秒//

* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(function(60*60*24+1));// 调用函数并打印
}


// 计算时间函数
public static String function(int number) {
String strDate = "键盘输入" + number + ",打印";
int final_day = 60 * 60 * 24;// 天
int final_h = 60 * 60;// 小时
int day = number / final_day;
int h = (number % final_day) / final_h;
int min = ((number % final_day) % final_h) / 60;// 分钟
int s = ((number % final_day) % final_h) % 60;// 秒
if (day > 0) {
strDate = strDate + day + "天";
}
if (h > 0) {
strDate = strDate + h + "小时";
}
if (min > 0) {
strDate = strDate + min + "分钟";
}
if (s > 0) {
strDate = strDate + s + "秒";
}
return strDate + ";";
}
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Test3 {


/**
* 3、 请列举您了解的一些排序算法,并用Java语言实现一个效率较高的。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {1,3,5,7,9,8,6,4,2};
print(function(arr));


}
/**
* 对数组进行排序 :选择排序
* @param arr无须数组
* @return 排好序的数组
*/
public static int[] function(int[] arr) {
int len = arr.length;
//循环数组
for (int i = len - 1; i > 0; i--) {
int index = i;//保存数组小标
for (int j = 0; j < i; j++) {
//比较数组中数字大小
if (arr[j] > arr[index]) {
index = j;
}
}
//如果前面的数组大于后面的数字进行交换
if (arr[index]>arr[i]) {
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
}
return arr;
}
//打印数组值
public static void print(int arr[]){
for(int a: arr){
System.out.print(a + " ");
}
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Test4 {


/**
* 4、 什么情况下适合用UDP协议,什么情况下适合用TCP协议?

* @param args
*/
// TCP:
// 1.建立连接,形成传输数据的通道。
// 2.在连接中进行大数据量传输
// 3.通过三次握手完成连接,是可靠协议
// 4.必须建立连接,效率会稍低。如:打电话,下载
// UDP:
// 1.将数据及源和目的封装成数据包中,面向无连接
// 2.每个数据包的大小在限制在64k内
// 3.因无连接,是不可靠协议
// 4.不需要建立连接,速度快。如:聊天,视频会议,桌面共享。
//


}

--------------------------------------------------------------------------------------------------------------------------------------------------

public class Test5 {


/**
* 5、 分析运行结果,说明原理。(没有分析结果不得分)

* @param args


*            继承的基本概念: 
*             (1)Java不支持多继承,也就是说子类至多只能有一个父类。
*             (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法。
*           (3)子类中定义的成员变量和父类中定义的成员变量相同时,则父类中的成员变量不能被继承。
*           (4)子类中定义的成员方法,并且这个方法的名字返回类型 ,以及参数个数和类型与父类的某个成员方法完全相同,则父类的成员方法不能被继承。 // 
*            分析以上程序示例,
*             1:虚拟机加载ExtendsDemo类,提取类型信息到方法区。
*             2:通过保存在方法区的字节码,虚拟机开始执行main方法,main方法入栈。
*             3:执行main方法的第一条指令,new BB();这句话就是给BB实例对象分配堆空间。因为BB继承AA父类,所以,虚拟机首先加载AA类到方法区
*             。然后加载BB类到方法区。将BB类的实例对象地址赋值给引用变量b。
*             4:调用b.fun1()方法,通过引用变量b持有的引用找到堆中的实例对象
*             ,通过实例对象持有的本类在方法区的引用,找到本类的类型信息,定位到fun1()方法。fun1()方法入栈。
*             开始执行fun1()方法中的字节码,在执行中调用fun2();优先在子类方法区找所以以打印456开始。
*             5:AA a = b;原理同上,
*             5:fun1(),fun2()方法执行完毕,fun2,fun1方法出栈,程序回到main方法,main方法执行完毕出栈,主线程消亡,虚拟机实例消亡,程序结束。 //
*             总结:相同的方法会被重写,
*           变量没有重写之说,如果子类声明了跟父类一样的变量,那意味着子类将有两个相同名称的变量。一个存放在子类实例对象中
*             ,一个存放在父类子对象中。父类的private变量,也会被继承并且初始化在子类父对象中,只不过对外不可见。
*/


}


class AA {
void fun1() {
System.out.println(this.fun2());
}


int fun2() {
return 123;
}
}


class BB extends AA {
int fun2() {
return 456;
}


public static void main(String args[]) {
BB b = new BB();
b.fun1();
AA a = b;
a.fun1();
}
}

---------------------------------------------------------------------------------------------------------------------

public class Test6 {


/**
* 6、 声明类Student,包含3个成员变量:name、age、score,要求可以通过 new Student("张三", 22, 95)
* 的方式创建对象, 并可以通过set和get方法访问成员变量

* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


}


}


class Student {
private String name;
private int age;
private int score;


public Student(String name) {
this.name = name;
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public Student( int age, int score) {
this.age = age;
this.score = score;
}
public Student(String name, int age, int score) {
this(age,score);
this.name=name;
}


public String getName() {
return name;
}


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


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}


public int getScore() {
return score;
}


public void setScore(int score) {
this.score = score;
}


}

----------------------------------------------------------------------------------------------------------------------------------------------

public class Test7 {


/**
* 7、 在打印语句中如何打印这3个x变量?

* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new A7().new B().func();
}


}


class A7 {
int x = 1;


class B {
int x = 2;


void func() {
int x = 3;
System.out.println(A7.this.x + " : " + this.x + " : " + x);
}
}
}

-------------------------------------------------------------------------------------------------------------------

public class Test8 {
/**
* 8、 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如:


* 原始字符串是"abc",打印得到下列所有组合情况: 
* "a" "b" "c" 
* "ab" "bc" "ca" "ba" "cb" "ac" "abc"
* "acb" "bac" "bca" "cab" "cba"

* @author Administrator

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


String str = s.next();
System.out.println(str);


s.close();
show(str);


}


/**
* @param str
* str是给定的字符串
*/
private static void show(String str) {
TreeSet<String> set = saveInSet(new StringBuilder(str), 0,
new StringBuilder(), new TreeSet<String>());
for (String s : set) {
System.out.println(s);
}
}


/**
* 返回集合,集合包含字符串所有字符的可能组合

* @param str
*            给定字符串转换成的StringBuilder对象,主要是为了操作字符方便
* @param count
*            计数,对第count进行排列组合
* @param buff
*            暂存放某种可能
* @param set
*            集合,去除重复元素,例如"aab"以第一个a开头会有aba,以第二个a开头也会有aba
* @return 返回TreeSet集合
*/
private static TreeSet<String> saveInSet(StringBuilder str, int count,
StringBuilder buff, TreeSet<String> set) {
for (int i = 0, len = str.length(); i < len; i++) {
// 获取字符
char c = str.charAt(i);
// 去掉原字符串的某个字符(保证某个字符不被重复利用)
str.deleteCharAt(i);
// 缓存添加该字符
buff.append(c);
// 将该种可能组合存入集合
set.add(buff.toString());


// str仍包含字符,则递归调用,开始取第二位字符
// 若还有第三位则继续递归……以此类推
if (str.length() != 0) {
// count用于记录目前在进行排列组合的第count位
count++;
// 递归
saveInSet(str, count, buff, set);
// 第n位递归结束后,需要继续对n-1位排列,位数-1
count--;
}


// 递归结束后,需要继续对n-1位排列,因此清除第n位的记录
buff.deleteCharAt(count);
// 删除的字符插回str
str.insert(i, c);
}
// 返回集合
return set;


}


}

----------------------------------------------------------------------------------------------------------------


public class Test9 {
/**
* 9、 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File from = new File("C:\\Users\\Administrator\\Desktop\\java");
File to = new File("C:\\Users\\Administrator\\Desktop\\java\\copy");
if (!to.exists())
to.mkdir();
ArrayList<File> al = new ArrayList<File>();
getFile(from, al);
BufferedInputStream bufi = null;
BufferedOutputStream bufo = null;
for (File file : al) {
bufi = new BufferedInputStream(new FileInputStream(file));// 输入流
String s = file.toString()
.substring(file.toString().lastIndexOf("\\"))
.replace(".java", ".txt");// 获取文件名并进行后缀名的修改


bufo = new BufferedOutputStream(new FileOutputStream(
new File(to, s)));// 输出流
byte[] buf = new byte[1024 * 1024];
int len = 0;
while ((len = bufi.read(buf)) != -1) {
bufo.write(buf, 0, len);
bufo.flush();
}
bufi.close();
bufo.close();
}
}


public static void getFile(File from, ArrayList<File> al)// 递归函数,获取所有.java的File.
{
File[] files = from.listFiles();
for (File file : files) {
if (file.isDirectory())
getFile(file, al);
else {
if (file.toString().endsWith(".java"))
al.add(file);
}
}
}
}

------------------------------------------------------------------------------------------------------------------------------

public class Test10 {


/**
* 10、
* 一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,
* 则不会发生这种问题。编程解决猫狗鱼过河问题。
*/
static List<String> there = new ArrayList<String>();
static List<String> here = new ArrayList<String>();


public static void main(String[] args) {
here.add("cat");
here.add("dog");
here.add("fish");


Test10 test10 = new Test10();
test10.take();
}


// 判断动物是否安全
public boolean isSafty(List<String> list) {
// 如果一个集合中同时出现了猫和狗,猫和鱼的都认为不安全
if (list.add("dog") && list.add("cat") || list.add("cat")
&& list.add("fish")) {
return false;
}
return true;
}


public void take() {
// 得到要带走的动物
String anim = here.get(0);
// 从剩下的集合中删除
here.remove(here.get(0));


if (anim.equals("cat")) {


// 添加到以带过去的集合里面
there.add(anim);


// 继续带下一个动物
if (here.isEmpty()) {
System.out.println();
System.out.println("最后在把" + anim + "带走了");
return;
} else {
System.out.println("带走了" + anim);
System.out.print("剩下:");
for (String s : here) {
System.out.print(s + "   ");
}
System.out.println();
System.out.println("----------------------------");
take();
}


} else if (anim.equals("dog")) {
there.add(anim);
System.out.println("然后把" + anim + "带走");
// 判断是否安全,之后继续带下一个动物
if (isSafty(there)) {
take();
} else {
String animal = there.get(0);
there.remove(animal);
here.add(animal);
System.out.println("然后把" + animal + "带回");
// 继续带下一个动物
take();
}
} else if (anim.equals("fish")) {
System.out.print("之后把" + anim + "带走");


take();
}
}
}

黑马程序员训练营入学考试题 1、方法中的内部类能不能访问方法中的局部变量,为什么? 2、编写一个类,在main方法中定义一个Map对象(采用泛型),加入若干个对象,然后遍历并打印出各元素的key和value。 3、取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)... 4、有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。 5、编写一个程序,获取10个1至20的随机数,要求随机数不能重复。 6、编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、售票中心。售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。 7、写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。 如: n = 4 则打印: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 ? 8、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。提示:十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。 9、28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路) 10、有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值