A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 菊花爆满山 中级黑马   /  2016-3-25 13:14  /  6611 人查看  /  71 人回复  /   6 人收藏 转载请遵从CC协议 禁止商业使用本文

我面了2次  第一次没学jdbc  没过  第2次估计悬   但是还是希望这些面试题可以帮到其他人

71 个回复

倒序浏览
第一题:编写程序,生成5个1至10之间的随机整数,存入一个List集合,
* 编写方法对List集合进行排序(自定义排序算法,禁用Collections.sort方法和TreeSet),
* 然后遍历集合输出。
第二题:定义一个标准的JavaBean,名叫Person,包含属性name、age。使用反射的方式创建一个实例、
* 调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,
* 不使用setAge方法直接使用反射方式对age赋值。
第三题:现有一sdudent表,有ID,age,name,sex这些列,请使用jdbc方式链接数据库,
* 并查询所有姓张的学生,
第四题:map中有如下数据(电话号=套餐价格)
【13265477888=168,15241698745=11,13699989898=20,18986866666=120】
在IP为127.0.0.1数据库名为stdb,连接数据库的用户名和密码为admin 和 123456中有一个numinfo表 相关字段为(id,iphonenum,tcmoney)(15分)
①        将map中的手机号取出来打印在控制台上。(3分)
②        判断map中所有的手机号在numinfo表中是否存在如果存在则输出“该机主已登记” 如果查询不到则将该号码以及对应的套餐价格存入到numinfo表中
(map中的数据不需要修改)(12分)
第五题:将"hello world"首字母转换成大写其余还是小写字母
回复 使用道具 举报
第一题:
  1. package com.heima.text;

  2. import java.util.ArrayList;
  3. import java.util.Random;

  4. /*
  5. * 编写程序,生成5个1至10之间的随机整数,存入一个List集合,
  6. * 编写方法对List集合进行排序(自定义排序算法,禁用Collections.sort方法和TreeSet),
  7. * 然后遍历集合输出。

  8. * */
  9. public class Demo1 {

  10.         public static void main(String[] args) {
  11.                 //定义集合存储元素
  12.                 ArrayList<Integer> al = new ArrayList<Integer>();
  13.                 Random r = new Random();   
  14.                 for(int x = 0; x < 5; x++) {
  15.                         int num = r.nextInt(10) + 1;   //通过Random对象中的方法获取 1 - 10之间的任意数
  16.                         al.add(num);
  17.                 }
  18.                 sortArrayList(al);
  19.                 //sortArrayList2(al);
  20.                 print(al);     //打印集合中的元素
  21.         }
  22.         //遍历集合
  23.         public static void print(ArrayList<Integer> al) {
  24.                 for(Integer i : al) {
  25.                         System.out.println(i);
  26.                 }
  27.         }
  28.         //冒泡排序
  29.         public static void sortArrayList(ArrayList<Integer> al) {
  30.                 for(int x = 0; x < al.size() - 1; x++) {
  31.                         for(int y = 0; y < al.size() - x - 1; y++) {
  32.                                 if(al.get(y) > al.get(y + 1)) {
  33.                                         swap(al, y, y + 1);
  34.                                 }
  35.                         }
  36.                 }
  37.         }
  38.         //插入排序
  39.         public static void sortArrayList2(ArrayList<Integer> al){
  40.                 for(int x = 0; x < al.size() - 1; x++) {
  41.                         for(int y = x + 1; y < al.size(); y++) {
  42.                                 if(al.get(x) > al.get(y)) {
  43.                                         swap(al, x, y);
  44.                                 }
  45.                         }
  46.                 }
  47.         }
  48.         //交换元素的值
  49.         public static void swap(ArrayList<Integer> al, int x, int y) {
  50.                 int temp = al.get(x);
  51.                 al.set(x, al.get(y));
  52.                 al.set(y, temp);
  53.         }

  54. }
