Interfaces And Abstract Classes - Study Mode
[#226] What will be the output when the following program is compiled and executed? abstract class TestAbstract{
String my_name
String myName(){
my_name = "Examveda"
return my_name
}
abstract void display()
}
public class Test extends TestAbstract{
void display(){
String n = myName()
System.out.print("My name is "+ n)
}
public static void main(String args[]){
Test t = new Test()
t.display()
}
}
Correct Answer
(A) Program will compile and execute successfully and prints
Explanation
Solution: The options B, C and D are incorrect options as in Java we can declare an abstract class comprising of abstract and non-abstract methods that will not lead to any compilation error. Therefore, option A is the correct answer implying that the 't' instance of Test class invokes the display method, which is implemented in the Test class. The display method invokes myName() method declared int the TestAbstract class and prints the name.
[#227] What happens if the following program is compiled and executed? interface MyInterface{
void display()
}
interface MySubInterface extends MyInterface{
void display()
}
public class Test implements MySubInterface{
public void display(){
System.out.print("Welcome to Examveda.")
}
public static void main(String args[]){
Test t = new Test()
t.display()
}
}
Correct Answer
(C) The code will compile and execute successfully showing the output Welcome to Examveda.
Explanation
Solution: The program will compile and execute successfully as you can declare methods with same name in an interface and the method of either interface can be used, implying the option a is incorrect. The option B and D are incorrect as the methods of an interface are implicitly public and abstract.
[#228] What will be the output of the following Java program? class Output
{
public static void main(String args[])
{
double x = 3.1
double y = 4.5
double z = Math.max( x, y )
System.out.print(z)
}
}
Correct Answer
(D) 4.5
[#229] What will be the output of the following Java code? class Output
{
public static void main(String args[])
{
Double i = new Double(257.578123456789)
float x = i.floatValue()
System.out.print(x)
}
}
Correct Answer
(C) 257.57812
[#230] What is the value of "d" in the following Java code snippet? double d = Math.round ( 2.5 + Math.random() )
Correct Answer
(B) 3