0% found this document useful (0 votes)
37 views7 pages

Java Abstract and Static Class Concepts

The document contains questions related to abstract classes, inner classes, static classes in Java. 1. The first question asks about the output of a code with a parent and child class where the child calls the parent constructor. The output would be 1 2. 2. The second question asks which statement about abstract classes is incorrect. The answer is an abstract class can be initiated by new operator. 3. The third question asks which statement about abstract classes is false. The answer is a class can inherit from multiple abstract classes. This summarizes the key questions and answers in the document around abstract classes, inner classes and static classes in Java.

Uploaded by

Chirag Ahuja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views7 pages

Java Abstract and Static Class Concepts

The document contains questions related to abstract classes, inner classes, static classes in Java. 1. The first question asks about the output of a code with a parent and child class where the child calls the parent constructor. The output would be 1 2. 2. The second question asks which statement about abstract classes is incorrect. The answer is an abstract class can be initiated by new operator. 3. The third question asks which statement about abstract classes is false. The answer is a class can inherit from multiple abstract classes. This summarizes the key questions and answers in the document around abstract classes, inner classes and static classes in Java.

Uploaded by

Chirag Ahuja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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. [Link](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.

44. What will be the output of the below program?


1
2
3abstract class X
4{
    public X()
5
    {
6
        [Link]("ONE");
7    }
8     
9    abstract void abstractMethod();
10}
11 
12class Y extends X
13{
14    public Y()
15    {
16        [Link]("TWO");
17    }
18     
19    @Override
20    void abstractMethod()
21    {
22        [Link]("THREE");
23    }
}
24
 
25
public class MainClass
26{
27    public static void main(String[] args)
28    {
29        X x = new Y();
30         
31        [Link]();
32    }
33}
Answer :
ONE
TWO
THREE

5: What will be the output of the following Java code?

1. class A
2. {
3. public int i;
4. protected int j;
5. }
6. class B extends A
7. {
8. int j;
9. void display()
10. {
11. super.j = 3;
12. [Link](i + " " + j);
13. }
14. }
15. class Output
16. {
17. public static void main(String args[])
18. {
19. B obj = new B();
20. obj.i=1;
21. obj.j=2;
22. [Link]();
23. }
24. }
a) 1 2
b) 2 1
c) 1 3
d) 3 1

6: What will be the output of the following code?

public class Test


{
public int a=0;
class innerClass
{
public int a=1;
void innermethod(int x)
{
[Link](“value of x = ” + x);
[Link](“value of this.x = ” + this.x);
[Link](“value of [Link].x = ” + Test.T=this.x);
}
}
}
public static void main( String args[] )
{
Test t=new Test();
[Link] im=[Link] innerClass();
[Link](55);
}

a)

value of x = 55

value of this.x = 0

value of [Link].x = 1

b)

value of x = 1

value of this.x = 0

value of [Link].x = 55

c)

value of x = 55

value of this.x = 1

value of [Link].x = 0

d)

value of x = 0

value of this.x = 55

value of [Link].x = 1

7. What will be the output of the following Java program?

1. class access
2. {
3. static int x;
4. void increment()
5. {
6. x++;
7. }
8. }
9. class static_use
10. {
11. public static void main(String args[])
12. {
13. access obj1 = new access();
14. access obj2 = new access();
15. obj1.x = 0;
16. [Link]();
17. [Link]();
18. [Link](obj1.x + " " + obj2.x);
19. }
20. }
a) 1 2
b) 1 1
c) 2 2
d) Compilation Error

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;
        [Link](x);
    }
    static
    {
        int x = 10;
        [Link](x + " ");
    }
}

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

11. What will be the output of the following Java program?

1. class static_out
2. {
3. static int x;
4. static int y;
5. void add(int a , int b)
6. {
7. x = a + b;
8. y = x + b;
9. }
10. }
11. class static_use
12. {
13. public static void main(String args[])
14. {
15. static_out obj1 = new static_out();
16. static_out obj2 = new static_out();
17. int a = 2;
18. [Link](a, a + 1);
19. [Link](5, a);
20. [Link](obj1.x + " " + obj2.y);
21. }
22. }
a) 7 7
b) 6 6
c) 7 9
d) 9 7

12. What will be the output of the following Java program?

