Showing posts with label Java Interview Questions. Show all posts
Showing posts with label Java Interview Questions. Show all posts
Wednesday, June 20, 2012
0
Wednesday, June 20, 2012
Anonymous
Ans:- A transient variable is a variable that may not be serialized.
2. Which containers use a border Layout as their default layout?
Ans:- The Window, Frame and Dialog classes use a border layout as their default layout.
3. Why do threads block on I/O?
Ans:- Threads block on I/O (that is enters the waiting state) so that other threads may execute while the I/O Operation is performed.
4. How are Observer and Observable used?
Ans:- Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.
5. What is synchronization and why is it important?
Ans:- With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors.
6. Can a lock be acquired on a class?
Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object..
7. What's new with the stop(), suspend() and resume() methods in JDK 1.2?
Ans:- The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.
8. Is null a keyword?
Ans:- The null is not a keyword.
9. What is the preferred size of a component?
Ans:- The preferred size of a component is the minimum component size that will allow the component to display normally.
10. What method is used to specify a container's layout?
Ans:- The setLayout() method is used to specify a container's layout.
11. Which containers use a FlowLayout as their default layout?
Ans:- The Panel and Applet classes use the FlowLayout as their default layout.
12. What state does a thread enter when it terminates its processing?
Ans:- When a thread terminates its processing, it enters the dead state.
13. What is the Collections API?
Ans:- The Collections API is a set of classes and interfaces that support operations on collections of objects.
14. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
Ans:- The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.
15. What is the List interface?
Ans:- The List interface provides support for ordered collections of objects.
16. How does Java handle integer overflows and underflows?
Ans:- It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.
17. What is the Vector class?
Ans:- The Vector class provides the capability to implement a growable array of objects
18. What modifiers may be used with an inner class that is a member of an outer class?
Ans:- A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.
19. What is an Iterator interface?
Ans:- The Iterator interface is used to step through the elements of a Collection.
20. What is the difference between the >> and >>> operators?
Ans:- The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.
Advanced Java Interview Questions
Advanced Java Interview Questions
Following are the Advanced Java Interview Questions presenting here, are collected from various students, professors, websites.
1. What is a transient variable?Following are the Advanced Java Interview Questions presenting here, are collected from various students, professors, websites.
Ans:- A transient variable is a variable that may not be serialized.
2. Which containers use a border Layout as their default layout?
Ans:- The Window, Frame and Dialog classes use a border layout as their default layout.
3. Why do threads block on I/O?
Ans:- Threads block on I/O (that is enters the waiting state) so that other threads may execute while the I/O Operation is performed.
4. How are Observer and Observable used?
Ans:- Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.
5. What is synchronization and why is it important?
Ans:- With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors.
6. Can a lock be acquired on a class?
Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object..
7. What's new with the stop(), suspend() and resume() methods in JDK 1.2?
Ans:- The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.
8. Is null a keyword?
Ans:- The null is not a keyword.
9. What is the preferred size of a component?
Ans:- The preferred size of a component is the minimum component size that will allow the component to display normally.
10. What method is used to specify a container's layout?
Ans:- The setLayout() method is used to specify a container's layout.
11. Which containers use a FlowLayout as their default layout?
Ans:- The Panel and Applet classes use the FlowLayout as their default layout.
12. What state does a thread enter when it terminates its processing?
Ans:- When a thread terminates its processing, it enters the dead state.
13. What is the Collections API?
Ans:- The Collections API is a set of classes and interfaces that support operations on collections of objects.
14. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
Ans:- The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.
15. What is the List interface?
Ans:- The List interface provides support for ordered collections of objects.
16. How does Java handle integer overflows and underflows?
Ans:- It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.
17. What is the Vector class?
Ans:- The Vector class provides the capability to implement a growable array of objects
18. What modifiers may be used with an inner class that is a member of an outer class?
Ans:- A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.
19. What is an Iterator interface?
Ans:- The Iterator interface is used to step through the elements of a Collection.
20. What is the difference between the >> and >>> operators?
Ans:- The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.
Monday, June 11, 2012
0
Monday, June 11, 2012
Anonymous
Java Basics Interview Questions
Following are the Java Basics Interview Questions presenting here are collected from various students, professors, websites.
1. What is the difference between a constructor and a method?
Ans:- A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
2. What is the purpose of garbage collection in Java, and when is it used?
Ans:- The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
3. Describe synchronization in respect to multithreading.
Ans:- With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources.
Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.
4. What is an abstract class?
Ans:- Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data.
Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.
5. What is the difference between an Interface and an Abstract class?
Ans:- An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
6. Explain different way of using thread?
Ans:- The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance, the only interface can help.
7. What is an Iterator?
Ans:- Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn.
Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
8. State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.
Ans:- public: Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature. This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
What you get by default ie, without any access modifier (ie, public private or protected). It means that it is visible to all within a particular package.
9. What is static in java?
Ans:- Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object.
A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.
10. What is final class?
Ans:- A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).
11. What if the main() method is declared as private?
Ans:- The program compiles properly but at runtime it will give "main() method not public." message.
12. What if the static modifier is removed from the signature of the main() method?
Ans:- Program compiles. But at runtime throws an error "NoSuchMethodError".
13. What if I write static public void instead of public static void?
Ans:- Program compiles and runs properly.
14. What if I do not provide the String array as the argument to the method?
Ans:- Program compiles but throws a runtime error "NoSuchMethodError".
15. What is the first argument of the String array in main() method?
Ans:- The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.
16. If I do not provide any arguments on the command line, then the String array of main() method will be empty or null?
Ans:- It is empty. But not null.
17. How can one prove that the array is not null but empty using one line of code?
Ans:- Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
18. What environment variables do I need to set on my machine in order to be able to run Java programs?
Ans:- CLASSPATH and PATH are the two variables.
19. Can an application have multiple classes having main() method?
Ans:- Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned.
Hence there is not conflict amongst the multiple classes having main() method.
20. Can I have multiple main() methods in the same class?
Ans:- No the program fails to compile. The compiler says that the main() method is already defined in the class.
Java Basics Interview Questions
Java Basics Interview Questions
Following are the Java Basics Interview Questions presenting here are collected from various students, professors, websites.
Ans:- A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
2. What is the purpose of garbage collection in Java, and when is it used?
Ans:- The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
3. Describe synchronization in respect to multithreading.
Ans:- With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources.
Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.
4. What is an abstract class?
Ans:- Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data.
Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.
5. What is the difference between an Interface and an Abstract class?
Ans:- An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
6. Explain different way of using thread?
Ans:- The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance, the only interface can help.
7. What is an Iterator?
Ans:- Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn.
Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
8. State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.
Ans:- public: Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature. This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
What you get by default ie, without any access modifier (ie, public private or protected). It means that it is visible to all within a particular package.
9. What is static in java?
Ans:- Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object.
A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.
10. What is final class?
Ans:- A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).
11. What if the main() method is declared as private?
Ans:- The program compiles properly but at runtime it will give "main() method not public." message.
12. What if the static modifier is removed from the signature of the main() method?
Ans:- Program compiles. But at runtime throws an error "NoSuchMethodError".
13. What if I write static public void instead of public static void?
Ans:- Program compiles and runs properly.
14. What if I do not provide the String array as the argument to the method?
Ans:- Program compiles but throws a runtime error "NoSuchMethodError".
15. What is the first argument of the String array in main() method?
Ans:- The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.
16. If I do not provide any arguments on the command line, then the String array of main() method will be empty or null?
Ans:- It is empty. But not null.
17. How can one prove that the array is not null but empty using one line of code?
Ans:- Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
18. What environment variables do I need to set on my machine in order to be able to run Java programs?
Ans:- CLASSPATH and PATH are the two variables.
19. Can an application have multiple classes having main() method?
Ans:- Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned.
Hence there is not conflict amongst the multiple classes having main() method.
20. Can I have multiple main() methods in the same class?
Ans:- No the program fails to compile. The compiler says that the main() method is already defined in the class.
Saturday, May 19, 2012
1
Saturday, May 19, 2012
Ravva Vamsi
1. What is the most important feature of Java?
Ans:- Java is a platform independent language.
2. What do you mean by platform independence?
Ans:- Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3. What is a JVM?
Ans:- JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
4. Are JVM's platform independent?
Ans:- JVM's are not platform independent. JVM's are platform specific run time implementation provided by the vendor.
5. What is the difference between a JDK and a JVM?
Ans:- JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.
6. What is a pointer and does Java support pointers?
Ans:- Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn't support the usage of pointers.
7. What is the base class of all classes?
Ans:- java.lang.Object
8. Does Java support multiple inheritance?
Ans:- Java doesn't support multiple inheritance.
9. Is Java a pure object oriented language?
Ans:- Java uses primitive data types and hence is not a pure object oriented language.
10. Are arrays primitive data types?
Ans:- In Java, Arrays are objects.
11. What is difference between Path and Classpath?
Ans:- Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.
12. What are local variables?
Ans:- Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.
Core Java Interview Questions - 1
Core Java Interview Questions
Following are the Core Java Interview Questions presenting here are collected from various students, professors, websites.
1. What is the most important feature of Java?
Ans:- Java is a platform independent language.
2. What do you mean by platform independence?
Ans:- Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3. What is a JVM?
Ans:- JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
4. Are JVM's platform independent?
Ans:- JVM's are not platform independent. JVM's are platform specific run time implementation provided by the vendor.
5. What is the difference between a JDK and a JVM?
Ans:- JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.
6. What is a pointer and does Java support pointers?
Ans:- Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn't support the usage of pointers.
7. What is the base class of all classes?
Ans:- java.lang.Object
8. Does Java support multiple inheritance?
Ans:- Java doesn't support multiple inheritance.
9. Is Java a pure object oriented language?
Ans:- Java uses primitive data types and hence is not a pure object oriented language.
10. Are arrays primitive data types?
Ans:- In Java, Arrays are objects.
11. What is difference between Path and Classpath?
Ans:- Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.
12. What are local variables?
Ans:- Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.
13. What are instance variables?
Ans:- Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.
14. How to define a constant variable in Java?
Ans:- The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.
static final int PI = 2.14; is an example for constant.
15. Should a main() method be compulsorily declared in all java classes?
Ans:- No not required. main() method should be defined only if the source class is a java application.
16. What is the return type of the main() method?
Ans:- Main() method doesn't return anything hence declared void.
17. Why is the main() method declared static?
Ans:- main() method is called by the JVM even before the instantiation of the class hence it is declared as static.
18. What is the arguement of main() method?
Ans:- main() method accepts an array of String object as arguement.
19. Can a main() method be overloaded?
Ans:- Yes. You can have any number of main() methods with different method signature and implementation in the class.
20. Can a main() method be declared final?
Ans:- Yes. Any inheriting class will not be able to have it's own default main() method.
Subscribe to:
Posts (Atom)