Answers to Java pen questions

Source: Internet
Author: User

I. Single-choice questions

1. Java is re-designed from the () language.

A. ADA B .C ++ C. pasacal D. Basic

Answer: B

2. Which of the following statements is true ()

A. After the Java program is compiled, the machine code is generated.

B. After the Java program is compiled, byte code is generated.

C. After the Java program is compiled, the DLL

D. None of the above are correct.

Answer: B

3. Which of the following statements are true ()

A. constructor in class cannot be omitted

B. the constructor must have the same name as the class, but the method cannot have the same name as the class.

C. constructor is executed when an object is new.

D. One class can only define one Constructor

Answer: c

4. The package that provides Java database access capabilities is ()

A. java. SQL B. java. AWT C. java. Lang D. java. Swing

Answer:

5. Which of the following operators is valid ()

A. & B. <> C. If D.: =

Answer:

6. Execute the following program code:

A = 0; C = 0;

Do {

-- C;

A = A-1;

} While (a> 0 );

The value of C is ()

A.0 B .1 C.-1 D. endless loop

Answer: c

7. Which of the following statements is true ()

A. Abstract modifiers can be used to modify fields, methods, and classes.

B. The body part of the abstract method must be wrapped in a pair of braces {}

C. Declare the abstract method. Braces are optional.

D. Do not write braces for declaring Abstract METHODS

Answer: d

8. Which of the following statements is true ()

A. Formal parameters can be considered as local variable

B. Formal parameters can be modified by field Modifiers

C. Formal parameters are actually transmitted parameters when a method is called

D. The form parameter cannot be an object

Answer:

9. Which of the following statements is true ()

A. the instance method can directly call the superclass instance method.

B. The instance method can directly call the superclass class method.

C. The instance method can directly call the instance methods of other classes.

D. The instance method can directly call the class methods of this class.

Answer: d

Ii. multiple choice questions

1. Java program types ()

A. Class B. Applet C. Application D. servlet

2. Which of the following statements are true ()

A. environment variables can be specified during source code compilation

B. The environment variables that can be specified during program compilation do not include the class path

C. javac can compile several Java source files at a time.

D. javac.exe can specify the directory in which the compilation result will be placed)

Answer: BCD

3. Which of the following identifiers is invalid ()

A. New B. $ usdollars c.1234 D. Car. Taxi

Answer: ACD

4. Which of the following statements are false ()

A. array is an object.

B. arrays belong to a native class

C. Int number = [] = {31,23, 33,43, 35,63}

D. The array size can be changed at will.

Answer: BCD

5. interfaces that cannot be modified are ()

A. Private B. Public C. Protected D. Static

Answer: ACD

6. Which of the following statements are true ()

A. Call by value does not change the actual parameter value.

B. Call by reference can change the reference address of the actual parameter.

C. Call by reference cannot change the reference address of the actual Parameter

D. Call by reference can change the actual parameter content

Answer: ACD

7. Which of the following statements are false ()

A. Use this in class methods to call class methods of this class.

B. You can call this class method directly when calling this class method in class methods.

C. Only class methods in this class can be called.

D. The instance method cannot be called in class methods.

Answer: ACD

8. Which of the following statements are false ()

A. Java object-oriented language allows separate processes and functions to exist

B. Java object-oriented language allows separate methods to exist

C. Methods in Java belong to members of the class (member)

D. Methods in the Java language must belong to a certain class (object), and the calling method and process or function must be the same

Answer: ABC

9. Which of the following statements are false ()

A.the main () method is required for Java class files that can be successfully run by java.exe.

B. j2sdk is a Java API

C. appletviewer.exe you can use the jar option to run the. jar file.

D. The main () method is required for Java class files that can be successfully run by appletviewer.

Answer: BCD

Iii. Answer questions

1. The name of the starting class in the Java program must be the same as the name of the file where the class is stored. ()

Answer: Correct

2. Unicode represents a word in 16 bits. ()

Answer: Correct

3. Data Types in the native class can be converted at will. ()

Answer: Error

1. Write the comparison statements of Boolean, Int, float, and pointer type variables A and zero respectively.
Answer:
Bool: If (! A) or if ()
INT: if (a = 0)
Float: const expressions exp = 0.000001
If (A <exp & A>-exp)
Pointer: if (! = NULL) or if (a = NULL)

2. What are the advantages of const compared with # define?
Answer: 1) const constants have data types, while macro constants do not. The compiler can perform type security checks on the former. However, only character replacement is performed for the latter, and there is no type security check, and unexpected errors may occur during character replacement.
2) some integrated debugging tools can debug const constants, but cannot debug macro constants.

3. What are the differences between arrays and pointers?
An array is either created in a static storage area (such as a global array) or on a stack. Pointers can point to any type of memory block at any time.
(1) Differences in modification content
Char A [] = "hello ";
A [0] = 'X ';
Char * P = "world"; // note that P points to a constant string
P [0] = 'X'; // This error cannot be found by the compiler. It is a runtime error.
(2) the sizeof operator can be used to calculate the array capacity (number of bytes ). Sizeof (P), P is the number of bytes of a pointer variable, rather than the memory capacity referred to by P. C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory. Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type.
Char A [] = "Hello World ";
Char * P =;
Cout <sizeof (a) <Endl; // 12 bytes
Cout <sizeof (p) <Endl; // 4 bytes
Calculate the memory size of arrays and pointers
Void func (char a [1, 100])
{
Cout <sizeof (a) <Endl; // 4 bytes instead of 100 bytes
}

