Programming Interview Questions

What will be the output of the below given program?

Options :

  1. 0

  2. 5

  3. Compilation Error

  4. 1

Answer : b. 5

What will be the output of the below given program?

Options :

  1. 0

  2. 5

  3. Compilation Error

  4. NullPointerException

Answer : b. 5

This is because static variables are related to class and not instance. Hence, the instance value doesn't hold good if a static variable is tried to be accessed.

Learn in detail about Java

What will be the output of the below given program?

Options :

  1. Inside Child

  2. Created a Father

  3. Compilation error

  4. Created a Father Hi Inside Child

Answer : c. Compilation error

As per the rule always the 'parent default' constructor should be present if a 'parameterized' constructor is created. In this case create a 'default' constructor as 'Father' class is missing.

What is a WeakHashMap?

Options :

  1. A hashtable-based Map implementation with weak keys

  2. A list with weak references to objects

  3. A hasttable map with duplictated keys

  4. A general purpose hashtable-based implementation to better store.

Answer : a. A hashtable-based map implementation with weak keys.

Which of the following is/are true?

Options :

  1. if("String ".trim() == "String")

  2. if(" String ".trim() == "String")

  3. if("String".trim() == "String")

  4. if("Str ing ".trim() == "String")

Answer : c.

trim() function is only performed if blankspaces are present at the beginning or end. If not present it will return the same string. Hence the function will return true. But if the string contains blankspaces, subString() is called to trim the string and return a new String using 'new' operator.

Which of the following lines allow main method to be executed by ClassLoader?

Options :

  1. public static String main(String args[]);

  2. protected static void main(String args[]);

  3. final public static void main(String args[]);

  4. public static void main();

  5. private static void main(String args[]);

  6. public void main(String args[]);

Answer : 3.

As only final, public, static and void is allowed with main method.

What is the output of the below given program?

Options :

  1. 1

  2. 0

  3. RunTimeException occurs

  4. Compile time error 'i should be initialized

Answer : d.

'Final' variable needs to be initialized always.

What is the output of the below given program?

Options :

  1. In Question05

  2. In Question05Sub

  3. Compilation error

  4. In Question05

Answer : c.

If the 'parent' method throws a 'Checked Exception' then the 'child' class overriding it can throw same exception or unchecked exception or any subclass of the Exception thrown (in this case subclass of IOException).

What is the output of the below given program?

Options :

  1. Print 0,1,2,3,4

  2. Print 1,2,3,4

  3. Compilation error

  4. throws RunTimeException

Answer : c.

The local variable 'i' cannot be accessed directly inside inner class. To make it accessible it should be declared as 'final'.

What is the output of the below given program?

Options :

  1. Inside Parent static
    Inside Child static
    Inside Parent init
    Parent Const
    Inside Child init
    Child Const

  2. Inside Parent static
    Inside Child static
    Inside Parent init
    Inside Child init
    Parent Const
    Child Const

  3. Inside Parent static
    Inside Child static
    Inside Parent init
    Inside Child init
    Child Const
    Parent Const

  4. Inside Parent init
    Inside Child init
    Inside Parent static
    Inside Child static
    Child Const
    Parent Const

Answer : a.

What is the output of the below given program?

Options :

  1. 147

  2. -109

  3. Compilation error

  4. Rutime error

Answer : b. -109

What is output of the below given program?

If an inner class enclosed with an outer class is compiled, then there is one ".class" file and a inner class ".class" file for each outer class. e.g.


Options :

  1. Hi

  2. Compilation Error

  3. Runtime Error

  4. 132

Answer : a. Hi

Learn More about Programming

What is output of the below given program?

Options :

  1. Inside f1 with String as argument

  2. Inside f1 with object as argument

  3. Compilation error

  4. Runtime error

Answer : b. Inside f1 with String as argument