Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.


Please wait...

JAVA Programming :: Basic Concepts

Home > JAVA Programming > Basic Concepts > General Questions

21. What is the result of evaluating the expression 14 ^ 23. Select the one correct answer.

Answer: Option A

Explanation:

^ is the symbol for XOR operator, which results in 0 when the bits are same, and 1 when the bits are different.

14 in binary can be represented as = 0000 1110

23 in binary can be represented as = 0001 0111

Therefore,         14 ^ 23                   = 0001 1001

which is equal to 25.
            

Workspace


Tags:  No Tags on this question yet!

22. What all gets printed when the following program is compiled and run. Select the one correct answer.

public class test {
public static void main(String args[]) {
inti=0, j=2;
do {
i=++i;
j;
} while(j>0);
System.out.println(i);
}
}

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

23. What all gets printed when the following gets compiled and run.

public class test {
public static void main(String args[])
{
inti=1, j=1;
try
{ i++;
j;
if(i/j > 1)
i++;
}
catch(ArithmeticException e)
{
System.out.println(0);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(1);
}
catch(Exception e)
{
System.out.println(2);
}
finally
{
System.out.println(3);
}
System.out.println(4);
}
}

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

24. What all gets printed when the following gets compiled and run.

public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
if(s1 == s2)
System.out.println(1);
else
System.out.println(2);
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}

Answer: Option A

Explanation:

Now, the == operator is meant for always for reference comparison, i.e., it will result True if the string objects are same else False. On the other hand, equals() method defined in the String class is meant for content comparison, i.e, it will result True if the contents are same else False.

Here, the content of s1 and s2 is "abc", so in the String constant pool, a single object is created having the content as "abc", and s1 and s2 both will refer to the same object created. Since, both are referring to the same string object, hence (s1==s2) becomes True, and if part will execute.

On the other hand, the content for both s1 and s2 are the same, hence "s1.equals(s2)"  also becomes True, so again the if part gets executed.

Therefore, the output will be-

1

3

Workspace


Tags:  No Tags on this question yet!

25. What all gets printed when the following gets compiled and run.

public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = new String("abc");
if(s1 == s2)
System.out.println(1);
else
System.out.println(2);
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}

Answer: Option C

Explanation:

String s1="abc";----------------> In this declaration, only one String object will be created in the String constant pool, and s1 will be pointing to that object.

String s2=new String("abc");---------->In this declaration, 2 objects will be created-one in the Heap area, and the other in the String constant pool, and s2 will always be pointing to the Heap area. But, already an object is created having the same content "abc". So, the existing object will be re-used.

                        Heap                                                                        String constant pool

           s2----------->"abc"                                                                     "abc"<---------------s1

Now, the == operator is meant for always for reference comparison, i.e., it will result True if the string objects are same else False. On the other hand, equals() method defined in the String class is meant for content comparison, i.e, it will result True if the contents are same else False.

Here, (s1==s2) will result in False, as s1 is referring to the object in String constant pool, while s2 is referring to the object in the Heap, so the else part gets executed.

On the other hand, "s1.equals(s2)" will result in True because their contents are same, so the if part gets executed.

Hence, the output is-

2

3






Workspace


Tags:  No Tags on this question yet!

Answer: Option E

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

27. What is the range of values that can be specified for an int. Select the one correct answer.

Answer: Option B

Explanation:

231 to 231 1

Workspace


Tags:  No Tags on this question yet!

28. What gets printed when the following code is compiled and run. Select the one correct answer.

public class test {
public static void main(String args[]) {
inti = 1;
do
{
i;
} while (i> 2);
System.out.println(i);
}
}

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

29. At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.

void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4

Answer: Option D

Explanation:

Before the statement labelled 4, the object s becomes available and eligible for garbage collection. The garbage collection activity is done by the garbage collector thread, whose work is to sweep out abandoned objects, or the objects which do not have any live reference anymore. The garbage collector thread comes once in a while in the heap area, and when it finds any abandoned objects, it sweeps out the abandoned objects after calling the finalize() method on that object.


Workspace


Tags:  No Tags on this question yet!

30. String s = new String("xyz");
Assuming the above declaration, which of the following statements would compilation. Select the one correct answer.

Answer: Option C

Explanation:

Option C is correct, because 's' is String type object. s=s+s, means s="xyz"+"xyz", so s="xyzxyz". Rest all other are not possible, hence it won't compile.

Workspace


Tags:  No Tags on this question yet!



Most Popular Qs.

Latest MCQ Qs.