What is the purpose of the "final" keyword in Java?
A. To declare an abstract method
B. To create a static class
C. To indicate that a class, method, or variable cannot be further extended, overridden, or modified
D. To indicate that a method is abstract
Select an option to see the answer and solution.
In Java, can an abstract class have constructors?
A. Only if the constructors are marked as "private"
B. Yes, an abstract class can have constructors
C. Only if the constructors are marked as "static"
D. None of These
Select an option to see the answer and solution.
What is the result of the following code snippet?abstract class MyAbstract { MyAbstract() { System.out.println("Constructor"); } } class MyClass extends MyAbstract { public static void main(String[] args) { MyClass obj = new MyClass(); } }
A. MyClass
B. Runtime exception
C. Constructor
D. Compilation error
Select an option to see the answer and solution.
In Java, can an interface have static methods with implementations?
A. No, interfaces cannot have static methods
B. Only if the methods are marked as "private"
C. Only if the methods are marked as "final"
D. Yes, starting from Java 8, interfaces can have static methods with implementations
Select an option to see the answer and solution.
What is the result of the following code snippet?interface MyInterface { static void myMethod() { System.out.println("Static Method"); } } class MyClass implements MyInterface { public static void main(String[] args) { MyInterface.myMethod(); } }
A. Static Method
B. Compilation error
C. Runtime exception
D. MyClass
Select an option to see the answer and solution.
In Java, can an abstract class have private methods with implementations?
A. Only if the methods are marked as "final"
B. Only if the methods are marked as "static"
C. No, abstract classes cannot have private methods
D. Yes, starting from Java 9, abstract classes can have private methods with implementations
Select an option to see the answer and solution.
What is the result of the following code snippet?abstract class MyAbstract { private void myMethod() { System.out.println("Private Method"); } } class MyClass extends MyAbstract { public static void main(String[] args) { MyClass obj = new MyClass(); obj.myMethod(); } }
A. Private Method
B. Runtime exception
C. Compilation error
D. MyClass
Select an option to see the answer and solution.
In Java, can an abstract class have static methods with implementations?
A. Only if the methods are marked as "private"
B. Yes, an abstract class can have static methods with implementations
C. Only if the methods are marked as "final"
D. None of These
Select an option to see the answer and solution.
What is the result of the following code snippet?abstract class MyAbstract { static void myMethod() { System.out.println("Static Method"); } } class MyClass extends MyAbstract { public static void main(String[] args) { MyAbstract.myMethod(); } }
A. MyClass
B. Runtime exception
C. Static Method
D. Compilation error
Select an option to see the answer and solution.
In Java, can an abstract class have default methods with implementations?
A. No, abstract classes cannot have default methods
B. Only if the methods are marked as "private"
C. Only if the methods are marked as "final"
D. Yes, starting from Java 8, abstract classes can have default methods with implementations
Select an option to see the answer and solution.
Given the following piece of code:
public class School{
public abstract double numberOfStudent();
}
which of the following statements is true?
A. The keywords public and abstract cannot be used together.
B. The method numberOfStudent() in class School must have a body.
C. You must add a return statement in method numberOfStudent().
D. Class School must be defined abstract.
Select an option to see the answer and solution.
Which of the following class definitions defines a legal abstract class?
A. class A { abstract void unfinished() { } }
B. class A { abstract void unfinished(); }
C. abstract class A { abstract void unfinished(); }
D. public class abstract A { abstract void unfinished(); }
Select an option to see the answer and solution.
Which of the following declares an abstract method in an abstract Java class?
A. public abstract method();
B. public abstract void method();
C. public void abstract Method();
D. public void method() {}
E. public abstract void method() {}
Select an option to see the answer and solution.
Which of the following statements regarding abstract classes are true?
A. An abstract class can be extended.
B. A subclass of a non-abstract superclass can be abstract.
C. A subclass can override a concrete method in a superclass to declare it abstract.
D. An abstract class can be used as a data type.
E. All of the above
Select an option to see the answer and solution.
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. 1 and 2
B. 2 and 4
C. 3 and 4
D. 1 and 3
E. 2 and 3
Select an option to see the answer and solution.
Which of the following is a correct interface?
A. interface A { void print() { } }
B. abstract interface A { print(); }
C. abstract interface A { abstract void print(); { }}
D. interface A { void print(); }
Select an option to see the answer and solution.
Determine 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. Nothing.
B. b is an instance of A.
C. b is an instance of C.
D. b is an instance of A followed by b is an instance of C.
Select an option to see the answer and solution.
Given the following piece of code:
public interface Guard{
void doYourJob();
}
abstract public class Dog implements Guard{ }
which of the following statements is correct?
A. This code will not compile, because method doYourJob() in interface Guard must be defined abstract.
B. This code will not compile, because class Dog must implement method doYourJob() from interface Guard.
C. This code will not compile, because in the declaration of class Dog we must use the keyword extends instead of implements.
D. This code will compile without any errors.
Select an option to see the answer and solution.
In Java, declaring a class abstract is useful
A. To prevent developers from further extending the class.
B. When it doesn't make sense to have objects of that class.
C. When default implementations of some methods are not desirable.
D. To force developers to extend the class not to use its capabilities.
E. When it makes sense to have objects of that class.
Select an option to see the answer and solution.
What will be the output?
interface A{
public void method();
}
class One{
public void method(){
System.out.println("Class One method");
}
}
class Two extends One implements A{
public void method(){
System.out.println("Class Two method");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method();
}
}A. will print Class One method
B. will print Class Two method
C. compiles fine but print nothing
D. Compilation Error
E. None of these
Select an option to see the answer and solution.