Computer Knowledge
Object-Oriented Programming
2,239 Questions
Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.
Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects
Object-Oriented Programming Questions
-
private
-
b.final
-
c.protected
-
d.don't use any keyword at all (make it default)
D
Correct answer
Explanation
In Java, a top-level class with no access modifier (default/package-private) is accessible only within its own package. Private cannot be used for top-level classes, final prevents inheritance but doesn't control access, and protected allows broader access.
-
Line 1, line 2 and line 3 only
-
b.Line 1 only
-
c.Line 1 and line 2 only
-
d.Line 2 only
C
Correct answer
Explanation
Abstract classes cannot be instantiated directly (true). Constructors cannot be abstract (true). Subclasses must implement abstract methods only if they are concrete - abstract subclasses can leave them undefined (false). Static methods cannot be abstract (false).
-
Visible only inside the package.
-
b.Visible only in the class and its subclass in the same package.
-
c.Visible in all classes in the same package and subclasses in other packages.
-
d.Visible only in the class where it is declared.
C
Correct answer
Explanation
Protected members are visible within the same package (like default) AND to subclasses in other packages. This combines package-level access with inheritance access. Private is class-only, default is package-only.
-
private
-
0
-
c. private OR protected
-
d. protected
-
Versioned oracle base
-
Versined obstacle base
-
versioned object
-
Versioning obj
-
Versioned oracle base
-
Versined obstacle base
-
versioned object
-
Versioning obj
B
Correct answer
Explanation
The Java compiler only inserts a default, no-argument constructor if the class does not define any constructors. If any constructor is explicitly defined, the compiler does not insert one.
-
The object itself
-
a copy of reference
-
the original reference
-
a reference to a copy
B
Correct answer
Explanation
In Java, object references are passed by value - meaning a copy of the reference is passed to the method, not the original reference itself or the object. The called method receives its own copy of the reference, which points to the same object. If the method reassigns the reference parameter, it doesn't affect the caller's reference - but modifications to the object's state are visible since both references point to the same object. Option B correctly captures this 'copy of reference' behavior.
B
Correct answer
Explanation
Constructors cannot be abstract because they're meant to be called when creating an instance of a class. An abstract class cannot be instantiated directly, so having an abstract constructor would be contradictory. When you inherit from an abstract class, you must call a concrete constructor from the base class. If constructors could be abstract, there would be no guaranteed way to initialize the base class. This is a fundamental rule in object-oriented programming.
B
Correct answer
Explanation
In Java, if a class contains one or more abstract methods, the class itself must be declared abstract. A non-abstract (concrete) class cannot have abstract methods because it cannot be instantiated - abstract methods have no implementation and must be implemented by concrete subclasses. This is a fundamental rule of Java's object-oriented design.
A
Correct answer
Explanation
According to the Java Language Specification, a source file is officially termed a 'compilation unit'. This is the formal terminology used throughout Java's documentation and specifications. Each compilation unit typically contains at most one public class or interface declaration and can include package and import statements.
-
Classes
-
Function
-
Method
-
Object
C
Correct answer
Explanation
In Java, functions are called 'methods' because they must belong to a class. C/C++ has standalone functions that exist independently of classes, while Java requires all code to be within classes. This terminology reflects Java's object-oriented design where everything exists within a class context.
B
Correct answer
Explanation
Java source files must use the .java extension as required by the Java compiler (javac). The .h extension is for C/C++ header files, and .class is the extension for compiled bytecode files produced by the compiler, not source files. Option C '>java' appears to be a typo or formatting error.
D
Correct answer
Explanation
When Java source code is compiled, each class is converted to bytecode and stored in a separate file with the .class extension. This bytecode file contains instructions that the JVM can execute. The filename matches the class name (case-sensitive).
-
Dependent
-
Neutral
-
Independent
-
None of the above
B
Correct answer
Explanation
Java is Architectural Neutral because the compiled bytecode can run on any processor architecture. The JVM abstracts the underlying hardware, allowing the same bytecode to execute on different platforms without recompilation. This is a key feature that enables Java's 'write once, run anywhere' capability.