Java笔试题
发布日期:2021-06-29 15:42:22 浏览次数:2 分类:技术文章

本文共 2966 字,大约阅读时间需要 9 分钟。

一、基础编程题

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();	}}

 

转载地址:https://codingchaozhang.blog.csdn.net/article/details/80970116 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring Boot快速入门---(一)spring boot的创建及几种启动方式
下一篇:Java程序员常见笔试题分析

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月09日 19时29分40秒