笔试题(一)—— java基础

import java.util.Random;
/**
 * 
 * @author Alexs
 */
/*
 * Demo1
 */
class Demo1 {
	public static void main(String[] args) {
		int num=2147483647;
		long temp=num+2L;
		System.out.println(temp);//2147483649    分析:num为int类型(4字节),与long类型(8字节)变量相加时自动提升
	}
}
/*
 * Demo2
 */
class Demo2{
	public static void main(String[] args) {
		char c='A';
		int num=10;
		switch(c) {
		case 'B':
			num++;
		case 'A':
			num++;
		case 'Y':
			num++;
			break;
		default:
			num--;
		}
		System.out.println(num);//12    分析:每个case是一个tag,当匹配到tag时会忽略其他tag然后顺序执行代码体,直到遭遇break或者}
	}
}
/*
 * Demo3
 */
class Demo3{
		public static void main(String[] args) {
			int x=10;
			double y=20.2;
			long z=10L;
			String str=""+x+y*z;
			System.out.println(str);//10202.0    分析:y*z时因为有double类型参与运算,所以默认会保留一位小数,+号在这里是连接符
		}
}
/*
 * Demo4
 */
class Demo4{
	public static void main(String[] args) {
		int sum=0;
		for(int x=0;x<10;x++) {
			sum+=x;
			if(x%3==0) {
				break;
			}
		}
		System.out.println(sum);//0    分析:0除以3商0余0,if的条件判断为true,直接跳出循环
	}
}
/*
 * Demo5
 */
class Demo5{
	public static void main(String[] args) {
		System.out.println(inc(10)+inc(8)+inc(-10));//35  分析:+号在这里是运算符,20+16+(-1)结果为35
	}
	public static int inc(int temp) {
		if(temp>0) {
			return temp*2;
		}
		return -1;
	}
}
/*
 * Demo6
 */
class Demo6{
	public static void main(String[] args) {
		boolean flag=10%2==1 && 10/3==0 && 1/0==0;
		System.out.println(flag?"A":"B");//B    分析:&符号代表所有条件判断都为true结果才会为true,&&代表短路运算,10%2==1为false,直接返回false
	}
}
/*
 * Demo7
 */
class Demo7{
	public static void main(String[] args) {
		int num=50;
		num=num++*2;
		System.out.println(num);//100    分析:num先乘以2并完成赋值之后才会自增
	}
}
/*
 * Demo8
 */
class Demo8{
	public static void main(String[] args) {
		int i=1;
		int j=i++;//分析:i先赋值再自增,所以j为1
		if(i==(++j)&&((i++)==j)) {//分析:i完成自增为2,j先完成自增再比对也为2,后面i先与j比对之后再完成自增
			i+=j;//分析:i完成第二次自增为3,j为2
			System.out.println("i="+i);//5
		}
	}
}
/*
 * Demo9
 */
class Demo9{
	public static void main(String[] args) {
		String str="";
		for(int x=0;x<5;x++) {
			str+=x;
		}
		System.out.println(str);//01234    分析:x的值从0到4,共循环5次
	}
}
/*
 * Demo10
 */
class Demo10{
	public int a() {
		int i=0; 
		i++;
		return i;
	}
	public static void main(String[] args) {
		Demo10 d=new Demo10();
		d.a();
		int j=d.a();
		System.out.println(j);//1    分析:a()的返回结果为1,j只是接收了a()的返回值
	}
}
/*
 * Demo11
 */
class supers{
	String name;
	public supers(){};
	public supers(String name) {
		this.name=name;
	}
	public void fun1() {
		System.out.println("this is class super!"+name);
	}
}

class sub extends supers{
	
	public void fun1() {
		System.out.println("this is class sub!"+name);
	}
}

class Demo11{
	public static void main(String[] args) {
		supers s=new sub();
		s.fun1();//this is class sub!null    分析:父类的对象指向子类的实例,子类的fun1()重写了父类的方法,name未赋值默认为null
	}
}
/*
 * Demo12
 */
class Demo12{
	public static void main(String[] args) {
		int i=9;
		switch (i) {
		default:
			System.out.println("default");
		case 0:
			System.out.println("zero");
		case 1:
			System.out.println("one");
		case 2:
			System.out.println("two");
		}
		/*
		 * default
		 * zero
		 * one
		 * two    分析:每个case是一个tag,当匹配到tag时会忽略其他tag然后顺序执行代码体,直到遭遇break或者}
		 */
	}
}
/*
 * Demo13 写一个单例模式
 */
class Demo13{
	//饿汉式
	private static final Demo13 d=new Demo13();
	private Demo13() {}
	public static Demo13 getInstance() {
		return d;
	}
}
/*
 * Demo14
 * 生成一个随机密码,密码包含大小写字母及数字,并且可根据输入参数对密码长度进行调整
 */
class Demo14{
	//定义一下字符串常量
	private static final String CHAR_ALL="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public String getRandomPwd(int num) {
		String result="";
		//实例化Random对象
		Random r=new Random();
		for(int i=0;i<num;i++) {
			//生成0-62之间的(小于62)int值
			int j=r.nextInt(CHAR_ALL.length());
			//截取下标跟跟int值对应的字符
			String singleChar=CHAR_ALL.substring(j, j+1);
			//拼接
			result+=singleChar;
		}
		return result;
	}
	public static void main(String[] args) {
		Demo14 d=new Demo14();
		System.out.println(d.getRandomPwd(6));
	}
}
/*
 * Demo15
 */
class Demo15 {
	 public static void main(String []args) {
			int i=9;
			int j=9;
			//System.out.println(i==j);//true
			//System.out.println(j==i++);//true
			//System.out.println(j==++i);//false
			//System.out.println(j++==i);//true
			//System.out.println(++j==i);//false
	 }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值