ads1

четверг, 10 декабря 2015 г.

10 interesting questions for Java, which you can ask at the interview

We have collected 10 examples of tricky program that displays any information. You have to look at the code and say that the program displays. It is possible that the program will not even compile. In this case, you need to specify that the compiler will output.
Such questions like asking for interviews, so no harm will refresh their knowledge of the programming language Java. The spoilers are hidden answers - that will bring the program or the compiler. Try to think of themselves before watching the correct answer.

What will bring the following program?

  • Every time we create an object of a class constructor is called first, and memory is allocated for all non-static variables.
  • Where B b = new B (); object Class B is assigned a new object of the same class.
  • Record B b = new B (); leads to recursive execution constructor that creates the infinite object. That is why during the execution of this code will be thrownjava.lang.StackOverFlowError in the flow of "main".
  • A common cause of this type of exception is bad recursion. This usually occurs because of improper termination conditions.

What will bring the following program?

  • We can call static methods, using a variable that refers to the null. The fact that the static methods are at the class level. That is why we can call the static function how to use the class itself, and by means of a variable equal to null.

What will bring the following program?

What will bring the following program?

What will bring the following program?

What will bring the following program?

  • Which line of the program will fail compile time error?

What will bring the following program?

What will bring the following program?

What will bring the following program?

What happens when you write System.out.println (null);?

There is, of course, a compiler error. In the method println has several variations. It can takeString, Object, or char []. When transferring to the function argument null the compiler can not determine which of the three methods to handle.
Overloaded methods:
  • public void prinltln (String str) {
  • public void prinltln (char [] ch) {
  • public void prinltln (Object ch) {
For the compiler call System.out.println (null) is ambiguous as to output null 'may be a more or less come two methods: println (String) and println (char []). But the compiler does not know to which specific method of contact. To compile was successful, we would write:
  • System.out.println ((String) null);
  • System.out.println ((char []) null);
  • System.out.println ((Object) null);
But here we must be careful. Call System.out.println ((char []) null); lead to the exclusion of runtime exception.

Комментариев нет:

Отправить комментарий