Java Programmer face Question collection (5)

Source: Internet
Author: User
Tags truncated

Java Fundamentals:
1, scope public,private,protected, and the difference when not written
A: The difference is as follows:
Scope Current class same package descendant class other package
Public√√√√
Protected√√√x
Friendly√√xx
Private√xxx
Default to friendly when not written

2. Differences between ArrayList and vectors, HashMap and Hashtable
A: ArrayList and vectors are mainly from two aspects.
I. Synchronization: vector is thread-safe, that is, synchronous, and ArrayList is a thread program is not secure, not synchronous
Two. Data growth: When growth is needed, vectors grow by default to the original, while ArrayList is half the original
HashMap and Hashtable are mainly from three aspects.
I. Historical reasons: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2
Two. Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous
Three. Value: Only HashMap can let you use NULL as the key or value of an entry for a table

3. Can I store a Chinese character in char type?
A: can be defined as a Chinese, because in Java encoding in Unicode, a char accounted for 16 bytes, so put a Chinese is no problem

4, multi-threaded there are several ways to achieve, what is it? How are there several implementations of synchronization?
A: There are two ways to implement multithreading, namely inheriting the thread class and implementing the Runnable interface
There are two implementations of synchronization, namely Synchronized,wait and notify

5, the Inheritance time class execution order question, generally is the choice question, asks you will print what?  
A: Parent class:  
Package test; 
public class Fatherclass  

public fatherclass ()  

System.out.println ("Fatherclass Create"); 


Subclass:  
Package test; 
Import test. fatherclass; 
public class ChildClass extends fatherclass 

public childclass ()  

System.out.println ("ChildClass Create");  

public static void Main (string[] args)  

Fatherclass fc = new Fatherclass ();  
ChildClass cc = new ChildClass ();  


Output:  
C:>java test. childclass 
Fatherclass create 
Fatherclass create 
ChildClass Create

6. How does the inner class be implemented?  
A: The sample code is as follows:  
Package test; 
public class outerclass 

Private class interclass 

public interclass ()  

System.out.println (" Interclass Create ");  


Public outerclass ()  

Interclass ic = new Interclass ();  
System.out.println ("Outerclass Create");  

public static void main (String [] args)  

Outerclass oc = new Outerclass ();  


Output:  
C:> Java test/outerclass 
Interclass create 
Outerclass create 
Another example:  
public class Outerclass { 
private double d1 = 1.0; 
//insert code here 

You need to insert an inner class declaration at line 3. Which inner class declarations is

Valid? (Choose.)
A. Class innerone{
public static double MethodA () {return d1;}
}
B. public class innerone{
Static double MethodA () {return d1;}
}
C. Private Class innerone{
Double MethodA () {return d1;}
}
D. Static Class innerone{
Protected double MethodA () {return d1;}
}
E. Abstract class innerone{
public abstract double MethodA ();
}
The description is as follows:
I. Static inner classes can have static members, and non-static inner classes cannot have static members. So A, B wrong
Two. A non-static member of a static inner class can access a static variable of an external class and cannot access a non-static variable of the outer class; return d1 error.

So D wrong
Three. Non-static members of non-static inner classes can access non-static variables of the outer class. So C is correct
Four. The answer is C, E

7, garbage collection mechanism, how to optimize the program?
I hope you'll fill it up.

8 Float type float f=3.4 is correct?
Answer: not correct. Accuracy is inaccurate and should be cast with coercion type, as follows: float f= (float) 3.4

9. Introduce the collection FrameWork in Java (including how to write your own data structure)?
Answer: The Collection framework is as follows:
Collection
├list
│├linkedlist
│├arraylist
│└vector
│└stack
└set
Map
├hashtable
├hashmap
└weakhashmap
Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements)
Map provides a key-to-value mapping

10, exception handling mechanism in Java, event mechanism?

11. Multi-shape and inheritance in Java?
I hope you'll fill it up.

12, abstract class and interface?
A: Abstract classes and interfaces are all used for abstraction, but abstract classes (in Java) can have their own partial implementations, while interfaces are completely an identity (with multiple inheritance capabilities).

