Important Java IQ(Inverview Questions) with answers_Part 5

Q. How does Java handle integer overflows and underflows?
A. It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.


Q. What if I write static public void instead of public static void?
A. Program compiles and runs properly.


Q. What is the difference between declaring a variable and defining a variable?
A. In declaration we only mention the type of the variable and its name without initializing it. Defining means declaration + initialization. E.g. String s; is just a declaration while String s = new String (“bob”); Or String s = “bob”; are both definitions.


Q. What type of parameter passing does Java support?
A. In Java the arguments (primitives and objects) are always passed by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.


Q. Explain the Encapsulation principle.
A. Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Objects allow procedures to be encapsulated with their data to reduce potential interference. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.


Q. What do you understand by a variable?

A. Variable is a named memory location that can be easily referred in the program. The variable is used to hold the data and it can be changed during the course of the execution of the program.


Q. What do you understand by numeric promotion?
A. The Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integral 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.


Q. What do you understand by casting in java language? What are the types of casting?
A. The process of converting one data type to another is called Casting. There are two types of casting in Java; these are implicit casting and explicit casting.


Q. What is the first argument of the String array in main method?
A. 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. If we do not provide any arguments on the command line, then the String array of main method will be empty but not null.


Q. How can one prove that the array is not null but empty?
A. Print array.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 array.length.


Q. Can an application have multiple classes having main method?
A. Yes. 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.


Q. When is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in Java?
A. Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.


Q. Can I have multiple main methods in the same class?
A. We can have multiple overloaded main methods but there can be only one main method with the following signature :


public static void main(String[] args) {}


No the program fails to compile. The compiler says that the main method is already defined in the class.


Q. Explain working of Java Virtual Machine (JVM)?
A. JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes.

Comments

Popular Posts