Java笔试题

一、基础编程题

1、编程输出一个倒立三角形图。

Java笔试题_第1张图片

package Test1;

public class Test1 {
	public static void main(String[] args) {
		for(int i = 0; i < 5; i++){
			for(int j = 5; j >i; j--){
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

2、打印昨天的当前时刻。

package Test1;

import java.util.Calendar;

public class Test2 {
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		System.out.println(cal.getTime());
	}
}

3、编写程序,取得当前时间的年月日,小时分秒。

package Test1;

import java.util.Calendar;

public class Test3 {
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		System.out.println(cal.get(Calendar.YEAR));
		System.out.println(cal.get(Calendar.MONTH)+1);
		System.out.println(cal.get(Calendar.DATE));
		System.out.println(cal.get(Calendar.HOUR));
		System.out.println(cal.get(Calendar.MINUTE));
		System.out.println(cal.get(Calendar.SECOND));
	}
}

二、中级编程题

4、编写冒泡排序法。

package Test1;

public class Test4 {
	public static void main(String[] args) {
		int[] nums = new int[]{43,23,12,56,34,78,109};
		display(nums);
		bubbleSort(nums);
		display(nums);
	}
	
	public static void bubbleSort(int[] nums){
		for(int i = 0; i < nums.length; i++) {
			for(int j = 0; j < nums.length - i - 1;j++) {
				if(nums[j] > nums[j+1]) {
					int temp = nums[j];
					nums[j] = nums[j+1];
					nums[j+1] = temp;
				}
			}
		}
	}
	
	public static void display(int[] nums) {
		for(int i = 0; i < nums.length; i++) {
			System.out.print(nums[i] + ",");
		}
		System.out.println();
	}
}

5、用Java代码实现堆栈。

package Test1;

public class Test5 {
	public static void main(String[] args) throws Exception {
		Stack stack=new Stack(5);
		stack.push(1);
		stack.push(2);
		stack.push(3);
		stack.push(4);
		stack.push(5);
		while(stack.top>=0)
		{
			System.out.println(stack.pop());
		}		
	}
}

class Stack{
	int[] data;
	int top;
	int maxSize;
	public Stack(int maxSize){
		this.maxSize = maxSize;
		data = new int[maxSize];
		top = -1;
	}
	
	public boolean push(int data){
		if(top + 1 == maxSize) {
			System.out.println("栈满了");
			return false;
		}
		this.data[++top] = data;
		return true;
	}
	
	public int pop() throws Exception{
		if(top == -1){
			throw new Exception("栈空了");
		}
		return this.data[top--];
	}
}

6、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。

package Test1;

class TestThread{
	private int j;
	public synchronized void inc(){
		j++;
		System.out.println(Thread.currentThread().getName() + "-inc:" + j);
	}
	public synchronized void dec(){
		j--;
		System.out.println(Thread.currentThread().getName() + "-dec:" + j);
	}

}

public class Test6{
	public static void main(String[] args){
		TestThread t=new TestThread();
		for (int i = 0; i < 2; i++){
			Thread inc=new Thread(new Inc(t));
			Thread dec=new Thread(new Dec(t));
			inc.start();
			dec.start();
		}
	}
}


class Inc implements Runnable{
	private TestThread obj;
	public Inc(TestThread obj){
		this.obj=obj;
	}
	public void run(){
		this.obj.inc();
	}
}
class Dec implements Runnable{
	private TestThread obj;
	public Dec(TestThread obj){
		this.obj=obj;
	}
	public void run(){
		this.obj.dec();
	}
}

	

 

你可能感兴趣的:(Java面试题)