您的位置:首页 > 编程语言 > Java开发

Java笔试题总结(一)

2012-04-15 21:45 211 查看
package com.lee;

import java.util.HashMap;
import java.util.Random;

public class tips {
public static void main(String[] args) {

// 在if()内的判断语句少写一个"="时,仍然能被执行。括号内执行赋值语句。
// 但判断条件不成立,会执行else语句。
boolean testa = false;
boolean testb;
if (testb = testa) {
System.out.println("testa: " + testa);
} else {
System.out.println("testb: " + testb);
}

// 虽然执行了str.replace('A', 'X'),但输出结果仍是ABC,而非XBC
String str = "ABC";
str.replace('A', 'X');
// str = str.replace('A', 'X');
System.out.println(str);

// 怎么样把一个double型转成int型
// 其JDK1.5以后就改了吧?好像是,可直接强转int testd = (int) testc
// 为了更确定一下他的考试思路,还可以用其它的方法吧?
double testc = 1000.09;
int testd = (int) testc;
System.out.println("testd: " + testd);

// 联想到把字符串转成int型,应该是常用到的吧。
String testf = "100";
Integer in = new Integer(testf);
int testg = in.intValue();
int testg2 = Integer.parseInt(testf);
System.out.println("testg: " + testg);
System.out.println("testg2: " + testg2);

// 貌似有考instenceof.具体的忘记了
// instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。
// instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
boolean testh;
String str2 = "123";
testh = (str2 instanceof String); // true
System.out.println("str2 instanceof String: " + testh);
testh = (str2 instanceof Object); // also true
System.out.println("str2 instanceof Object: " + testh);
// testh = ( str instanceof Date ); // false, not a Date or subclass

// null值不是任何对象的实例,所以下面这个例子返回了false,无论这个变量声明的是什么类型。
String testi = null;
boolean testjj = (testi instanceof String);
System.out.println("null instanceof String: " + testjj);

// Java打印一布尔型变量是true还是1?
boolean testk = true;
System.out.println("Java打印一布尔型变量是true还是1?: " + testk);
System.out.println("============小节的分割线===============");

System.out.println(100.0 * 0.6);

int a1 = 'A';
System.out.println(a1);

// System.out.println(Integer.parseInt("+1"));//运行错误

int b1 = 0;
// if (b1){} //java中if里必为boolean

String b2 = "helloworld!";
System.out.println(b2.substring(2, 5)); // 从第二个到第四个,第五个不包括 输出llo
System.out.println(b2.substring(2, 2)); // 不输出

String b3 = "abdcdedf";
int x1 = b3.lastIndexOf("cd", 4);
int x2 = b3.lastIndexOf('d', 2);
int x3 = b3.lastIndexOf('d', 1);
System.out.println("x1: " + x1);
System.out.println("x2: " + x2);
System.out.println("x3: " + x3);
// x1从数组第4个开始找,即从第二个d开始,退找遇到的第一个以cd开始的子串,返回cd中的c的位置
// x2,从第2个开始到前面第一个字符,在这个子串中找最后一个出现的d。即,从d开始从abd中找到d的位置为2。
// 如果找不到,则返回-1

HashMap hm = new HashMap();
hm.put("abc", null);
System.out.println(hm.toString());
System.out.println(hm.values());

int sizeof = 3; // sizeof不是关键字,可用于变量命名
int TRUE = 3; // true是关键字,TRUE不是关键字
//int goto = 3;//goto是关键字

String bs = "hello";
String bt = "hello";
char bc[] = { 'h', 'e', 'l', 'l', 'o' };
System.out.println(bs.equals(bc)); // 输出false
System.out.println(bs.equals("hello")); // 输出true
System.out.println(bc); // 输出hello
System.out.println(bs == bt); // true
System.out.println("============2@小节的分割线===============");
short b4 = 1;
// b4=b4+1; 编译不通过
b4 += 1; // 编译通过

String r5 = "hello";
r5 += "there";
System.out.println("r5: " + r5);
// char r6=r5[3]; 这样写是不对的
int i = r5.length();
r5 = r5 + 3;
System.out.println("r5: " + r5);

System.out.println(4 | 7); // 输出7  //4的二进制:0100 7的二进制:0111 输出:0111
System.out.println(4 & 7); // 输出4  //4的二进制:0100 7的二进制:0111 输出:0100
System.out.println(3 | 2); // 输出3  //3: 0011 2: 0010  0011
System.out.println(3 | 6); // 输出7  //3: 0011 6: 0110  0111c

String b16 = "hello";
int b21 = 6;
b21 >>>= 1;
System.out.println("b21: " + b21);
int b19 = b16.length();
System.out.println("b19: " + b19);
// char b17=b16[1];
String[] b18 = { "hello", "java" };
int b20 = b18.length; // 字符串用length(),数组用length
System.out.println("b20: " + b20);

int b22 = 2; // 不用第三个变量来交换两个变量的数值
int b23 = 5;
b23 = b22 + b23;
b22 = b23 - b22;
b23 = b23 - b22;
System.out.println("b22: " + b22);
System.out.println("b23: " + b23);

// System.out.println(Integer.parseInt("+1"));
// 上面的句子抛出:java.lang.NumberFormatException异常

int x = 1, y = 2, z = 3;
y += z-- / ++x;
int b15 = 3 / 2;
System.out.println("b15:" + b15);
System.out.println("y:" + y);

int aa = 0x22;
int bi = 1;
int bj;
bj = bi++;
System.out.println("bi: " + bi + "bj: " + bj);
System.out.println(aa);

float[] b24[] = new float[5][6];
float[][] b25 = new float[6][6];
float b26[][] = new float[5][];
// float []b27[] = new float[][6]; //error

// float b8=2.0; //error
double b9 = 2.0;
long b10 = 2;

System.out.println("Math.round(11.4): " + Math.round(11.4)); //11 四舍五入,最近的一个长整形
System.out.println("Math.round(11.5): " + Math.round(11.5));	// 12
System.out.println("Math.round(-11.4): " + Math.round(-11.4)); // -11
System.out.println("Math.round(-11.5): " + Math.round(-11.5)); //-11
System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));   //12
System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68)); //-12

