Java Abstract Class MCQ Questions
This section focuses on the "Abstract class" in Java programming language. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.
1. A class which is declared with the ________ keyword is known as an abstract class in Java.
A. abstract
B. util
C. extends
D. None of the above
View Answer
Ans : A
Explanation: A class which is declared with the abstract keyword is known as an abstract class in Java.
2. Abstract class can have constructors and static methods?
A. TRUE
B. FALSE
C. Abstract class can have constructors but can not have static methods.
D. Abstract class can not have constructors but can have static methods.
View Answer
Ans : A
Explanation: It can have constructors and static methods also.
3. What is the syntax of abstract class in java?
A. abstract A{}
B. abstract class A
C. abstract class A{}
D. abstract class A[]
View Answer
Ans : C
Explanation: The syntax of abstract class in java is abstract class A{}.
4. Which of these is not abstract?
A. Thread
B. AbstractList
C. List
D. None of the Mentioned
View Answer
Ans : A
Explanation: Thread is not an abstract class.
5. A method which is declared as abstract and does not have implementation is known as an _____________?
A. Abstract Interface
B. Abstract Thread
C. Abstract List
D. abstract Method
View Answer
Ans : D
Explanation: A method which is declared as abstract and does not have implementation is known as an abstract method.
6. Which of these packages contains abstract keyword?
A. java.lang
B. java.util
C. java.io
D. java.system
View Answer
Ans : A
Explanation: java.lang packages contains abstract keyword.
7. An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : A
Explanation: Yes, An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
8. Which of these is not a correct statement?
A. Every class containing abstract method must be declared abstract
B. Abstract class defines only the structure of the class not its implementation
C. Abstract class can be initiated by new operator
D. Abstract class can be inherited
View Answer
Ans : C
Explanation: Abstract class cannot be directly initiated with new operator, Since abstract class does not contain any definition of implementation it is not possible to create an abstract object.
9. What will be output for the folllowing code?
abstract class Bank {
private abstract void withdraw(); // Line 1
abstract void deposit();
public void balance(){} //Line 2
}
class office extends Bank{ // Line 3
void deposit() { // Line 4
// TODO Auto-generated method stub
}
}
A. Compilation error in Line 1(abstract method cannot be private)
B. Compilation error in Line 2(abstract class cannot have concrete method)
C. Compilation error in Line 3(abstract class cannot be extended)
D. Compilation error in Line 4(deposit method should have public access modifier)
View Answer
Ans : A
Explanation: Yes, you are right!! As private method can't be overridden and abstract method should be overridden in child classes, so this line will give compilation error.
10. What will be output for the folllowing code?
abstract class Bank
{
private String bankName;
Bank(String bankName)
{
this.bankName = bankName;
}
public String getBankName()
{
return bankName;
}
}
class office extends Bank {
office() {
super("Axis Bank");
}
public static void main(String[] args) {
Bank bank = new office();
System.out.println(bank.getBankName());
}
}
A. Compilation error will occur because ""abstract class cannot have constructor""
B. Compilation error will occur because ""abstract class must have an abstract method""
C. Compilation error will occur while invoking the super class constructor
D. Code will be compiled successfully
View Answer
Ans : D
Explanation: As there is no syntax error, so the code will compile successfully.
Discussion