java笔试编程题

java笔试编程题
java笔试编程题

我给你讲,你可能看过很多网上的资料啊,什么经典编程五十题啊等等,那个我感觉你可以看看,但是水平不够的话,应付笔试就显的太多了,本文内容是我个人经历数家公司笔试后的倾心总结,,eclipse上均能运行成功,都是我个人总结的,特别是红色笔标记的更应该烂记于心,不要总是到用到的时候才后悔莫及!祝你面试成功!

1.写一个函数,例如:给你的a b c 则输出abc acb bac bca cab cba

import java.util.ArrayList;

import java.util.List;

public class NumTest {

public static void main(String[] args) {

String s="ABCD";//原字符串

List result = list(s, "");//列出字符的组合,放入result

System.out.println(result.size());;

System.out.println(result);

}

public static List list(String base,String buff){

List result = new ArrayList();//存放结果信息。

if(base.length()<=0){

result.add(buff);

}

for(int i=0;i

List temp = list(new

StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i));

result.addAll(temp);

}

return result;

}

}

2.写一个函数,给你一个字符串倒序输出来

public String getString(String str){

if(str!=null){

String newStr = "";

for(int i=0;i

{

char c = str.charAt(str.length()-1-i);

newStr = newStr + c;

}

return newStr;

}else{

return null;

}

}

3.不使用中间变量把两个变量的值互换

int a=10;

int b=100;

a=a*b;

b=a/b;

a=a/b;

System.out.print("a="+a+" b="+b);

4.冒泡排序

public class Sort {

/*

* 冒泡排序

*/

public static void main(String []args)

{

int a[]={12,34,5,65,87,45,565,8,4,3,56,65}; DataSort(a);

}

//排序

public static void DataSort(int b[])

{

for(int i=0;i

{

for(int j=0;j

{

int temp=0;

if(b[j]>b[j+1])

{

temp=b[j];

b[j]=b[j+1];

b[j+1]=temp;

}

}

}

PrintSort(b);

}

//打印

public static void PrintSort(int c[])

{

for(int k=0;k

{

System.out.print(c[k]+" ");

}

}

}

5.写一个复制文件的程序

File fileOld = new File(pathOld);

File fileNew = new File(pathNew);

if(fileOld.exists()){

try {

FileInputStream fis = new FileInputStream(fileOld);

FileOutputStream fos = new FileOutputStream(fileNew); int read = 0;

while ((read = fis.read()) != -1) {

fos.write(read);

fos.flush();

}

fos.close();

fis.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

6. 将this is a test 转化为This Is A Test

String str="this is a man";

char c[]=new char[str.length()];

str.getChars(0,str.length(),c,0);

if(c[0]>='a'&&c[0]<='z')

{

c[0]=(char)(c[0]-32);

}

for(int i=1;i<=c.length-1;i++)

{

if(c[i]==' ')

{

c[i+1]=(char)(c[i+1]-32);

}

}

str=new String(c);

7.题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。import java.util.*;

public class lianxi07 {

public static void main(String[] args) {

int digital = 0;

int character = 0;

int other = 0;

int blank = 0;

char[] ch = null;

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

ch = s.toCharArray();

for(int i=0; i

if(ch >= '0' && ch <= '9') {

digital ++;

} else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {

character ++;

} else if(ch == ' ') {

blank ++;

} else {

other ++;

}

}

System.out.println("数字个数: " + digital);

System.out.println("英文字母个数: " + character);

System.out.println("空格个数: " + blank);

System.out.println("其他字符个数:" + other );

}}

8.题目:打印出如下图案(菱形)

*

***

*****

*******

*****

***

*

public class lianxi19 {

public static void main(String[] args) {

int H = 7, W = 7;//高和宽必须是相等的奇数

for(int i=0; i<(H+1) / 2; i++) {

for(int j=0; j

}

for(int k=1; k<(i+1)*2; k++) {

System.out.print('*');

}

System.out.println();

}

for(int i=1; i<=H/2; i++) {

for(int j=1; j<=i; j++) {

System.out.print(" ");

}

for(int k=1; k<=W-2*i; k++) {

System.out.print('*');

}

System.out.println();

}

}}

9.题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。public class lianxi20 {

public static void main(String[] args) {

int x = 2, y = 1, t;

double sum = 0;

for(int i=1; i<=20; i++) {

sum = sum + (double)x / y;

t = y;

y = x;

x = y + t;

}

System.out.println("前20项相加之和是: " + sum);

}

}

10.单例模式

public class Classname

{

private static Classname instance;

private Classname(){}

public static Classname getInstance()

{

instance = new Classname;

return instance;

}

}

或者是

一个Singleton模式的例子

public class Singleton {

private static Singleton single = new Singleton();

private Singleton(){}

public Singleton getInstance(){

return single;

}

}

11.乘法口诀表

public class ChengFa {

public static void main(String args[]){

for(int i=1;i<=9;i++){

for(int j=1;j<=i;j++){

System.out.print(j+"*"+i+"="+i*j+"\t");

}

System.out.println();

}

}

}

12.计算从0到100每个数字出现的次数

public class CountTimes_1 {

public static void main(String args[]){

String s = "";

for(int i = 1;i <= 100;i++){

s=s+i;

}

int count0=0;

for(int i=0;i

if(s.charAt(i)=='1'){

count0++;

}

}

System.out.println("0出现了:"+count0+"次");

}

}

13.递归实现回文判断

public class HuiWen {

public static void main(String[] args){

String str = "abbba";

int j = 0;

System.out.println(HuiWen.loopWord(str, j));;

}

static boolean loopWord(String str, int i) {

if (str.charAt(i) == str.charAt(str.length() - 1 - i)) { if (i == (str.length() + 1) / 2)

return true;

return loopWord(str, i + 1);

} else {

return false;

}

}

}

14. public class DiGui_11 {

/*

* 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少,用递归算法实现。

* */

static int find30(int n){

if (n <= 0)

return 0;

else if(n > 0 && n <= 2)

return 1;

return find30(n-1)+find30(n-2);

}

public static void main(String[] args){

System.out.println(DiGui_11.find30(30));

}

}

15. 将一整数逆序后放入一数组中(要求递归实现) Ex : 1234 变为 {4,3,2,1}

public class DiGui4321 {

public static void main(String[] args) {

int[] a = new int[4];

int number = 4321;

DiGui4321.revert(a, 0, number);

//int[] b = DiGui4321.revert(a, 0, number);

/*for(int j = 0;j < b.length;j++){

System.out.print(b[j] + ",");

}*/

}

static int[] revert(int rs[], int i, int number) {

if (i < rs.length) {

rs[i] = number % 10;

System.out.print(rs[i] + ",");

number = (number - number % 10) / 10;

return revert(rs, i + 1, number);

} else {

return rs;

}

}

}

16.题目:设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。

public class XianCheng {

private int j;

public static void main(String args[]){

XianCheng tt=new XianCheng();

Inc inc=tt.new Inc();

Dec dec=tt.new Dec();

for(int i=0;i<2;i++){

Thread t=new Thread(inc);

t.start();

t=new Thread(dec);

t.start();

}

}

private synchronized void inc(){

j++;

System.out.println(Thread.currentThread().getName()+"-inc:"+j);

}

private synchronized void dec(){

j--;

System.out.println(Thread.currentThread().getName()+"-dec:"+j);

}

class Inc implements Runnable{

public void run(){

for(int i=0;i<100;i++){

inc();

}

}

}

class Dec implements Runnable{

public void run(){

for(int i=0;i<100;i++){

dec();

}

}

}

}

17.下边是个经常考的经典选择题,不是编程题目,你可以看看到底改变了什么?

public class ZhiChuanDi {

public static void change(String s,int[] a){ s = "xyz";

a[0] = 4;

}

public static void main(String[] args){

String str = "abc";

int[] a = {1,2,3};

ZhiChuanDi.change(str, a);

System.out.println(str + "laobai" + a[0]);

}

}

18.这个我只想告诉你collections里的一个方法,我是被问过滴import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class CollectionsTest

{

static List list = new ArrayList();

String a = "123";

public static void main(String[] args) {

int[] a = {1,7,5,69,34,25,18,78};

for (int j : a) {

list.add(j);

}

Collections.sort(list);

System.out.print(list);

}

}

相关主题
相关文档
最新文档