Tag: java

Questions Related to java

Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached?

  1. Two objects are eligible for GC

  2. Five objects were created

  3. Four objects were created.

  4. Three objects are eligible for GC


Correct Option: A,B
  1. y string = Java,y string = Java

  2. y string = Javay string = Java

  3. y string = Java, y string = Java

  4. y String = Java, y string = Java


Correct Option: C

Syntax of System.arraycopy method??

  1. System.arraycopy(fromarray,frominden,toarray,toindex,count);

  2. System.arraycopy(toarray,toinden,fromarray,fromindex,count);

  3. System.arraycopy(fromarray,toarray,count);

  4. System.arraycopy();


Correct Option: A
  1. ten times

  2. zero times

  3. one to nine times

  4. more than ten times


Correct Option: D
int x = 0;
int y = 10;
do {
 y--;
 ++x;
} while (x < 5);
System.out.print(x + "," + y);

What is the result?

  1. 5,6

  2. 5,5

  3. 6,5

  4. 6,6


Correct Option: B
Explanation:

To solve this question, the user needs to understand the logic of the do-while loop and the post/pre-increment and decrement operators.

The given code initializes two integer variables x and y to 0 and 10, respectively. Then, it enters a do-while loop with the condition x < 5. Inside the loop, the value of y is decremented by 1, and the value of x is incremented by 1. This process continues until the value of x becomes greater than or equal to 5. Finally, it prints the values of x and y.

Now, let's go through each option and explain why it is right or wrong:

A. 5,6: This option is incorrect because the loop stops when the value of x becomes 5, at which point y is equal to 5, not 6.

B. 5,5: This option is correct. The loop stops when the value of x becomes 5, at which point y is also equal to 5.

C. 6,5: This option is incorrect because the loop stops when the value of x becomes 5, at which point x is equal to 5, not 6.

D. 6,6: This option is incorrect because the loop stops when the value of x becomes 5, at which point x is equal to 5, and y is equal to 5.

Therefore, the correct answer is:

The Answer is: B

String[] elements = {
 "for",
 "tea",
 "too"
};
String first = (elements.length > 0) ? elements[0] : null;

What is the result?

  1. Compilation fails

  2. An exception is thrown at runtime

  3. The variable first is set to null

  4. The variable first is set to elements[0]


Correct Option: D