复制代码
回复 使用道具 举报 1 0
第二题:
  1. package com.heima.text;

  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;

  6. /*
  7. * 定义一个标准的JavaBean,名叫Person,包含属性name、age。使用反射的方式创建一个实例、
  8. * 调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,
  9. * 不使用setAge方法直接使用反射方式对age赋值。

  10. * */
  11. public class Demo2 {

  12.         public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
  13.                 Class<Person> c = Person.class;
  14.                 //使用反射的方式创建一个实例、
  15.                 Constructor<Person> con  = c.getDeclaredConstructor(String.class, int.class);
  16.                 Person p = con.newInstance("LiHua", 25);
  17.                 System.out.println(p);
  18.                
  19.                 //使用反射方式调用setName方法对名称进行设置
  20.                 Method m = c.getDeclaredMethod("setName", String.class);
  21.                 m.invoke(p, "ZhangShan");
  22.                 System.out.println(p);
  23.                
  24.                 //不使用setAge方法直接使用反射方式对age赋值
  25.                 Field f = c.getDeclaredField("age");
  26.                 f.setAccessible(true);
  27.                 f.set(p, 20);
  28.                 System.out.println(f.get(p));
  29.         }

  30. }
  31. class Person {
  32.         private String name;
  33.         private int age;
  34.         Person(String name, int age) {
  35.                 this.name = name;
  36.                 this.age = age;
  37.         }
  38.         public String getName() {
  39.                 return name;
  40.         }
  41.         public void setName(String name) {
  42.                 this.name = name;
  43.         }
  44.         public int getAge() {
  45.                 return age;
  46.         }
  47.         public void setAge(int age) {
  48.                 this.age = age;
  49.         }
  50.         public String toString() {
  51.                 return name + ":" + age;
  52.         }
  53. }
复制代码
回复 使用道具 举报
第3题:
  1. package com.heima.text;

  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.Scanner;

  6. import org.junit.Test;

  7. /*
  8. * 现有一sdudent表,有ID,age,name,sex这些列,请使用jdbc方式链接数据库,
  9. * 并查询所有姓张的学生,
  10. * */
  11. public class Demo3 {
  12.         //向student表中插入数据
  13.         @Test
  14.         public void fun1() throws Exception {
  15.                 Connection con = null;
  16.                 PreparedStatement pre = null;
  17.                 Scanner sc = null;
  18.                 try {
  19.                         con = JdbcUtils.getConnection();
  20.                         String sql = "insert into tb values(?, ?)";
  21.                         pre = con.prepareStatement(sql);
  22.                         sc = new Scanner(System.in);
  23.                         for(int x = 0; x < 10; x++) {
  24.                                 String line = sc.nextLine();
  25.                                 String[] strArr = line.split(",");
  26.                                 pre.setInt(1, Integer.parseInt(strArr[0]));
  27.                                 pre.setString(2, strArr[1]);
  28.                                 pre.addBatch(); //添加批   这一组数据就保存到集合中了
  29.                         }
  30.                         pre.executeBatch(); //执行批
  31.                 }finally {
  32.                         sc.close();
  33.                         if(pre != null) pre.close();
  34.                         if(con != null) con.close();
  35.                 }
  36.         }
  37.         //遍历整个student表
  38.         @Test
  39.         public void fun2() throws Exception {
  40.                 Connection con = null;
  41.                 PreparedStatement pre = null;
  42.                 ResultSet rs = null;
  43.                 try {
  44.                         con = JdbcUtils.getConnection();
  45.                         String sql = "select * from stu";
  46.                         pre = con.prepareStatement(sql);
  47.                        
  48.                         rs = pre.executeQuery();
  49.                         int count = rs.getMetaData().getColumnCount();
  50.                         while(rs.next()) {
  51.                                 for(int x = 1; x <= count; x++) {
  52.                                         System.out.print(rs.getString(x));
  53.                                         if(x < count) {
  54.                                                 System.out.print(", ");
  55.                                         }
  56.                                 }
  57.                                 System.out.println();
  58.                         }
  59.                 }finally {
  60.                         if(rs != null) rs.close();
  61.                         if(pre != null) pre.close();
  62.                         if(con != null) con.close();
  63.                 }
  64.         }
  65.         //遍历出性张的学生
  66.         @Test
  67.         public void fun3() throws Exception {
  68.                 Connection con = null;
  69.                 PreparedStatement pre = null;
  70.                 ResultSet rs = null;
  71.                 try {
  72.                         con = JdbcUtils.getConnection();
  73.                         String sql = "select * from stu where name like ?";
  74.                         pre = con.prepareStatement(sql);
  75.                         pre.setString(1, "张%");
  76.                        
  77.                         rs = pre.executeQuery();
  78.                         int count = rs.getMetaData().getColumnCount();
  79.                         while(rs.next()) {
  80.                                 for(int x = 1; x <= count; x++) {
  81.                                         System.out.print(rs.getString(x));
  82.                                         if(x < count) {
  83.                                                 System.out.print(", ");
  84.                                         }
  85.                                 }
  86.                                 System.out.println();
  87.                         }
  88.                 }finally {
  89.                         if(rs != null) rs.close();
  90.                         if(pre != null) pre.close();
  91.                         if(con != null) con.close();
  92.                 }
  93.         }
  94. }
