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
-
Classes in class diagrams may be grouped into packages in order to illustrate the overall organization of a model
-
In object diagrams, names of instances are in italics or all-caps
-
If package B depends on package A, then any change in A will require a change in B
-
Object diagrams and class diagrams are completely interchangeable
A
Correct answer
Explanation
In UML class diagrams, classes can be grouped into packages to organize the model. Object diagram names are underlined (not italicized), package dependencies do not always force changes, and class/object diagrams are not fully interchangeable.
-
methods in the associated classes
-
subclasses needed to accomplish the same functionality
-
case statements and conditionals
-
coupling between classes in the system
C
Correct answer
Explanation
Polymorphism's key advantage is replacing conditional logic (case/switch statements and if-else chains) with dynamic dispatch through virtual functions. Instead of checking type and executing corresponding code, polymorphism lets each subclass implement its own behavior, invoked through a common interface. This reduces conditional complexity.
-
Multiple Inheritance and Overloading
-
Operator Overloading and Overriding
-
Multiple Inheritance and Operator Overloading
-
Pointers and Single Inheritance
C
Correct answer
Explanation
Java does not support multiple inheritance of classes (to avoid the diamond problem) nor does it support user-defined operator overloading. Therefore, this pair is not supported in Java.
B
Correct answer
Explanation
In Java, Map is a separate interface from Collection. Map does NOT extend Collection - they are distinct hierarchies. Collection is for groups of objects, Map is for key-value pairs.
-
is final
-
is public
-
is serializable
-
has a constructor which takes a StingBuffer Object as an Argument
B
Correct answer
Explanation
Using raw types like List instead of parameterized generic types like List bypasses compile-time type safety checks. Therefore, it is false to say that writing the raw type version is preferable.
-
Virtual function
-
Function overloading
-
Operator Overloading
-
All the Above
D
Correct answer
Explanation
Virtual functions enable runtime polymorphism through dynamic binding. Function overloading and operator overloading provide compile-time polymorphism. All three are established polymorphism mechanisms in C++.
-
a) ifstream
-
b) ofstream
-
Both a and b
-
none of the above
A
Correct answer
Explanation
ifstream is the C++ standard library class specifically designed for reading input from files. ofstream is for output (writing files), making ifstream the correct choice for file input operations.
-
any name
-
x
-
exforsys
-
None of the Above
C
Correct answer
Explanation
In Java and similar object-oriented languages, a constructor must have the exact same name as its class. If the class is named 'exforsys', the constructor must also be named 'exforsys'. The constructor name cannot be arbitrary (option A), cannot be the same as a field name (option B), and option D is incorrect because there is a specific answer.
-
Does not compile
-
prints 0
-
prints 10
-
prints 7
A
Correct answer
Explanation
The code fails to compile due to two errors: (1) 'class B extends class A' - should be 'class B extends A' without the word 'class' before A, and (2) class A has a private constructor, so it cannot be instantiated in main(). The code has multiple compilation errors preventing it from running.
-
public void amethod(int i) throws FileNotFoundException{ }
-
public void amethod(int i) throws IOException, RuntimeException{ }
-
public void amethod(int i) throws RuntimeException{ }
-
public void amethod(int i) throws Exception{ }
-
public void amethod(int i) { }
-
public void amethod(int i) throws Throwable{ }
A,B,C,E
Correct answer
Explanation
When overriding a method that throws IOException, you can: throw no exceptions (E), throw fewer or same checked exceptions (A - FileNotFoundException is subclass of IOException), add unchecked exceptions freely (B, C - RuntimeException is unchecked), but you cannot throw broader checked exceptions (D - Exception, F - Throwable). Options A, B, C, E all follow these rules.
-
private
-
protected
-
transient
-
public
-
final
D,E
Correct answer
Explanation
A top-level Java class can only be declared with 'public', 'abstract', or 'final' access modifiers, or default (package-private) access. 'private', 'protected', and 'transient' cannot be applied to top-level classes.
-
A Bar is a Baz
-
A Foo has a Bar
-
A Baz is a Foo
-
A Foo is a Baz
-
A Baz has a Bar
C,E
Correct answer
Explanation
Baz extends Foo, so every Baz object is also a Foo through inheritance (is-a relationship). Baz contains a Bar field named 'thing', so a Baz has-a Bar. Option C correctly states the inheritance relationship, and E correctly states the composition relationship.
-
It exhibits high cohesion
-
It exhibits low cohesion
-
It exhibits tight coupling
-
It exhibits loose coupling
-
None of the Above
A
Correct answer
Explanation
The Waiter class methods all work closely together to perform related tasks regarding order handling and serving. This strong functional relatedness within a single class represents high cohesion.
-
Does not compile
-
prints "ABC"
-
prints "ACB"
-
prints "CAB"
-
run time exception
A
Correct answer
Explanation
In Java, either this() or super() can be the first statement in a constructor, but never both. The no-arg B() constructor attempts to call this(4) followed by super(), which violates Java's constructor chaining rules. Since this(4) must be first and it will implicitly call super(), the explicit super() call causes compilation failure.