0% found this document useful (0 votes)
64 views

Abstract, Inner, Static Classes Questions: Class Public Public

The document contains 15 questions related to abstract classes, inner classes, and static methods in Java. Question 1 asks about the output of a code sample that creates an instance of class B which extends class A, and prints the values of inherited fields. The correct answer is 1 2. Question 2 asks which statement about abstract classes is false; the answer is that an abstract class can be instantiated with the new operator.

Uploaded by

Chirag Ahuja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Abstract, Inner, Static Classes Questions: Class Public Public

The document contains 15 questions related to abstract classes, inner classes, and static methods in Java. Question 1 asks about the output of a code sample that creates an instance of class B which extends class A, and prints the values of inherited fields. The correct answer is 1 2. Question 2 asks which statement about abstract classes is false; the answer is that an abstract class can be instantiated with the new operator.

Uploaded by

Chirag Ahuja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Abstract, inner, static classes questions

1. What will be the output of the following Java code?

2. class A
3. {
4. public int i;
5. public int j;
6. A()
7. {
8. i = 1;
9. j = 2;
10. }
11. }
12. class B extends A
13. {
14. int a;
15. B()
16. {
17. super();
18. }
19. }
20. class super_use
21. {
22. public static void main(String args[])
23. {
24. B obj = new B();
25. System.out.println(obj.i + " " + obj.j)
26. }
27. }

a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error

2. 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

3. Which of the following is FALSE about abstract classes in Java


(A) If we derive an abstract class and do not implement all the abstract
methods, then the derived class should also be marked as abstract using
‘abstract’ keyword
(B) Abstract classes can have constructors
(C) A class can be made abstract without any abstract method
(D) A class can inherit from multiple abstract classes.

4. Can We Instantiate an Abstract Class in Java?

5: What is Inner Class in Java? Explain with help of an example.

6: Why can outer Java classes access inner class private members?

7. What is the main difference between static and non-static nested classes? Also provide an
example for each of them?

8. What are member inner classes in java?

9. Create an abstract class 'Animals' with two abstract methods 'cats' and 'dogs'. Now create a class 'Cats'
with a method 'cats' which prints "Cats meow" and a class 'Dogs' with a method 'dogs' which prints "Dogs
bark", both inheriting the class 'Animals'. Now create an object for each of the subclasses and call their
respective methods.

10. what is the output of this question?


class Test1 {
public
    static void main(String[] args)
    {
        int x = 20;
        System.out.println(x);
    }
    static
    {
        int x = 10;
        System.out.print(x + " ");
    }
}

Option
A) 10 20 ..
B) 20 10
C) 10 10
D) 20 20

11. Why non-static variable cannot be referenced from a static method in Java?
12. What will happen if we do not override all abstract methods in
subclass?

13. What will happen if we do not provide implementation for all


abstract methods in subclass?

14. Why abstract class has constructor even though you cannot create
object?

15. Why should we create reference to superclass (abstract class


reference)?

You might also like