Q: What will be the output of the following Java program?
interface calculate
{
int VAR = 0;
void cal(int item);
}
class display implements calculate
{
int x;
public void cal(int item)
{
if (item<2)
x = VAR;
else
x = item * item;
}
}
class interfaces
{
public static void main(String args[])
{
display[] arr=new display[3];
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x);
}
}
A: 0 0 4
Q: Suppose the abstract class Message is defined belowA concrete subclass of Message, FrenchMessage, is defined. Which methods must FrenchMessage define?
public abstract class Message
{
private String value;
public Message(String initial)
{
value = initial;
}
public String getMessage()
{
return value;
}
public abstract String translate();
}
A: translate() only
Q: Predict the output of the following program.
abstract class demo
{
public int a;
demo()
{
a = 10;
}
abstract public void set();
abstract final public void get();
}
class Test extends demo
{
public void set(int a)
{
this.a = a;
}
final public void get()
{
System.out.println("a = " + a);
}
public static void main(String[] args)
{
Test obj = new Test();
obj.set(20);
obj.get();
}
}
A: Compilation error
Q: Determine the output of the code.
interface A { }
class C { }
class D extends C { }
class B extends D implements A { }
public class Test extends Thread
{
public static void main(String[] args)
{
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
A: b is an instance of Ab is an instance of C
Q: Determine the output of the following code:
interface A { }
class C { }
class D extends C { }
class B extends D implements A { }
public class Test extends Thread
{
public static void main(String[] args)
{
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
A: b is an instance of A followed by b is an instance of C
Q: Which of the following class definitions defines a legal abstract class?
A: abstract class A { abstract void unfinished(); }
Q: Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct?1. A a = new A();2. A a = new B();3. B b = new A();4. B b = new B();
A: 2 and 4
Q: What is the output of the following code?
interface X
{
int i = 5;
}
class Y implements X
{
void f()
{
i = 10;
System.out.println("i="+i);
}
}
public class Main {
public static void main(String[] args) {
Y obj = new Y();
obj.f();
}
}
A: Compile time error
Q: From the given piece of code, which of the following statements is correct?
public interface Guard{
void doYourJob();
}
abstract public class Dog implements Guard{ }
A: This code will compile without any errors.
Q: Which statement about methods in an interface is true?
A: All methods in an interface are automatically public.
Q: Which of these methods can be used to obtain the command name for invoking an ActionEvent object?
A: getActionCommand()
Q: What will be the output of the below code?
interface Walk {
public default int getSpeed()
{
return 5;
}
}
interface Run {
public default int getSpeed()
{
return 10;
}
}
public class Animal implements Walk,Run {
public static void main(String args [ ] ) {
Animal an = new Animal();
System.out.println(an.getSpeed());
}
}
A: Compilation fails due to an error at line 13
Q: In Java, declaring a class abstract is useful:
A: When it doesn't make sense to have objects of that class.
Q: What is the output of the following code?
interface X
{
void f();
}
interface Y extends X
{
void g();
}
class C implements Y
{
public void f() {
System.out.println("Hello");
}
public void g() {
System.out.println("World");
}
}
public class Main {
public static void main(String[] args) {
C o = new C();
o.f();
o.g();
}
}
A: HelloWorld