Object Oriented Programming using Java Questions and Answers – Base Class

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Base Class”.

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

class Worker
{  
    float salary=98000;  
}  
class Manager extends Worker
{  
    int bonus=25000;  
    public static void main(String args[])
    {  
        Manager p=new Manager();  
        System.out.println(+p.salary);  
    }  
}

a) 98000.0
b) 98000
c) Compilation error
d) 25000
View Answer

Answer: a
Explanation: When you inherit from an existing class, you can reuse methods and fields of the inherited class. In this case, the variable ‘salary’ which is of datatype float is a part of the class ‘Worker’. Hence, it can be accessed as the ‘extend’ keyword has been used by the current class.
Output:

$ javac Manager.java
$ java Manager
98000.0

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

advertisement
advertisement
class Base
{  
    void player1()
    {
        System.out.println("player1");
    }  
}  
class Intermediate extends Base
{  
    void player2()
    {
        System.out.println("player2");
    }  
}  
class SubClass extends Intermediate
{  
    void player3()
    {
        System.out.println("player3");
    }  
    public static void main(String args[])
    {  
        SubClass d=new SubClass();  
        d.player3();  
    }
}

a) player3
b)

player3
player2
player1

c) Compilation error
d)

advertisement
player1
player2
player3
View Answer
Answer: a
Explanation: A class can also be derived from one class, which is already derived from another base class. This is called Multilevel inheritance. In the above program, SubClass inherits the properties of Intermediate, which in turn has inherited the properties of Base class.
Output:

$ javac SubClass.java
$ java SubClass
player3

advertisement

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

class Base 
{
    public void display() 
    {
        System.out.println("Base class called");
    }
} 
class Derived extends Base 
{
    public void display() 
    {
        System.out.println("Derived class called");
    }
    public static void main(String[] args) 
    {
        Base obj = new Derived();
        obj.display();
    }
}

a) Base class called
b) Derived class called
c)

Base class called
Derived class called

d)

Derived class called
Base class called
View Answer
Answer: b
Explanation: In the above program, ‘obj’ is a reference of Base type and refers to an object of Derived class. Functions are virtual by default in Java. So the run time polymorphism happens and show() in Derived is called.
Output:

$ javac DerivedClass.java
$ java DerivedClass
Derived class called

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

class Tier1
{
    public void Display() 
    {
        System.out.println("Tier1");
    }    
}
class Tier2 extends Tier1 
{
    public void Display() 
    {
        System.out.println("Tier2");
    } 
}  
class Tier3 extends Tier2 
{
    public void Display() 
    {
        super.super.Display(); 
        System.out.println("Tier3");
    } 
    public static void main(String[] args) 
    {
        Tier3 obj = new Tier3();
        obj.Display();
    } 
}

a) Tier1
b)

Tier3
Tier2
Tier1

c) Tier3
d) Compilation Error
View Answer

Answer: d
Explanation: The ‘super’ keyword in Java cannot be nested as mentioned in the above program. The member functions and data members of Tier1 can be accessed only by tier2 using the ‘super’ keyword.
Output:

$ javac Tier3.java
$ java Tier3
java.lang.RuntimeException: Uncompilable source code

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

class SampleClass1
{
    System.out.println("Class1");
}
class SampleClass2
{
    System.out.println("Class2");
} 
class Base extends SampleClass2, SampleClass1
{
    void display()
    {
        System.out.println("Inherited");
    }
    public static void main(String[] args) 
    {
        Base obj = new Base();
        obj.Display();
    }
}

a) Inherited
b)

Class2
Class1
Inherited

c)

Inherited
Class1
Class2

d) Compilation error
View Answer

Answer: d
Explanation: In Java, a class can not extend more than one class. Class Base is extending two classes – Class SampleClass2 and Class SampleClass1. It is a compile time error as Java doesn’t support multiple inheritance.
Output:

$ javac Base.java
$ java Base
Uncompilable source code

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

class Base 
{
    int num1;
}    
class Derived extends Base 
{
    int num2;
    void display() 
    {
        super.num1 = num2 + 1;
        System.out.println(j + " " + i);
    }
    public static void main(String args[])
    {
        Derived obj = new Derived();
        obj.num1 = 1;
        obj.num2 = 2;   
        obj.display();     
    }
}

a) 2 2
b) 3 3
c) 2 3
d) 3 2
View Answer

Answer: c
Explanation: The most common use of the super keyword is to eliminate the confusion between super-classes and sub-classes that have methods with the same name. In this case, the variable ‘num1’ is a part of the class Base and since the value of the variable ‘num2’ is 2, ‘i’ results in 3.
Output:

$ javac Derived.java
$ java Derived
2 3

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

class Base
{
    static void staticMethod()
    {
        System.out.println("Class X");
    }
}
class Derived extends Base
{
    static void staticMethod()
    {
        System.out.println("Class Y");
    }
    public static void main(String[] args)
    {
        Base.staticMethod();
    }
}

a) Class X
b) Class Y
c) Compilation error
d)

Class X
Class Y
View Answer
Answer: a
Explanation: In this case, ‘Base’ is the super-class and ‘Derived’ is the sub-class. Since static methods do not require an object to be called, the member function staticMethod() can be called using the class name. As the class name mentioned in the output is Base, the print statement in Base class is executed.
Output:

$ javac Derivedclass.java
$ java Derivedclass
Class X

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

class Base 
{ 
    public void print_for() 
    { 
        System.out.println("1"); 
    } 
}  
class Derived extends Base 
{ 
    public void print_fr() 
    { 
        System.out.println("2"); 
    } 
    public static void main(String[] args) 
    { 
        Derived g = new Derived();  
        g.print_for();    
    } 
}

a) 2
b) 1
c) Compilation error
d) -1
View Answer

Answer: a
Explanation: The object ‘g’ belongs to the class ‘Derived’. Higher priority is given to the functions in the classtype belonging to the object incase of multiple functions having same prototype in different classes. The object ‘g’ is used to call the function print_for(). As Derived is a subclass and the object ‘g’ belongs to it, 2 will be the output.
Output:

$ javac DerivedClass1.java
$ java DerivedClass1
2

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

class BaseClass 
{
    public static void main(String[] args) 
    {
        String s = new String("HelloWorld");
        Class strClass = s.getClass();
        System.out.println("Base class of String: " + strClass.getSuperclass());
    }
}

a) Compilation error
b) Null
c) class java.io
d) class java.lang.Object
View Answer

Answer: d
Explanation: All classes in Java are inherited from Object class. In the same way, even the String class is a part of Object class. Therefore, the Object class is the superclass to all other classes and it defines methods that all its subclasses share.
Output:

$ javac BaseClass.java
$ java BaseClass
class java.lang.Object

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

class Sum
{
    int z;
    public void addition(int x, int y) 
    {
        z = x + y;
        System.out.println(z);
    }
}
class Product extends Sum 
{
    public void multiplication(int x, int y) 
    {
        z = x * y;
        System.out.println(z);
    }    
public static void main(String args[]) 
    {
        int a = 2, b = 15;
        Product obj = new Product();
        obj.addition(a, b);
        obj.multiplication(a, b);
    }
}

a)

17
30

b)

30
17

c) Compilation error
d)

30
30
View Answer
Answer: a
Explanation: In this example, you can observe two classes namely Product and Sum. Using ‘extends’ keyword, the Product class inherits the method addition() of Sum class. In the given program, when an object to Product class is created, a copy of the contents of the superclass Sum is made within it. Hence addition() and multiplication() methods can be accessed using the created object.
Output:

$ javac Product.java
$ java Product
17
30

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.