Turn to Java Testing Questions

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

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;

? There are 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 OPERATOR: &, |, ^ ,~ , >>,<>>>

? Value assignment operator: = Extended value assignment operator: + =,-=, * =,/=

? String concatenation operator: +

? Shape OPERATOR :()

 

8. Functions and usage of the super and this keywords

? Using 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?

? An expression is a combination of operators and operands. It is a key component of any programming language.

? Expressions allow programmers to perform mathematical calculations, compare values, perform logical operations, and perform 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

* @ Param date

* @ Return Str

*/

Public String datetostr (Java. util. Date)

{

Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); // Note: Is this method outdated?

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.

* @ Param n

* @ Return Boolean

*/

Public Boolean isprimes (int n)

{

For (INT I = 2; I <= math. SQRT (n); I ++ ){

If (N % I = 0)

{

Return false;

}

}

Return true;

}

 

 

14. Write a method, input any integer, and return its factorial.

/**

* Obtain the factorial of any integer

* @ Param n

* @ Return n!

*/

Public int 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)

* @ Param Dataset

* @ Param data

* @ Param beginindex

* @ Param endindex

* @ Return Index

*/

Public int binarysearch (INT [] dataset, int data, int beginindex, int endindex)

{

Int midindex = (beginindex + endindex)/2;

If (Data dataset [endindex] | beginindex> endindex) Return-1;

If (Data {

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)

* @ Param Dataset

* @ Param data

* @ Return Index

*/

Public int binarysearch (INT [] dataset, int data)

{

Int beginindex = 0;

Int endindex = dataset. Length-1;

Int midindex =-1;

If (Data dataset [endindex] | beginindex> endindex) Return-1;

While (beginindex <= endindex ){

Midindex = (beginindex + endindex)/2;

If (Data 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;

/**

* @ Author Leno

* Animal Interfaces

*/

Interface animal

{

Public void eat (Food food );

}

/**

* @ Author Leno

* Animal: Cat

*/

Class cat implements animal

{

Public void eat (Food food)

{

System. Out. println ("kitten" + food. getname ());

}

}

/**

* @ Author Leno

* Animal: Dog

*/

Class dog implements animal

{

Public void eat (Food food)

{

System. Out. println ("Puppy chew" + food. getname ());

}

}

 

/**

* @ Author Leno

* Abstract Food class

*/

Abstract class food

{

Protected string name;

Public String getname (){

Return name;

}

 

Public void 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;

}

}

 

/**

* @ Author Leno

* Breeders

*/

Class Feeder

{

/**

* The breeder feeds some food to an animal.

* @ Param animal

* @ Param food

*/

Public void feed (animal, Food food)

{

Animal. Eat (food );

}

}

 

/**

* @ Author Leno

* Test whether a breeder feeds animals.

*/

Public class testfeeder {

 

Public static void 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

}

}
 

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.