Computer Knowledge
Object-Oriented Programming
2,686 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
A
Correct answer
Explanation
Constructors in object-oriented languages like Java and C# do not have a return type, not even void. They implicitly return the instance of the class being created. This is a fundamental characteristic that distinguishes constructors from regular methods.
B
Correct answer
Explanation
Constructors cannot be abstract (they must be implemented), static (they operate on instances), or native/synchronized (these modifiers don't apply to constructors). Constructors can be final in some languages to prevent subclassing, but the statement claims all modifiers are valid which is incorrect.
A
Correct answer
Explanation
Constructors can indeed have public, protected, or private access modifiers. Public constructors allow instantiation from anywhere, protected from subclasses and same package, and private restricts instantiation (used in singleton pattern). The default constructor's access matches the class's access modifier.
B
Correct answer
Explanation
Constructors MUST have the exact same name as their class. This is a fundamental rule in languages like Java and C#. The constructor name matches the class name exactly, including capitalization. This is how the compiler identifies constructors.
A
Correct answer
Explanation
Constructors can use this() to call another constructor in the same class (constructor chaining). This allows you to avoid code duplication by having one constructor do the common work and others call it with appropriate default parameters. The called constructor must have a different parameter list.
B
Correct answer
Explanation
When a constructor uses this() to call another constructor, it MUST be the first statement in the constructor body. This ensures initialization happens in the correct order - you can't do other work before delegating to another constructor. If it's not first, the compiler will generate an error.
A
Correct answer
Explanation
Every class in Java DOES have at least one constructor. If you don't explicitly define any constructor, the compiler provides a default no-arg constructor. Abstract classes also have constructors (they're called when concrete subclasses are instantiated). The statement is correct.
A
Correct answer
Explanation
If a class does not explicitly define any constructors, the Java compiler automatically provides a default, no-argument constructor with the same access modifier as the class.
A
Correct answer
Explanation
In Java, constructors can be overloaded (multiple constructors with different signatures in the same class), but they cannot be overridden because they are not inherited by subclasses.
B
Correct answer
Explanation
The default constructor's access modifier IS the same as the class modifier. If the class is public, the default constructor is public. If the class has package-private access, the default constructor is also package-private. This ensures the constructor is accessible wherever the class itself is accessible.
A
Correct answer
Explanation
When a constructor uses super() to call the parent class constructor, it MUST be the first statement. This ensures the parent class is properly initialized before the child class does its own initialization. The compiler enforces this rule and will complain if super() is not first.
-
"X extends Y" is correct if and only if X is a class and Y is an interface
-
"X extends Y" is correct if and only if X and Y are either both classes or both interfaces
-
"X extends Y" is correct if and only if X is an interface and Y is a class
-
None of these
B
Correct answer
Explanation
In Java, the extends keyword is used for both class inheritance and interface inheritance. A class can extend another class, and an interface can extend another interface, but they cannot be mixed - a class implements an interface using the implements keyword, not extends.
-
shadowing
-
Coupling
-
Cohesion
-
None of the above
A
Correct answer
Explanation
Variable shadowing occurs when a variable declared within a specific scope has the same name as a variable in an outer scope. The local variable takes precedence, hiding the instance variable within that scope.
-
Interface can extend one or more interfaces
-
Interface can extend one or more class
-
Interface cannot implement a class or interface
-
All of the above
A
Correct answer
Explanation
In Java and similar OO languages, an interface can extend one or more interfaces (multiple inheritance is allowed for interfaces), but cannot extend classes. Classes implement interfaces. Option C is incomplete, and 'All of the above' is incorrect because interfaces don't extend classes.