13, Java communication programming, programming questions (or questions and answers), with the Java socket programming, read the server several characters, and then write to the local display?
A: Server-side programs:
Package test;
Import java.net.*;
Import java.io.*;

public class server 

private ServerSocket ss; 
Private Socket socket; 
Private BufferedReader in; 
private PrintWriter out; 
Public Server ()  

try 
{  
Ss=new ServerSocket (10000);  
while (true)  

socket = Ss.accept ();  
String remoteip = Socket.getinetaddress (). gethostaddress ();  
String remoteport = ":" +socket.getlocalport ();  
System.out.println ("A client come in! IP: "+remoteip+remoteport);  
in = new BufferedReader (new

InputStreamReader (Socket.getinputstream ()));  
String line = In.readline ();  
System.out.println ("Cleint Send is:" + line);  
out = new PrintWriter (Socket.getoutputstream (), true);  
Out.println ("Your Message received! ");  
Out.close ();  
In.close ();  
Socket.close ();  

}catch (IOException e)  

out.println ("wrong");  


public static void Main (string[] args)  

new Server ();  

}; 
Client-side program:  
Package test; 
Import java.io.*; 
Import java.net.*;

Public class client ,

Socket socket; 
BufferedReader in; 
PrintWriter out; &NBSP,
Public Client ()  ,

try 

System.out.println ("Try to Connect to 127.0.0.1:10000 ");  
socket = new Socket (" 127.0.0.1 ", 10000);  
System.out.println (" The Server Connected! ");  
System.out.println ("Please enter some Character:");  .
BufferedReader line = new BufferedReader (new

InputStreamReader (system.in));
out = new PrintWriter (Socket.getoutputstream (), true);
Out.println (Line.readline ());
in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
System.out.println (In.readline ());
Out.close ();
In.close ();
Socket.close ();
}catch (IOException e)
{
Out.println ("wrong");
}
}
public static void Main (string[] args)
{
New Client ();
}
};

14, with Java implementation of a sort, Java class implementation of the method of serialization (two kinds)? As in the collection framework, what interfaces are implemented to implement the comparison?
A: Use the insertion method to sort the code as follows
Package test;
Import java.util.*;
Class Insertsort
{
ArrayList al;
Public insertsort (int num,int MoD)
{
Al = new ArrayList (num);
Random rand = new Random ();
System.out.println ("The ArrayList Sort Before:");
for (int i=0;i<num; i++)
{
Al.add (New Integer (Math.Abs (rand.nextint ())% mod + 1));
System.out.println ("al[" +i+ "]=" +al.get (i));
}
}
public void Sortit ()
{
Integer Tempint;
int maxsize=1;
for (int i=1;i<al.size (); i++)
{
Tempint = (Integer) al.remove (i);
if (Tempint.intvalue () >= ((Integer) Al.get (MaxSize-1)). Intvalue ())
{
Al.add (Maxsize,tempint);
maxsize++;
System.out.println (Al.tostring ());
} else {
for (int j=0;j<maxsize; j + +)
{
If

((Integer) Al.get (j)). Intvalue () >=tempint.intvalue ())
{
Al.add (J,tempint);
maxsize++;
System.out.println (Al.tostring ());
Break
}
}
}
}
System.out.println ("The ArrayList Sort After:");
for (int i=0;i<al.size (); i++)
{
System.out.println ("al[" +i+ "]=" +al.get (i));
}
}
public static void Main (string[] args)
{
Insertsort is = new Insertsort (10,100);
Is. Sortit ();
}
}
The Java class implementation is instantiated by implementing the Java.io.Serializable interface
Implementation of the collection framework to implement the comparable interface and the Comparator interface

15, Programming: Write a function to intercept the string, input as a string and number of bytes, output is a byte-truncated string. But to ensure that Chinese characters are not truncated half, such as "I abc" 4, should be cut to "I ab", input "I ABC Han def", 6, should be output as "I abc" rather than "I abc+ Han half."
A: The code is as follows:
Package test;

