2013校园招聘java笔试题

来源:互联网 发布:破解马赛克的软件 编辑:程序博客网 时间:2024/05/16 08:30

 记录一些找工作时碰到的笔试题。希望能对大家有帮助。

package Exam;import java.util.Date;import java.text.SimpleDateFormat;/* * 2013任子行校园招聘java笔试题 * 把Date类型转成指定格式的String * 把二进制字符串转成int型 */public class DateString {/** * @param args */public static String DateToString(Date date) {SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//sd.applyPattern("yyyy-MM-dd HH:mm:ss");String dateStr = sd.format(new Date());System.out.println(dateStr);return dateStr;}public static int StringToInt(String num) {int result = 0;for(int i = 0; i < num.length(); i++) {//System.out.println("i = " + i);result += (num.charAt(i)-48)<<(num.length()-i-1);//System.out.println(Integer.valueOf(num.charAt(i)));//System.out.println(result);}System.out.println(result);return result;} public static int StringToInt1(String num) {System.out.println(Integer.parseInt(num, 2));return Integer.parseInt(num, 2);}public static void main(String[] args) {// TODO Auto-generated method stubDateToString(new Date());StringToInt("1101");StringToInt1("1101");}}
package Exam;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.Writer;//任子行2013校园招聘 java笔试题//把一个UTF-8编码文件转成GBK编码文件public class EncodingTranser {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub   try {changeEncoding("C:/Users/ldl/workspace/广州笔试/src/Exam/Joseph.java", "C:/gbk.txt");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void changeEncoding(String utf8File, String gbkFile) throws IOException {String line_separator = System.getProperty("line.separator");System.out.println(line_separator);StringBuffer content = new StringBuffer();FileInputStream fis = new FileInputStream(utf8File);DataInputStream dis = new DataInputStream(fis);BufferedReader br = new BufferedReader(new InputStreamReader(dis, "GBK"));String line ;while((line =br.readLine())!=null) {content.append(line + line_separator);}br.close();dis.close();fis.close();Writer osw = new OutputStreamWriter(new FileOutputStream(gbkFile), "utf-8");osw.write(content.toString());osw.close();}}
约瑟夫环问题,打印出每次出队人的编号

package Exam;import java.util.Scanner;/* * 约瑟夫环问题,打印出每次出队人的编号 */public class Joseph {private static class Node {int no;Node next;public Node(int no) {this.no = no;}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in); System.out.println("请输入总人数\n");int total = scanner.nextInt();System.out.println("请输入出队数\n");int num = scanner.nextInt();    Node header = new Node(1); //头结点    Node pointer = header; //当前的节点        //构造循环链表    for(int i = 2; i <= total; i++) {    pointer.next = new Node(i);    pointer = pointer.next;    }        pointer.next = header;                //当链表中的节点数多于一个时,执行出队操作        while(pointer.next != pointer) {            for(int i = 1; i < num; i++) { //从当前节点开始数num-1次            pointer = pointer.next;            }            System.out.print(pointer.next.no + " "); //即将出队的人报数            pointer.next = pointer.next.next;     //下一个就是要出队的编号,让num-1指向num+1             }        System.out.print(pointer.no); //最后一个生存下来的人报数                 //注意:程序结束前 关闭资源        scanner.close();    }}
对List排序

package Exam;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;/** * 2013任子行校园招聘java笔试题 * 排序:证件类型小的排在前面,如果证件类型相同, * 证件号码小的排在前面 */class Human {private String id_type;private int id_num;public Human(String id_type, int id_num) {this.id_type = id_type;this.id_num = id_num;}public String getId_type() {return id_type;}public void setId_type(String id_type) {this.id_type = id_type;}public int getId_num() {return id_num;}public void setId_num(int id_num) {this.id_num = id_num;}}public class ListSort {/** * 这段代码要考生完成 * 调用Collections.sort(List, Comparator()) */public static void sorortList(List<Human> list) {Collections.sort(list, new Comparator<Human>() {@Overridepublic int compare(Human o1, Human o2) {// TODO Auto-generated method stubif(o1.getId_type().equals(o2.getId_type())) {return Integer.valueOf(o1.getId_num()).compareTo(Integer.valueOf(o2.getId_num()));}elsereturn o1.getId_type().compareTo(o2.getId_type());}});}public static void main(String[] args) {// TODO Auto-generated method stubList<Human> list = new ArrayList<Human>();list.add(new Human("idCard", 366));list.add(new Human("idCard", 361));list.add(new Human("idCard", 363));list.add(new Human("idCard", 362));list.add(new Human("stuCard", 110));list.add(new Human("stuCard", 120));list.add(new Human("stuCard", 119));for(Human human : list)System.out.println("证件类型 " + human.getId_type() + "证件号码 " + human.getId_num());sorortList(list);System.out.println();for(Human human : list)System.out.println("证件类型 " + human.getId_type() + "证件号码 " + human.getId_num());}}
给定字符串,求其出现重复的最长子串

package Exam;import java.util.Scanner;/** * 2013UC校园招聘试题 * 给定字符串,求其出现重复的最长子串 * 如drucwebejfucwebd中,最长子串是ucweb */public class LRS {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);String src = sc.nextLine();lrs(src);//注意养成关闭资源的好习惯sc.close();}private static int lrs(String src) {int max = 0;     int startIndex = 0;//从间隔为1开始,依次递增,查找连续重复子串for(int distance = 1; distance < src.length(); distance++) {//从第一个字符开始,和间隔distance的字符开始查找for(int temp = 0,index = 0; index < src.length()-distance; index++) {//如果两个字符相同,则temp递增;if(src.charAt(index)==src.charAt(index+distance)) {temp++;}//如果不同,temp从0开始重新计数else {temp = 0;}//如果当前累加temp比max大,则更新maxif(temp > max) {max = temp;//同时更新最长重复子串的起始的索引startIndex = index-temp+1;}}}//打印最长重复子串System.out.println("最长重复子串个数  " + max);for(int i = 0; i < max; i++)System.out.print(src.charAt(startIndex+i));System.out.println();return max;}}

package Exam;import java.util.HashMap;import java.util.Map;/** *  * 找出字符串中出现次数最多的单词,如果有多个最多的则返回null * 如"how do you do",返回do; 如"how old are you",返回null; */public class MostString {/** * @param args */public static String findMost(String s) {String[] rs = s.split(" ");//for(String str : rs) //System.out.print(str + " ");//System.out.println();Map<String, Integer> map = new HashMap<String, Integer>();int max = -1;int value = 0;String key = null;//把各个单词放入HashMap,键为单词,值为单词出现的次数for(String str : rs) {if(map.containsKey(str)) {value = map.get(str);map.put(str, value+1);}elsemap.put(str, 1);if((value=map.get(str)) > max) {max = value;key = str;}}//for(Entry entry : map.entrySet()) //System.out.println(entry.getKey() + " " + entry.getValue());//System.out.println();////System.out.println("max = " + max + ", key = " + key);//先删除最大值对应的那个实体map.remove(key);//如果删除后还存在最大值的实体,则说明字符串中存在多个最大值if(map.containsValue(max))return null;return key;}public static void main(String[] args) {// TODO Auto-generated method stubString str = "how do you do";System.out.println(str);System.out.println(findMost(str));str= "how old are you";System.out.println(str);System.out.println(findMost(str));}}

package Exam;/** * 2013校园招聘3G门户java笔试题 * 两个线程,一个执行加法操作,一个执行减法操作 * 初始值为0,操作后的范围0或1 */class Mutex {private static int i;//没把Mutex声明为静态的:(private static Mutex m = null;private Mutex() {}public static Mutex getInstance() {if(m == null) m = new Mutex();return m;}public synchronized void add() {if(i!=0)try {wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(i==0) {System.out.println(Thread.currentThread().getName() + ": " + (++i));this.notifyAll();}}public synchronized void minus() {if(i!=1)try {wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(i==1) {System.out.println(Thread.currentThread().getName() + ": " + --i);this.notifyAll();}}}class Thread1 implements Runnable {private Mutex m;public Thread1(Mutex m) {this.m = m;}@Overridepublic void run() {// TODO Auto-generated method stubfor(int i = 0; i < 3; i++)    m.add();}}class Thread2 implements Runnable {private Mutex m;public Thread2(Mutex m) {this.m = m;}@Overridepublic void run() {// TODO Auto-generated method stubfor(int i = 0; i < 3; i++)    m.minus();}}public class OneOrZeroThread {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMutex m = Mutex.getInstance();Thread t1 = new Thread(new Thread1(m), "Thread1");Thread t2 = new Thread(new Thread2(m), "Thread2");t1.start();t2.start();}}

package Exam;/** * 2013校园招聘迅雷java笔试题 * 三个线程各说的一句话,循环打印出10次 *  */public class PrintABC {public static void main(String[] args) {MajusculeABC maj = MajusculeABC.newInstance();Thread t_a = new Thread(new Thread_ABC(maj , 'A', "i want to ask u."));Thread t_b = new Thread(new Thread_ABC(maj , 'B', "Loved."));Thread t_c = new Thread(new Thread_ABC(maj , 'C', "......"));t_a.start();t_b.start();t_c.start();}}class MajusculeABC {private char charactor = 'A';/* 限制此类只创建一个对象 */private static MajusculeABC maObj =null;private MajusculeABC() {}public static MajusculeABC newInstance(){if(maObj == null) {maObj = new MajusculeABC();}return maObj;}public void setCharactor() {this.charactor += 1;if(this.charactor == 'D') {this.charactor = 'A';}}public char getCharactor() {return this.charactor;}}class Thread_ABC implements Runnable {private MajusculeABC maj;private char charactor = ' ';private String dialog;public Thread_ABC(MajusculeABC maj, char charactor, String dialog) {this.maj = maj;this.charactor = charactor;this.dialog = dialog;}public void run() {int i = 0;while (i<10) {synchronized (maj) {while(this.charactor != maj.getCharactor()) {try {maj.wait();} catch (InterruptedException e) {}}System.out.println(this.charactor + ":" +this.dialog);i++;maj.setCharactor();maj.notifyAll();}}}}

package Exam;/* * 2013艾科校园招聘java笔试题 */public class ThreeToTen {/** * @param args */public static int change(String s) {int n = 0;for(int i = 0; i < s.length(); i++) {char c = s.charAt(i);n = n + (c-48)*(int)Math.pow(3, s.length()-i-1);}return n;}public static void main(String[] args) {// TODO Auto-generated method stubint result = change("1211");System.out.println(result);}}

package Exam;public class TwoStringBufferTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubStringBuffer sb1 = new StringBuffer("A");StringBuffer sb2 = new StringBuffer("B");method(sb1, sb2);System.out.println(sb1);System.out.println(sb2);}public static void method(StringBuffer x, StringBuffer y) {x.append(y);y = x;}}

package Exam;/** * 2013校园招聘迅雷Java笔试题  * 实现删除字符串中指定的字符的算法,尽可能的高效. */public class XunLei {public String removeChars(String source, String remove) {int i = 0;int j = 0;StringBuffer sb = new StringBuffer(source);//先找到第一个属于要删除的字符  for(; !isRemoved(remove, source.charAt(i))&& i < source.length(); j=++i) {;}//找到一个不属于要删除的字符覆盖下一个需要覆盖的字符for(; i < source.length(); i++) {if(!isRemoved(remove, source.charAt(i))) {sb.setCharAt(j++, source.charAt(i));}}//改变StringBuffer的长度sb.setLength(j);return sb.toString();}private boolean isRemoved(String remove, char ch) {for(int i = 0; i< remove.length(); i++) if(ch==remove.charAt(i))return true;return false;}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub String source = "frome now on, i will study hard every day";String remove = "true";        String result = new XunLei().removeChars(source, remove);        System.out.println(result);}}

一道疑惑了很久的选择题。问程序的输出是什么?

package Exam;import java.util.Date;public class YY extends Date {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub        new YY().yy();}public void yy() {// TODO Auto-generated method stubSystem.out.println(this.getClass().getName());System.out.println(super.getClass().getName()); //为什么System.out.println(getClass().getSuperclass().getName());}}
输出结果是:

Exam.YY
Exam.YY
java.util.Date

现在解析一下:Object的getClass()方法的API,解释是:返回此 Object 的运行时类。返回的 Class 对象是由所表示类的 static synchronized 方法锁定的对象。但是super.getClass()为何返回的还是子类的类型,由于getClass()在Object类中定义成了final,子类不能覆盖该方法。注意:super不是超类的引用,而是表示在子类中调用父类的方法或属性而已。如果想得到父类的名称,应该用如下代码:getClass().getSuperClass().getName();

package Exam;/** * 2013任子行校园招聘Java笔试题 * 多线程模拟买票问题 * */public class TicketsSole {public static void main(String[] args) {     TicketThread ts = new TicketThread();Thread t1 = new Thread(ts, "线程1:");Thread t2 = new Thread(ts, "线程2:");Thread t3 = new Thread(ts, "线程3:");t1.start();t2.start();t3.start();}static class TicketThread implements Runnable {private int tickets = 100;@Overridepublic void run() {for(int i = 0; i < 100; i++)    sale();}private synchronized void sale() {if(tickets > 0) {    try { Thread.sleep(10);} catch (InterruptedException e) { e.printStackTrace();}     System.out.println(    Thread.currentThread().getName()+     "卖出一张票,剩余" + (--tickets) + "张票");    }}}}



原创粉丝点击