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
-
Parent
-
Child
-
Both
-
None of these
A
Correct answer
Explanation
Static methods are bound at compile time based on reference type, not actual object type. Parent p = new Child(); p.staticMethod() calls Parent's version. This is because static methods belong to the class, not instances.
A
Correct answer
Explanation
Java allows declaring a derived class before its base class in source code. The compiler reads the entire file, so order doesn't matter. However, this is poor practice for readability. The statement is technically True.
B
Correct answer
Explanation
Static methods CANNOT be overridden - they can only be hidden. Overriding requires dynamic dispatch based on actual object type, but static methods are bound at compile time. If you define the same static method in a child class, it hides, not overrides.
-
return type should be different
-
access speciifer should be different
-
arguments should be different
-
the overloaded methods should throw a different excpetion
C
Correct answer
Explanation
Method overloading requires different parameter lists (type, count, or order). Return type, access specifier, and exception throws alone don't distinguish overloaded methods - the compiler would see them as duplicate methods.
-
Overriding method can not throw more generic exception than base method
-
Overriding method can throw new or broader checked exceptions
-
None of the above
-
Both a and b
A
Correct answer
Explanation
In Java, an overriding method in a subclass cannot throw broader or more generic checked exceptions than the overridden method in the superclass. Doing so violates the Liskov Substitution Principle and causes a compilation error.
-
Overloading
-
Overriding
-
Both 1 and 2
-
Dynamic polymorphism
A
Correct answer
Explanation
The println method in Java exhibits overloading because there are multiple versions of println that accept different parameter types (int, String, boolean, etc.), all sharing the same name but different signatures. Overriding requires inheritance and redefining a method in a subclass, which is not what println demonstrates.
-
Shared
-
Local
-
Both
-
None of the above
B
Correct answer
Explanation
Local object repository is simplest for basic record-and-run scenarios because it stores objects within the test itself, eliminating the need to manage external repository files. Shared repositories require more setup and maintenance coordination across multiple tests. The 'Both' option is misleading because using both types simultaneously would actually add complexity rather than simplify.
-
Overload
-
Load
-
Both A&B
-
None of the above
B
Correct answer
Explanation
The Load() method is specifically designed to load object repository files into memory for use during test execution. This method belongs to the ObjectRepositoryUtil object and accepts the file path as a parameter. The 'Overload' option is not a standard method for this purpose in QTP.
-
class aclass : public superclass
-
class aclass inherit superclass
-
class aclass <-superclass
-
None of the Above
A
Correct answer
Explanation
In C++, public inheritance is declared using a colon followed by the 'public' keyword and the base class name. Option A shows the correct syntax: 'class aclass : public superclass'. Options B and C use incorrect syntax not valid in C++.
-
class aClass{public:int x;};
-
/* A comment */
-
char x=12;
-
None of the Above
A
Correct answer
Explanation
The syntax class aClass { public: int x; }; is valid C++ for declaring a class with a public member. Option B is just a comment. Option C declares a variable, not a class definition. Therefore A is correct.
-
Constructor
-
Destructor
-
Constitutor
-
None of the Above
-
class aclass : public superclass
-
class aclass inherit superclass
-
class aclass <-superclass
-
None of the Above
-
class aClass{public:int x;};
-
/* A comment */
-
char x=12;
-
None of the Above
-
Constructor
-
Destructor
-
Constitutor
-
None of the Above
-
Constructor
-
Destructor
-
Constitutor
-
None of the Above
A
Correct answer
Explanation
A constructor is a special member function that initializes the data members of a class. It has the same name as the class and is automatically called when an object is created. Destructors clean up resources when objects are destroyed, and 'Constitutor' is not a valid C++ term.