[Recommended] sharing: typical Java questions [problem points: 100 points]

Source: Internet
Author: User
Tags arithmetic operators bitwise operators float double throw exception java keywords

 

 

L jbs
1. List the advantages of 10 Java languages
A: free, open-source, cross-platform (platform independence), easy to use, fully functional, object-oriented, robust, multithreading, structure neutral, mature enterprise application platform, Wireless Application
2. List 10 Object-Oriented Programming terms in Java
A: Package, class, interface, object, attribute, method, constructor, inheritance, encapsulation, polymorphism, abstraction, Model
3. LIST 6 commonly used Java packages
Java. Lang; Java. util; Java. IO; Java. SQL; Java. AWT; java.net; Java. Applet; javax. Swing
4. What are the functions and features of identifiers in Java?
Role: The identifier is used to name variables, classes, and methods.
Features: it can start with a letter, underscore "_", or "$ ".
It can be followed by letters, underscores (_), and hyphens ($) or numbers.
Java is case sensitive and its identifiers are no exception
5. What are the characteristics of the keywords in Java? List at least 20 keywords
In Java, some words with specific meanings and used for special purposes are called keywords (keyword)
All Java keywords are in lower case, and true, false, and null are not Java keywords;
Although Goto and const have never been used, they are also reserved as Java keywords;
• A total of 51 keywords in Java
Abstract assert Boolean break byte continue
Case catch char class const double
Default do extends else final float
For goto long if implements Import
Native New null instanceof int Interface
Package private protected public return short
Static strictfp super switch synchronized this
While void throw throws transient try
Volatile

6. How to classify data types in Java?

It can be divided into simple data types and reference data types:
Simple data types: numeric type (byte, short, Int, long, float double), numeric type (char), Boolean Type (Boolean );
Reference data type: Class, interface, array.
7. Classification and examples of operators in Java
• Delimiter:, [], ()
• Arithmetic Operators: +,-, *,/, %, ++ ,――
• Relational operators: >,<,>=, <=, == ,! =
• Boolean logical operator :!, &, |, ^, &, |
• Bitwise operators: &, |, ^ ,~ , >>,<>>>
• Value assignment operator: = Extended value assignment operator: + =,-=, * =,/=
• String concatenation operator: +
• Styling OPERATOR :()

8. Functions and usage of the super and this keywords
• Use super in a Java class to reference the components of the parent class
-It can be used to access the attribute super defined in the parent class.
-It can be used to call the member method super defined in the parent class.
-It can be used to call the super constructor of the parent class in the subclass constructor.
-Tracing not only refers to direct parent class super
• To solve the naming conflicts and uncertainty of variables, the keyword "this" is introduced to represent the current object of the method in which the variable is located. Java
-Constructor indicates the new object created by the constructor.
-Indicates the object that calls the method.
• Keyword usage this
-Reference instance variables and methods of the class in the method or constructor of the class.
-Pass the current object as a parameter to other methods or constructors
-Used to call other overloaded Constructors

9. What is an expression in Java? What is the function?
• Expressions are the combination of operators and operands. They are a key component of any programming language.
• Expressions allow programmers to perform mathematical computation, Value Comparison, logical operations, and object operations in Java.
• Examples of some expressions:
-X
-X + 10
-Y = x + 10
-Arr [10]
-Student. gename ()

10. Make a table to list all modifiers in Java and their applicability (Can you modify constructors, attributes, free blocks, etc)
Class Attribute method builder free block internal class
Public y
Protected y
(Default) y
Private y
Final y
Abstract y
Static y

