Java test post 3-30 questions during Java interview (E)

Source: Internet
Author: User

 * Q1. how cocould Java classes direct program messages to the system console, but error messages, say to a file?

A. the class system has a variableOut
That represents the standard output, and the variableErr
That represents the standard error device. By default, they both point
At the system console. This how the standard output cocould be
Re-directed:

Stream ST = new stream (New fileoutputstream ("output.txt"); system. seterr (ST); system. setout (ST );



* Q2. what's the difference between an interface and an abstract class?

A. An abstract class may contain code in method bodies, which is
Not allowed in an interface. With abstract classes, you have to inherit
Your class from it and Java does not allow multiple inheritance. On
Other hand, you can implement multiple interfaces in your class.



* Q3. why wocould you use a synchronized block vs. Synchronized Method?

A. Synchronized blocks place locks for shorter periods than synchronized methods.



* Q4. explain the usage of the keyword transient?

A. This keyword indicates that the value of this member variable
Does not have to be serialized with the object. When the class will be
De-serialized, this variable will be initialized with a default value
Of its Data Type (I. e. Zero for integers ).

* Q5. how can you force garbage collection?

A. You can't force GC, but cocould request it by calling system. GC (). JVM does not guarantee that GC will be started immediately.



* Q6. how do you know if an explicit object casting is needed?

A. If you assign a superclass object to a variable of a subclass's data type, You Need To Do explicit casting. For example:

Object A; Customer B; B = (customer);

When you assign a subclass to a variable having a supeclass type, the casting is already med automatically.



* Q7. what's the difference between the Methods Sleep () and wait ()

A. The Code sleep (1000); puts thread aside for exactly one second.
The Code wait (1000), causes a wait of up to one second. A thread cocould
Stop waiting earlier if it has es the specified y () or policyall () call.
The method wait () is defined in the class object and the method sleep ()
Is defined in the class thread.



* Q8. can you write a Java class that cocould be used both as an applet as well as an application?

A. Yes. Add a main () method to the applet.



* Q9. What's the difference between constructors and other methods?

A. constructors must have the same name as the class and can not
Return a value. They are only called once while regular methods cocould
Be called when times.



* Q10. can you call one constructor from another if a class has multiple Constructors

A. Yes. Use this () syntax.



* Q11. explain the usage of Java packages.

A. This is a way to organize files when a project consists
Multiple modules. It also helps resolve naming conflicts when different
Packages have classes with the same names. Packages access level also
Allows you to protect data from being used by the non-authorized
Classes.



* Q12. if a class is located in a package, what do you do to change in the OS environment to be able to use it?

A. You need to add a directory or a jar file that contains
Package directories to the classpath environment variable. Let's say
Class employee belongs to a package com. XYZ. hr; and is located in
File C:/dev/COM/XYZ/HR/employee. java. In this case, you 'd need to add
C:/dev to the variable classpath. If this class contains the Method
Main (), You cocould test it from a command prompt window as follows:

C:/> JAVA com. XYZ. hr. Employee



* Q13. what's the difference between j2sdk 1.5 and j2sdk 5.0?

A. There's no difference, Sun Microsystems just re-branded this version.

* Q14. what wocould you use to compare two string variables-the operator = or the method equals ()?

A. I 'd use the method equals () to compare the values of the strings
And the = to check if two variables point at the same instance of
String object.

* Q15. does it matter in what order catch statements for filenotfoundexception and iow.tipon are written?

A. Yes, it does. The filenofoundexception is inherited from the ioexception. Exception's subclasses have to be caught first.



* Q16. can an inner class declared inside of a method access local variables of this method?

A. It's possible if these variables are final.



* Q17. what can go wrong if you replace & with & in the following code:

String A = NULL; if (! = NULL & A. Length ()> 10 ){...}


A. A single ampersand here wowould lead to a nullpointerexception.

* Q18. what's the main difference between a vector and an arraylist

A. Java Vector class is internally synchronized and arraylist is not.

* Q19. when shocould the method invokelater () be used?

A. This method is used to ensure that swing components are updated through the event-dispatching thread.

* Q20. how can a subclass call a method or a constructor defined in a superclass?

A. Use the following syntax: Super. mymethod (); To call
Constructor of the superclass, just write super (); In the first line
The subclass's constructor.

* Q1. how cocould Java classes direct program messages to the system console, but error messages, say to a file?

A. the class system has a variableOut
That represents the standard output, and the variableErr
That represents the standard error device. By default, they both point
At the system console. This how the standard output cocould be
Re-directed:

Stream ST = new stream (New fileoutputstream ("output.txt"); system. seterr (ST); system. setout (ST );



* Q2. what's the difference between an interface and an abstract class?

A. An abstract class may contain code in method bodies, which is
Not allowed in an interface. With abstract classes, you have to inherit
Your class from it and Java does not allow multiple inheritance. On
Other hand, you can implement multiple interfaces in your class.



* Q3. why wocould you use a synchronized block vs. Synchronized Method?

A. Synchronized blocks place locks for shorter periods than synchronized methods.



* Q4. explain the usage of the keyword transient?

A. This keyword indicates that the value of this member variable
Does not have to be serialized with the object. When the class will be
De-serialized, this variable will be initialized with a default value
Of its Data Type (I. e. Zero for integers ).

* Q5. how can you force garbage collection?

A. You can't force GC, but cocould request it by calling system. GC (). JVM does not guarantee that GC will be started immediately.



* Q6. how do you know if an explicit object casting is needed?

A. If you assign a superclass object to a variable of a subclass's data type, You Need To Do explicit casting. For example:

Object A; Customer B; B = (customer);

When you assign a subclass to a variable having a supeclass type, the casting is already med automatically.



* Q7. what's the difference between the Methods Sleep () and wait ()

A. The Code sleep (1000); puts thread aside for exactly one second.
The Code wait (1000), causes a wait of up to one second. A thread cocould
Stop waiting earlier if it has es the specified y () or policyall () call.
The method wait () is defined in the class object and the method sleep ()
Is defined in the class thread.



* Q8. can you write a Java class that cocould be used both as an applet as well as an application?

A. Yes. Add a main () method to the applet.



* Q9. What's the difference between constructors and other methods?

A. constructors must have the same name as the class and can not
Return a value. They are only called once while regular methods cocould
Be called when times.



* Q10. can you call one constructor from another if a class has multiple Constructors

A. Yes. Use this () syntax.



* Q11. explain the usage of Java packages.

A. This is a way to organize files when a project consists
Multiple modules. It also helps resolve naming conflicts when different
Packages have classes with the same names. Packages access level also
Allows you to protect data from being used by the non-authorized
Classes.



* Q12. if a class is located in a package, what do you do to change in the OS environment to be able to use it?

A. You need to add a directory or a jar file that contains
Package directories to the classpath environment variable. Let's say
Class employee belongs to a package com. XYZ. hr; and is located in
File C:/dev/COM/XYZ/HR/employee. java. In this case, you 'd need to add
C:/dev to the variable classpath. If this class contains the Method
Main (), You cocould test it from a command prompt window as follows:

C:/> JAVA com. XYZ. hr. Employee



* Q13. what's the difference between j2sdk 1.5 and j2sdk 5.0?

A. There's no difference, Sun Microsystems just re-branded this version.

* Q14. what wocould you use to compare two string variables-the operator = or the method equals ()?

A. I 'd use the method equals () to compare the values of the strings
And the = to check if two variables point at the same instance of
String object.

* Q15. does it matter in what order catch statements for filenotfoundexception and iow.tipon are written?

A. Yes, it does. The filenofoundexception is inherited from the ioexception. Exception's subclasses have to be caught first.



* Q16. can an inner class declared inside of a method access local variables of this method?

A. It's possible if these variables are final.



* Q17. what can go wrong if you replace & with & in the following code:

String A = NULL; if (! = NULL & A. Length ()> 10 ){...}


A. A single ampersand here wowould lead to a nullpointerexception.

* Q18. what's the main difference between a vector and an arraylist

A. Java Vector class is internally synchronized and arraylist is not.

* Q19. when shocould the method invokelater () be used?

A. This method is used to ensure that swing components are updated through the event-dispatching thread.

* Q20. how can a subclass call a method or a constructor defined in a superclass?

A. Use the following syntax: Super. mymethod (); To call
Constructor of the superclass, just write super (); In the first line
The subclass's constructor.

** Q21. what's the difference between a queue and a stack?

A. Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule

** Q22. you can create an abstract class that contains only
Abstract METHODS. On the other hand, you can create an interface that
Declares the same methods. So can you use abstract classes instead
Interfaces?

A. Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

** Q23. what comes to mind when you hear about a young generation in Java?

A. garbage collection.

** Q24. what comes to mind when someone mentions a shallow copy in Java?

A. Object cloning.

** Q25. if you're overriding the method equals () of an object, which other method you might also consider?

A. hashcode ()

** Q26. you are planning to do an Indexed search in a list
Of objects. Which of the two Java collections shocould you use: arraylist
Or shard list?

A. arraylist

** Q27. how wocould you make a copy of an entire Java object with its State?

A. Have this class implement cloneable interface and call its method clone ().

** Q28. how can you minimize the need of garbage collection and make the memory use more effective?

A. Use object pooling and weak object references.

** Q29. there are two classes: A and B. The class B need
Inform a Class A when some important event has happened. What Java
Technique wocould you use to implement it?

A. If these classes are threads I 'd consider every Y () or policyall (). For regular classes you can use the observer interface.

** Q30. what access level do you need to specify in
Class declaration to ensure that only classes from the same directory
Can access it?

A. You do not need to specify any access level, and Java will use a default package access level.

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.