Programming





Interview Question and Answers Part 2

Interview Question and Answers Part 2


by Administrator | 31-Mar-2020
Java

Tags:  Interview  Question And Answer

Share: 


  1. What is the argument type of a main( ) method?

The argument type is a String array

  1. Can a program have multiple main methods in the same class?

No, the program will not compile if we do so.

  1. Can a main() method be overloaded?

Yes, by method overloading it can be done.

  1. Why do we need public static void main(String args[]) method in Java?

We need them because:

public: The method can be accessed outside the class/package.

static: this method is static so that we don’t need to create an instance.

void: Your application need not return a value, as the JVM launcher would return the value when it exits.

main(): This is the entry point for the application. The JVM is written to name this method as main().

  1. What is the use of String args[] in the main() method?

String args[] will help the program to receive command-line arguments. JVM will create an array of String and will store the received arguments inside String args[].

  1. Can main method be declared final?

Yes, the main method can be declared final, in addition to being public static.

  1. How can one prove that the array is not null but empty using one line of code?

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.

  1. What is the first argument of the String array in main() method?

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.

  1. If I do not provide any arguments on the command line, then the String array of main() method will be empty or null?

It is empty. But not null.

  1. How can one prove that the array is not null but empty using one line of code?

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.

  1. Can an application have multiple classes having the main method?

Yes, an application can have multiple classes having the main method, but while starting the application, mention the class name which is to be run i.e the class name which is to be executed. The JVM will only look for the main() method in that class which you have mentioned. Hence there will not be any conflict amongst the multiple classes having the main method.

  1. What is the use of -Xms and -Xmx?

It is used to create or increase the size of the JVM memory. java -Xms64m -Xmx256m HelloWorld. Requesting for min 64MB and max 256MB.

  1. What is the output of the following line of code? System.out.println("// Is this a comment?"); Output

// Is this a comment?

  1. How to assign null value to int?

int a=10;

Integer a1=(Integer)a;

a1=null;

a=a1;

System.out.println(a);

  1. Why char uses 2 byte in java and what is \u0000 ?

Because java uses the Unicode system rather than the ASCII code system. \u0000 is the lowest range of Unicode system.

  1. What is the difference between declaring a variable and defining a variable?

In declaration, we just mention the type of the variable and its name. We do not initialize it.

But defining means declaration + initialization.

For e.g. String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.

  1. What is the difference between an argument and a parameter?

While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

  1. What happens if you don’t initialize an instance variable of any of the primitive types in Java? Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a boolean will be initialized to false, etc.
  2. When does a static variable memory will destroy?

At the time of JVM shut down.

  1. Which arithmetic operations can result in the throwing of an ArithmeticException?

/ (Division) and % (Modulus) can result in the throwing of an ArithmeticException.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.