0
Q:

public abstract class Shape {

private int x;

private int y;

public abstract void draw();

public void setAnchor(int x, int y) {

this.x = x;

this.y = y;

}

}

Which two classes use the Shape class correctly?

A) public class Circle implements Shape { private int radius; B) public abstract class Circle extends Shape { private int radius; }
C) public class Circle extends Shape { private int radius; public void draw(); D) None

Answer:   B) public abstract class Circle extends Shape { private int radius; }



Explanation:

Only B is true

Subject: Java
Q:

What is the difference between creating a thread by extending Thread class and by implementing Runnable interface? Which one should prefer?

Answer

When creating a thread by extending the Thread class, it is not mandatory to override the run method (If we are not overriding the run method , it is useless), because Thread class have already given a default implementation for run method. But if we are implementing Runnable , it is mandatory to override the run method. The preferred way to create a thread is by implementing Runnable interface, because it give loose coupling.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1567
Q:

What are the the different ways for creating a thread?

Answer

A thread can be created by subclassing Thread, or by implementing the Runnable interface.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1594
Q:

How does a try statement determine which catch clause should be used to handle an exception?

Answer

When an exception is thrown , the catch block of the try statement are examined in the order in which they appear. The first catch block that is capable of handling the exception is executed. The remaining catch blocks are ignored

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1580
Q:

What is static initializer block? What is its use?

Answer

A static initializer block is a block of code that declares with the static keyword. It normally contains the block of code that must execute at the time of class loading. The static initializer block will execute only once at the time of loading the class only.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1802
Q:

What are the restrictions when overriding a method ?

Answer

Overridden methods must have the same name, argument list, and return type (i.e., they must have the exact signature of the method we are going to override, including return type.)


The overriding method cannot be less visible than the method it overrides( i.e., a public method cannot be override to private).


The overriding method may not throw any exceptions that may not be thrown by the overridden method

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2513
Q:

Can we compile a java program without main?

Answer

Yes, we can. In order to compile a java program, we don't require any main method. But to execute a java program we must have a main in it (unless it is an applet or servlet). Because main is the starting point of a java program.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1706
Q:

What is inheritance?

Answer

Inheritance is the ability to create new classes based on existing classes. It is useful to reuse existing code.

Report Error

View answer Workspace Report Error Discuss

0 2112
Q:

What is encapsulation?

Answer

Encapsulation describes the ability of an object to hide its data and methods from the rest of the world 

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1847