复制代码
回复 使用道具 举报
第四题:
  1. package com.heima.text;

  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import java.util.Scanner;
  10. import java.util.Set;

  11. import org.junit.Test;

  12. /*
  13. *
  14. * map中有如下数据(电话号=套餐价格)
  15. 【13265477888=168,15241698745=11,13699989898=20,18986866666=120】
  16. 在IP为127.0.0.1数据库名为stdb,连接数据库的用户名和密码为admin 和 123456中有一个numinfo表 相关字段为(id,iphonenum,tcmoney)(15分)
  17. ①        将map中的手机号取出来打印在控制台上。(3分)
  18. ②        判断map中所有的手机号在numinfo表中是否存在如果存在则输出“该机主已登记” 如果查询不到则将该号码以及对应的套餐价格存入到numinfo表中
  19. (map中的数据不需要修改)(12分)

  20. * */
  21. public class Demo4 {
  22.        
  23.         @Test
  24.         //首先遍历集合 获取集合中每一个健值 看是否包含在表中
  25.         public void  fun1() throws Exception{
  26.                 Map<String, Integer> m = new HashMap<>();
  27.                 ArrayList<String> al = new ArrayList<>();
  28.                
  29.                 m.put("13265477888", 168);
  30.                 m.put("15241698745", 11);
  31.                 m.put("18986866666", 120);
  32.                 Set<String> keySet = m.keySet();
  33.                 Iterator<String> it = keySet.iterator();
  34.                 String[] strArr = new String[m.size()];
  35.                 int num = 0;
  36.                 while(it.hasNext()) {
  37.                         strArr[num++] = it.next();
  38.                 }
  39.                
  40.                 Connection con = null;
  41.                 PreparedStatement pre = null;
  42.                 PreparedStatement pre2 = null;
  43.                 ResultSet rs = null;
  44.                 try {
  45.                         con = JdbcUtils.getConnection();
  46.                         String sql = "select * from numinfo";
  47.                         String sql2 = "insert into numinfo(iphonenum, tcmoney) values(?, ?)";
  48.                         pre = con.prepareStatement(sql);
  49.                         pre2 = con.prepareStatement(sql2);
  50.                        
  51.                         rs = pre.executeQuery();
  52.                         while(rs.next()) {
  53.                                 al.add(rs.getString("iphonenum"));
  54.                         }
  55.                        
  56.                        
  57.                         for(int x = 0; x < strArr.length; x++) {
  58.                                
  59.                                 if(!al.contains(strArr[x])) {
  60.                                         pre2.setString(1, strArr[x]);
  61.                                         pre2.setInt(2, m.get(strArr[x]));
  62.                                         pre2.executeUpdate();
  63.                                 }else {
  64.                                         System.out.println(strArr[x] +"该机主已登记");
  65.                                 }
  66.                         }
  67.                 }finally {
  68.                         if(rs != null) pre.close();
  69.                         if(pre != null) pre.close();
  70.                         if(pre2 != null) pre.close();
  71.                         if(con != null) con.close();
  72.                 }
  73.         }
  74.         @Test
  75.         //向numinfo表中存储已有的数据 键盘录入的方式
  76.         public void fun2() throws Exception{
  77.                 Connection con = null;
  78.                 PreparedStatement pre = null;
  79.                 Scanner sc = new Scanner(System.in);
  80.                 try {
  81.                         con = JdbcUtils.getConnection();
  82.                         String sql = "insert into numinfo values(?, ?, ?)";
  83.                         pre = con.prepareStatement(sql);
  84.                         for(int x = 0; x < 5; x++) {
  85.                                 String line = sc.nextLine();
  86.                                 String[] strArr = line.split(",");
  87.                                 pre.setString(1, strArr[0]);
  88.                                 pre.setString(2, strArr[1]);
  89.                                 pre.setInt(3, Integer.parseInt(strArr[2]));
  90.                                 pre.addBatch();
  91.                         }
  92.                         pre.executeBatch();
  93.                        
  94.                 }finally {
  95.                         sc.close();
  96.                         if(pre != null) pre.close();
  97.                         if(con != null) con.close();
  98.                        
  99.                 }
  100.         }
  101.         @Test
  102.         //①        将map中的手机号取出来打印在控制台上。
  103.         public void fun3() {
  104.                 Map<String, Integer> m = new HashMap<>();
  105.                 m.put("13265477888", 168);
  106.                 m.put("15241698745", 11);
  107.                 m.put("18986866666", 120);
  108.                 Set<String> keySet = m.keySet();
  109.                 Iterator<String> it = keySet.iterator();
  110.                 while(it.hasNext()) {
  111.                         System.out.println(it.next());
  112.                 }
  113.         }

  114. }