1. class Output
2. {
3. public static void main(String args[])
4. {
5. int a1[] = new int[10];
6. int a2[] = {1, 2, 3, 4, 5};
7. [Link]([Link] + " " + [Link]);
8. }
9. }
a) 10 5
b) 5 10
c) 0 10
d) 0 5

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. What will be the output of the following ?


abstract class Bank{    
abstract int getRateOfInterest();    
}    
class SBI extends Bank{    
int getRateOfInterest(){return 7;}    
}    
class PNB extends Bank{    
int getRateOfInterest(){return 8;}    
}    
    
class TestBank{    
public static void main(String args[]){    
Bank b;  
b=new SBI();  
[Link]("Rate of Interest is: "+[Link]()+" %");    
b=new PNB();  
[Link]("Rate of Interest is: "+[Link]()+" %");    
}}  

Ans.  
Rate of Interest is: 7 %
Rate of Interest is: 8 %

Common questions

Powered by AI

While abstract classes cannot be instantiated directly, they can have constructors to initialize fields common to all subclasses, offering a unified initialization strategy. This offers a structural advantage by ensuring every subclass receives base class constructor logic, helping maintain integrity and reducing redundancy in code where multiple subclasses share the same initialization pattern .

Abstract classes in Java serve to provide a template for other classes. They can contain implemented methods as well as abstract methods that must be implemented by subclasses. An abstract class cannot be instantiated directly using the 'new' operator, but they can have constructors which can be called upon object creation of subclasses. An abstract class can also be declared without containing any abstract method. If a subclass inherits an abstract class and does not provide implementations for all abstract methods, it should also be declared abstract .

In Java, static initialization blocks are executed when the class is loaded, whereas instance initialization blocks run when an instance of the class is created. The document example with `static { int x = 10; ... }` runs before the main method is executed due to its static nature. This leads to an understanding that the static initialization block outputs its print statement before `main` method statements. Hence, the output is '10 20' not '20 10', revealing execution priority of blocks over instance initialization .

Access modifiers in Java determine the visibility of class members. In the provided example, class A declares a public integer 'i' and a protected integer 'j'. Class B, which extends A, has access to 'i' directly because it is public. The protected member 'j' is accessible within B only through the super keyword, not directly when B redeclares a different integer 'j'. As a result, the output for the 'display()' method in class B shows '1 2', demonstrating isolated scopes for 'j'.

Inner class methods can access both their own instance variables and those of the enclosing class due to their contextual association within an instance of the outer class. This ability enables inner classes to functionally enhance the flexibility of the outer class, offering an integrated approach to handling operations that require cooperative access to both scopes, simplifying data manipulation without restrictive exposure .

Polymorphism allows methods to be invoked on objects of different classes at runtime. In the rate of interest example, abstract class `Bank` defines an abstract method `getRateOfInterest`, which is implemented differently by `SBI` and `PNB` classes. When a `Bank` reference points to `SBI` or `PNB`, `getRateOfInterest()` method calls SBI or PNB’s implementation. This decouples reference and method execution, showcasing polymorphism as the program dynamically resolves method during runtime .

The 'super' keyword in Java is used to call the constructor of the parent class from the child class. In the given code, class B extends class A, and within B's constructor, 'super()' calls the constructor of class A, initializing i and j to 1 and 2 respectively. This ensures that when the class B instance is created, it inherits and initializes parent class properties correctly. The correct output for the main class is '1 2', indicating that 'super()' is used correctly to inherit parent properties .

In Java, arrays are objects that contain a fixed number of values of a single type. The declaration `int a1[] = new int[10]` creates an array with space for 10 integers, initializing them to default values, here 0. Conversely, `int a2[] = {1, 2, 3, 4, 5}` initializes an array with specified elements directly. These allocations define the `length` property which is actively used to avoid out-of-bounds errors. The disparity between initialized lengths emphasizes the need for dynamic considerations based on use-case requirements to optimize memory and performance .

Static variables belong to the class, not to any specific instance, and as such, they maintain a single shared value across all instances. In the given example, every increment operation using the 'increment' method modifies the same `x` variable. First, `x` is set to 0 by obj1, then incremented twice, once by obj1 and once by obj2. Both obj1.x and obj2.x return the result '2', illustrating shared static state .

A member inner class in Java is a class defined within a body of an outer class. It can access all fields, methods, and the constructors of its containing outer class. They are associated with an instance of the outer class, meaning they need an instance of the outer class to be instantiated. This provides a logical grouping for classes and an ease of encapsulation, as well as access to outer class members .

You might also like