外企中的程序面试-JAVA
2007-06-09 18:41
267 查看
以前在外企面试遇到过的程序题目,翻译过来.................写不出代码可以写流程——面试要求.............................
1.If there's a database and a table (you make) , design a class to print this table ---the column name and info
给定一个数据库和一张表(自定义),写一个类,能将这张表的列名,列的信息打印出来。(10分钟)
考点: JDBC 数据库连接,查询;对ResultSet 的操作
import java.sql.*;
/**
* @ author simon lv
* */
public class DBTest{
private Connection conn = null;
private String class_forname = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String user ="sa";
private String pwd="sa";
private String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=EclipseDATA";
//得到连接
private Connection getConn(){
try{
Class.forName(class_forname);
conn = DriverManager.getConnection(url,user,pwd);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Have been connectted to Database successfully!"+url);
return conn;
}
//查询数据库 返回数据集
public ResultSet getRs(String str){
Connection con = this.getConn();
try{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(str);
return rs;
}catch(Exception e){
}
return null;
}
//打印数据表
public void printDataFormDB(){
String str="select * from userinfo";
ResultSet rs = this.getRs(str);
if(rs==null){
System.out.println("/nThere's no any record!");
}else{
System.out.println("/nby sql:"+str+" ,have bean got the ResultSet!/n ");
try{
ResultSetMetaData meta = rs.getMetaData();
System.out.println("return ResultSetMetaData !");
int columnCount = meta.getColumnCount();
System.out.println("The sum of colunms :"+columnCount);
int rowCount = 0;
while(rs.next()){
rowCount++;
for(int i=1;i<=columnCount;i++){
System.out.print(meta.getColumnName(i)+":"+rs.getObject(i)+" ");
}
System.out.print("/n");
}
System.out.println("have"+rowCount+" row(s) data");
}catch(Exception e){
e.printStackTrace();
}
}
}
//主程序入口
public static void main(String args[]){
try{
DBTest ts = new DBTest();
ts.printDataFormDB();
}catch(Exception e){
e.printStackTrace();
}
}
}
打印结果:
by sql:select * from userinfo ,have bean got the ResultSet!
return ResultSetMetaData !
The sum of colunms :3
id:1 username:simon pwd:exceptioin
id:2 username:111 pwd:111
have2 row(s) data
2.随机生成100个Integer 对象并对其排序(要求:将排序前的随机数 和 排序后的同时打印出来)(5分钟)
import java.util.*;
import java.util.List;
import java.util.ArrayList;
/**
*要求随机输入100个Integer 对象并排序
* */
public class RandomTest{
List lst = new ArrayList();
public void sortRandomNum(){
Random rdm = new Random();
for(int i=0;i<100;i++){
lst.add(i,new Integer((int)rdm.nextInt(100)));
}
System.out.println("排序之前的100个随机数:/n");
for(int j=0;j<100;j++){
System.out.print(lst.get(j)+" ");
}
System.out.println("/n排序之后的100个随机数:/n");
Collections.sort(lst);
for(int j=0;j<100;j++){
System.out.print(lst.get(j)+" ");
}
}
public static void main(String[] args) {
RandomTest test = new RandomTest();
test.sortRandomNum();
}
}
3.也是英语题目 最简单
/**
* 一个小程序,如果是3的倍数 就打印 Fizz
* 如果是5的倍数 就打印 BBOO
* 如果同时是3,又同时是5的倍数就打印 FizzBBOO
**/
public class StringDemo{
public void method(){
for(int i=1;i<101;i++){
if(i%3==0&&i%5!=0){
System.out.print("Fizz ");
}
else if(i%5==0&&i%3!=0){
System.out.print("BBOO ");
}
else if(i%15==0){
System.out.print("FizzBBOO ");
}
else{
System.out.print(" "+i+" ");
}
}}
public static void main(String args []){
StringDemo demo = new StringDemo();
demo.method();
}
}
4.Here's a file —— "ccc.txt" and write a method ——public int getLongestString (String text) ,when you input some words like "shanghai" and then will return the times of "shanghai" (ccc.txt includes "shanghai" )...........and this file is big one ....
一个ccc.txt 的文件,很大(要用缓冲区??我认为),写一个方法public int getLongestString (String text) 当输入text 时,会在 ccc.txt 中找它,返回 text 在 ccc.txt 中出现的次数。
………………
这一题当时没做完,原因是对IO流已经忘的差不多了,现在写完..........................
指定文件例如 F 盘 的 test.txt 文件,如下图
import java.io.*;
/**
*@ author:Simon Lv
**/
public class FindString{
String fileName ="F://test.txt";
// the main method for finding words from a file
public int getLongestString (String text)throws IOException{
InputStream in = new FileInputStream(fileName);
InputStreamReader reader;
reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);
String data;
int total = 0;
while((data=br.readLine())!=null){
total+=this.getTimesInEveryLine(data,text);
}
br.close();
System.out.print(text+" 在 "+ fileName +" 这个文件中出现的次数是: ");
return total;
}
// 判断一个字符串中在另外一个字符串中出现的次数
public int getTimesInEveryLine(String data,String text){
int total = 0;
for(String tmp = data;tmp!=null&&tmp.length()>=text.length();){
if(tmp.indexOf(text)==0){
total++;
tmp = tmp.substring(text.length());
}else{
tmp= tmp.substring(1);
}
}
return total;
}
public static void main(String saf[])throws IOException{
FindString find = new FindString();
int i = find.getLongestString ("shanghai");
System.out.println(i+" 次/n");
int j = find.getLongestString ("上海");
System.out.println(j+" 次/n");
}
}
输出结果如图
1.If there's a database and a table (you make) , design a class to print this table ---the column name and info
给定一个数据库和一张表(自定义),写一个类,能将这张表的列名,列的信息打印出来。(10分钟)
考点: JDBC 数据库连接,查询;对ResultSet 的操作
import java.sql.*;
/**
* @ author simon lv
* */
public class DBTest{
private Connection conn = null;
private String class_forname = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String user ="sa";
private String pwd="sa";
private String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=EclipseDATA";
//得到连接
private Connection getConn(){
try{
Class.forName(class_forname);
conn = DriverManager.getConnection(url,user,pwd);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Have been connectted to Database successfully!"+url);
return conn;
}
//查询数据库 返回数据集
public ResultSet getRs(String str){
Connection con = this.getConn();
try{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(str);
return rs;
}catch(Exception e){
}
return null;
}
//打印数据表
public void printDataFormDB(){
String str="select * from userinfo";
ResultSet rs = this.getRs(str);
if(rs==null){
System.out.println("/nThere's no any record!");
}else{
System.out.println("/nby sql:"+str+" ,have bean got the ResultSet!/n ");
try{
ResultSetMetaData meta = rs.getMetaData();
System.out.println("return ResultSetMetaData !");
int columnCount = meta.getColumnCount();
System.out.println("The sum of colunms :"+columnCount);
int rowCount = 0;
while(rs.next()){
rowCount++;
for(int i=1;i<=columnCount;i++){
System.out.print(meta.getColumnName(i)+":"+rs.getObject(i)+" ");
}
System.out.print("/n");
}
System.out.println("have"+rowCount+" row(s) data");
}catch(Exception e){
e.printStackTrace();
}
}
}
//主程序入口
public static void main(String args[]){
try{
DBTest ts = new DBTest();
ts.printDataFormDB();
}catch(Exception e){
e.printStackTrace();
}
}
}
打印结果:
by sql:select * from userinfo ,have bean got the ResultSet!
return ResultSetMetaData !
The sum of colunms :3
id:1 username:simon pwd:exceptioin
id:2 username:111 pwd:111
have2 row(s) data
2.随机生成100个Integer 对象并对其排序(要求:将排序前的随机数 和 排序后的同时打印出来)(5分钟)
import java.util.*;
import java.util.List;
import java.util.ArrayList;
/**
*要求随机输入100个Integer 对象并排序
* */
public class RandomTest{
List lst = new ArrayList();
public void sortRandomNum(){
Random rdm = new Random();
for(int i=0;i<100;i++){
lst.add(i,new Integer((int)rdm.nextInt(100)));
}
System.out.println("排序之前的100个随机数:/n");
for(int j=0;j<100;j++){
System.out.print(lst.get(j)+" ");
}
System.out.println("/n排序之后的100个随机数:/n");
Collections.sort(lst);
for(int j=0;j<100;j++){
System.out.print(lst.get(j)+" ");
}
}
public static void main(String[] args) {
RandomTest test = new RandomTest();
test.sortRandomNum();
}
}
3.也是英语题目 最简单
/**
* 一个小程序,如果是3的倍数 就打印 Fizz
* 如果是5的倍数 就打印 BBOO
* 如果同时是3,又同时是5的倍数就打印 FizzBBOO
**/
public class StringDemo{
public void method(){
for(int i=1;i<101;i++){
if(i%3==0&&i%5!=0){
System.out.print("Fizz ");
}
else if(i%5==0&&i%3!=0){
System.out.print("BBOO ");
}
else if(i%15==0){
System.out.print("FizzBBOO ");
}
else{
System.out.print(" "+i+" ");
}
}}
public static void main(String args []){
StringDemo demo = new StringDemo();
demo.method();
}
}
4.Here's a file —— "ccc.txt" and write a method ——public int getLongestString (String text) ,when you input some words like "shanghai" and then will return the times of "shanghai" (ccc.txt includes "shanghai" )...........and this file is big one ....
一个ccc.txt 的文件,很大(要用缓冲区??我认为),写一个方法public int getLongestString (String text) 当输入text 时,会在 ccc.txt 中找它,返回 text 在 ccc.txt 中出现的次数。
………………
这一题当时没做完,原因是对IO流已经忘的差不多了,现在写完..........................
指定文件例如 F 盘 的 test.txt 文件,如下图
import java.io.*;
/**
*@ author:Simon Lv
**/
public class FindString{
String fileName ="F://test.txt";
// the main method for finding words from a file
public int getLongestString (String text)throws IOException{
InputStream in = new FileInputStream(fileName);
InputStreamReader reader;
reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);
String data;
int total = 0;
while((data=br.readLine())!=null){
total+=this.getTimesInEveryLine(data,text);
}
br.close();
System.out.print(text+" 在 "+ fileName +" 这个文件中出现的次数是: ");
return total;
}
// 判断一个字符串中在另外一个字符串中出现的次数
public int getTimesInEveryLine(String data,String text){
int total = 0;
for(String tmp = data;tmp!=null&&tmp.length()>=text.length();){
if(tmp.indexOf(text)==0){
total++;
tmp = tmp.substring(text.length());
}else{
tmp= tmp.substring(1);
}
}
return total;
}
public static void main(String saf[])throws IOException{
FindString find = new FindString();
int i = find.getLongestString ("shanghai");
System.out.println(i+" 次/n");
int j = find.getLongestString ("上海");
System.out.println(j+" 次/n");
}
}
输出结果如图
相关文章推荐
- java应用死循环排查方法或查找程序消耗资源的线程方法(面试)
- java面试之程序初始化
- 视频教程:Java七大外企经典面试套路之基础篇
- JAVA程序面试经验,让你知己知彼更胜一筹
- 面试总结:java程序执行过程 + JVM内存管理 + GC垃圾回收机制
- 中软面试(一):java写一个随机生成四位数的程序 每位数字不重复
- 视频教程:Java七大外企经典面试套路之基础篇
- java基础:Java七大外企经典面试精讲视频
- Java面试-程序设计基础
- 笔试面试之java程序(一)
- java面试笔试题:设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1.写出程序
- Java 面试中你最易忽略9道的程序题
- 别人的Morgan Stanley面试,注重基础,java core、多线程、设计模式、设计程序。。。
- 视频教程:Java七大外企经典面试套路之基础篇
- JAVA实现的一个冒泡程序(面试常考)
- JAVA面试——程序基础篇
- java面试程序题收集
- Java面试常被问到的8大排序算法(分析+程序)
- Java面试-程序阅读
- java基础:Java七大外企经典面试精讲视频