System.out.println("============3#小节的分割线===============");

int c1 = 1;
boolean[] c2 = new boolean[3];
boolean c3 = c2[c1];
System.out.println("c3: " + c3);

Random random = new Random();
int n = random.nextInt(100);
System.out.println("n: "+n);
StringBuffer sb = new StringBuffer();
for (int i1 = 0; i1 <= n; i1++) {
int c = random.nextInt(255);
if ((c <= '9' && c >= '0') || (c <= 'Z' && c >= 'A')
|| (c <= 'z' && c >= 'a')) {
sb.append((char) c);
i1++;
}
}
System.out.println(sb.toString());

}

}

/*
* 匿名内部类就是没有名字的内部类。什么情况下需要使用匿名内部类?如果满足下面的一些条件,使用匿名内部类是比较合适的: ·只用到类的一个实例。
* ·类在定义后马上用到。 ·类非常小(SUN推荐是在4行代码以下) ·给类命名并不会导致你的代码更容易被理解。 在使用匿名内部类时,要记住以下几个原则:
* ·匿名内部类不能有构造方法。 ·匿名内部类不能定义任何静态成员、方法和类。
* ·匿名内部类不能是public,protected,private,static。 ·只能创建匿名内部类的一个实例。
* ·一个匿名内部类一定是在new的后面,用其隐含实现一个接口或实现一个类。 ·因匿名内部类为局部内部类,所以局部内部类的所有限制都对其生效。
*/

/*
* DB并发操作通常会带来三类问题,它们是丢失更新、不一致分析和读脏数据。
*
* 为了保证数据库的完整性,事务必须具有原子性、一致性、隔离性、持久性四个性质,即事物ACID性质。
*
* 表达式operator+(x,y)还可以表示为(x+y)
*
* 详细设计的任务是确定每个模块的内部特性,即模块的算法、( 使用的数据 )。
*
* 数据总线用来在CPU与内存储器(或I/O设备)之间交换信息;地址总线由CPU发出 ,用来确定CPU要访问的内存单元(或I/O端口)的地址信号。
*
* 用顺序存储的方法将完全二叉树中的所有结点逐层存放在数组A[1] ~ A
中,结点A[i]若有左子树, 则左子树的根结点是A[2i]
*
* 系统流程图是描述(体系结构)的工具
*
* 正数的补码与原码相同,负数的补码是用正数的补码按位取反,末位加1求得。 若x补 =0.1101010 ,则 x 原=0.1101010
* 若[X]补=1.1011 ,则真值 X = -0.0101 [X]补=1.1011,其符号位为1,真值为负;真值绝对值可由其补码经求补运算得到,
* 即按位取后得0.0100再末位加1得0.0101,故其真值为-0.0101。
*
* Jsp路径太深文件名太长就无法读取文件,jsp路径最大长度是多少 255
*
* 一个数据库表A(name varchar(10),age int),请写出一个SQL语句,按照age排序查出年龄最大的10条记录数
*
* HashTable和HashMap的区别 1.HashTable的方法是同步的,HashMap未经同步
* 2.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。
* 细见:http://blog.csdn.net/paincupid/archive/2009/11/21/4846699.aspx
*
* java变量命名规则:可以任何字母开始,另外还可以以"_"和"$"开始,一般"$"是很多代码生成器用的,不能以数字开头
*
* octal 八进制 hex 十六进制
*
* 递归函数sum(int a[ ],int n)的返回值是数组a[ ]的前n个元素之和 public int sum(int a[],int n){ if
* (n>0) return sum(a,n-1)+a[n-1]; else return 0; }
*
* 现在的服务器中寻找一种服务使用JNDI,全称Java Naming and Directory Interface
*
* 请填写你所知道的线程同步的方法:wait(); sleep(); notify(); notityAll()
*
* Servlet生命周期中有哪几个方法?2、 init(); service(); doGet()/doPost(); destroy()
*
* 多态、继承、封装 polymorphism inheritance encapsulation
*
* 在JSP中数据共享有四种范围,分别是page,request,session,application
*
*
*
*/


答案:

testb: false

ABC

testd: 1000

testg: 100

testg2: 100

str2 instanceof String: true

str2 instanceof Object: true

null instanceof String: false

Java打印一布尔型变量是true还是1?: true

============小节的分割线===============

60.0

65

llo

x1: 3

x2: 2

x3: -1

{abc=null}

[null]

false

true

hello

true

============2@小节的分割线===============

r5: hellothere

r5: hellothere3

7

4

3

7

b21: 3

b19: 5

b20: 2

b22: 5

b23: 2

b15:1

y:3

bi: 2bj: 1

34

Math.round(11.4): 11

Math.round(11.5): 12

Math.round(-11.4): -11

Math.round(-11.5): -11

正数:Math.round(11.68)=12

负数:Math.round(-11.68)=-12

============3#小节的分割线===============

c3: false

n: 36

plYkkw
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: