Question
What will be the output of the following Java snippet?
class A { public void display() { System.out.println("Class A"); }} class B extends A { public void display() { System.out.println("Class B"); }} public class Test { public static void main(String[] args) { A obj = new B(); obj.display(); }}Solution
This program demonstrates runtime polymorphism in Java through method overriding. When the method display() is called on the object obj, the overridden method in class B is executed. This is because obj is instantiated as new B(), and at runtime, the JVM determines the appropriate method to execute based on the actual type of the object. Thus, even though obj is declared as type A, the overridden display() method in B is executed, producing the output "Class B" . This behavior is a key feature of Java's dynamic method dispatch mechanism. Explanation of Incorrect Options: A) Class A : This would be true if the display() method in class B was not overridden. However, since B overrides A's method, this option is incorrect. C) Compilation Error : The code is syntactically correct, so it compiles without any issues. D) Runtime Error : The program runs successfully, as all method calls are valid and properly resolved at runtime. E) None of the above : This is incorrect, as the correct output is explicitly "Class B" .
If √3 tan 2θ – 3 = 0, then find the value of tanθ secθ – cosθ where 0 < θ < 90°
If cos θ + sec θ = √2, then the value of cos³ θ+ sec³ θ is:
- If 8cos 2 A + 3sin 2 A = 9, then calculate the value of (sec 2 A - 1).
- If sin (3A − 2B) = (√3/2) and cos (A + B) = (1/2), where 0° < A, B < 90°, then find the value of ‘A’.
- If cosec A = (2/√3), then what will be the value of tan⁴ A?
The minimum value of 9 cos2 θ + 36 sec2 θ is
- If 2 sin x = √3, then the value of x:
sin2 9 ° + sin2 10 ° + sin2 11 ° + sin2 12 ° + ……… + sin2 81 ° = ?
...If tan A = 1 and tan B = √ 3, what is the value of cos A . cos B - sin A . sin B?
If P, Q, R, S are the vertex of a cyclic quadrilateral. Find the value of
`1/sec p` + `1/sec q` + `1/sec r` +`1/sec s`
...