Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 801–810 of 958
Topics in Java Programming
Q.801 Easy OOP in Java
Can a class extend multiple classes in Java?
A Yes, using multiple inheritance
B No, Java supports only single inheritance
C Yes, but only if they are abstract classes
D Yes, using the 'extends' keyword multiple times
Correct Answer:  B. No, Java supports only single inheritance
EXPLANATION

Java supports only single class inheritance to avoid the diamond problem. However, multiple interface implementation is allowed.

Take Test
Q.802 Medium OOP in Java
What is the output of the following code?
class Parent { void show() { System.out.println("Parent"); } }
class Child extends Parent { void show() { System.out.println("Child"); } }
public class Test { public static void main(String[] args) { Parent p = new Child(); p.show(); } }
A Parent
B Child
C Compile-time error
D Runtime error
Correct Answer:  B. Child
EXPLANATION

This demonstrates runtime polymorphism. Although the reference is of type Parent, the actual object is Child. The overridden show() method in Child class is executed.

Take Test
Q.803 Easy OOP in Java
Which keyword is used to achieve runtime polymorphism in Java?
A final
B static
C virtual
D None of the above
Correct Answer:  D. None of the above
EXPLANATION

Java achieves runtime polymorphism through method overriding using inheritance. Unlike C++, Java doesn't use the 'virtual' keyword; it's implicit for all non-final methods.

Take Test
Q.804 Easy OOP in Java
What happens when you try to instantiate an interface in Java?
A It creates an anonymous inner class instance
B It throws a compile-time error
C It throws a runtime error
D It works like a normal class instantiation
Correct Answer:  B. It throws a compile-time error
EXPLANATION

Interfaces cannot be instantiated directly. You must create a concrete class that implements the interface or use an anonymous inner class.

Take Test
Q.805 Medium OOP in Java
Which statement is true about the default access modifier (package-private) in Java?
A Members are accessible from any package
B Members are accessible only within the same package
C Members are accessible only within the same class
D Members are not accessible anywhere
Correct Answer:  B. Members are accessible only within the same package
EXPLANATION

The default (package-private) access modifier allows access to members from any class within the same package, but not from classes in other packages.

Take Test
Q.806 Easy OOP in Java
Which of the following correctly describes encapsulation?
A Wrapping data and methods together and hiding internal details
B Creating multiple classes with same functionality
C Extending one class from another
D Implementing an interface
Correct Answer:  A. Wrapping data and methods together and hiding internal details
EXPLANATION

Encapsulation is the bundling of data (variables) and methods into a single unit (class) while hiding the internal implementation details from the outside world. It is achieved using access modifiers.

Take Test
Q.807 Hard OOP in Java
What is the main difference between method overriding and method overloading?
A Overriding is for same class, overloading is for different classes
B Overriding changes method signature, overloading keeps it same
C Overriding is runtime polymorphism, overloading is compile-time polymorphism
D Overriding requires final keyword, overloading doesn't
Correct Answer:  C. Overriding is runtime polymorphism, overloading is compile-time polymorphism
EXPLANATION

Method overloading is compile-time (static) polymorphism with same method name but different parameters in the same class. Method overriding is runtime (dynamic) polymorphism where a child class provides a specific implementation of a parent method.

Take Test
Q.808 Medium OOP in Java
Consider the code:
abstract class Animal { abstract void sound(); void sleep() { System.out.println("Zzz"); } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
What can Dog objects do?
A Only call sound()
B Only call sleep()
C Call both sound() and sleep()
D Neither sound() nor sleep()
Correct Answer:  C. Call both sound() and sleep()
EXPLANATION

Dog inherits the concrete method sleep() from Animal and provides implementation for the abstract method sound(). Therefore, Dog objects can call both methods.

Take Test
Q.809 Hard OOP in Java
In Java, can you create an object of an abstract class?
A Yes, always
B No, never
C Yes, using anonymous inner class
D Only if it has concrete methods
Correct Answer:  C. Yes, using anonymous inner class
EXPLANATION

Abstract classes cannot be directly instantiated, but you can create objects using anonymous inner classes that implement the abstract methods.

Take Test
Q.810 Hard OOP in Java
What is the output of this code?
class A { int x = 10; }
class B extends A { int x = 20; }
public class Test { public static void main(String[] args) { A a = new B(); System.out.println(a.x); } }
A 10
B 20
C Compilation Error
D Runtime Error
Correct Answer:  A. 10
EXPLANATION

Variable shadowing occurs here. The reference type is A, so a.x accesses A's x field which is 10. Method overriding works with methods, not instance variables.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips