0

Select the output of Java Program

Description: You will be given sample program or statement, Select appropriate output or statement from the given option
Number of Questions: 20
Created by:
Tags: java programming language
Attempted 0/20 Correct 0 Score 0

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

interface <interface name> extends <interface name1>Is correct???

  1. True

  2. False


Correct Option: A

Is Multiple Inheritance allowed in JAVA???

  1. True

  2. False


Correct Option: B

clone method used for..???

  1. Used for Cloning or copying integer var

  2. Used for Cloning or copying float var

  3. Used for Cloning or copying String var

  4. Used for Cloning an array


Correct Option: D

By default type of variable(data members) in interface..???

  1. public,final

  2. Only Final

  3. Only Public

  4. public,final,static


Correct Option: D

String name="ALIEN"; name.reverse();
Is code successfully compiled..???

  1. True

  2. False


Correct Option: B
int x = 10;
do {
 x--;
} while (x &lt; 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 &lt; 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 &lt; 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

public static void main(String[] args) {
 Integer i = new Integer(1) + new Integer(2);
 switch (i) {
  case 3:
   System.out.println("three");
   break;
  default:
   System.out.println("other");
   break;
 }
}

What is the output?

  1. three

  2. other

  3. Compilation Fails

  4. Exception


Correct Option: A

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) A. three - This option is correct because the value of i is 3 after adding 1 and 2 using the Integer class. The switch case matches with the value of i and prints "three" before the break statement.

Option B) B. other - This option is incorrect because the value of i is 3, which matches the case 3 in the switch statement. Therefore, "other" will not be printed.

Option C) C. Compilation Fails - This option is incorrect because there are no syntax errors in the code. The code will compile without any issues.

Option D) D. Exception - This option is incorrect because there are no exceptions thrown in the code. The code will run without any exceptions.

The correct answer is A. three. This option is correct because the value of i is 3 after adding 1 and 2 using the Integer class. The switch case matches with the value of i and prints "three" before the break statement.

String[] elements = {
 "for",
 "tea",
 "too"
};
String first = (elements.length &gt; 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
int x = 12;
while (x &lt; 10) {
    x--;  
}  
System.out.print(x);

What is the result?

  1. 0

  2. 10

  3. 12

  4. print statement never be executed


Correct Option: C
Explanation:

To solve this question, the user needs to know about while loop and comparison operators.

In the given code snippet, the while loop will only execute if the condition inside the parentheses is true. The code initializes the variable x to 12 and checks if it is less than 10. Since 12 is greater than 10, the condition is false, and the code inside the while loop will never execute. The print statement will execute outside of the loop, which will output the value of the variable x.

Options A, B, and C can be eliminated since none of them represent the correct value.

Option D is incorrect because the print statement will execute regardless of whether the while loop is executed or not.

Therefore, the correct answer is:

The Answer is: C. 12

System.out.format("Pi is approximately %d.", Math.PI); What is the result?

  1. Compilation fails

  2. Pi is approximately 3

  3. Pi is approximately 3.141593

  4. An exception is thrown at runtime


Correct Option: D

AI Explanation

To answer this question, let's analyze the given code:

System.out.format("Pi is approximately %d.", Math.PI);

The System.out.format method is used to format and print a formatted string to the console. In this case, the string being printed is "Pi is approximately %d.".

The %d is a format specifier that is used for printing integers. However, Math.PI is a double value, not an integer.

Therefore, when this code is executed, it will throw a java.util.IllegalFormatConversionException at runtime because the format specifier %d is not compatible with the Math.PI value.

Hence, the correct answer is D) An exception is thrown at runtime.

NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(4);
nf.setMinimumFractionDigits(2);  
String a = nf.format(3.1415926);
String b = nf.format(2);

Which statement is true about the result?

  1. The value of b is 2

  2. The value of b is 2.00

  3. The value of a is 3.1415

  4. The value of a is 3.141


Correct Option: B

AI Explanation

