Tag: programming languages

Questions Related to programming languages

Which ALL are true?

  1. A finalizer may NOT be invoked explicitly.

  2. The finalize method declared in class Object takes no action

  3. super.finalize() is called implicitly by any overriding finalize method

  4. The finalize method for a given object will be called no more than once by the garbage collector

  5. The order in which finalize will be called on two objects is based on the order in which the two objects became finalizable


Correct Option: B,D

AI Explanation

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

A. A finalizer may NOT be invoked explicitly. - This option is incorrect because a finalizer can be invoked explicitly using the finalize() method. However, it is generally not recommended to do so.

B. The finalize method declared in class Object takes no action. - This option is correct. The finalize() method declared in the Object class does not perform any specific action. It is an empty method.

C. super.finalize() is called implicitly by any overriding finalize method. - This option is incorrect. The super.finalize() method is not called implicitly by any overriding finalize() method. It needs to be explicitly called if required.

D. The finalize method for a given object will be called no more than once by the garbage collector. - This option is correct. The finalize() method for a given object will be called by the garbage collector at most once. After the finalize() method is called, the object becomes eligible for garbage collection.

E. The order in which finalize will be called on two objects is based on the order in which the two objects became finalizable. - This option is not mentioned in the correct answer. Therefore, it is incorrect. The order in which the finalize() method is called on two objects is not determined by the order in which they became finalizable.

The correct answer is B, D. Option B is correct because the finalize() method declared in class Object takes no action. Option D is correct because the finalize() method for a given object will be called no more than once by the garbage collector.

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

AI Explanation

To answer this question, we need to understand the conditional (ternary) operator in Java.

The conditional operator (?:) has the following syntax:

(condition) ? expression1 : expression2

If the condition is true, expression1 is evaluated and becomes the result of the entire expression. If the condition is false, expression2 is evaluated and becomes the result.

In the given code snippet:

String first = (elements.length > 0) ? elements[0] : null;

The condition is "elements.length > 0", which checks if the length of the array "elements" is greater than 0.

If the condition is true (i.e., the array has at least one element), the expression "elements[0]" is evaluated and assigned to the variable "first". So, the variable "first" is set to the value of the first element in the array.

If the condition is false (i.e., the array is empty), the expression "null" is evaluated and assigned to the variable "first". So, the variable "first" is set to null.

Therefore, the correct answer is:

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

Given: 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, we need to understand the syntax and behavior of the System.out.format() method.

In the given code snippet, System.out.format() is used to format output to the console. The format string "Pi is approximately %d." contains a placeholder %d, which is used to specify that a decimal integer value will be inserted at that position in the output.

However, the Math.PI constant in Java is a double value, not an integer. Therefore, when attempting to format the output using %d, a runtime exception will be thrown.

So, the correct answer is:

D. An exception is thrown at runtime

Variables are not interpolated inside which operator when used normally?

  1. " "

  2. < >

  3. ' '

  4. / /


Correct Option: C

Which of the following statements are correct when using the RETAINstatement?

  1. Variables read with the SET statement will not be affected by it

  2. The RETAIN statement is used when a variable needs to be

  3. Variables will be initialized to the desired value in each iteration

  4. Variables are created during execution


Correct Option: A
  1. It will be initialised to 1 in the begining

  2. It will have the observation number that caused the error

  3. It will not be dropped while writing into the dataset

  4. It can be used in expressions


Correct Option: D

p = 5461;x='1001';a=x ||'/'||pWhat is the value of a?

  1. 1001/ 5461

  2. Error because of concatenating character and numeric variable

  3. 1001/ 5461

  4. 1001/5461


Correct Option: C
  1. Error because of giving formats while the format statement is not provided

  2. Missing

  3. 1234

  4. $1,234


Correct Option: B