java笔试题

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 165 收藏 0 点赞 0 评论 0

Java基础部分
1. 怎样得到一个文件的后缀名,io的设计思路
ü 调用File.getName()得到文件完整名fname
ü 得到文件名中的.号位置 int poc = fname.indexOf(“.”);
ü 得到后缀 ftype = fname.substring(poc+1);
public static void test3(){
//得到文件的后缀名
File f=new File(“F:/121.txt”);
if(f.exists()){
String fileName=f.getName();
int index=fileName.indexOf(“.”);
String s=fileName.substring(index+1);
System.out.println(s);
}
}

2. int x=1 int y=‘a’<‘A’ ? x–:x++; , 求x 的最终值 :2;

3. Integer a和int b运行之后a和b有什么不同?都没有赋值的情况下他们的初始值各是什么?
答:Integer a 定义为整型变量对象,默认值为null;int b 定义为整数变量,默认值为0;

4. 什么原因,怎么解决NullPointerException异常?
答:对没有赋值或为null的对象进行处理会导致NullPointerException异常,最好在使用对象变量之前对该变量进行非null判断

5. PHP中:echo和print的区别?
答:echo可以同时输出多个字符串,并不需要圆括号;print只可以同时输出一个字符串,需要圆括号。

6. (编程题)求一百以内质数
public class ZhiShu {
public static void main(String[] args) {
System.out.println(“1~100的质数有:”);
for (int n = 1; n <= 100; n++) {
boolean b = true;
if (n != 1) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
b = false;
break;
}
}
}
if (b) {
System.out.println(n );
}
}
}
}

7. 六行五列字符串数组(分别用C和java实现)
Java: int a[][]= new int[6][5]; //java申明不需要初始化,此时有默认值
C: int b[6][5]; //C申明必须要初始化值

