10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
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();
[Link](+[Link]);
}
}
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 [Link]
$ java Manager
98000.0
[Link] 1/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
2. What will be the output of the following Java code?
class Base
{
void player1()
{
[Link]("player1");
}
}
class Intermediate extends Base
{
void player2()
{
[Link]("player2");
}
}
class SubClass extends Intermediate
{
void player3()
{
[Link]("player3");
}
public static void main(String args[])
{
SubClass d=new SubClass();
d.player3();
}
}
a) player3
b)
Free 30-Day Java Certification Bootcamp is Live. Join Now!
player3
player2
player1
c) Compilation error
d)
player1
player2
[Link] 2/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
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 [Link]
$ java SubClass
player3
advertisement
3. What will be the output of the following Java code?
class Base
{
public void display()
{
[Link]("Base class called");
}
}
class Derived extends Base
{
public void display()
{
[Link]("Derived class called");
}
public static void main(String[] args)
[Link] 3/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
{
Base obj = new Derived();
[Link]();
}
}
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 [Link]
$ java DerivedClass
Derived class called
4. What will be the output of the following Java code?
class Tier1
{
public void Display()
{
[Link]("Tier1");
}
}
class Tier2 extends Tier1
{
public void Display()
{
[Link]("Tier2");
}
[Link] 4/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
}
class Tier3 extends Tier2
{
public void Display()
{
[Link]();
[Link]("Tier3");
}
public static void main(String[] args)
{
Tier3 obj = new Tier3();
[Link]();
}
}
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 [Link]
$ java Tier3
[Link]: Uncompilable source code
5. What will be the output of the following Java code?
class SampleClass1
{
[Link]("Class1");
}
class SampleClass2
{
[Link]("Class2");
}
[Link] 5/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
class Base extends SampleClass2, SampleClass1
{
void display()
{
[Link]("Inherited");
}
public static void main(String[] args)
{
Base obj = new Base();
[Link]();
}
}
a) Inherited
b)
Class2
Class1
Inherited
c)
Inherited
Class1
Class2
d) Compilation error
View Answer
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;
[Link](j + " " + i);
}
public static void main(String args[])
{
Derived obj = new Derived();
obj.num1 = 1;
obj.num2 = 2;
[Link] 6/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
[Link]();
}
}
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 [Link]
$ java Derived
2 3
7. What will be the output of the following Java code?
class Base
{
static void staticMethod()
{
[Link]("Class X");
}
}
class Derived extends Base
{
static void staticMethod()
{
[Link]("Class Y");
}
public static void main(String[] args)
{
[Link]();
}
}
a) Class X
b) Class Y
c) Compilation error
d)
[Link] 7/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
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 [Link]
$ java Derivedclass
Class X
8. What will be the output of the following Java code?
class Base
{
public void print_for()
{
[Link]("1");
}
}
class Derived extends Base
{
public void print_fr()
{
[Link]("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
[Link] 8/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
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 [Link]
$ 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 = [Link]();
[Link]("Base class of String: " + [Link]());
}
}
a) Compilation error
b) Null
c) class [Link]
d) class [Link]
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 [Link]
$ java BaseClass
class [Link]
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;
[Link](z);
[Link] 9/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
}
}
class Product extends Sum
{
public void multiplication(int x, int y)
{
z = x * y;
[Link](z);
}
public static void main(String args[])
{
int a = 2, b = 15;
Product obj = new Product();
[Link](a, b);
[Link](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:
[Link] 10/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
$ javac [Link]
$ 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.
« Prev - Object Oriented Programming using » Next - Object Oriented Programming using Java
Java Questions and Answers – Object Class Questions and Answers – Derived Class
advertisement
Recommended Articles:
1. Object Oriented Programming using Java Questions and Answers – Method Overloading
2. Object Oriented Programming using Java Questions and Answers – Wrapper Class
3. Object Oriented Programming using Java Questions and Answers – Object Class
4. Object Oriented Programming using Java Questions and Answers – Types of Constructors
5. Object Oriented Programming using Java Questions and Answers – Inner Class
6. Object Oriented Programming using Java Questions and Answers – Constructor Overloading
7. Object Oriented Programming using Java Questions and Answers – Constructors
8. Object Oriented Programming using C++ Questions and Answers – Base Class
9. Object Oriented Programming using Java Questions and Answers – Final Class
[Link] 11/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
10. Object Oriented Programming using Java Questions and Answers – Static Class
advertisement
Additional Resources:
Object Oriented Programming MCQ Questions
Java Programming MCQ Questions
Java Programming Examples
Java Programs on Classes and Objects
Java Applet Programs
Popular Pages:
Event Handling in Java with Examples
Java Matrix Programs
Java Programs on Collections
Sorting Algorithms in Java
Java Array Programs
Subscribe Sanfoundry Newsletter and Posts
Name*
[Link] 12/13
10/4/25, 7:36 AM Base Class - Java OOPs Questions and Answers - Sanfoundry
Email*
Subscribe
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for
over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C,
Full Stack and Scalable website designs.
You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join
my Telegram tech discussions.
If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn
more here.
About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact
© 2011-2025 Sanfoundry. All Rights Reserved.
[Link] 13/13