Tag: programming languages

Questions Related to programming languages

  1. Methods cannot be overriden to be more private

  2. static methods cannot be overloaded

  3. private methods cannot be overloaded

  4. An overloaded method cannot throw exceptions not checked in the base class


Correct Option: A
  1. System.out.println(Math.floor(-4.7));

  2. System.out.println(Math.round(-4.7));

  3. System.out.println(Math.ceil(-4.7));

  4. System.out.println(Math.min(-4.7));


Correct Option: C
Explanation:

To solve this question, the user needs to understand the difference between the Math.floor(), Math.round(), Math.ceil(), and Math.min() methods in Java.

  • Math.floor() returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
  • Math.round() returns the closest long or int, as given by the methods of the same name, to the argument, with ties rounding to positive infinity.
  • Math.ceil() returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
  • Math.min() returns the smaller of two double values as an argument.

Now, let's go through each option and determine which one will output -4.0:

A. System.out.println(Math.floor(-4.7));

  • This option will output -5.0 because Math.floor() returns the largest double value that is less than or equal to the argument, which in this case is -5.0.

B. System.out.println(Math.round(-4.7));

  • This option will output -5 because Math.round() returns the closest long or int to the argument, which in this case is -5.

C. System.out.println(Math.ceil(-4.7));

  • This option will output -4.0 because Math.ceil() returns the smallest double value that is greater than or equal to the argument, which in this case is -4.0.

D. System.out.println(Math.min(-4.7));

  • This option will not compile because Math.min() requires two arguments, and only one is given.

Therefore, the answer is:

The Answer is: C

  1. s3=s1 + s2

  2. s3=s1 - s2

  3. s3=s1 & s2

  4. s3=s1 && s2


Correct Option: A
Explanation:

To solve this question, the user needs to know the basics of Java String operations.

Option A: s3=s1 + s2

This is a legal operation. The + operator can be used to concatenate two strings. In this case, s1 and s2 are concatenated and the result is stored in s3.

Option B: s3=s1 - s2

This is an illegal operation. The - operator is not defined for strings in Java.

Option C: s3=s1 & s2

This is an illegal operation. The & operator is a bitwise operator and cannot be used with strings in Java.

Option D: s3=s1 && s2

This is an illegal operation. The && operator is a logical operator and cannot be used with strings in Java.

Therefore, the legal operation is:

The Answer is: A

  1. 0

  2. 1

  3. 2

  4. -1


Correct Option: A
Explanation:

To solve this question, the user needs to understand the basic concept of a do-while loop in Java. The loop executes the code block at least once, even if the condition is initially false. The user must evaluate the code block inside the loop to determine what value is assigned to the variable i and printed to the console.

In this code, the loop decrements the value of i by 1 until i is no longer greater than 2. Since i is initially 1, it will be decremented to 0 before the condition fails and the loop ends. Therefore, the correct answer is:

The Answer is: A. 0

  1. This() is used to invoke a constructor of the same class. super() cannot invoke a superclass constructor.

  2. Super() is used to invoke a constructor of the same class. This() is used to invoke a superclass constructor.

  3. This() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

  4. None of the above.


Correct Option: C