"Interview" "Java" Java engineer interview article #2__java

Source: Internet
Author: User


The individual ability is limited, the answer may unavoidably have the incomplete place, asks the understanding.


1, whether you can inherit the string class.


A: The string class is the final class and is not allowed to be inherited.

Public final class String
    implements Java.io.Serializable, Comparable<string>, charsequence {...}


2, ArrayList, vector differences and HashMap, hashtable differences


the difference between ArrayList and vectors


data growth : When growth is needed, the vector defaults to the original one, while ArrayList is half the original.

synchronicity : Vector is thread safe, that is, synchronous, while ArrayList is not secure, not synchronized


The difference between Hashmap and Hashtable


1.Hashtable is a subclass of dictionary, and HashMap is an implementation class of the map interface;
The methods in the 2.Hashtable are synchronized, and the methods in HashMap are not synchronized by default. That is to say, in multithreaded applications,

Hashtable can be safely used without specialized operations, while for HashMap, additional synchronization mechanisms are required.

However, the HashMap synchronization problem can be solved by a static method of collections:
Map Collections.synchronizedmap (map m)
This method returns a synchronized map that encapsulates all the methods of the underlying hashmap, making the underlying hashmap secure even in multi-threaded environments.
3. In HashMap, NULL can be used as a key with only one key, and a value of one or more keys can be null.

When the Get () method returns a null value, you can either indicate that the key is not in HashMap or that the value of the key is null.

Therefore, it is not possible in HashMap to determine whether a key exists in the HashMap by the Get () method, but rather by using the ContainsKey () method.


3, what is GC. Why should there be a GC.


GC is the meaning of garbage collection (Gabage Collection), memory processing is where programmers are prone to problems,

Forgetting or wrong memory recycling can cause a program or system to become unstable or even crash, and the GC functionality provided by Java

Automatically monitoring objects beyond scope to automatically reclaim memory, the Java language does not provide free

The display action method for allocating memory. Java programmers do not have to worry about memory management because the garbage collector automatically

Management. To request garbage collection, you can call one of the following methods: System.GC () Runtime.getruntime (). GC ().


4. What is the difference between an abstract class (abstract classes) and an interface (interface)?


A class that declares the existence of a method without implementing it is called an abstract class (AbstractClass), which is used to create a manifest

A class of some basic behavior and declares a method for that class, but it cannot implement the class in that class. Cannot create abstract class

The instance. However, you can create a variable whose type is an abstract class and have it point to an instance of a specific subclass.

Cannot have an abstract constructor or an abstract static method. Subclasses of the abstract class provide implementations for all abstract methods in their parent class,

Otherwise they are also abstract classes. Instead, implement the method in the subclass. Other classes that know their behavior can be implemented in the class

These methods.
Interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be achieved by implementing such a

interface and get it. All the methods in the interface are abstract, and none have a program body. Interface can only define static final members

Variable. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface,

It defines the method (which will be given by the program body) to all such interfaces. It can then be implemented in any object of the class that implements the interface

The method on which the interface is invoked. Because of an abstract class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will

Effect. A reference can be converted to an interface type or transformed from an interface type, and the instanceof operator can be used to determine the class of an object

Whether the interface is implemented.


5, scope public, private, protected and do not write when the difference


Public: All other classes are accessible

Private: Only the class itself can access

Protected: Subclasses in the other classes and all other packages in this package can access the

When not written: The classes in this package can access


6, the difference between overload and override. Can the overloaded method change the type of the return value?


A: The overriding overriding and overloaded overloading of methods are different manifestations of Java polymorphism. Overriding overriding is a manifestation of polymorphism between parent classes and subclasses, and overload overloading is an expression of polymorphism in a class. If you define a method in a subclass with the same name and parameters as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is invoked, and the definition in the parent class is "masked". If multiple methods with the same name are defined in a class, either with a different number of parameters or with different parameter types, they are called overloads of the Method (overloading). The overloaded method is to change the type of the return value.



7. What are the common anomalies? Give an example to illustrate.


Answer: Big divide, error and exception

(1) The error class is considered a serious error that cannot be recovered, such as system internal error and resource exhaustion error; This kind of error program is almost powerless,

Should not throw this type of error, but let the program interrupt;

(2) The exception class may be a minor error, which is divided into derived from runtimeexception and non derived runtimeexception classes;

Rules for distinguishing between runtimeexception and runtimeexception:

Programming errors result in runtimeexception, such as 0 exceptions, array access, null pointer access, and so on;

A situation occurs, such as attempting to open an incorrect URL, trying to find a class object based on a string that does not represent an existing class.

Try to read the data after the end of the file, and so on.


8, the output of the program is what.


Test2.java

class test1{
  test1 () {
    amethod ();
  }
  public void Amethod () {
    System.out.println (' in Test1 amethod! ');
  }
public class Test2 extends test1{
  test2 () {
    amethod ();
  }
  public void Amethod () {
    System.out.println (' in Test2 amethod! ');
  }
  public static void Main (String args[]) {
    new test2 ();
    New Test1 ();
}
}

A: Output two lines of "in Test2 amethod!", because the new Test2 object in the main function will first call the Test2 inherited

The constructor of the parent class test1 (), and then calls its own constructor test2 (), and when the Test2 object invokes Test1 (),

Perform test1 function weight amethod (), this time, because the Amethod function has been rewritten in test2,

Therefore the output here is "in Test2 amethod!" instead of "in Test1 Amethod". ”。

The following call is a few ordinary calls, generally not easy to write wrong.

In Test2 amethod!
In Test2 amethod!
In Test1 amethod!

9, the programming implementation class: The command line input 10 integers, the output maximum and the minimum value.

* * Input 10 integer, output maximum and minimum value/import java.io.*;
	public class inputoutput{final int numofarr=10;


	int arrint[]=new Int[numofarr]; public void Setarrintbyinput () {try{//First constructs InputStreamReader with system.in, then constructs BufferedReader BufferedReader
			BufferedReader (New InputStreamReader (system.in));
			
			int index=0;
				while (Index < Numofarr) {System.out.print ("Enter a integer:arrint[" +index+ "]=");
				
				Resolves a string to a signed decimal integer arrint[index]=integer.parseint (stdin.readline ());
			index++;
		The catch (Exception e) {System.out.println ("IOException");

		} public int Getmaxofarrint () {int max=this.arrint[0]; for (int i=1;i<this.
			numofarr;i++) {if (Max < arrint[i]) {max=arrint[i];
	} return max;

		public int getminofarrint () {int min=this.arrint[0]; for (int i=1;i<this.
			numofarr;i++) {if (min > Arrint[i]) {min=arrint[i];
	} return min; public void Printarrint () {for int i=0;i<this. numofarr;i++{if (i!=0 && i%5==0) {System.out.print ("\ n");
		} System.out.print (arrint[i]+ "");
	} System.out.println ();
		public static void Main (String args[]) {inputoutput _inputoutput=new inputoutput ();
		_inputoutput.setarrintbyinput ();
		
		_inputoutput.printarrint ();
		int Max=_inputoutput.getmaxofarrint ();

		int Min=_inputoutput.getminofarrint ();
		
		System.out.println ("Max:" +max);
	System.out.println ("min:" +min); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.