4. What are the differences between Overloading, overwriting, and hiding of class member functions?
Answer:
A. Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
B. Override refers to the function of the derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.
C. "hide" means that the function of the derived class shields the base class functions with the same name as it. The rules are as follows:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

5. There are two int variables: A and B, don't use "if", "? : "," Switch "or other judgement statements, find out the biggest one of the two numbers.
Answer: (a + B) + ABS (a-B)/2

6. How to print the file name of the current source file and the current row number of the source file?
Answer:
Cout <_ file __;
Cout <__line __;
_ File _ and _ line _ are pre-defined macros of the system, which are not defined in a file but defined by the compiler.

7. After the main function is executed, is it possible to execute another piece of code to explain it?
Answer: Yes. You can use _ onexit to register a function. It will execute int fn1 (void), FN2 (void), fn3 (void), and FN4 (void) after main );
Void main (void)
{
String STR ("Zhanglin ");
_ Onexit (fn1 );
_ Onexit (FN2 );
_ Onexit (fn3 );
_ Onexit (FN4 );
Printf ("this is executed first./N ");
}
Int fn1 ()
{
Printf ("next./N ");
Return 0;
}
Int FN2 ()
{
Printf ("executed ");
Return 0;
}
Int fn3 ()
{
Printf ("is ");
Return 0;
}
Int FN4 ()
{
Printf ("this ");
Return 0;
}
The _ onexit function is passed the address of a function (func) to be called when the program terminates normally. successive callto _ onexit create a register of functions that are executed in LIFO (last-in-first-out) Order. the functions passed to _ onexit cannot take parameters.

8. How can I determine whether a program is compiled by C or C ++?
Answer:
# Ifdef _ cplusplus
Cout <"C ++ ";
# Else
Cout <"C ";
# Endif

9. There is a group of integers in the file, which must be sorted and output to another file.
Answer:

# I nclude

# I nclude

Using namespace STD;


Void Order (vector & Data) // bubble sort
{
Int COUNT = data. Size ();
Int tag = false; // you can specify whether or not to continue the bubble.
For (INT I = 0; I <count; I ++)
{
For (Int J = 0; j <count-I-1; j ++)
{
If (data [J]> data [J + 1])
{
Tag = true;
Int temp = data [J];
Data [J] = data [J + 1];
Data [J + 1] = temp;
}
}
If (! Tag)
Break;
}
}


Void main (void)
{
Vectordata;
Ifstream in ("C: // data.txt ");
If (! In)
{
Cout <"file error! ";
Exit (1 );
}
Int temp;
While (! In. EOF ())
{
In> temp;
Data. push_back (temp );
}
In. Close (); // close the input file stream
Order (data );
Ofstream out ("C: // result.txt ");
If (! Out)
{
Cout <"file error! ";
Exit (1 );
}
For (I = 0; I <data. Size (); I ++)
Out <data [I] <"?;

10. linked list: the node Structure of a linked list
Struct Node
{
Int data;
Node * next;
};
Typedef struct node;

(1) Head of the known head node of the linked list. Write a function to reverse the list (Intel)

Node * reverselist (node * head) // reverse sequence of the linked list
{
If (Head = NULL | head-> next = NULL)
Return head;
Node * P1 = head;
Node * P2 = p1-> next;
Node * P3 = P2-> next;
P1-> next = NULL;
While (P3! = NULL)
{
P2-> next = p1;
P1 = P2;
P2 = P3;
P3 = P3-> next;
}
P2-> next = p1;
Head = P2;
Return head;
}

(2) It is known that the two linked lists head1 and head2 are in an orderly order. Please combine them into a linked list. (Keep all nodes, even if the size is the same)
Node * Merge (node * head1, node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
Node * P1 = NULL;
Node * P2 = NULL;
If (head1-> data {
Head = head1;
P1 = head1-> next;
P2 = head2;
}
Else
{
Head = head2;
P2 = head2-> next;
P1 = head1;
}
Node * pcurrent = head;
While (P1! = NULL & p2! = NULL)
{
If (P1-> data <= P2-> data)
{
Pcurrent-> next = p1;
Pcurrent = p1;
P1 = p1-> next;
}
Else
{
Pcurrent-> next = P2;
Pcurrent = P2;
P2 = P2-> next;
}
}
If (P1! = NULL)
Pcurrent-> next = p1;
If (P2! = NULL)
Pcurrent-> next = P2;
Return head;
}

(3) It is known that the two linked lists head1 and head2 are in an orderly order. Please combine them into a linked list. This time, we need to use a recursive method. (Autodesk)
Answer:
Node * mergerecursive (node * head1, node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
If (head1-> data {
Head = head1;
Head-> next = mergerecursive (head1-> next, head2 );
}
Else
{
Head = head2;
Head-> next = mergerecursive (head1, head2-> next );
}
Return head;
}

 

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.