复制代码
回复 使用道具 举报
楼主面试什么专业呢?是就业班还是基础班?
回复 使用道具 举报
第5题:
  1. package com.heima.text;
  2. /*
  3. * 将"hello world"首字母转换成大写其余还是小写字母
  4. * */
  5. public class Demo5 {

  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 String str = "hello world";
  9.                 StringBuilder sb = new StringBuilder(str);
  10.                 for(int x = 0; x < sb.length(); x++) {
  11.                         String s = sb.charAt(x) + "";
  12.                         if(x == 0) {
  13.                                 sb.deleteCharAt(0);
  14.                                 s = s.toUpperCase();
  15.                                 sb.insert(0, s);
  16.                                 break;
  17.                         }
  18.                 }
  19.                 str = sb.toString();
  20.                 System.out.println(str);
  21.         }

  22. }
复制代码
回复 使用道具 举报
面试是视频 还是去了面试的  
回复 使用道具 举报
lgdbest 发表于 2016-3-25 13:35
面试是视频 还是去了面试的

我视频面试的
回复 使用道具 举报

然后 面试是在本机上敲代码,不能查API 是不是
回复 使用道具 举报 1 0
劲爆对策 发表于 2016-3-25 13:25
楼主面试什么专业呢?是就业班还是基础班?

我面试的是进javaEE就业班
回复 使用道具 举报
lgdbest 发表于 2016-3-25 13:38
然后 面试是在本机上敲代码,不能查API 是不是

对 不能查API和笔记
回复 使用道具 举报
菊花爆满山 发表于 2016-3-25 13:42
我面试的是进javaEE就业班

楼主基础是自学还是培训班,我自学java基础,报了安卓就业班,考试题得了27分,还剩最后一个面试步骤了,你考试题有没有满分呢?感觉3分会把我卡掉
回复 使用道具 举报
这是插入排序的算法:
  1. /**
  2. * 插入排序需要双倍内存,因为他会创建一个返回列表,并添加元素列表的所有元素。 原理:
  3. *
  4. * 注意:(1)条件语句的else在if是返回的情况下可以不写else{}块来包括
  5. * (2)带标签的循环,标签开头字母小写(3)插入排序的返回列表使用链表实现LinkedList因为他适合在中间和头部插入大量元素。
  6. */
  7. public class 插入排序 {

  8.         public static void main(String[] args) {
  9.                 // 创建一个String类型的列表
  10.                 List<String> toSort = new ArrayList<>();
  11.                 toSort.add("w");
  12.                 toSort.add("s");
  13.                 toSort.add("f");
  14.                 toSort.add("y");
  15.                 toSort.add("v");

  16.                 // 测试insertSort方法
  17.                 List<String> sortedList = insertSort(toSort);
  18.                 // 迭代并打印到控制台
  19.                 sortedList.forEach(element -> System.out.println(element));
  20.         }

  21.         public static <E extends Comparable<E>> List<E> insertSort(List<E> values) {
  22.                 // 优化
  23.                 if (values.size() < 2)
  24.                         return values;
  25.                 // 用于返回的列表
  26.                 List<E> sorted = new LinkedList<>();
  27.                 // 定义一个带标签的对原始列表的迭代
  28.                 originalList: for (E orig : values) {
  29.                         // 对排序列表的迭代
  30.                         for (int i = 0; i < sorted.size(); i++) {
  31.                                 // 如果原始列表元素小于等于排序列表元素,那么把原始列表元素插入到该元素前面
  32.                                 if (orig.compareTo(sorted.get(i)) <= 0) {
  33.                                         sorted.add(i, orig);
  34.                                         // 插入后直接进行下一个原始元素的循环
  35.                                         continue originalList;
  36.                                 }
  37.                                 // 如果不小于等于,那么进入下一个排序列表元素
  38.                         }
  39.                         // 如果在排序列表元素迭代完仍然没有添加,则加入到排序列表末尾
  40.                         sorted.add(sorted.size(), orig);
  41.                 }

  42.                 return sorted;

  43.         }
  44. }
复制代码
回复 使用道具 举报

你第二题的JavaBean一般是提供一个默认的无参构造方法
回复 使用道具 举报
劲爆对策 发表于 2016-3-25 13:45
楼主基础是自学还是培训班,我自学java基础,报了安卓就业班,考试题得了27分,还剩最后一个面试步骤了, ...

入学测试题我是满分  没大碍我觉得  主要是面试
回复 使用道具 举报

ok  了解了。谢谢啊
回复 使用道具 举报
多谢了,学习啦
回复 使用道具 举报
多谢了。。。
回复 使用道具 举报
1234下一页
您需要登录后才可以回帖 登录 | 加入黑马