Examveda
Examveda

How many objects will be created?
String a = new String("Examveda");
String b = new String("Examveda");
String c = "Examveda";
String d = "Examveda";

A. 4

B. 3

C. 2

D. None of this

Answer: Option B

Solution(By Examveda Team)

Object will be created each time whenever we use new keyword. So, 2 object will be created simply for the first two line and matter is with remaining two bottom line. String c="examveda" creates an object and store it in String pool, next time when we are writing String d="examveda" it will first check in String pool whether object already exists or not. Since, it is existing, no new object will be created. Hence reference "d" points to existing object "examveda". So ultimately 3 object will be created at the end.


This Question Belongs to Java Program >> Strings

Join The Discussion

Comments ( 3 )

  1. Shatakshi Gupta
    Shatakshi Gupta :
    4 years ago

    First line will create 2 objects- 1 in pool and 1 in heap ------------>2
    Second line will create - 1 in heap, pool already has same object ---->1
    third and fourth line will refer to existing pool object --->0

    so total 2+1=3

  2. Hiranya Konakalla
    Hiranya Konakalla :
    4 years ago

    If there is a String a ,String B and String C= A+B how many objects are created in memory?

  3. Himanshu Bagga
    Himanshu Bagga :
    5 years ago

    In the case of String, String a = new String("Examveda");

    2 objects will be created .

    So answer should be 2 +2+ 1 = 5 i.e. None of these as per options

Related Questions on Strings