J2EE基础部分
1、运算符优先级问题,下面代码的结果是多少?(笔试)
- package test;
- public class Test {
- public static void main(String[] args) {
- int k = 0;
- int ret = ++k + k++ + ++k + k;
- // ret的值为多少
- System.err.println(ret);
- }
- }
2、运算符问题,下面代码分别输出什么?(笔试)
- package test;
- public class Test {
- public static void main(String[] args) {
- int i1 = 10, i2 = 10;
- System.err.println("i1 + i2 = " + i1 + i2);
- System.err.println("i1 - i2 = " + i1 - i2);
- System.err.println("i1 * i2 = " + i1 * i2);
- System.err.println("i1 / i2 = " + i1 / i2);
- }
- }
- package test;
- public class Test {
- public void myMethod(String str) {
- System.err.println("string");
- }
- public void myMethod(Object obj) {
- System.err.println("object");
- }
- public static void main(String[] args) {
- Test t = new Test();
- t.myMethod(null);
- }
- }
- package test;
- import java.util.Date;
- public class Test {
- public static void main(String[] args) {
- Date date = new Date();
- System.err.println(date.getMonth() + " " + date.getDate());
- }
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- double val = 11.5;
- System.err.println(Math.round(val));
- System.err.println(Math.floor(val));
- System.err.println(Math.ceil(val));
- }
- }
- package test;
- import java.io.File;
- public class Test {
- public static void main(String[] args) {
- new Test().read("D:/test", "");
- }
- public void read(String path, String tab) {
- File file = new File(path);
- File[] childFiles = file.listFiles();
- for (int i = 0; childFiles != null && i < childFiles.length; i++) {
- System.err.println(tab + childFiles[i].getName());
- if (childFiles[i].isDirectory()) {
- read(childFiles[i].getPath(), tab + "\t");
- }
- }
- }
- }
7、从键盘读入10个整数,然后从大到小输出。(笔试)
- package test;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // 注意这里的数组,不是int的
- Integer[] arr = new Integer[10];
- for (int i = 0; i < 10; i++) {
- arr[i] = in.nextInt();
- }
- Arrays.sort(arr, new Comparator<Integer>() {
- @Override
- public int compare(Integer o1, Integer o2) {
- if (o1 > o2) return -1;
- if (o1 < o2) return 1;
- return 0;
- }
- });
- System.err.println(Arrays.toString(arr));
- }
- }
8、下面代码的结果是什么?
- package test;
- public class Test extends Base {
- public static void main(String[] args) {
- Base b = new Test();
- b.method();
- Test t = new Test();
- t.method();
- }
- @Override
- public void method() {
- System.err.println("test");
- }
- }
- class Base {
- public void method() throws InterruptedException {
- System.err.println("base");
- }
- }
- package test;
- public class Test extends Base {
- public static void main(String[] args) {
- new Test().method();
- }
- public void method() {
- System.err.println(super.getClass().getName());
- System.err.println(this.getClass().getSuperclass().getName());
- }
- }
- class Base {
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- String str1 = new String("abc");
- String str2 = new String("abc");
- System.err.println(str1.equals(str2));
- StringBuffer sb1 = new StringBuffer("abc");
- StringBuffer sb2 = new StringBuffer("abc");
- System.err.println(sb1.equals(sb2));
- }
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- System.err.println(new Test().method1());
- System.err.println(new Test().method2());
- }
- public int method1() {
- int x = 1;
- try {
- return x;
- } finally {
- ++x;
- }
- }
- public int method2() {
- int x = 1;
- try {
- return x;
- } finally {
- return ++x;
- }
- }
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- System.err.println(method());
- }
- public static boolean method() {
- try {
- return true;
- } finally {
- return false;
- }
- }
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- }
- public synchronized void m1() {
- }
- public static synchronized void m2() {
- }
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- Integer i1 = 127;
- Integer i2 = 127;
- System.err.println(i1 == i2);
- i1 = 128;
- i2 = 128;
- System.err.println(i1 == i2);
- }
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- String str1 = "a";
- String str2 = "a";
- String str3 = new String("a");
- System.err.println(str1 == str2);
- System.err.println(str1 == str3);
- str3 = str3.intern();
- System.err.println(str1 == str3);
- }
- }
- package test;
- public class Test {
- public static void main(String[] args) {
- System.err.println(12 - 11.9 == 0.1);
- }
- }
- package test;
- import java.math.BigInteger;
- public class Test {
- public static void main(String[] args) {
- BigInteger one = new BigInteger("1");
- BigInteger two = new BigInteger("2");
- BigInteger three = new BigInteger("3");
- BigInteger sum = new BigInteger("0");
- sum.add(one);
- sum.add(two);
- sum.add(three);
- System.out.println(sum.toString());
- }
- }
- package test;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- public class Test {
- public static void main(String[] args) {
- Set<String> set = new HashSet<String>();
- set.add("one");
- set.add("two");
- set.add("three");
- set.add("four");
- set.add("five");
- for (Iterator<String> it = set.iterator(); it.hasNext();) {
- System.err.println(it.next());
- }
- }
- }
转载地址:http://blog.csdn.net/smcwwh/article/details/7315041