Core Java Interview Questions - Part 2

Here is the Second set of Core Java Interview Questions. You can find the previous set of interview questions from below :
1. What would you use to compare two String variables - the operator == or the method equals()?
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 a String object.
2. What does the "static" keyword mean in front of a variable? A method? A class? Curly braces {}?
  • static variable: means a class level variable
  • static method: Can be invoked even before a single instance of a class is created. It is not allowed to access the not static members of the class.
  • static class: no such thing.
  • static free floating block: is executed at the time the class is loaded. There can be multiple such blocks. This may be useful to load native libraries when using native methods.
3. What does the "final" keyword mean in front of a variable? A method? A class?
FINAL for a variable : value is constant
FINAL for a method : final method cannot be overridden or hidden by new access specifications.
FINAL for a class : cannot be derived, The best example of a final class is the String class.
4. What is difference between final, finalize() and finally?
final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.
finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage collection.
finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.
5. What is casting?
Casting is used to convert the value of one type to another. There are two types of casting in Java, these are Implicit casting and explicit casting. Casting operation is used to convert between types and the instanceof operator is used to check for type information at run time.
6. What are the types of casting?
There are two types of casting in Java, these are Implicit casting and explicit casting.
Implicit casting is the process of simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not workout for all application scenarios.
int i = 4000;
long h = i; //Implicit casting
Explicit casting in the process in which the compiler are specifically informed to about transforming the object.
long ln = 700.20;
t = (int) ln; //Explicit casting
7. What do you understand by downcasting?
The process of Downcasting refers to the casting from a general to a more specific type, i.e. casting down the hierarchy.
8. What modifiers may be used with top-level class?
public, abstract and final can be used for top-level class.
9. What are inner class and anonymous class?
Inner class : classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.
Anonymous class : Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.
10. Describe the wrapper classes in Java.
Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
eg: boolean  - java.lang.Boolean
int - java.lang.Integer
void - java.lang.Void
11. What is the difference between StringBuffer and String class?
A StringBuffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
The String class represents character strings. All string literals in Java programs are immutable and their values cannot be changed after they are created.
12. What is the difference between the boolean '&' operator and the '&&' operator?
If an expression involving the boolean & operator is evaluated, both operands are evaluated.
Whereas when an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then only the second operand is evaluated. If the first operand evaluates to false, the evaluation of the second operand is skipped.
13. Why there are no global variables in Java?
Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:
  1. The global variables breaks the referential transparency
  2. Global variables creates collisions in namespace.
14. Why Java does not support pointers?
Because pointers are unsafe. Java uses reference types to hide pointers and programmers feel easier to deal with reference types without pointers.
15. How is it possible for two String objects with identical values not to be equal under the == operator?
The '==' operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory.

Please do provide your valuable comments and suggestions. Thanks in Advance.


  1. i really like that you are giving information on core and advance java concepts. Being enrolled at i found your information very helpful indeed.thanks for it.


Related Posts Plugin for WordPress, Blogger...

Copyright © 2012 | ScriptSplash | Powered by Blogger Templates