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

What will be the output?

  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
package inheritance;
class TestInheritance4 {
 String name = "JAVA";
}
package inheritance1;
import
import inheritance.TestInheritance4;
public class TestInheritance_4 extends TestInheritance4 {
 void printName() {
  System.out.println("name =" + name);
 }
 public static void main(String[] args) {
  TestInheritance_4 T_4 = new TestInheritance_4();
  T_4.printName();
 }
}
  1. name = JAVA

  2. Compiler Error

  3. Exception

  4. name = null


Correct Option: B

AI Explanation

To answer this question, we need to understand the concept of packages and inheritance in Java.

In the given code, there are two packages: inheritance and inheritance1. The TestInheritance4 class is defined in the inheritance package, and the TestInheritance_4 class is defined in the inheritance1 package.

The TestInheritance_4 class extends the TestInheritance4 class, which means it inherits the name field from the parent class.

Now, let's go through the options:

Option A) name = JAVA - This option is incorrect. Since the TestInheritance_4 class extends the TestInheritance4 class, it can access the name field. However, the name field is not explicitly assigned any value in the TestInheritance_4 class, so its default value of null will be printed.

Option B) Compiler Error - This option is correct. The code will give a compiler error because the import statement in the TestInheritance_4 class is incomplete. It is missing the required package name. The correct import statement should be import inheritance.TestInheritance4;.

Option C) Exception - This option is incorrect. There is no exception thrown in the code.

Option D) name = null - This option is incorrect. Although the name field is not explicitly assigned any value in the TestInheritance_4 class, the default value of null is not printed. The code will give a compiler error before reaching the point where printName() is called.

Therefore, the correct answer is B) Compiler Error.

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
int x = 10;
do {
 x--;
} while (x < 10);

How many times the loop will be executed?

  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