To answer this question, let's go through each statement and evaluate whether it is true or false:

Option A) The value of b is 2 - This statement is false. The NumberFormat object nf is set to format the numbers with a minimum of 2 decimal places. Therefore, the value of b will have 2 decimal places.

Option B) The value of b is 2.00 - This statement is true. The NumberFormat object nf is set to format the numbers with a minimum of 2 decimal places. When the value 2 is formatted using this NumberFormat object, it will be displayed with 2 decimal places, resulting in the value "2.00".

Option C) The value of a is 3.1415 - This statement is false. The NumberFormat object nf is set to format the numbers with a maximum of 4 decimal places. Therefore, the value of a will be rounded to 4 decimal places.

Option D) The value of a is 3.141 - This statement is false. The NumberFormat object nf is set to format the numbers with a minimum of 2 decimal places. Therefore, the value of a will have 2 decimal places.

The correct answer is Option B. The value of b is "2.00" because the NumberFormat object nf is set to format the numbers with a minimum of 2 decimal places.

public class Yippee2 {
  static public void main(String [] yahoo) {
  for(int x = 1; x &lt; yahoo.length; x++) {
  System.out.print(yahoo[x] + " ");
  }
 }
}

and the command line invocation: java Yippee2 a b c What is the result?

  1. a b

  2. b c

  3. a b c

  4. Compilation fails


Correct Option: B

AI Explanation

To determine the result of the given code and command line invocation, let's go through each option:

Option A) a b - This option is incorrect because it does not include the last element c.

Option B) b c - This option is correct because the code prints the elements starting from index 1 (yahoo[1]) to the last index (yahoo.length - 1). In this case, yahoo[1] is b and yahoo[2] is c. Therefore, the output will be b c.

Option C) a b c - This option is incorrect because it includes the first element a, which is not printed by the code.

Option D) Compilation fails - This option is incorrect because the code does not have any compilation errors. It is a valid Java code.

The correct answer is B. The code will output b c.

public static void main(String[] args) {
  String str = "null"; 
 if (str == null) {
  System.out.println("null");
  } else (str.length() == 0) {
  System.out.println("zero");
  } else {
  System.out.println("some");
  }  
}    

What is the result?

  1. null

  2. zero

  3. some

  4. Compilation fails


Correct Option: D
Explanation:

To solve this question, the user needs to know that the given code is testing whether a string value is null or empty. They also need to know the syntax of if-else statements in Java.

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

A. null: This option is incorrect because the code checks if the string is null using the expression str == null, which evaluates to false since str is "null" (a non-null string literal).

B. zero: This option is incorrect because the code does not check if the string is empty using the expression str.length() == 0. Instead, it tries to use an invalid syntax by placing an else block after the if block. This leads to a compilation error.

C. some: This option is incorrect because the code won't reach this block due to the compilation error mentioned above.

D. Compilation fails: This option is correct. The code will fail to compile due to the invalid syntax in the else block. The correct syntax for an if-else statement is:

if (condition) {
    // code block for true case
} else {
    // code block for false case
}

Therefore, the correct code should be:

public static void main(String[] args) {
  String str = "null"; 
  if (str == null) {
    System.out.println("null");
  } else if (str.length() == 0) {
    System.out.println("zero");
  } else {
    System.out.println("some");
  }  
}

The Answer is: D

String[] elements = { "for", "tea", "too" }; 
String first = (elements.length &gt; 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
int x = 12;  
while (x &lt; 10) {  
   x--;  
}  
System.out.print(x); 

What is the result?

  1. 0

  2. 10

  3. 12

  4. print statement never be executed


Correct Option: C

AI Explanation

To answer this question, let's go through the code step by step:

  1. The code initializes the variable x with the value 12.
  2. The while loop condition is x &lt; 10. Since x is initially 12, the condition is false, and the code inside the while loop will not be executed.
  3. After the while loop, the code prints the value of x, which is 12.

Therefore, the result of the code is 12.

The correct answer is option C.

- Hide questions