Core Java Interview Questions - Part 3

Here is the Third set of Core Java Interview Questions. You can find the previous set of interview questions from below :
1. Where and how can you use a private constructor.
Private constructor can be used if you do not want any other class to instanstiate the object , the instantiation is done from a static public method. This method is used when dealing with the factory method pattern when the designer wants only one controller (fatory method ) to create the object.
2. Can a top level class be private or protected?
No. A top level class can not be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access.If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected.
3. Differentiate between continue and break.
In break, control comes out of the loop whereas in continue control stops at “continue” statement. The remaining statements aren’t executed as control enters following loop.
4. What restrictions are placed on method overriding in Java Programming?
Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.
5. Why equals should be overridden?
Two objects are considered equal if referring to the same object as the equals method uses only the operator == to compare. Hence while using the object as keys, equals() method needs to be overridden. Similarly while using hashmaps or hashtables the method hashcode needs to be overridden as the object is placed in the hash using hashcode value.
6. Can we override a static method?
The static methods cannot be overridden. If a subclass defines a static method with the same signature as a static method in the superclass, the method in the subclass hides the one in the superclass.
7. What are Transient and Volatile Modifiers?
Transient: Transient 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).
Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
8. What is Garbage Collection?
When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection.
Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program can't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program.
9. How you can force the garbage collection?
Garbage collection automatic process and can't be forced. You could request it by calling System.gc() or Runtime.gc(). JVM does not guarantee that GC will be started immediately and when all the objects will garbage collected.
Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use.
10. What are immutable objects in Java? Can we change the value of an immutable object?
An object is considered to be immutable if its value cannot change after it is created. Immutable objects are particularly very useful in multi-threaded applications. Since they cannot change state, they won’t get corrupted by thread interference or observed in an inconsistent state. Objects of java.lang.Integer and java.lang.String classes are the examples of immutable objects. To create an immutable object You need to make the class final and all its members private or final, so that once objects gets created no one can modify its state.
11. What do you understand by numeric promotion?
The Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In the numerical promotion process the byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.
12. What is an applet?
Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser. Applet needs no explicit installation on local machine and runs itself automatically in a java-enabled browser.
13. What is the lifecycle of an applet?
  1. init() method - Can be called when an applet is first loaded
  2. start() method - Can be called each time an applet is started.
  3. paint() method - Can be called when the applet is minimized or maximized.
  4. stop() method - Can be used when the browser moves off the applet’s page.
  5. destroy() method - Can be called when the browser is finished with the applet.
14. What is JVM (Java Virtual Machine)?
JVM stands for Java Virtual Machine. JVM is a software implementation which stands on the top of the real hardware platform and operating system. It provides abstraction between the compiled java program and the hardware and operating system. So the compiled program does not have to worry about what hardware and operating system he has to run in, it’s all handled by the JVM and thus attaining portability. All Java programs are compiled in to bytecodes. JVM can only understand and execute Java bytecodes.
15. Explain the advantages and disadvantages of JAVA.
JAVA offers a number of advantages to developers.
  1. Java is simple: Java was designed to be easy to use and learn than other programming languages. Java is much simpler than C++ as it uses automatic memory allocation and garbage collection where else C++ requires the programmer to allocate memory and to collect garbage.
  2. Java is object-oriented: Programming in Java is centered on creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code.
  3. Java is platform-independent: Java programs are compiled into Java Virtual Machine code called bytecode. The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the bytecode generated by the Java compiler can run on any platform.
  4. Java is distributed: Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.
  5. Java is secure: Java is one of the first programming languages to consider security as part of its design. The Java language, compiler, interpreter, and runtime environment were each developed with security in mind.
  6. Java is robust: Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that would first show up during execution time in other languages.
  7. Java is multithreaded: Multithreaded is the capability for a program to perform several tasks simultaneously within a program. In Java, multithreaded programming has been smoothly integrated into it.
Disadvantages of JAVA :
  1. Performance: Java can be perceived as significantly slower and more memory-consuming than natively compiled languages such as C or C++.
  2. Single-paradigm language: Java is predominantly a single-paradigm language. However, with the addition of static imports in Java 5.0 the procedural paradigm is better accommodated than in earlier versions of Java.

Please do comment if you find any discrepancies or want to suggest new questions.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
 

Copyright © 2012 | ScriptSplash | Powered by Blogger Templates