11. Write a method and print the 9-9 multiplication table with a for loop.
/**
* Print a 9-9 multiplication table in a For Loop
*/
Publicvoid nineninemultitable ()
{
For (INT I = 1, j = 1; j <= 9; I ++ ){
System. Out. Print (I + "*" + J + "=" + I * j + "");
If (I = J)
{
I = 0;
J ++;
System. Out. println ();
}
}
}
12. How to convert a java. util. date object to a string in the format of "20:23:22"
/**
* Convert a date to a string in a fixed format
* @ Paramdate
* @ Returnstr
*/
Public String datetostr (Java. util. Date)
{
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
String STR = SDF. Format (date );
Return STR;
}
13. Write a method to determine whether any integer is a prime number.
/**
* Determine whether any integer is a prime number.
* @ Paramn
* @ Returnboolean
*/
Publicboolean isprimes (int n)
{
For (INT I = 2; I <= math. SQRT (n); I ++ ){
If (N % I = 0)
{
Returnfalse;
}
}
Returntrue;
}
14. Write a method, input any integer, and return its factorial.
/**
* Obtain the factorial of any integer
* @ Paramn
* @ Returnn!
*/
Publicint factorial (int n)
{
// Recursion
If (n = 1)
{
Return 1;
}
Return N * factorial (n-1 );
// Non-recursion
// Int multi = 1;
// For (INT I = 2; I <= N; I ++ ){
// Multi * = I;
//}
// Return multi;
}
15. Write a method and use the binary search method to determine whether any integer exists in any integer array. If yes, return the index position in the array. If no integer exists, return-1.
/**
* Binary search for the position of a specific integer in an integer array (recursion)
* @ Paramdataset
* @ Paramdata
* @ Parambeginindex
* @ Paramendindex
* @ Returnindex
*/
Publicint binarysearch (INT [] dataset, int data, int beginindex, int endindex)
{
Int midindex = (beginindex + endindex)/2;
If (Data <dataset [beginindex] | DATA> dataset [endindex] | beginindex> endindex) Return-1;
If (Data <dataset [midindex])
{
Return binarysearch (dataset, Data, beginindex, midIndex-1 );
} Elseif (data> dataset [midindex])
{
Return binarysearch (dataset, Data, midindex + 1, endindex );
} Else
{
Return midindex;
}
}

/**
* Binary search for the position of a specific integer in an integer array (non-recursive)
* @ Paramdataset
* @ Paramdata
* @ Returnindex
*/
Publicint binarysearch (INT [] dataset, int data)
{
Int beginindex = 0;
Int endindex = dataset. Length-1;
Int midindex =-1;
If (Data <dataset [beginindex] | DATA> dataset [endindex] | beginindex> endindex) Return-1;
While (beginindex <= endindex ){
Midindex = (beginindex + endindex)/2;
If (Data <dataset [midindex]) {
Endindex = midIndex-1;
} Elseif (data> dataset [midindex]) {
Beginindex = midindex + 1;
} Else
{
Return midindex;
}
}
Return-1;
}
16. An example of a breeder feeding animals with food reflects the use of object-oriented ideas and interfaces (abstract classes) in Java.
Package com. softeem. Demo;

/**
* @ Authorleno
* Animal Interfaces
*/
Interface animal
{
Publicvoid eat (Food food );
}
/**
* @ Authorleno
* Animal: Cat
*/
Class cat implements animal
{
Publicvoid eat (Food food)
{
System. Out. println ("kitten" + food. getname ());
}
}
/**
* @ Authorleno
* Animal: Dog
*/
Class dog implements animal
{
Publicvoid eat (Food food)
{
System. Out. println ("Puppy chew" + food. getname ());
}
}

/**
* @ Authorleno
* Abstract Food class
*/
Abstractclass food
{
Protected string name;
Public String getname (){
Returnname;
}

Publicvoid setname (string name ){
This. Name = Name;
}
}

/**
* @ Authorleno
* A type of food: Fish
*/
Class fish extends food
{
Public fish (string name ){
This. Name = Name;
}
}
/**
* @ Authorleno
* A type of food: Bone
*/
Class bone extends food
{
Public bone (string name ){
This. Name = Name;
}
}

/**
* @ Authorleno
* Breeders
*
*/
Class Feeder
{
/**
* The breeder feeds some food to an animal.
* @ Paramanimal
* @ Paramfood
*/
Publicvoid feed (animal, Food food)
{
Animal. Eat (food );
}
}

/**
* @ Authorleno
* Test whether a breeder feeds animals.
*/
Publicclass testfeeder {

Publicstaticvoid main (string [] ARGs ){
Feeder feeder = new feeder ();
Animal animal = new dog ();
Food food = new bone ("meat bone ");
Feeder. Feed (animal, food); // feed the meat to the dog
Animal = new CAT ();
Food = new fish ("fish ");
Feeder. Feed (animal, food); // feed the cat

}
}
17. describes the Exception Handling Mechanism in Java
• If an exception occurs during program execution, an exception class object is automatically generated and submitted to the Java runtime system. This process is called throw exception. Java
• When the system receives an exception object during the Java runtime, it looks for code that can handle this exception and submits the current exception object to it for processing. This process is called a catch exception.
• If the system cannot find a method to capture exceptions during Java runtime, the runtime system will terminate and the corresponding Java program will exit.
• Programmers can only handle exceptions, but cannot handle errors.

Come:
Http://topic.csdn.net/u/20080617/15/706679c5-e108-4ec0-801b-75728ad19fe6.html? 24942

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.