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

Multiple choice technology programming languages
    1. Add Language (string Name) {}
    1. Add Language ( ) { }
    1. Add Language (String Name) { }
    1. Allow the compiler to add a default constructor.
Reveal answer Fill a bubble to check yourself
A,D Correct answer
Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Java is pass-by-value, but when you pass an object reference, you're passing the value of the reference (a pointer). This means the called method receives a reference to the same object, not a copy of the object. Changes to the object's state are visible to the caller.

Multiple choice technology programming languages
  1. a. public

  2. b. private

  3. c. static

  4. d. friend

Reveal answer Fill a bubble to check yourself
A,B Correct answer
Explanation

For inner classes (non-static nested classes), the modifiers public and private are legal at the class level within an outer class. However, 'static' is NOT legal for a non-static inner class - to make it static, it must be explicitly declared as a 'static nested class' with different syntax. The 'friend' keyword doesn't exist in Java.

Multiple choice technology programming languages
  1. a. Compile Error: Methods in Base not found

  2. b. Compile Error: Unable to access protected method in base class

  3. c. Compilation followed by the output "amethod"

  4. d. Compile error: Superclass Class1.Base of class Class1.Class1 not found

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Class1 is in package Class1, Base is in package Base. Class1 extends Base. When Class1 tries to create 'new Base()', the compiler looks for Base.Class1.Base (inner class interpretation) because of how package names interact. The actual Base class is in a different package and isn't found with that qualification. This causes 'Superclass Class1.Base of class Class1.Class1 not found' - the compiler is confused about the class hierarchy due to package naming.

Multiple choice technology programming languages
  1. a. All of the variables in an interface are implicitly static

  2. b. All of the variables in an interface are implicitly final

  3. c. All of the methods in an interface are implicitly abstract

  4. d. A method in an interface can access class level variables

Reveal answer Fill a bubble to check yourself
A,B,C Correct answer
Explanation

Interface variables are implicitly public static final (constants), making A and B true. Interface methods are implicitly public abstract, making C true. Interface methods cannot access class-level variables because interfaces have no implementation or state - D is false. Interface methods define only the contract, not behavior.

Multiple choice technology programming languages
  1. a. An interface can only contain method and not variables

  2. b. Interfaces cannot have constructors

  3. c. A class may extend only one other class and implement only one interface

  4. d. Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the Interfaces.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Interfaces CAN contain variables (which become constants), so A is false. Interfaces cannot have constructors - B is true (no instance creation). Classes can implement MULTIPLE interfaces, so C's 'only one interface' is false. D is misleading - interfaces don't 'require' implementing classes to create functionality; they define the contract that implementing classes must fulfill.

Multiple choice technology programming languages
  1. Alpha a = x;

  2. Foo f= (Delta)x;

  3. Foo f= (Alpha)x;

  4. Beta b = (Beta)(Alpha)x;

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The actual object is a Beta instance (created with new Beta()). At runtime, you cannot cast a Beta object to Delta - they are sibling classes in the hierarchy, not in an inheritance relationship. The cast (Delta)x fails because x's actual type (Beta) cannot be converted to Delta. This throws ClassCastException. Options A, C, D are valid casts because Beta extends Alpha and implements Foo, so upcasting to Alpha or Foo is safe.

Multiple choice technology programming languages
  1. Alpha a = x;

  2. Foo f= (Delta)x;

  3. Foo f= (Alpha)x;

  4. Beta b = (Beta)(Alpha)x;

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

To solve this question, the user needs to know about inheritance in Java and how casting works. The key to answering this question correctly is understanding how casting works between different classes in an inheritance hierarchy.

Now, let's go through each option and explain why it is right or wrong:

A. Alpha a = x; - This code is valid because Beta is a subclass of Alpha, so it can be upcast to an Alpha without issue. This will not cause a ClassCastException.

B. Foo f= (Delta)x; - This code is invalid because Delta is a subclass of Beta, so it cannot be downcast to Delta from Beta. This will throw a ClassCastException.

C. Foo f= (Alpha)x; - This code is valid because Beta is a subclass of Alpha, so it can be upcast to an Alpha without issue. This will not cause a ClassCastException.

D. Beta b = (Beta)(Alpha)x; - This code is invalid because Alpha is a superclass of Beta, so it cannot be downcast to Beta from Alpha. This will throw a ClassCastException.

Therefore, the correct answer is:

The Answer is: B

Multiple choice technology security
  1. The type safety mechanism in the Java language prevents the execution of malicious code

  2. Two classes with the same fully qualified name but which are defined by different instances of a class loader are NOT of the same type

  3. All signed classes are implicitly trusted and granted full access

  4. The principal role of a TrustManager is to determine if presented authentication credentials should be trusted

  5. Option 1 AND Option 4

  6. Option 2 AND Option 4

Reveal answer Fill a bubble to check yourself
F Correct answer
Explanation

Option 2 is true: classes with identical fully qualified names loaded by different class loaders are treated as different types due to namespace isolation. Option 4 is also true: TrustManager specifically validates authentication credentials like X.509 certificates. Options 1 and 3 are false: type safety doesn't prevent all malicious code, and signed classes aren't automatically granted full access.

Multiple choice technology programming languages
  1. A static method may override another static method.

  2. A static method cannot override a non-static method.

  3. A non-static method cannot override a static method.

  4. A non-static method may be overloaded by a static method.

  5. A synchronized method cannot be overridden.

Reveal answer Fill a bubble to check yourself
B,C,D Correct answer
Explanation

Static methods belong to the class, not instances - they can't override non-static methods (different binding mechanism), and non-static methods can't override static methods. Overloading a non-static method with a static one works by signature but is poor practice (different binding). Static methods can be hidden by other static methods (shadowing), not truly overridden. The synchronized keyword affects thread safety, not override capability.