a daily sentence: not once and for all happiness, only the first bitter after the sweet effort! Come on!
java应届生或自学初学者笔试面试题目总结,希望对大家有帮助,题目来自自己学习总结和老师讲解,网络资源,书籍资料。敲出来大家看,每次更新10+道题目,希望大家能找到一份好工作。
Part I: The basic part
基础部分的包括:基本语法,类似相关的语法,内部类、继承、异常、线程、集合、IO、虚拟机、其他。
11 ". Can the Java source file contain more than one class (not an inner class)? What are the restrictions?
There can be multiple, but only one public class, and the class name of public must match the file name.
2 does Java have a goto?
Goto belongs to a reserved word in Java and is not currently used.
3 Talk about the difference between & and &&
Both & and && can be logical AND operator, representing logic and (and), when the result of both sides of the operator is true, the result of the entire operation is true, otherwise, the end result is false if one side is false.
&& A short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated, for example, for an if (str! = null &&!str.equals (")) expression, when STR is NULL, The subsequent expression does not execute, so no exception is thrown, and if && is changed to &, a NullPointerException exception is thrown. For if (x==3 & ++y>0) expression, if x==3 is not true, Y will add, and for if (x==3 && ++y) expression, if x==3 is not true, Y will not increment.
& can also be used as a bitwise operator, when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, for example: 0x31 & The result of 0x0f is 0x01.
Note: This topic first said the similarities between the two, and the special points of && and &, and cited some classic examples to show that they understand thoroughly deep, practical experience.
4 How do I jump out of the current multiple nested loops in Java?
To jump out of multiple loops in Java, you can define a label in front of the outer loop statement, and then use a labeled break statement in the code of the inner loop body to jump out of the outer loop. For example:
ok:for(int i=0; i<10; i++){ for(int j=0; j<10; j++){ System.out.println("i=" + i + ",j=" + j); if(j == 5) break ok; }}
Individuals do not like to label this way, but let the result of the outer loop condition expression be controlled by the inner Loop body code, for example, to find a number in a two-dimensional array:
int arr[][] = {{1,2,3}, {4,5,6,7}, {9}};boolean found = false;for(int i=0; i<arr.length && !found; i++){ for(int j=0; j<arr[i].length; j++){ System.out.println("i=" + i + ",j=" + j); if(arr[i][j] == 5){ found = true; break; } }}
Can the 5 switch statement function on a byte? Can you work on long? Can you work on a string?
In switch (EXPRL), EXPRL can only be an integer expression or enumeration constant. An integer expression can be an int primitive type or an integer wrapper type, and since Byte,short,char can be implicitly converted to int, these types and the wrapper types of those types are also possible. Obviously, the long and string types do not conform to the syntax of switch, and cannot be implicitly converted to the int type, so they cannot function in a switch statement.
6 short S1 = 1;S1 = S1 + 1; what's wrong? Short S1 = 1;s1 + = 1; what's wrong?
For short S1 = 1;S1 = S1 + 1; Because the type of the expression is automatically promoted by the s1+1 operation, the result is type int, and when assigned to the short type S1, the compiler will report an error that requires a cast type;
For short S1 = 1;s1 + = 1; since + = is a Java-language-defined operator, the Java compiler will treat it specially, so it can compile correctly.
Can I store a Chinese character in a 7 char variable? Why?
Char-type variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so the char type variable can of course store Chinese characters. However, if a special Chinese character is not included in the Unicode encoding character set, the character cannot be stored in a char type variable. Note: Unicode encoding accounts for two bytes, so the char type variable is also two bytes.
8 Use the most efficient method to calculate 2 times 8 equals a few.
Answer: 2 << 3
Because speaking of a number to the left N-bit, will be equivalent to multiplying by 2 of the N-square, then a number multiplied by 8 just move it to the left 3 bits, and bit operation is directly supported by the CPU, the most efficient.
9 Please design a calculator that can calculate 10 billion.
First of all to understand the question of what is the point of investigation, one is to first of all the basic principles of the computer to clear the underlying details, to know the addition and subtraction of the bit operation principle and know that the arithmetic operations in the computer will be out of bounds, the second is to have a certain object-oriented design ideas.
First, the computer uses a fixed number of bytes to store numeric values, so the computer can be represented by a certain range of values, in order to facilitate interpretation and understanding, we first use a byte type integer as an example, it is stored with a byte, the maximum value represented by 128 to +127. 1 binary in memory corresponding to 11111111, if two-1 add, do not consider the Java operation when the type promotion, the operation will produce a carry, the binary result is 1, 11111110, because the carry after more than the byte type of storage space, so the carry part is discarded, That is, the final result is 11111110, that is, 2, which is exactly the way of using overflow to achieve the operation of negative numbers. 128 in memory corresponding to the binary is 10000000, if two-128 add, regardless of Java, the operation will produce a carry, the binary result is 1, 00000000, beyond the range of byte, the carry part is discarded, the final result is 00000000, that is, 0, The result is obviously incorrect, which means that the arithmetic operations in the computer are considered to be out of bounds, and the results of the two numeric values cannot exceed the numerical range of the type in the computer. Since the type of expression is automatically promoted in Java, we cannot use the byte type as an experiment to demonstrate the phenomenon of this problem, and you can use the following integer to try it out:
int a = Integer.MAX_VALUE;int b = Integer.MAX_VALUE;int sum = a + b;System.out.println("sum=" + sum);
先不考虑long类型,由于int的正整数范围为231,表示的最大数值约等于2*1000*1000*1000,也就是20亿的大小,所以,要实现一个100亿的计算器,我们得自己设计一个类可以用于表示很大的数值,并且提供了与另一种个整数进行相加减乘除的功能,大概功能如下:
(1) There are two member variables within this class, one representing a symbol and a binary number representing the numeric value in a byte array;
(2) There is a construction method that converts a string containing a multi-digit value into an internal symbol and byte array;
(3) Provide the function of subtraction.
public class BigInteger{ int sign; byte[] val; public BigInteger(String val){ sign = ; val = ; } public BigInteger add(BigInteger other){} public BigInteger subtract(BigInteger other){} public BigInteger multiply(BigInteger other){} public BigInteger divide(BigInteger other){}}
备注:想要写出这个完整的代码,是比较复杂的,笔试和面试中可能没有足够的时间写出,感兴趣的伙伴可参看jdk中自带的java.math.Biginteger类的源码。
10 When you use the final keyword to decorate a variable, is the reference immutable or does the applied object not change?
When you use the final keyword to decorate a variable, it means that the reference variable cannot be changed, and the contents of the object referred to by the reference variable can become. For example:
final StringBuffer a = new StringBuffer(“immutable”);
The EXECUTE statement will report a compilation error:
a = new StringBuffer(“”);
But the following is done by compiling:
a.append(“broken!”);
When someone defines a method's parameters, it may want to block the method from modifying the passed-in Parameter object in the following form:
public void method(final StringBuffer param){}
In fact, this is not possible, within the method can add the following code to modify the Parameter object:
param.append(“aaa”);
Java Written test interview topic (i)