Tag: programming languages

Questions Related to programming languages

  1. (Y)x2.do2();

  2. x2.do2();

  3. ((Y)x2).do2();

  4. None of the above statements will compile


Correct Option: C
Explanation:

To solve this question, the user needs to know about inheritance, method overriding, and type casting.

In the given code, class X defines a method do1() and class Y extends class X and includes a new method do2(). In the main method, three objects are created, x1 is an object of class X, x2 is a reference of class X that points to an object of class Y, and y1 is an object of class Y.

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

A. (Y)x2.do2(); This option will not compile because x2 is a reference of class X and does not have access to the method do2() of class Y. To call a method of a subclass, we need to cast the reference to that subclass. However, we cannot cast x2 to class Y because it is not an object of class Y.

B. x2.do2(); This option will not compile because the reference x2 is of class X and does not have access to the method do2(). Since x2 is a reference of class X, only the methods defined in class X or its superclasses can be called using this reference.

C. ((Y)x2).do2(); This option will compile because we are casting the reference x2 to class Y, which has access to the do2() method. Since x2 is pointing to an object of class Y, we can safely cast it to class Y. The casted reference ((Y)x2) can then access the do2() method of class Y.

D. None of the above statements will compile. This option is incorrect because option C will compile.

Therefore, the correct answer is:

The Answer is: C. ((Y)x2).do2();

  1. Beagle b2 = (Beagle) dog1;

  2. Runtime Exception

  3. Beagle b4 = dog2;

  4. None of the above statements will compile


Correct Option: A
Explanation:

To determine which statements will compile, let's analyze each option:

A. Beagle b2 = (Beagle) dog1; This statement will not compile because it is trying to cast a Dog object (dog1) to a Beagle object. However, dog1 is not an instance of Beagle, so the cast is not valid.

B. Runtime Exception This option is not applicable as it is not a valid code statement.

C. Beagle b4 = dog2; This statement will not compile because it is trying to assign a Dog object (dog2) to a Beagle variable (b4). In Java, you cannot assign an object of a superclass to a variable of its subclass type without explicit casting.

D. None of the above statements will compile This option is incorrect because statement A will compile.

Therefore, the correct answer is A) Beagle b2 = (Beagle) dog1.

class Scoop { static int thrower() throws Exception { return 42; } public static void main(String [] args) { try { int x = thrower(); } catch (Exception e) { x++; } finally { System.out.println("x = " + ++x); } } } What is the result?

  1. x = 42

  2. x = 43

  3. x = 44

  4. Compilation fails.


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concepts of exceptions and the try-catch-finally block.

The thrower() method is defined to throw an Exception, but it returns the integer 42. In the main() method, we try to call thrower(), but since it throws an exception, we catch the exception in the catch block. However, the variable x is not defined outside of the try block, so attempting to increment it in the catch block will result in a compilation error.

Therefore, the correct answer is:

The Answer is: D

  1. 0

  2. 1

  3. Compilation Error

  4. Runtime Exception


Correct Option: C
  1. There can be any number of var-arg parameters in a method

  2. Only one var-arg parameter is allowed and it must be declared as last parameter in method definition.

  3. Only one var-arg parameter is allowed and it must be declared as first parameter in method definition.

  4. None of the above


Correct Option: B