Topics Index

Guess Output1399

If you need explanation Read this topic

If you need Answer Take test on this topic

Guess Output1399
What will be the output of the following program?
public class Mummy
    static public void main(String[] b)
        String a = null;
        if (a != null & a.length() > 10)
            System.out.println("Merit Campus");

B. Merit Campus
C. Expection in thread "main" java.lang.NullPointerException
D. Compilation Error or Runtime Error for some other reason
Topic: Java length() Method | length() Method In Java - Strings

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C.
here in the program, we are assigning null to a String reference.. and in the if condition we are using bitwise & operator which will check both the conditions even if the first condition is false.. so while checking with the second condition i.e., a.length() which leads to NullPointerException as 'a' still refers to null...

So the program will raise "Exception in thread "main" java.lang.NullPointerException

Posted by Gopinath Manchikanti    2015-02-27 14:26:04

ANS Is C( java.lang.nil pointer exception)

Here the concept of Strings is used.

---Here, string is declared with null assigned to it.

---After that if condition is used where bitwise & operator is used that means if block will be executed only if both the conditions evaluate to be true.

---First condition is not equal to operator where it is checked whether string is not equal to null or not as string is null so it will be true.

---In Second condition, length() function is used which tells the length of string in integer as string is null so length function will not work so it throws NullPointerExcption as pointer to string object is null.

Main() throws java.lang.NullPointerExcption

Posted by Mânïshå Mùlchåndânï    2015-02-27 15:29:26

ans c.. null value can be assigned to an object’s reference and denotes that the object is currently pointing to unknown piece of data. A NullPointerException is thrown when an application is trying to use or access an object whose reference equals to null .coming to this program string object a is assigned with null,so a is pointing to nothing. thn if condition checks a is null or not ,so this condition failed,here & is used ,nxt condition we tried to access length of null which is an illegal access so nullPointerException get thrown

Posted by Maheshwari Natarajan    2015-02-27 18:02:45

This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-03-02 07:04:42

© meritcampus 2019

All Rights Reserved.

Open In App