Class Splitstring
{
String Splitstr;
int splitbyte;
Public splitstring (String str,int bytes)
{
SPLITSTR=STR;
Splitbyte=bytes;
System.out.println ("The String is:′" +splitstr+ "; Splitbytes= "+splitbyte);
}
public void Splitit ()
{
int loopcount;


Loopcount= (Splitstr.length ()%splitbyte==0)? (Splitstr.length ()/splitbyte):(splitstr.length ()/split

BYTE+1);
System.out.println ("would Split into" +loopcount);
for (int i=1;i<=loopcount; i++)
{
if (I==loopcount) {


System.out.println (splitstr.substring (i-1) *splitbyte,splitstr.length ());
} else {


System.out.println (splitstr.substring (i-1) *splitbyte, (I*splitbyte)));
}
}
}
public static void Main (string[] args)
{
splitstring ss = new splitstring ("Test dd DSAF male 3443n Chinese 43 Chinese

0ewldfls=103 ", 4);
Ss. Splitit ();
}
}

16, Java multithreaded programming. Write a multithreaded program in Java, such as write four threads, two plus 1, two to one variable minus one, output.
I hope you'll fill it up.

17, the difference between string and StringBuffer.
Answer: The length of the string is immutable and the length of the StringBuffer is variable. If you are frequently manipulating the contents of a string, especially if the content is to be modified, use StringBuffer, and if you need a string at the end, use the ToString () method of StringBuffer

JSP aspect

1. What are the built-in objects of JSP?
A: JSP has the following 9 basic built-in components (which can correspond to the 6 internal components of ASP):
Request client requests, this request will contain parameters from the Get/post request
Response Web page back to the client's response
The properties of the PageContext Web page are managed here
Session period associated with the request
What the application servlet is doing
Output used to transmit a response
Config Servlet's architectural parts
Page JSP webpage itself
Exception exceptions for error pages, not captured

2. What are the actions of JSP?
A: JSP has the following 6 basic actions
Jsp:include: A file is introduced when the page is requested.
Jsp:usebean: Find or instantiate a JavaBean.
Jsp:setproperty: Sets the properties of the JavaBean.
Jsp:getproperty: Outputs the properties of a JavaBean.
Jsp:forward: Move the request to a new page.
Jsp:plugin: Generates an object or embed tag for a Java plug-in based on browser type

3. What is the difference between dynamic include and static include in JSP?
A: Dynamic include is implemented with Jsp:include action
<jsp:include page= "included.jsp" flush= "true"/> It always checks for changes in the included files, is suitable for containing dynamic pages, and can take parameters
Static include is implemented with the include pseudo-code, and will not check for changes in the included files, for static pages
<%@ include file= "included.htm"%>

4. What are the differences between the two ways of jumping?
Answer: There are two kinds, namely:
<jsp:include page= "included.jsp" flush= "true" >
<jsp:forward page= "nextpage.jsp"/>
The former page does not turn to the page that the include refers to, but displays the result of the page, the main page or the original. It will return after execution, equivalent to a function call. and can take parameters. The latter completely turns to the new page and will not come back. Equivalent to the go to statement.

servlet aspect

1. Say a servlet's life cycle?
A: The servlet has a good definition of lifetime, including loading and instantiation, initialization, processing requests, and end of service. This lifetime is expressed by the init,service and destroy methods of the Javax.servlet.Servlet interface.

2, the version of the servlet (forgot to ask which two version of the different)?
I hope you'll fill it up.

3. What is the difference between forward () and redirect () in the JAVA SERVLET API?
A: The former is only the control in the container of the steering, in the client browser address bar will not show the post-turn address, the latter is a full jump, the browser will get the address of the jump, and resend the request link. This way, you can see the link address after the jump from the address bar of the browser. Therefore, the former is more efficient, when the former can satisfy the need, try to use the forward () method, and, as such, it also helps to hide the actual link. In some cases, for example, you need to jump to a resource on a different server, you must use the Sendredirect () method.

Java Programmer face Question collection (5)

Related Article

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.