我是走流程考完入学考试报的面试,只有2天时间,由于本人 大四事情特别多,没法好好的去复习Java基础,打算抱着侥幸的心理面试看看效果。面试是这样的,上来给2道编程题如下图
2道编程题共30分,时间50分钟。
不让查看参考任何资料。
第一题,由于
- <div>Class.forName("com.mysql.jdbc.Driver");</div><div>String url = "jdbc:mysql://localhost:3306/test";</div>
复制代码 这中间的两个字符串记不得,直接到底身亡。
第二题,很简单,由于第一题的冲击波太大,影响了第二题的发挥,随机数竟然使用java.util.Random,直接面试失败。
接下来给出这两个面试题的答案:
1.
- <div>package Test;</div><div>
- </div><div>import java.sql.Connection;</div><div>import java.sql.DriverManager;</div><div>import java.sql.PreparedStatement;</div><div>import java.sql.ResultSet;</div><div>import java.sql.SQLException;</div><div>import java.util.ArrayList;</div><div>import java.util.Iterator;</div><div>import java.util.List;</div><div>
- </div><div>class Student {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private String name;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private int age;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private int score;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public String getName() {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return name;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public void setName(String name) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>this.name = name;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public int getAge() {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return age;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public void setAge(int age) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>this.age = age;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public int getScore() {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return score;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public void setScore(int score) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>this.score = score;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div>}</div><div>public class Test1 {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static Connection getConnection() throws ClassNotFoundException, SQLException {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Class.forName("com.mysql.jdbc.Driver");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String url = "jdbc:mysql://localhost:3306/test";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String user = "root";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String password = "123456";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return DriverManager.getConnection(url, user, password);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static List<Student> getStudents() throws </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ClassNotFoundException, SQLException {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>List<Student> list = new ArrayList<Student>();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String sql = "select * from tb_stu";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Connection conn = getConnection();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>PreparedStatement ps = conn.prepareStatement(sql);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ResultSet rs = ps.executeQuery();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>while(rs.next()) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Student s = new Student();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>s.setName(rs.getString("name"));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>s.setAge(rs.getInt("age"));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>s.setScore(rs.getInt("score"));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>list.add(s);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return list;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) throws ClassNotFoundException, SQLException {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>List<Student> list = getStudents();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>//1.</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>for(Student s : list) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(s.getName()+","+s.getAge()+","+s.getScore());</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>//2.</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Iterator i = list.iterator();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>while(i.hasNext()) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Student s = (Student)i.next();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(s.getName()+","+s.getAge()+","+s.getScore());</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div>}</div>
复制代码
2.
- <div>package Test;</div><div>
- </div><div>import java.util.Random;</div><div>import java.util.Scanner;</div><div>
- </div><div>public class Test2 {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("请输入一个字符串");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Scanner scan = new Scanner(System.in);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String str1 = scan.nextLine();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("请输入想要查找的字符");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String str2 = scan.nextLine();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>for(int i=0; i < 4; i++) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if(str1.contains(str2)) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(str2+"出现的位置是:"+str1.indexOf(str2)+" 次数:"+ times(str1,str2));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.exit(0);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>else {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("请重新输入");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>str2 = scan.nextLine();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>char c = str1.charAt((int)(Math.random()*str1.length()));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("由于5次输入均错误,所以随机选择了一个字符" + c);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(c+"出现的位置是:"+str1.indexOf(c)+" 次数:"+ times(str1,c+""));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>/**</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> * 一个字符ch在字符串str中出现的次数</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> * @param str</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> * @param ch</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> * @return</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> */</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>private static int times(String str1, String str2) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>int sum = 0;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>char[] cha = str1.toCharArray();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>for(int i=0; i<str1.length(); i++) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if(cha[i] == str2.toCharArray()[0])</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sum++;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>return sum;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div>}</div>
复制代码 给我面试的是盖洪晓老师,老师说了,后面还有理论知识题20分。
每人有3次面试机会,12两次相等,第3次需要重新走流程。
面试内容涵盖:面向对象 集合 线程 IO 反射 jdbc mysql http(没有网络编程和GUI)
这次失败主要由于没有做好准备,下次我会准备好的,黑马,等我!
|
|