8. (编程题)把int值保存在byte数组中〈高位在前〉
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
public class IntToByte {
public static void main(String[] args) throws Exception {
System.out.println(byteArrayToInt(intToByteArray1(456548)));
System.out.println(byteArrayToInt(intToByteArray2(456548)));
}
/**
* 将int值转换称byte数组 方法1
*/
public static byte[] intToByteArray1(int i) {
byte[] result = new byte[4];
result[0] = (byte) ((i >> 24) & 0xFF);
result[1] = (byte) ((i >> 16) & 0xFF);
result[2] = (byte) ((i >> 8) & 0xFF);
result[3] = (byte) (i & 0xFF);
return result;
}
/**
* 将int值转换称byte数组 方法2
*/
public static byte[] intToByteArray2(int i) throws Exception {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(buf);
out.writeInt(i);
byte[] b = buf.toByteArray();
out.close();
buf.close();
return b;
}

9. (编程题)写一个方法:输入的是文件名,输出的是文件内容字符串(FileToString.java 考察IOStringStringBuffer的区别)
File f = new File(“hello.txt”);
StringBuffer sb = new StringBuffer();
try{
FileReader in = new FileReader(f);
char[] buf = new char[1024];
while(in.read(buf)!=-1){
//将每次读取的内容放到StringBuffer中
sb.append(new String(buf).trim());
}
System.out.println(sb.toString());
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
10. (编程题)创建个静态方法,传个对象,循环打印对象的类名跟方法名,用代码(反射机制)
public static void display(Object clazz) throws ClassNotFoundException {
System.out.println(“对象的类名…..” + clazz.getClass().getName());
System.out.println();
// 得到构造方法
Constructor cons[] = clazz.getClass().getConstructors();
for (int i = 0; i < cons.length; i++) {
Constructor c = cons;

String name = c.getName();
Class cs[] = c.getParameterTypes();
System.out.print(“对象的构造方法…..” + c.getModifiers() + ” ” + name
+ “(“);
for (int j = 0; j < cs.length; j++) {
System.out.print(” ” + cs[j].getName() + ” “);
}
System.out.println(” )”);
}
System.out.println();
// 得到其他方法
Method method[] = clazz.getClass().getMethods();
for (int i = 0; i < method.length; i++) {
Method m = method;
String name = m.getName();
Class ms[] = m.getParameterTypes();
System.out.print(“对象的其他方法…..” + m.getModifiers() + ” ” + name
+ “(“);
for (int j = 0; j < ms.length; j++) {
System.out.print(” ” + ms[j].getName() + ” “);
}
System.out.println(” )”);
}
}

11. (编程题)写一个判断对称数的方法
/**
* 对称数就是顺读和反读都一样吗,你可以用一个String接受用户输入,
* 然后把它反转,判断前后是不是equals就可以了。
* 例如 123321就是对称数
*/
public static void RegularNumberTest() {
String num1 = new Scanner(System.in).nextLine();
String num2 = “”;
for (int j = num1.length() – 1; j >= 0; j–) {
char a = num1.charAt(j);
num2 += a;
}
if (num2.equals(num1)) {
System.out.println(num1 + “是对称数.”);
}else{
System.out.println(num1 + “不是对称数.”);
}
}

12. String和StringBuilder的区
答:StringBuilder是一个可变的字符序列,该类提供一个与 StringBuffer 兼容的 API,但不保证同步,存储效率比StringBuffer高;而String 是不可变的对象;

13. String str=new String(“abc”)内存中怎么分配的?
答:先在栈中分配一块空间,存放str,然后会在堆中分配一块空间,存放abc,且栈中的str指向堆中的abc地址

14. (编程题)1.1.2.3.5.8.13.21.求N位是多少,用递归实现
// 1.1.2.3.5.8.13.21.求N位是多少
public static int amwih(int i) {
if (i <= 0)
return 0;
else if (i > 0 && i <= 2)
return 1;
else
return amwih(i – 1) + amwih(i – 2);
}

15. 128>>1和128<<1结果分别是多少?
答:128>>1 = 64;
128<<1 = 256;

16. (编程题)用程序打印出下面的金字塔:(杨辉三角)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 n-1 ……n-1 1

import java.util.Scanner;
public class YangHuiSanJiao {
int array[][];
public void creat() {
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array.length; j++) {
if (j == 0)
array[j] = 1;
else if (i == j)
array[j] = 1;
else if (j > i)
array[j] = 0;
else
array[j] = array[i – 1][j – 1] + array[i – 1][j];
}
}
public static void main(String args[]) {
int n, m;
System.out.println(“请输入行数n:”);
Scanner sc = new Scanner(System.in);
n = m= sc.nextInt();
YangHuiSanJiao aa = new YangHuiSanJiao();
aa.array = new int[n]

;
aa.creat();
for (int i = 0; i < aa.array.length; i++) {
for (int j = 0; j < aa.array.length; j++)
if (aa.array[j] == 0)
System.out.print(” “);
else
System.out.print(aa.array[j] + ” “);
System.out.println();
}
}
}

17. (编程题)给出一个数字,如123456,输出123.456,输入1234567,输出123.456.7,但是输入如12,输出是12。(但是不能用format)即针对整数使用财会方式进行输出提示,考察对字符的使用技巧
public static String numberFormat(int num) {
StringBuffer sb = new StringBuffer(String.valueOf(num));
int sblen = sb.length();
int index = 0;
while (index + 3 < sblen) {
index = index + 3;
sb = sb.insert(index, “.”);
index++;
sblen++;
}
return sb.toString();
}

18. 简要介绍java如何实现安全性
Java通过提供一个”安全沙箱“来保证从网络或者其他不信任的地方下载并运行的程序不会破坏本地数据,其中包括:
ü 类装载器结构
ü class文件检验器
ü 内置于Java虚拟机(及语言)的安全特性
ü 安全管理器及Java API

19. 为什么说java是解释性的?
答:java是由java源代码编译成字节码存放在 .class文件中的. 而执行时,是由java虚拟机(jvm)读取字节码文件翻译并执行的. 所以java是解释性的

20. (编程题) 1)实现从字符串”<text>你好,欢迎来到我们公司面试</text>”,提取中文字符串 2)把上题中的”我们公司”改写成”玄武科技”
String str = “<text>你好,欢迎来到我们公司面试</text>”;
int indexS = str.indexOf(“<text>”) + 6;
int indexE = str.lastIndexOf(“</text>”);
str = str.substring(indexS, indexE);
System.out.println(str);
str = str.replaceAll(“我们公司”, “玄武科技”);
System.out.println(str);

21. (编程题)写一个流的读入程序,每次只能读一行
try {
BufferedReader br = new BufferedReader(new FileReader(new File(“hello.txt”)));
String s = null;
// readLine()包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
while ((s=br.readLine())!=null) {
System.out.println(s); // 转换成字符
}
catch (Exception e) {
e.printStackTrace();
}

22. CURD是什么,怎么实现
CURD: Cread、Update、Read、Delete,就是对数据库记录的创建(添加insert)、修改、读取(select)、删除;
在java编程中主要是通过jdbc来实现的,但可以使用其他的中间件来简化jdbc的复杂操作,例如hibernate、ibatis等;

23. (编程题)求一个二叉树中值的和(考察遍历二叉树)
// 前序遍历,同时记录出所有节点值的和
private int firstRootSum(TreeNode root, int sum) {
if (root!= null){
sum = sum+root.getNum();
sum = firstRootSum(root.getLeft(),sum);// 左
sum = firstRootSum(root.getRight(),sum);// 右
}
return sum;
}
24. Java代码如下person p=new person();中p放在什么位置? 
答:其中对象句柄p放在栈中

25. (编程题)字符串的分割如“abc|ddd||0dc”写一个类似split的方法分割输出出来
//Object[] obj = SplitString2.splitString(“|abc|de||e|||”, “|”);
public static Object[] splitString(String str, String regex) {
ArrayList alist = new ArrayList();
StringBuffer sb = new StringBuffer(str);
int index = -1;
while ((index = sb.indexOf(regex)) != -1) {
if (index != 0) {
alist.add(sb.substring(0, index));
}
sb = sb.replace(0, index + 1, “”);
}
alist.add(sb.toString());
alist.remove(“”);
return alist.toArray();
}

评论列表
文章目录