2009-02-16 16:37:00
再问大家一个面试题:
请设计一个典型的线程程序来说明同步的必要性.
我们朋友碰到过,他当时就难住了..
火车站卖票,银行存钱。。。。
比如一共火车票200张,remainder=200;
南站卖一张,remainder--
北京站卖一张,remainder--
这时候就需要保证这个减法操作不能被打断
1.张表,学生表S,课程C,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联;
1)写出建表语句;
答:建表语句如下(mysql数据库):
create table s(id integer primary key, name varchar(20));
create table c(id integer primary key, name varchar(20));
create table sc(
sid integer references s(id),
cid integer references c(id),
primary key(sid,cid)
);
2)写出SQL语句,查询选修了所有选修课程的学生;
答:SQL语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id) =
(select count(*) from c);
3)写出SQL语句,查询选修了至少5门以上的课程的学生。
答:SQL语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id)>=5;
2.数据库表(Test)结构如下:(SQL)
IDNAMEAGEMANAGER(所属主管人ID)
106A30104
109B19104
104C20111
107D35109
112E25120
119F45NULL
要求:列出所有年龄比所属主管年龄大的人的ID和名字?
答:SQL语句如下:
select employee.name from test employee where employee.age>
(select manager.age from test manager where manager.id=employee.manager);
3.有3个表(15分钟):(SQL)
Student 学生表 (学号,姓名,性别,年龄,组织部门)
Course 课程表 (编号,课程名称)
Sc 选课表 (学号,课程编号,成绩)
表结构如下:
1)写一个SQL语句,查询选修了’计算机原理’的学生学号和姓名(3分钟)
答:SQL语句如下:
select stu.sno, stu.sname from Student stu
where (select count(*) from sc where sno=stu.sno and cno =
(select cno from Course where cname='计算机原理')) != 0;
2)写一个SQL语句,查询’周星驰’同学选修了的课程名字(3分钟)
答:SQL语句如下:
select cname from Course where cno in (select cno from sc where sno=(select sno from Student where sname='周星驰'));
3)写一个SQL语句,查询选修了5门课程的学生学号和姓名(9分钟)
答:SQL语句如下:
select stu.sno, stu.sname from student stu
where (select count(*) from sc where sno=stu.sno) = 5;
4.写四个线程,两个对j加1,两个对j减1;(Core Java)
答:代码如下:
package test;
public class TestThread {
int j;
public TestThread(int j) {
this.j ;
}
private synchronized void inc(){
j++;
System.out.println(j + "--Inc--" + Thread.currentThread().getName());
}
private synchronized void dec(){
j--;
System.out.println(j + "--Dec--" + Thread.currentThread().getName());
}
public void run() {
(new Dec()).start();
new Thread(new Inc()).start();
(new Dec()).start();
new Thread(new Inc()).start();
}
class Dec extends Thread {
public void run() {}{
for(int i=0; i <100; i++){
dec();
}
}
}
class Inc implements Runnable {
public void run() {
for(int i=0; i <100; i++){
inc();
}
}
}
public static void main(String[] args) {
(new TestThread(5)).run();
}
}
5. 写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数。(Core Java)
答:代码如下:
public int countWords(String file, String find) throws Exception {
int count = 0;
Reader in = new FileReader(file);
int c;
while ((c = in.read()) != -1) {
while (c == find.charAt(0)) {
for (int i = 1; i < find.length(); i++) {
c = in.read();
if (c != find.charAt(i)) break;
if (i == find.length() - 1) count++;
}
}
}
return count;
}
8.用程序给出随便大小的10个数,序号为1-10,按从小到大顺序输出,并输出相应的序号。(Core Java)
答:代码如下:
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class RandomSort {
public static void printRandomBySort() {
Random random = new Random(); // 创建随机数生成器
List list = new ArrayList();
for (int i = 0; i < 10; i++) { // 生成10个随机数,并放在集合list中
list.add(random.nextInt(1000));
}
Collections.sort(list); // 对集合中的元素进行排序
Iterator it = list.iterator();
int count = 0;
while (it.hasNext()) { // 顺序输出排序后集合中的元素
System.out.println(++count + ":" + it.next());
}
}
public static void main(String[] args) {
printRandomBySort();
}
}
9.写一个函数,2个参数,1个字符串,1个字节数,返回截取的字符串,要求字符串中的中文不能出现乱码:如(“我ABC”,4)应该截为“我AB”,输入(“我ABC汉DEF”,6)应该输出为“我ABC”而不是“我ABC+汉的半个”。(Core Java)
答:代码如下:
public String subString(String str, int subBytes) {
int bytes = 0; // 用来存储字符串的总字节数
for (int i = 0; i < str.length(); i++) {
if (bytes == subBytes) {
return str.substring(0, i);
}
char c = str.charAt(i);
if (c < 256) {
bytes += 1; // 英文字符的字节数看作1
} else {
bytes += 2; // 中文字符的字节数看作2
if(bytes - subBytes == 1){
return str.substring(0, i);
}
}
}
return str;
}
15.用你熟悉的语言写一个连接ORACLE数据库的程序,能够完成修改和查询工作;(JDBC)
答:JDBC示例程序如下:
public void testJdbc(){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
//step1:注册驱动;
Class.forName("oracle.jdbc.driver.OracleDriver");
//step 2:获取数据库连接;
con=DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.39:1521:TARENADB",
"sd0605","sd0605");
/*************查 询*************
//step 3:创建Statement;
String sql = "SELECT id, fname, lname, age, FROM Person_Tbl";
ps = con.prepareStatement(sql);
//step 4:执行查询语句,获取结果集;
rs = ps.executeQuery();
//step 5:处理结果集—输出结果集中保存的查询结果;
while (rs.next()){
System.out.print("id = " + rs.getLong("id"));
System.out.print(" , fname = " + rs.getString("fname"));
System.out.print(" , lname = " + rs.getString("lname"));
System.out.print(" , age = " + rs.getInt("age"));
}
/*******************************JDBC 修 改*********************/)
sql = "UPDATE Person_Tbl SET age=23 WHERE id = ?";
ps = con.prepareStatement(sql);
ps.setLong(1, 88);
int rows = ps.executeUpdate();
System.out.println(rows + " rows affected.");
} catch (Exception e){
e.printStackTrace();
} finally{
try{
con.close(); //关闭数据库连接,以释放资源。
} catch (Exception e1) {
}
}
}
26.写一个自定义标签;(Web)
答:代码如下:
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class TimeTag extends SimpleTagSupport{
private boolean isServer = true;
public void setServer(boolean isServer){
this.isServer = isServer;
}
public void doTag() throws JspException, IOException{
JspWriter out = getJspContext().getOut();
if(isServer) {
out.println(new java.util.Date());
}else{
out.println(" <script language="javascript">");
out.println("document.write(new Date());");
out.println(" </script>");
}
}
}
27.写出熟悉的JSTL标签;(Web)
答:如下:
<c:if>
<c:forEach>
<c:set>
<c:import>
<c:redirect>
28.说出struts中的标签;(Web):
答:如下:
<bean:message /> <html:errors />
<bean:include /> <html:messages />
<bean:define /> <html:html>
<bean:write /> <html:form>
<bean:resource /> <html:link>
<bean:cokkie /> <html:text>
<bean:heder /> <logic:present />
<bean:parameter /> <logic:equal />
<bean:size />
31.找出程序中的问题,并写出理由(5分钟)(Web)
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import javax.naming.*;
import java.sql.*;
import java.io.IOException;
public class TestServlet extends HttpServlet{
private Connection conn;
protected void doGet(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException{
try{
Class.forName(“COM.ibm.db2.jdbc.app.DB2Driver”);
conn = DriverManager.getConnection(“jdbc:db2:mydb”,”db2”,”db2”);
PreparedStatement stmt = conn.prepareStatement("delete from mytb where id=?");
for (int i=0;i <5;i++) {
stmt.setInt(1,i);
stmt.executeUpdate();
}
conn.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
32.写出Singleton模式。写出你熟悉的设计模式;
答:有两种方式:
第一种方法:
public class Singleton{
private Singleton(){}
private static Singleton singleton=new Singleton();
public static Singleton getInstance(){
return singleton;
}
}
第二种方法:
public class Singleton{
private Singleton(){}
private static Singleton singleton=null;
public static Singleton getInstance(){
If(singleton==null) singleton=new Singleton();//需要的时候才new,节省空间。
return singleton;
}
}
一般采用第一种方式。
36.统计一篇文章中单词个数(c++)
答:代码如下:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream fin("t.txt");
if(!fin){
cout < <"can't open file" < <endl;
return -1;
}
int count = 0;
char buf[256];
memset(buf, 0, 256);
while(1){
fin2>>buf;
if(fin2.eof())
break;
count++;
}
cout < <"The number of the words is : " < <count < <endl;
fin2.close();
return 0;
}
25.请定义一个自己的异常,该异常在除数大于50时被抛出。(需要编程)
答:程序如下:
class A
{
int chufa(int a,int b)throws myexception
{
if(b>50)
throw new myexception("除数不能大于50");
return a/b;
}
}
class myexception extends Exception
{
myexception(String str)
{
super(str);
}
}
class Text
{
public static void main (String[] args)
{
A a=new A();
try
{
a.chufa(1,51);
}
catch(myexception ex)
{
ex.printStackTrace()
}
}
}